array.c: combination on a shared copy

* array.c (rb_ary_combination): iterate on a shared copy, and use
  array of indexes instead of array of chosen objects.
  [ruby-core:63149] [Bug #9939]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46418 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-06-13 08:34:17 +00:00
parent 95bac4f75e
commit 537b4a6422
3 changed files with 23 additions and 12 deletions

View File

@ -1,4 +1,8 @@
Fri Jun 13 17:32:56 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> Fri Jun 13 17:33:14 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* array.c (rb_ary_combination): iterate on a shared copy, and use
array of indexes instead of array of chosen objects.
[ruby-core:63149] [Bug #9939]
* array.c (yield_indexed_values): extract from permute0(), * array.c (yield_indexed_values): extract from permute0(),
rpermute0(), and rcombinate0(). rpermute0(), and rcombinate0().

20
array.c
View File

@ -4938,21 +4938,19 @@ rb_ary_combination(VALUE ary, VALUE num)
} }
} }
else { else {
volatile VALUE t0 = tmpbuf(n+1, sizeof(long)); VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
long *stack = (long*)RSTRING_PTR(t0); volatile VALUE t0;
volatile VALUE cc = tmpary(n); long *stack = ALLOCV_N(long, t0, n+1);
VALUE *chosen = RARRAY_PTR(cc);
long lev = 0; long lev = 0;
MEMZERO(stack, long, n); RBASIC_CLEAR_CLASS(ary0);
MEMZERO(stack+1, long, n);
stack[0] = -1; stack[0] = -1;
for (;;) { for (;;) {
chosen[lev] = RARRAY_AREF(ary, stack[lev+1]);
for (lev++; lev < n; lev++) { for (lev++; lev < n; lev++) {
chosen[lev] = RARRAY_AREF(ary, stack[lev+1] = stack[lev]+1); stack[lev+1] = stack[lev]+1;
} }
rb_yield(rb_ary_new4(n, chosen)); if (!yield_indexed_values(ary0, n, stack+1)) {
if (RBASIC(t0)->klass) {
rb_raise(rb_eRuntimeError, "combination reentered"); rb_raise(rb_eRuntimeError, "combination reentered");
} }
do { do {
@ -4961,8 +4959,8 @@ rb_ary_combination(VALUE ary, VALUE num)
} while (stack[lev+1]+n == len+lev+1); } while (stack[lev+1]+n == len+lev+1);
} }
done: done:
tmpbuf_discard(t0); ALLOCV_END(t0);
tmpary_discard(cc); RBASIC_SET_CLASS_RAW(ary0, rb_cArray);
} }
return ary; return ary;
} }

View File

@ -2291,6 +2291,15 @@ class TestArray < Test::Unit::TestCase
assert_equal(:called, (0..100).to_a.combination(50) { break :called }, "[ruby-core:29240] ... must be yielded even if 100C50 > signed integer") assert_equal(:called, (0..100).to_a.combination(50) { break :called }, "[ruby-core:29240] ... must be yielded even if 100C50 > signed integer")
end end
def test_combination_clear
bug9939 = '[ruby-core:63149] [Bug #9939]'
assert_separately([], <<-'end;')
100_000.times {Array.new(1000)}
a = [*0..100]
a.combination(3) {|*,x| a.clear}
end;
end
def test_product2 def test_product2
a = (0..100).to_a a = (0..100).to_a
assert_raise(RangeError) do assert_raise(RangeError) do