* array.c (rb_ary_repeated_combination): Support for repeated_combination.size
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37504 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
68c90c4a2d
commit
ba59365db9
13
array.c
13
array.c
@ -4645,6 +4645,17 @@ rcombinate0(long n, long r, long *p, long index, long rest, VALUE values)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_ary_repeated_combination_size(VALUE ary, VALUE args)
|
||||||
|
{
|
||||||
|
long n = RARRAY_LEN(ary);
|
||||||
|
long k = NUM2LONG(RARRAY_PTR(args)[0]);
|
||||||
|
if (k == 0) {
|
||||||
|
return LONG2FIX(1);
|
||||||
|
}
|
||||||
|
return binomial_coefficient(k, n + k - 1);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* ary.repeated_combination(n) { |c| block } -> ary
|
* ary.repeated_combination(n) { |c| block } -> ary
|
||||||
@ -4678,7 +4689,7 @@ rb_ary_repeated_combination(VALUE ary, VALUE num)
|
|||||||
long n, i, len;
|
long n, i, len;
|
||||||
|
|
||||||
n = NUM2LONG(num); /* Combination size from argument */
|
n = NUM2LONG(num); /* Combination size from argument */
|
||||||
RETURN_ENUMERATOR(ary, 1, &num); /* Return Enumerator if no block */
|
RETURN_SIZED_ENUMERATOR(ary, 1, &num, rb_ary_repeated_combination_size); /* Return enumerator if no block */
|
||||||
len = RARRAY_LEN(ary);
|
len = RARRAY_LEN(ary);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
/* yield nothing */
|
/* yield nothing */
|
||||||
|
@ -450,6 +450,11 @@ class TestEnumerator < Test::Unit::TestCase
|
|||||||
check_consistency_for_combinatorics(:repeated_permutation)
|
check_consistency_for_combinatorics(:repeated_permutation)
|
||||||
assert_equal 291733167875766667063796853374976,
|
assert_equal 291733167875766667063796853374976,
|
||||||
(1..42).to_a.repeated_permutation(20).size # 42 ** 20
|
(1..42).to_a.repeated_permutation(20).size # 42 ** 20
|
||||||
|
|
||||||
|
check_consistency_for_combinatorics(:repeated_combination)
|
||||||
|
assert_equal 28258808871162574166368460400,
|
||||||
|
(1..59).to_a.repeated_combination(42).size
|
||||||
|
# 1.upto(100).inject(:*) / 1.upto(42).inject(:*) / 1.upto(58).inject(:*)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user