Merge pull request #6777 from dearblue/array-combination.4

This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-04-23 18:59:05 +09:00
committed by GitHub
2 changed files with 4 additions and 10 deletions
+1 -8
View File
@@ -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
+3 -2
View File
@@ -1600,10 +1600,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;