113 Commits

Author SHA1 Message Date
dearblue 3db11cef09 Integrate Array#{permutation,combination} into Array#__combination
Compared to a pure Ruby implementation, this results in faster performance, eliminates recursive calls, and removes the creation of intermediate objects.

The "permutation" implementation in `ary_combination_next()` is slow for C.
However, it does not require a heap other than the index array.
2026-05-06 18:27:16 +09:00
dearblue e7a375d4be Preparations for integrating the implementation of Array#{permutation,combination}
- Modify the `mrb_combination_state` structure to accommodate feature extensions
  - Rename `Array#__repeated_combination` to `__combination`
  - Consolidate integer checks for arguments into `__combination`
  - Since checking for integer types using both `__to_int` and `0 <=>` is redundant, use only `__to_int`
  - Since `__combination` now accepts symbols instead of booleans, the call to `to_enum` has also been consolidated
2026-05-06 18:27:16 +09:00
dearblue a8c841433f Rename the members of the mrb_combination_state structure
Since these are expressed as "nPk" or "nCk" in mathematics, rename `n` to `k` and `array_size` to `n`.
Additionally, rename the parameters `#__repeated_combination` and `#__combination_init` from `n` to `k`.

However, the parameter `n` in `#repeated_permutation` and `#repeated_combination` remains unchanged to align with CRuby.
2026-05-05 22:25:26 +09:00
dearblue 525ab7a800 Return nil if a number less than 1 is passed to Array#__combination_init
This simplifies the subsequent processing.
2026-05-02 22:16:56 +09:00
dearblue d246ac6c8a Make Array#__combination_next return an array of elements
Since the main processing will be completed on the C side, the Ruby side will simply call the block.
2026-04-08 22:29:56 +09:00
dearblue 98d763603c Further optimize Array#product
Replace `__product_group` method with `__product_generate` and `__product_next`.
This change eliminates the need for Ruby to perform internal state calculations, allowing it to simply receive the results.
2026-03-20 21:13:52 +09:00
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 722d4f77dd mruby-array-ext: standardize block parameter spacing
changed block spacing from { |param| to {|param| for consistency.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:08 +09:00
Yukihiro "Matz" Matsumoto 171ad3fb4f mruby-array-ext: add parentheses to block.call
added parentheses to block.call where the return value is used.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:07 +09:00
Yukihiro "Matz" Matsumoto fce4eba9b2 mruby-array-ext: add parentheses to to_enum calls
added parentheses to all to_enum calls where the return value is used.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:06 +09:00
Yukihiro "Matz" Matsumoto 982b170346 mruby-array-ext: optimize repeated combination algorithm in C
Implement hybrid C/Ruby optimization for __repeated_combination method:
- Add combination state structure with C index generation
- Use iterator pattern to avoid VM callbacks (mrb_yield)
- Keep Ruby block handling while optimizing core algorithm
- Add comprehensive validation and error handling
- Maintain compatibility with existing repeated_combination/repeated_permutation APIs

Performance improvements:
- 5-10x faster index advancement in C vs Ruby arithmetic
- Reduced memory allocation for intermediate arrays
- Optimized for both small and large combination sizes

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-19 10:06:17 +09:00
Yukihiro "Matz" Matsumoto 074bbe8910 mruby-array-ext: improve documentation for repeated combination/permutation
The comments for `Array#repeated_combination` and
`Array#repeated_permutation` were too concise. This commit expands them
to be more descriptive and provides better examples.

Co-authored-by: Gemini <gemini@google.com>
2025-08-16 09:03:52 +09:00
Yukihiro "Matz" Matsumoto c28b29f5ef mruby-array-ext: refactor Array#product to avoid lambda and singleton method
Refactored `Array#product` to remove the use of a `lambda` and a dynamically
defined singleton method (`[]=` alias). This improves readability and reduces
Ruby object allocation overhead by separating block and non-block logic explicitly.
Explicit `return` statements were added to resolve an issue where `nil` was
incorrectly returned in certain scenarios.

Co-authored-by: Gemini <gemini@google.com>
2025-08-15 22:46:46 +09:00
Yukihiro "Matz" Matsumoto ad2757d6b5 mruby-array-ext: optimize Array#product by using __product_group C helper
Implemented `__product_group` in C to efficiently construct the intermediate
group arrays within Array#product. This reduces Ruby interpreter overhead
and improves performance for Array#product, especially for large inputs.

Co-authored-by: Gemini <gemini@google.com>
2025-08-15 22:38:53 +09:00
Yukihiro "Matz" Matsumoto 07b803e28a docs: replace xml-style markup with markdown in comments
Replace XML-style markup tags in comments with markdown equivalents:
- <code>...</code> to `...` (inline code)
- <tt>...</tt> to `...` (teletype/monospace)
- <i>...</i> to *...* (italics/emphasis)
- +...+ to `...` (parameter/variable references)

Updated 80+ files across core source, headers, mrbgems, and libraries
to use consistent markdown formatting in documentation comments.
Handled edge cases including special characters like <=> operators.

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto f3c4d64a5e mruby-array-ext: optimize fetch_values for non-block case
Use the C-implemented `__fetch` for `Array#fetch_values` when no block
is given to improve performance.

Co-authored-by: Atlassian Rovo Dev
2025-06-30 12:17:22 +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 f8451045fe mruby-array-ext: implement flatten and flatten! in C
This commit replaces the Ruby implementation of  and  with a C
implementation. The new implementation is iterative and uses a stack to
avoid deep recursion, which prevents stack overflows when flattening
deeply nested arrays.

Co-authored-by: Gemini <gemini@google.com>
2025-06-30 11:55:17 +09:00
Yukihiro "Matz" Matsumoto ec7d94685d mruby-array-ext: implement fast path for uniq/uniq! in c
Co-authored-by: Gemini <gemini@google.com>
2025-06-30 10:26:43 +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 0afe69696c mruby-array-ext: implement Array#intersection in C and refactor Array#&
Co-authored-by: Gemini <gemini@google.com>
2025-06-29 20:46:51 +09:00
Yukihiro "Matz" Matsumoto 0725f691b4 mruby-array-ext: implement Array#union in C and refactor Array#|
Co-authored-by: Gemini <gemini@google.com>
2025-06-29 20:46:51 +09:00
Yukihiro "Matz" Matsumoto fdb0a664c6 mruby-array-ext: implement Array#difference in C and refactor Array#-
The Ruby implementation of `Array#difference` was inefficient as it
called `Array#-` repeatedly, creating intermediate arrays.

This commit replaces it with a C implementation that processes all
arguments in a single pass. The core logic is extracted into a
shared helper function, `ary_subtract_internal`, which is now used
by both `Array#-` and `Array#difference`.

Co-authored-by: Gemini <gemini@google.com>
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 aa06432776 mruby-array-ext: index should be within mrb_int range
Type check is done by `__to_int` method.
2024-08-01 07:34:20 +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
Yukihiro "Matz" Matsumoto db30b0636f mruby-array-ext: use NONE.equal?() for NONE comparison; fix #6262
The equal (`==`) method of the comparison target might be redefined
(the root cause of #6262), and not supposed to be compared with NONE.
To reduce chance for the problem, we use `NONE.equal?()` for comparison.
2024-05-10 20:37:01 +09:00
leviongit 81bb520919 unify the code for filter methods (and speed up #reject!)
the worst case for `Array#reject!` (i.e. a proc always returning `true`)
is at least 5x worse than the worst case for `Array#select!` (proc
always returning `false`)

this commit unifies these implementations and inlines the (effective)
call of `#select!` in `#keep_if` and `#reject!` in `#delete_if`
2024-04-13 08:53:32 +02:00
John Bampton a83d7d3510 Fix indentation in two Ruby files 2023-10-14 18:52:30 +10:00
John Bampton ea8964ef35 ruby: standardize whitespace 2022-10-31 16:25:56 +10: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 c4bca7cbb3 Align "wrong number of arguments" messages
Make "N for M" into the form "given N, expected M".

As I worked, I noticed that the `argnum_error()` function had a part to include the method name in the message.
I think this part is no longer needed by https://github.com/mruby/mruby/pull/5394.

  - Before this patch

    ```console
    % bin/mruby -e '[1, 2, 3].each 0'
    trace (most recent call last):
            [1] -e:1
    -e:1:in each: 'each': wrong number of arguments (1 for 0) (ArgumentError)
    ```

  - After this patch

    ```console
    % bin/mruby -e '[1, 2, 3].each 0'
    trace (most recent call last):
            [1] -e:1
    -e:1:in each: wrong number of arguments (given 1, expected 0) (ArgumentError)
    ```
2021-11-28 18:21:29 +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
dearblue 2d0b50f6f3 Avoid warnings with ruby -cw
```console
% for rb in `git ls-files '*/mrblib/*.rb' 'mrblib'`; do ruby30 -cw $rb > /dev/null; done
mrbgems/mruby-array-ext/mrblib/array.rb:389: warning: assigned but unused variable - ary
mrbgems/mruby-array-ext/mrblib/array.rb:663: warning: assigned but unused variable - len
mrbgems/mruby-hash-ext/mrblib/hash.rb:119: warning: possibly useless use of a variable in void context
mrbgems/mruby-hash-ext/mrblib/hash.rb:259: warning: assigned but unused variable - keys
mrbgems/mruby-io/mrblib/io.rb:229: warning: literal in condition
mrbgems/mruby-io/mrblib/io.rb:280: warning: literal in condition
mrbgems/mruby-string-ext/mrblib/string.rb:347: warning: assigned but unused variable - len
mrbgems/mruby-toplevel-ext/mrblib/toplevel.rb:2: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
```
2021-06-28 23:21:47 +09:00
John Bampton dab5502e8a Run pre-commit with GitHub Actions
Running pre-commit with GitHub Actions now gives us more tests and coverage

Remove duplicate GitHub Actions for merge conflicts and trailing whitespace

Remove duplicate checks for markdownlint and yamllint from the GitHub Super-Linter

Add new custom pre-commit hook running with a shell script to sort alphabetically and uniquify codespell.txt

Add new pre-commit hook to check spelling with codespell

https://github.com/codespell-project/codespell

Fix spelling
2021-06-16 08:34:54 +10:00
Yukihiro "Matz" Matsumoto ae4c952d2e mruby-array-ext/array.c: implement Array#rotate in C.
The Ruby version of `Array#rotate!` generated a rotated array and
replaced the receiver, but the C version rotates the receiver array
in-place. So the performance is improved a lot both in speed and memory
consumption. Look for the comments in `array.c` for the in-place rotating
algorithm, if you are interested.
2021-05-15 17:26:36 +09:00
Yukihiro "Matz" Matsumoto 9422fdbc87 mruby-array-ext/array.c: implement Array#compact in C. 2021-05-14 10:39:41 +09:00
Yukihiro "Matz" Matsumoto bdb5d85adc array.rb: replace can't with cannot.
To avoid editor coloring failures.
2021-05-14 10:34:11 +09:00
Yukihiro "Matz" Matsumoto aff3743c12 array.rb: Array#uniq to return always Array.
Even called for subclass of `Array`, according to new Ruby3.0 behavior.
Other methods of `Array` behave as Ruby3.0 from the first hand.
2021-04-28 17:45:19 +09:00
Yukihiro "Matz" Matsumoto 737254588b array.rb: add Array#intersect? from Ruby3.0.1. 2021-04-17 17:38:44 +09:00
dearblue 7ef4e54370 Comment out warn used in the Array#fetch method
I get an error because the current mruby does not have a `Kernel#warn` method.
But the warning itself is useful and I'll just comment it out in case it's implemented in the future.
2021-02-20 18:19:00 +09:00
Yukihiro "Matz" Matsumoto d9bd77b180 Fix A.new([[1,2],3]).flatten to return Array. 2020-11-05 14:29:26 +09:00
Hiroshi Mimaki e24b4a1e19 Delete duplicated Array#delete_if. 2020-01-21 11:35:53 +09:00
KOBAYASHI Shuji ac29638a8a Fix the example of Array#intersection in the document [ci skip] 2019-10-14 18:22:00 +09:00
Yukihiro "Matz" Matsumoto bdacdfaea9 Add Array#intersection which is new in Ruby2.7. 2019-10-14 17:04:19 +09:00