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

ruby-changes:47575

From: nobu <ko1@a...>
Date: Wed, 30 Aug 2017 16:55:25 +0900 (JST)
Subject: [ruby-changes:47575] nobu:r59691 (trunk): array.c: refine descending_factorial

nobu	2017-08-30 16:55:19 +0900 (Wed, 30 Aug 2017)

  New Revision: 59691

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59691

  Log:
    array.c: refine descending_factorial
    
    * array.c (descending_factorial): reduce factorial multipication.

  Modified files:
    trunk/array.c
Index: array.c
===================================================================
--- array.c	(revision 59690)
+++ array.c	(revision 59691)
@@ -5079,10 +5079,16 @@ permute0(const long n, const long r, lon https://github.com/ruby/ruby/blob/trunk/array.c#L5079
 static VALUE
 descending_factorial(long from, long how_many)
 {
-    VALUE cnt = LONG2FIX(how_many >= 0);
-    while (how_many-- > 0) {
-	VALUE v = LONG2FIX(from--);
-	cnt = rb_int_mul(cnt, v);
+    VALUE cnt;
+    if (how_many > 0) {
+	cnt = LONG2FIX(from);
+	while (--how_many > 0) {
+	    long v = --from;
+	    cnt = rb_int_mul(cnt, LONG2FIX(v));
+	}
+    }
+    else {
+	cnt = LONG2FIX(how_many == 0);
     }
     return cnt;
 }

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

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