67 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 2ae1160b39 mruby-array-ext: add Array#find and Array#rfind
Array#find is an optimized version of Enumerable#find for arrays,
using direct index access instead of each iterator.

Array#rfind finds from the end of the array, returning the first
match when scanning backwards.

Both methods support the ifnone parameter for default values.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-25 18:01:44 +09:00
Yukihiro "Matz" Matsumoto 6dc73d2517 mruby-array-ext: unify array#- tests
Merged the separate "Array#-" and "Array#- with large arrays" test
blocks into a single comprehensive test. The unified test covers both
basic functionality (type checking, simple subtraction) and the
hash-based implementation for large arrays (>32 elements).

Co-authored-by: Atlassian Rovo Dev
2025-07-05 18:32:46 +09:00
Yukihiro "Matz" Matsumoto 69d278589d mruby-array-ext: add Array#deconstruct for pattern matching
Implement new method for Ruby 2.7+ pattern matching compatibility.
Returns the array itself to enable case/in pattern matching syntax.
Complements Hash#deconstruct_keys for complete pattern matching support.

Co-authored-by: Atlassian Rovo Dev
2025-07-01 22:34:01 +09:00
Yukihiro "Matz" Matsumoto 90fd382a9e mruby-array-ext: implement Array#fetch with hybrid approach
Moved Array#fetch from Ruby to C using hybrid implementation for
better performance. The C implementation handles all non-block cases
with unified API that eliminates Ruby conditional logic.

Key improvements:
- Fast C implementation for common cases (no blocks)
- Shared index normalization helper reusable for other methods
- Unified C call eliminates NONE sentinel comparison in Ruby
- Block cases use C helper for index normalization

Added comprehensive test coverage including edge cases, default values,
block handling, and error message format verification. Combined tests
to focus on functionality rather than implementation details.

Co-authored-by: Atlassian Rovo Dev
2025-06-30 12:17:21 +09:00
Yukihiro "Matz" Matsumoto 87c39d3c0b mruby-array-ext: implement Array#insert in C
This commit also corrects the behavior of `Array#insert` when a negative
index is out of bounds. It now raises an `IndexError`, which is
consistent with CRuby.

Co-authored-by: Gemini <gemini@google.com>
2025-06-30 12:10:34 +09:00
Yukihiro "Matz" Matsumoto 9e8cda73f6 mruby-array-ext: implement Array#fill in C
Implemented shared C argument parser and separate fill logic to eliminate code
duplication while maximizing performance. The implementation uses C implemented
__fill_parse_args for unified argument handling and __fill_exec for fast
C-based value filling.

Added comprehensive test coverage for both shared argument parsing
and C fill implementation, including range arguments, block handling,
and array extension scenarios.

Co-authored-by: Atlassian Rovo Dev
Co-authored-by: Gemini <gemini@google.com>
2025-06-30 10:26:42 +09:00
Yukihiro "Matz" Matsumoto f205c2233d mruby-array-ext: increase SET_OP_HASH_THRESHOLD from 16 to 32
We have more chance to avoid hash allocation in set-like methods. Since
memory situation heavily depends on the platform, we may need to make
this threshold configurable in the future.

Co-authored-by: Atlassian Rovo Dev
2025-06-29 20:46:51 +09:00
Yukihiro "Matz" Matsumoto da813e0486 mruby-array-ext: implement Array#intersect? in C for better performance
Moved Array#intersect? implementation from Ruby to C to improve memory
usage and performance with early termination optimization. The C
implementation uses hash-based lookup for large arrays (>16 elements)
and linear search for smaller arrays.

Added comprehensive test coverage including early termination scenarios,
empty arrays, size optimization verification, and edge cases with
duplicates and large arrays.

