0001-array.c-rb_ary_permutation-rb_ary_combiation-test-ru.patch
| b/ChangeLog | ||
|---|---|---|
| 1 |
Thu Jan 28 11:48:00 Kenta Murata <mrkn@mrkn.jp> |
|
| 2 | ||
| 3 |
* array.c (rb_ary_permutation, rb_ary_combiation), test/ruby/test_array.rb |
|
| 4 |
(TestArray#test_permutation_length, TestArray#test_combination_length): |
|
| 5 |
length method was added for an enumerator generated by Array#permutation |
|
| 6 |
and Array#combination. |
|
| 7 | ||
| 1 | 8 |
Thu Jan 28 09:44:19 2010 NARUSE, Yui <naruse@ruby-lang.org> |
| 2 | 9 | |
| 3 | 10 |
* regcomp.c (onig_compile): initialize ScanEnv. |
| b/array.c | ||
|---|---|---|
| 3853 | 3853 |
long r, n, i; |
| 3854 | 3854 | |
| 3855 | 3855 |
n = RARRAY_LEN(ary); /* Array length */ |
| 3856 |
RETURN_ENUMERATOR(ary, argc, argv); /* Return enumerator if no block */ |
|
| 3857 | 3856 |
rb_scan_args(argc, argv, "01", &num); |
| 3858 | 3857 |
r = NIL_P(num) ? n : NUM2LONG(num); /* Permutation size from argument */ |
| 3859 | ||
| 3858 |
if (!rb_block_given_p()) {
|
|
| 3859 |
VALUE enu = rb_enumeratorize(ary, ID2SYM(rb_frame_this_func()), argc, argv); |
|
| 3860 |
long nlen = (r == 0) ? 1 : (r == 1) ? n : (r <= n) ? (r-1)*n : 0; |
|
| 3861 |
rb_iv_set(enu, "@length", LONG2NUM(nlen)); |
|
| 3862 |
rb_define_attr(rb_singleton_class(enu), "length", 1, 0); |
|
| 3863 |
return enu; |
|
| 3864 |
} |
|
| 3860 | 3865 |
if (r < 0 || n < r) {
|
| 3861 | 3866 |
/* no permutations: yield nothing */ |
| 3862 | 3867 |
} |
| ... | ... | |
| 3936 | 3941 |
long n, i, len; |
| 3937 | 3942 | |
| 3938 | 3943 |
n = NUM2LONG(num); |
| 3939 |
RETURN_ENUMERATOR(ary, 1, &num); |
|
| 3940 | 3944 |
len = RARRAY_LEN(ary); |
| 3945 |
if (!rb_block_given_p()) {
|
|
| 3946 |
VALUE enu = rb_enumeratorize(ary, ID2SYM(rb_frame_this_func()), 1, &num); |
|
| 3947 |
long nlen = combi_len(len, n); |
|
| 3948 |
rb_iv_set(enu, "@length", LONG2NUM(nlen)); |
|
| 3949 |
rb_define_attr(rb_singleton_class(enu), "length", 1, 0); |
|
| 3950 |
return enu; |
|
| 3951 |
} |
|
| 3941 | 3952 |
if (n < 0 || len < n) {
|
| 3942 | 3953 |
/* yield nothing */ |
| 3943 | 3954 |
} |
| b/test/ruby/test_array.rb | ||
|---|---|---|
| 1355 | 1355 |
assert_equal(@cls[], @cls[1,2,3,4].combination(5).to_a) |
| 1356 | 1356 |
end |
| 1357 | 1357 | |
| 1358 |
def test_combination_length |
|
| 1359 |
assert_equal(6, @cls[1,2,3,4].combination(2).length) |
|
| 1360 |
assert_equal(4, @cls[1,2,3,4].combination(3).length) |
|
| 1361 |
end |
|
| 1362 | ||
| 1358 | 1363 |
def test_product |
| 1359 | 1364 |
assert_equal(@cls[[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]], |
| 1360 | 1365 |
@cls[1,2,3].product([4,5])) |
| ... | ... | |
| 1388 | 1393 |
assert_equal(@cls[1, 2, 3, 4].permutation.to_a, b) |
| 1389 | 1394 |
end |
| 1390 | 1395 | |
| 1396 |
def test_permutation_length |
|
| 1397 |
assert_equal(6, @cls[1, 2, 3].permutation.length) |
|
| 1398 |
assert_equal(1, @cls[1, 2, 3].permutation(0).length) |
|
| 1399 |
assert_equal(3, @cls[1, 2, 3].permutation(1).length) |
|
| 1400 |
assert_equal(6, @cls[1, 2, 3].permutation(3).length) |
|
| 1401 |
assert_equal(0, @cls[1, 2, 3].permutation(4).length) |
|
| 1402 |
end |
|
| 1403 | ||
| 1391 | 1404 |
def test_take |
| 1392 | 1405 |
assert_equal([1,2,3], [1,2,3,4,5,0].take(3)) |
| 1393 | 1406 |
assert_raise(ArgumentError, '[ruby-dev:34123]') { [1,2].take(-1) }
|
| 1394 |
- |
|