From d246ac6c8a757a4084e8515bcee7f021b0686082 Mon Sep 17 00:00:00 2001 From: dearblue Date: Wed, 8 Apr 2026 21:25:09 +0900 Subject: [PATCH] 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. --- mrbgems/mruby-array-ext/mrblib/array.rb | 9 +-------- mrbgems/mruby-array-ext/src/array.c | 5 +++-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index bcfdc1ddc..524d01658 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -689,14 +689,7 @@ class Array if n > 0 # Use C iterator for complex cases state = __combination_init(n, permutation) - while (indices = __combination_next(state)) - # Convert indices to elements in Ruby - tmp = [nil] * n - i = 0 - while i < n - tmp[i] = self[indices[i]] - i += 1 - end + while tmp = __combination_next(state) yield tmp end end diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index 70473c087..821538810 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -1608,10 +1608,11 @@ ary_combination_next(mrb_state *mrb, mrb_value self) } } - /* Build current combination indices */ + /* Build current combination */ mrb_value result = mrb_ary_new_capa(mrb, state->n); + const mrb_value *p = RARRAY_PTR(self); for (mrb_int i = 0; i < state->n; i++) { - mrb_ary_push(mrb, result, mrb_fixnum_value(state->indices[i])); + mrb_ary_push(mrb, result, p[state->indices[i]]); } mrb_int pos = state->n - 1;