Co-authored-by: Atlassian Rovo Dev
2025-06-29 20:46:50 +09:00
Yukihiro "Matz" Matsumoto ea5de2b8da mruby-array-ext: implement Array#& in C for better performance
Moved Array#& (set intersection) implementation from Ruby to C to improve
memory usage and performance. The C implementation uses hash-based
deduplication for large arrays (>16 elements) and linear search for
smaller arrays, following the same hybrid pattern as Array#| and Array#-.

Key improvements:
- Hash-based approach uses mrb_hash_delete_key() for proper deduplication
- Linear search approach checks result array to ensure uniqueness
- Maintains order preservation from the first array
- Eliminates temporary object creation in Ruby implementation

Added comprehensive test coverage for both small and large array scenarios,
including edge cases like no intersection, complete intersection, and
duplicate handling.

Co-authored-by: Atlassian Rovo Dev
2025-06-28 01:33:04 +09:00
Yukihiro "Matz" Matsumoto df35982297 mruby-parray-ext: implement Array#| in C for better performance
The C implementation uses hash-based deduplication for large arrays
(>16 elements) and linear search for smaller arrays, following the same
pattern as other set operations.

Co-authored-by: Atlassian Rovo Dev
2025-06-28 00:47:10 +09:00
Yukihiro "Matz" Matsumoto 409f39e911 mruby-array-ext: implement Array#- in C
Refactor Array#- to a C implementation for improved memory and performance,
especially for set operations. Uses a hybrid approach for efficiency.

Co-authored-by: Gemini <gemini@google.com>
2025-06-28 00:47:09 +09:00
Yukihiro "Matz" Matsumoto da4cfbf89c mruby-array-ext: add a new method Array#fetch_values 2024-09-10 10:22:34 +09:00
Yukihiro "Matz" Matsumoto eee83ed7af array.c: implement Array#index and Array#rindex in C
No need to override Array#index in mruby-array-ext. We can call
`to_enum` from C implemented methods.
2024-06-29 15:03:12 +09:00
dearblue 586525f99a Add Array#{repeated_combination,repeated_permutation} methods
Ruby 1.9.2 feature.

ref: https://docs.ruby-lang.org/ja/3.1.0/method/Array/i/repeated_combination.html
ref: https://docs.ruby-lang.org/ja/3.1.0/method/Array/i/repeated_permutation.html
2021-12-12 21:28:03 +09:00
dearblue 5ea26260fa Added Array#product method
Ruby-1.9.0 feature.

ref: https://docs.ruby-lang.org/ja/3.0.0/method/Array/i/product.html
2021-10-31 23:02:37 +09:00
Yukihiro "Matz" Matsumoto 737254588b array.rb: add Array#intersect? from Ruby3.0.1. 2021-04-17 17:38:44 +09:00
Hiroshi Mimaki 7d9a49efab Delete duplicated Array#delete_if test. 2020-01-21 12:50:26 +09:00
KOBAYASHI Shuji b5bb981959 Remove test that depend on mruby-enumerator from mruby-array-ext
`Object.const_defined?(:Enumerator)` is always false because
`mruby-enumerator` is not specified in `test_dependency`. I don't
think this test is necessary.
2019-11-05 19:45:04 +09:00
Yukihiro "Matz" Matsumoto bdacdfaea9 Add Array#intersection which is new in Ruby2.7. 2019-10-14 17:04:19 +09:00
Yukihiro "Matz" Matsumoto 67ea80b2bd Fixed a bug in Array#difference. 2019-10-14 17:04:19 +09:00
Yukihiro "Matz" Matsumoto f4117d81d2 Add Array#difference method from Ruby2.6. 2019-09-16 10:10:09 +09:00
KOBAYASHI Shuji fe6cf851bf Array#permutation with a negative argument should not yield
Before this patch:

  $ bin/mruby -e '[1].permutation(-1){|v| p v}'  #=> [1]

After this patch (same as Ruby):

  $ bin/mruby -e '[1].permutation(-1){|v| p v}'  #=> no output
