mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
merge revision(s) r46417,r46418: [Backport #9939]
* array.c (yield_indexed_values): extract from permute0(), rpermute0(), and rcombinate0(). * 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/branches/ruby_2_1@46808 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
aa8380e17a
commit
99ea5ebb9e
4 changed files with 48 additions and 45 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Sun Jul 13 23:10:03 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* array.c (yield_indexed_values): extract from permute0(),
|
||||||
|
rpermute0(), and rcombinate0().
|
||||||
|
|
||||||
Sun Jul 13 23:02:36 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sun Jul 13 23:02:36 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* array.c (rb_ary_permutation): `p` is the array of size `r`, as
|
* array.c (rb_ary_permutation): `p` is the array of size `r`, as
|
||||||
|
|
77
array.c
77
array.c
|
@ -4682,6 +4682,25 @@ rb_ary_cycle(int argc, VALUE *argv, VALUE ary)
|
||||||
#define tmpary(n) rb_ary_tmp_new(n)
|
#define tmpary(n) rb_ary_tmp_new(n)
|
||||||
#define tmpary_discard(a) (ary_discard(a), RBASIC_SET_CLASS_RAW(a, rb_cArray))
|
#define tmpary_discard(a) (ary_discard(a), RBASIC_SET_CLASS_RAW(a, rb_cArray))
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Build a ruby array of the corresponding values and yield it to the
|
||||||
|
* associated block.
|
||||||
|
* Return the class of +values+ for reentry check.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
yield_indexed_values(const VALUE values, const long r, const long *const p)
|
||||||
|
{
|
||||||
|
const VALUE result = rb_ary_new2(r);
|
||||||
|
VALUE *const result_array = RARRAY_PTR(result);
|
||||||
|
const VALUE *const values_array = RARRAY_CONST_PTR(values);
|
||||||
|
long i;
|
||||||
|
|
||||||
|
for (i = 0; i < r; i++) result_array[i] = values_array[p[i]];
|
||||||
|
ARY_SET_LEN(result, r);
|
||||||
|
rb_yield(result);
|
||||||
|
return !RBASIC(values)->klass;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Recursively compute permutations of +r+ elements of the set
|
* Recursively compute permutations of +r+ elements of the set
|
||||||
* <code>[0..n-1]</code>.
|
* <code>[0..n-1]</code>.
|
||||||
|
@ -4699,7 +4718,7 @@ rb_ary_cycle(int argc, VALUE *argv, VALUE ary)
|
||||||
static void
|
static void
|
||||||
permute0(long n, long r, long *p, long index, char *used, VALUE values)
|
permute0(long n, long r, long *p, long index, char *used, VALUE values)
|
||||||
{
|
{
|
||||||
long i,j;
|
long i;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
if (used[i] == 0) {
|
if (used[i] == 0) {
|
||||||
p[index] = i;
|
p[index] = i;
|
||||||
|
@ -4710,17 +4729,7 @@ permute0(long n, long r, long *p, long index, char *used, VALUE values)
|
||||||
used[i] = 0; /* index unused */
|
used[i] = 0; /* index unused */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* We have a complete permutation of array indexes */
|
if (!yield_indexed_values(values, r, p)) {
|
||||||
/* Build a ruby array of the corresponding values */
|
|
||||||
/* And yield it to the associated block */
|
|
||||||
VALUE result = rb_ary_new2(r);
|
|
||||||
VALUE *result_array = RARRAY_PTR(result);
|
|
||||||
const VALUE *values_array = RARRAY_PTR(values);
|
|
||||||
|
|
||||||
for (j = 0; j < r; j++) result_array[j] = values_array[p[j]];
|
|
||||||
ARY_SET_LEN(result, r);
|
|
||||||
rb_yield(result);
|
|
||||||
if (RBASIC(values)->klass) {
|
|
||||||
rb_raise(rb_eRuntimeError, "permute reentered");
|
rb_raise(rb_eRuntimeError, "permute reentered");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4889,21 +4898,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 {
|
||||||
|
@ -4912,8 +4919,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;
|
||||||
}
|
}
|
||||||
|
@ -4934,24 +4941,14 @@ rb_ary_combination(VALUE ary, VALUE num)
|
||||||
static void
|
static void
|
||||||
rpermute0(long n, long r, long *p, long index, VALUE values)
|
rpermute0(long n, long r, long *p, long index, VALUE values)
|
||||||
{
|
{
|
||||||
long i, j;
|
long i;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
p[index] = i;
|
p[index] = i;
|
||||||
if (index < r-1) { /* if not done yet */
|
if (index < r-1) { /* if not done yet */
|
||||||
rpermute0(n, r, p, index+1, values); /* recurse */
|
rpermute0(n, r, p, index+1, values); /* recurse */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* We have a complete permutation of array indexes */
|
if (!yield_indexed_values(values, r, p)) {
|
||||||
/* Build a ruby array of the corresponding values */
|
|
||||||
/* And yield it to the associated block */
|
|
||||||
VALUE result = rb_ary_new2(r);
|
|
||||||
VALUE *result_array = RARRAY_PTR(result);
|
|
||||||
const VALUE *values_array = RARRAY_PTR(values);
|
|
||||||
|
|
||||||
for (j = 0; j < r; j++) result_array[j] = values_array[p[j]];
|
|
||||||
ARY_SET_LEN(result, r);
|
|
||||||
rb_yield(result);
|
|
||||||
if (RBASIC(values)->klass) {
|
|
||||||
rb_raise(rb_eRuntimeError, "repeated permute reentered");
|
rb_raise(rb_eRuntimeError, "repeated permute reentered");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5032,7 +5029,6 @@ rb_ary_repeated_permutation(VALUE ary, VALUE num)
|
||||||
static void
|
static void
|
||||||
rcombinate0(long n, long r, long *p, long index, long rest, VALUE values)
|
rcombinate0(long n, long r, long *p, long index, long rest, VALUE values)
|
||||||
{
|
{
|
||||||
long j;
|
|
||||||
if (rest > 0) {
|
if (rest > 0) {
|
||||||
for (; index < n; ++index) {
|
for (; index < n; ++index) {
|
||||||
p[r-rest] = index;
|
p[r-rest] = index;
|
||||||
|
@ -5040,14 +5036,7 @@ rcombinate0(long n, long r, long *p, long index, long rest, VALUE values)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
VALUE result = rb_ary_new2(r);
|
if (!yield_indexed_values(values, r, p)) {
|
||||||
VALUE *result_array = RARRAY_PTR(result);
|
|
||||||
const VALUE *values_array = RARRAY_PTR(values);
|
|
||||||
|
|
||||||
for (j = 0; j < r; ++j) result_array[j] = values_array[p[j]];
|
|
||||||
ARY_SET_LEN(result, r);
|
|
||||||
rb_yield(result);
|
|
||||||
if (RBASIC(values)->klass) {
|
|
||||||
rb_raise(rb_eRuntimeError, "repeated combination reentered");
|
rb_raise(rb_eRuntimeError, "repeated combination reentered");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#define RUBY_VERSION "2.1.2"
|
#define RUBY_VERSION "2.1.2"
|
||||||
#define RUBY_RELEASE_DATE "2014-07-13"
|
#define RUBY_RELEASE_DATE "2014-07-13"
|
||||||
#define RUBY_PATCHLEVEL 172
|
#define RUBY_PATCHLEVEL 173
|
||||||
|
|
||||||
#define RUBY_RELEASE_YEAR 2014
|
#define RUBY_RELEASE_YEAR 2014
|
||||||
#define RUBY_RELEASE_MONTH 7
|
#define RUBY_RELEASE_MONTH 7
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue