mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
0fab703028
Replace binary search with linear scan in mt_get(), mt_put(), mt_del(), mt_chain_has(), and mrb_mt_foreach(). The method cache makes repeated lookups O(1), so linear scan on cache misses is acceptable. This removes the sorting requirement, allowing ROM entry arrays to be declared const. On embedded systems, const static data resides in flash/ROM instead of RAM, saving ~8.4KB for ~700 method entries on 32-bit MCUs. Co-authored-by: Claude <noreply@anthropic.com>
mruby-array-ext
This mrbgem extends the Array class in mruby, providing a rich set of additional methods for array manipulation. These extensions enhance mruby's built-in array capabilities, offering functionalities commonly found in standard Ruby.
Functionality
The mruby-array-ext gem adds the following methods to the Array class:
Set Operations
uniq: Returns a new array by removing duplicate values.uniq!: Removes duplicate elements from self. Returnsnilif no changes are made.-(difference): Returns a new array that is a copy of the original array, removing any items that also appear in another array.|(union): Returns a new array by joining this array with another array, removing duplicates.&(intersection): Returns a new array containing elements common to two arrays, with no duplicates.difference: Returns a new array that is a copy of the original array, removing all occurrences of any item that also appear in the specified other arrays.union: Returns a new array by joining this array with other arrays, removing duplicates.intersection: Returns a new array containing elements common to this array and specified other arrays, removing duplicates.intersect?: Returnstrueif the array and another array have at least one element in common.
Element Manipulation and Access
flatten: Returns a new array that is a one-dimensional flattening of self.flatten!: Flattens self in place. Returnsnilif no modifications were made.fetch: Tries to return the element at a given position. Throws anIndexErrorif the index is out of bounds, unless a default value or block is provided.fill: Sets selected elements of self to a given object or the result of a block.compact: Returns a copy of self with allnilelements removed.compact!: Removesnilelements from self. Returnsnilif no changes were made.rotate(count=1): Returns a new array by rotating self so that the element atcountis the first element.rotate!(count=1): Rotates self in place.insert(index, obj...): Inserts the given values before the element with the given index.slice!(index)/slice!(start, length)/slice!(range): Deletes element(s) given by an index or range and returns the deleted object(s).at(index): Returns the element atindex. Returnsnilif the index is out of range.dig(idx, ...): Extracts a nested value specified by a sequence of indices.fetch_values(idx, ...): Returns an array containing the values associated with the given indexes. RaisesIndexErrorif an index is not found, unless a block is provided.values_at(selector, ...): Returns an array containing the elements corresponding to the given selector(s).
Searching and Comparison
assoc(obj): Searches through an array of arrays, comparingobjwith the first element of each contained array. Returns the first matching contained array ornil.rassoc(obj): Searches through an array of arrays, comparingobjwith the second element of each contained array. Returns the first matching contained array ornil.bsearch { |x| block }: Finds a value from the array which meets the given condition using binary search.bsearch_index { |x| block }: Finds the index of a value from the array which meets the given condition using binary search.
Iterators and Combinatorics
reverse_each: Calls the given block for each element in reverse order.permutation(n=self.size): Yields all permutations of lengthnof the elements of the array.combination(n): Yields all combinations of lengthnof elements from the array.product(*arys): Returns an array of all combinations of elements from self and the given arrays.repeated_combination(n): Yields all repeated combinations of lengthn.repeated_permutation(n): Yields all repeated permutations of lengthn.
Conversions and Other Utilities
to_h: Returns the result of interpreting the array as an array of[key, value]pairs.transpose: Assumes that self is an array of arrays and transposes the rows and columns.
Filtering
delete_if { |item| block }: Deletes every element of self for which the block evaluates totrue.reject! { |item| block }: Equivalent todelete_if, but returnsnilif no changes were made.keep_if { |item| block }: Deletes every element of self for which the given block evaluates tofalse.select! { |item| block }: Deletes elements for which the block returns afalsevalue. Returnsselfif changes were made, otherwisenil.
Aliases
append(alias forpush)prepend(alias forunshift)filter!(alias forselect!)
Usage
To use this gem, add it to your build_config.rb or mrbgem.rake file. For detailed examples of each method, please refer to the comments in the source code (mrblib/array.rb and src/array.c).
Example:
a = [1, 2, 2, 3, nil, 4]
p a.compact #=> [1, 2, 2, 3, 4]
p a.uniq #=> [1, 2, 3, nil, 4]
b = ["a", "b", "c"]
p b.rotate #=> ["b", "c", "a"]
This gem is part of the mruby project.