summaryrefslogtreecommitdiff
path: root/array.c
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 /array.c
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
Diffstat (limited to 'array.c')
-rw-r--r--array.c20
1 files changed, 9 insertions, 11 deletions
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;
}