* array.c (rb_ary_permutation, rb_ary_repeated_permutation, rb_ary_repeated_combination, rb_ary_product): use ary_make_shared_copy instead of ary_make_substitution. [ruby-dev:42067] [Bug #3708]
* test/ruby/test_array.rb (test_product, test_repeated_permutation, test_repeated_combination): append assertions against [Bug #3708]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29044 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
fee5e4ff5f
commit
1d04aedb5a
10
ChangeLog
10
ChangeLog
@ -1,3 +1,13 @@
|
|||||||
|
Thu Aug 19 12:04:39 2010 Kenta Murata <mrkn@mrkn.jp>
|
||||||
|
|
||||||
|
* array.c (rb_ary_permutation, rb_ary_repeated_permutation,
|
||||||
|
rb_ary_repeated_combination, rb_ary_product):
|
||||||
|
use ary_make_shared_copy instead of ary_make_substitution.
|
||||||
|
[ruby-dev:42067] [Bug #3708]
|
||||||
|
|
||||||
|
* test/ruby/test_array.rb (test_product, test_repeated_permutation,
|
||||||
|
test_repeated_combination): append assertions against [Bug #3708].
|
||||||
|
|
||||||
Thu Aug 19 11:11:24 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
Thu Aug 19 11:11:24 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* enum.c (enum_inject): fix typo of rdoc.
|
* enum.c (enum_inject): fix typo of rdoc.
|
||||||
|
8
array.c
8
array.c
@ -4010,7 +4010,7 @@ rb_ary_permutation(int argc, VALUE *argv, VALUE ary)
|
|||||||
long *p = (long*)RSTRING_PTR(t0);
|
long *p = (long*)RSTRING_PTR(t0);
|
||||||
volatile VALUE t1 = tmpbuf(n,sizeof(char));
|
volatile VALUE t1 = tmpbuf(n,sizeof(char));
|
||||||
char *used = (char*)RSTRING_PTR(t1);
|
char *used = (char*)RSTRING_PTR(t1);
|
||||||
VALUE ary0 = ary_make_substitution(ary); /* private defensive copy of ary */
|
VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
|
||||||
RBASIC(ary0)->klass = 0;
|
RBASIC(ary0)->klass = 0;
|
||||||
|
|
||||||
MEMZERO(used, char, n); /* initialize array */
|
MEMZERO(used, char, n); /* initialize array */
|
||||||
@ -4180,7 +4180,7 @@ rb_ary_repeated_permutation(VALUE ary, VALUE num)
|
|||||||
else { /* this is the general case */
|
else { /* this is the general case */
|
||||||
volatile VALUE t0 = tmpbuf(r, sizeof(long));
|
volatile VALUE t0 = tmpbuf(r, sizeof(long));
|
||||||
long *p = (long*)RSTRING_PTR(t0);
|
long *p = (long*)RSTRING_PTR(t0);
|
||||||
VALUE ary0 = ary_make_substitution(ary); /* private defensive copy of ary */
|
VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
|
||||||
RBASIC(ary0)->klass = 0;
|
RBASIC(ary0)->klass = 0;
|
||||||
|
|
||||||
rpermute0(n, r, p, 0, ary0); /* compute and yield repeated permutations */
|
rpermute0(n, r, p, 0, ary0); /* compute and yield repeated permutations */
|
||||||
@ -4266,7 +4266,7 @@ rb_ary_repeated_combination(VALUE ary, VALUE num)
|
|||||||
else {
|
else {
|
||||||
volatile VALUE t0 = tmpbuf(n, sizeof(long));
|
volatile VALUE t0 = tmpbuf(n, sizeof(long));
|
||||||
long *p = (long*)RSTRING_PTR(t0);
|
long *p = (long*)RSTRING_PTR(t0);
|
||||||
VALUE ary0 = ary_make_substitution(ary); /* private defensive copy of ary */
|
VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
|
||||||
RBASIC(ary0)->klass = 0;
|
RBASIC(ary0)->klass = 0;
|
||||||
|
|
||||||
rcombinate0(len, n, p, 0, n, ary0); /* compute and yield repeated combinations */
|
rcombinate0(len, n, p, 0, n, ary0); /* compute and yield repeated combinations */
|
||||||
@ -4325,7 +4325,7 @@ rb_ary_product(int argc, VALUE *argv, VALUE ary)
|
|||||||
/* Make defensive copies of arrays; exit if any is empty */
|
/* Make defensive copies of arrays; exit if any is empty */
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
if (RARRAY_LEN(arrays[i]) == 0) goto done;
|
if (RARRAY_LEN(arrays[i]) == 0) goto done;
|
||||||
arrays[i] = ary_make_substitution(arrays[i]);
|
arrays[i] = ary_make_shared_copy(arrays[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -1528,6 +1528,10 @@ class TestArray < Test::Unit::TestCase
|
|||||||
acc = [1,2].product(*[o]*10)
|
acc = [1,2].product(*[o]*10)
|
||||||
assert_equal([1,2].product([3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4]),
|
assert_equal([1,2].product([3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4]),
|
||||||
acc)
|
acc)
|
||||||
|
|
||||||
|
a = []
|
||||||
|
[1, 2].product([0, 1, 2, 3, 4][1, 4]) {|x| a << x }
|
||||||
|
assert(a.all?{|x| !x.include?(0) })
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_permutation
|
def test_permutation
|
||||||
@ -1574,6 +1578,9 @@ class TestArray < Test::Unit::TestCase
|
|||||||
a.repeated_permutation(4) {|x| b << x; a.replace(@cls[9, 8, 7, 6]) }
|
a.repeated_permutation(4) {|x| b << x; a.replace(@cls[9, 8, 7, 6]) }
|
||||||
assert_equal(@cls[9, 8, 7, 6], a)
|
assert_equal(@cls[9, 8, 7, 6], a)
|
||||||
assert_equal(@cls[1, 2, 3, 4].repeated_permutation(4).to_a, b)
|
assert_equal(@cls[1, 2, 3, 4].repeated_permutation(4).to_a, b)
|
||||||
|
|
||||||
|
a = @cls[0, 1, 2, 3, 4][1, 4].repeated_permutation(2)
|
||||||
|
assert(a.all?{|x| !x.include?(0) })
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_repeated_combination
|
def test_repeated_combination
|
||||||
@ -1600,6 +1607,9 @@ class TestArray < Test::Unit::TestCase
|
|||||||
a.repeated_combination(4) {|x| b << x; a.replace(@cls[9, 8, 7, 6]) }
|
a.repeated_combination(4) {|x| b << x; a.replace(@cls[9, 8, 7, 6]) }
|
||||||
assert_equal(@cls[9, 8, 7, 6], a)
|
assert_equal(@cls[9, 8, 7, 6], a)
|
||||||
assert_equal(@cls[1, 2, 3, 4].repeated_combination(4).to_a, b)
|
assert_equal(@cls[1, 2, 3, 4].repeated_combination(4).to_a, b)
|
||||||
|
|
||||||
|
a = @cls[0, 1, 2, 3, 4][1, 4].repeated_combination(2)
|
||||||
|
assert(a.all?{|x| !x.include?(0) })
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_take
|
def test_take
|
||||||
|
Loading…
x
Reference in New Issue
Block a user