summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-06-13 08:34:17 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-06-13 08:34:17 +0000
commit537b4a64227aebf357a5c3f570e01be5f2f824fd (patch)
tree7b090ed0a815cfe046566f64ed76e44a3f59a520
parent95bac4f75e0f8ccaf0b54c848b6a187cda63b115 (diff)
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
-rw-r--r--ChangeLog6
-rw-r--r--array.c20
-rw-r--r--test/ruby/test_array.rb9
3 files changed, 23 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index b3dad59e84..06f85d8fc1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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().
diff --git a/array.c b/array.c
index f23c58336e..77a0ea6690 100644
--- a/array.c
+++ b/array.c
@@ -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;
}
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 676b20b1c4..a3d7163605 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -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