1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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 ]

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(),
rpermute0(), and rcombinate0().

20
array.c
View file

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