2019-09-01 09:28:11 +09:00
KOBAYASHI Shuji 28d1f6cdc4 Array#(permutation|combination) without block should return self 2019-08-30 17:04:25 +09:00
KOBAYASHI Shuji 87cc58ba3d Refine Array#(permutation|combination) test
- No guarantees about the order in which the permutations/combinations
  are yielded.

- Drop dependency on `Enumerator`.
2019-07-25 22:31:54 +09:00
KOBAYASHI Shuji c1e6f324c2 Comment out the empty Array#bsearch_index test 2019-05-31 20:50:54 +09:00
Yukihiro "Matz" Matsumoto 698f5f707c Removed to_ary conversion method. 2018-11-19 12:08:28 +09:00
Yukihiro "Matz" Matsumoto 5bbcea9b3b Removed try_convert method from Array and Hash. 2018-11-19 12:08:28 +09:00
Yukihiro "Matz" Matsumoto f23f2bbdad Implement Array#union which is introduced in Ruby2.6. 2018-09-21 00:01:48 +09:00
dearblue 366848996a Clear terminated space 2018-09-07 22:32:34 +09:00
Tomasz Dąbrowski e65fa4d7a2 implement Array.transpose 2017-11-17 12:39:34 +01:00
Yukihiro "Matz" Matsumoto 0c3ee0ba66 Add `Array#{permutation,combination}. 2017-10-20 10:38:58 +09:00
Christopher Aue a3bfd735a0 Added Array#bsearch_index 2017-07-28 23:51:49 +02:00
Christopher Aue 451574f142 Refactored Array#bsearch 2017-07-28 23:51:40 +02:00
Christopher Aue 8a6ce74b4d Tested Array#bsearch more thoroughly 2017-07-28 17:15:35 +02:00
Kenji Okimoto 33f77fe367 Implement Array#slice! 2017-03-28 15:07:57 +09:00
Yukihiro "Matz" Matsumoto 739dad6e87 Fixed a memory problem in Array#to_h
Reported from Alex Snaps via Mathieu Leduc-Hamel,
both from shopify.com.  Thank you!
2016-11-16 01:29:04 +09:00
Yukihiro "Matz" Matsumoto 817b884343 add #dig tests 2016-03-23 12:34:32 +09:00
takahashim 621487a0cd add {Array|Hash|String}.try_convert 2015-11-24 08:39:39 +09:00
Yukihiro "Matz" Matsumoto fafe86d364 Array#index to take block; fix #2968 close #2970 2015-10-01 22:35:06 +09:00
Mitsutaka Mimura a0b12bf6b4 Add Array#to_h 2014-06-02 23:06:02 +09:00
Yukihiro "Matz" Matsumoto 6d6957078e no one uses Array.try_convert, so removed it that discourages duck typing; close #2317 2014-05-25 23:18:45 +09:00
take_cheeze 44dc05f12a Implement Struct#values_at and Array#values_at .
Add API `mrb_get_values_at()` to mruby/range.h .
2014-05-02 23:20:48 +09:00
Nobuyoshi Nakada ab67c57f65 remove trailing spaces 2014-04-30 09:50:14 +09:00
Jun Hiroe cdef46e3d9 Add Array#select_bang 2014-04-27 23:34:24 +09:00
Jun Hiroe d6414fe112 Add Array#keep_if 2014-04-25 18:57:20 +09:00
Yukihiro "Matz" Matsumoto 49abcd4c02 resolve conflict 2014-04-25 04:43:10 +09:00
Jun Hiroe d4bec4d657 Add Array#delete_if 2014-04-24 23:04:46 +09:00
ksss f961d7abeb Array#fill fix bug when argument use range object 2014-04-24 21:08:49 +09:00
Jun Hiroe c81b9838bb Add Array#bsearch 2014-04-24 21:04:13 +09:00
Jun Hiroe a48e0018fd Add Array#insert 2014-04-23 00:22:24 +09:00