[前][次][番号順一覧][スレッド一覧]

ruby-changes:25447

From: marcandre <ko1@a...>
Date: Wed, 7 Nov 2012 02:11:59 +0900 (JST)
Subject: [ruby-changes:25447] marcandRe: r37504 (trunk): * array.c (rb_ary_repeated_combination): Support for repeated_combination.size

marcandre	2012-11-07 02:11:51 +0900 (Wed, 07 Nov 2012)

  New Revision: 37504

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37504

  Log:
    * array.c (rb_ary_repeated_combination): Support for repeated_combination.size
      [Feature #6636]

  Modified files:
    trunk/array.c
    trunk/test/ruby/test_enumerator.rb

Index: array.c
===================================================================
--- array.c	(revision 37503)
+++ array.c	(revision 37504)
@@ -4645,6 +4645,17 @@
     }
 }
 
+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:
  *     ary.repeated_combination(n) { |c| block } -> ary
@@ -4678,7 +4689,7 @@
     long n, i, len;
 
     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);
     if (n < 0) {
 	/* yield nothing */
Index: test/ruby/test_enumerator.rb
===================================================================
--- test/ruby/test_enumerator.rb	(revision 37503)
+++ test/ruby/test_enumerator.rb	(revision 37504)
@@ -450,6 +450,11 @@
     check_consistency_for_combinatorics(:repeated_permutation)
     assert_equal 291733167875766667063796853374976,
       (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
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]