Module: BerkeleyLibrary::Util::Arrays
Instance Method Summary collapse
-
#count_while(values:) ⇒ Object
Counts how many contiguous elements from the start of an sequence of values satisfy the given block.
-
#find_index(*args, in_array:, start_index: 0, &block) ⇒ Object
Given a block or a value, finds the index of the first matching value at or after the specified start index.
-
#find_indices(for_array:, in_array:, &block) ⇒ Object
Given two lists, one of which is a superset of the other, with elements in the same order (but possibly with additional elements in the superset), returns an array the length of the subset, containing for each element in the subset the index of the corresponding element in the superset.
-
#invert(arr) ⇒ Array<Integer, nil>?
Given an array of unique integers a1, returns a new array a2 in which the value at each index i2 is the index i1 at which that value was found in a1.
-
#merge(a1, a2) ⇒ Array
Merges two arrays in an order-preserving manner.
-
#ordered_superset?(superset:, subset:) ⇒ Boolean
Recursively checks whether the specified list contains, in the same order, all values in the other specified list (additional codes in between are fine).
Instance Method Details
#count_while(arr: ) ⇒ Enumerator #count_while(arr: , &block) ⇒ Integer
Counts how many contiguous elements from the start of an sequence of values satisfy the given block.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/berkeley_library/util/arrays.rb', line 32 def count_while(values:) return to_enum(:count_while, values: values) unless block_given? values.inject(0) do |count, x| matched = yield x break count unless matched count + 1 end end |
#find_index(value, in_array: , start_index: ) ⇒ Integer? #find_index(&block) ⇒ Integer? #find_index ⇒ Enumerator
Given a block or a value, finds the index of the first matching value at or after the specified start index.
92 93 94 95 96 97 98 |
# File 'lib/berkeley_library/util/arrays.rb', line 92 def find_index(*args, in_array:, start_index: 0, &block) raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..1" if args.size > 1 return Enumerator.new { |y| find_index(in_array: in_array, start_index: start_index, &y) } if args.empty? && !block_given? return unless (relative_index = in_array[start_index..].find_index(*args, &block)) relative_index + start_index end |
#find_indices(for_array: , in_array: ) ⇒ Array<Integer>? #find_indices(for_array: , in_array: ) {|source, target| ... } ⇒ Array<Integer>?
Given two lists, one of which is a superset of the other, with elements in the same order (but possibly with additional elements in the superset), returns an array the length of the subset, containing for each element in the subset the index of the corresponding element in the superset.
66 67 68 69 70 |
# File 'lib/berkeley_library/util/arrays.rb', line 66 def find_indices(for_array:, in_array:, &block) return find_indices_matching(for_array, in_array, &block) if block_given? find_all_indices(for_array, in_array) end |
#invert(arr) ⇒ Array<Integer, nil>?
Given an array of unique integers a1, returns a new array
a2 in which the value at each index i2 is the
index i1 at which that value was found in a1.
E.g., given [0, 2, 3], returns [0, nil, 1, 2]. The indices need
not be in order but must be unique.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/berkeley_library/util/arrays.rb', line 110 def invert(arr) return unless arr # noinspection RubyNilAnalysis Array.new(arr.size).tap do |inv| arr.each_with_index do |v, i| next inv[v] = i unless (prev_index = inv[v]) raise ArgumentError, "Duplicate value #{v} at index #{i} already found at #{prev_index}" end end end |
#merge(a1, a2) ⇒ Array
Merges two arrays in an order-preserving manner.
128 129 130 131 132 133 134 |
# File 'lib/berkeley_library/util/arrays.rb', line 128 def merge(a1, a2) return a1 if a2.empty? return a2 if a1.empty? shorter, longer = a1.size > a2.size ? [a2, a1] : [a1, a2] do_merge(shorter, longer) end |
#ordered_superset?(superset:, subset:) ⇒ Boolean
Recursively checks whether the specified list contains, in the same order, all values in the other specified list (additional codes in between are fine)
16 17 18 |
# File 'lib/berkeley_library/util/arrays.rb', line 16 def ordered_superset?(superset:, subset:) !find_indices(in_array: superset, for_array: subset).nil? end |