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

ruby-changes:34344

From: nobu <ko1@a...>
Date: Sat, 14 Jun 2014 10:54:36 +0900 (JST)
Subject: [ruby-changes:34344] nobu:r46425 (trunk): array.c: combinate0

nobu	2014-06-14 10:54:33 +0900 (Sat, 14 Jun 2014)

  New Revision: 46425

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

  Log:
    array.c: combinate0
    
    * array.c (combinate0): extract.

  Modified files:
    trunk/array.c
Index: array.c
===================================================================
--- array.c	(revision 46424)
+++ array.c	(revision 46425)
@@ -4884,6 +4884,27 @@ rb_ary_permutation(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/array.c#L4884
     return ary;
 }
 
+static void
+combinate0(const long len, const long n, long *const stack, const VALUE values)
+{
+    long lev = 0;
+
+    MEMZERO(stack+1, long, n);
+    stack[0] = -1;
+    for (;;) {
+	for (lev++; lev < n; lev++) {
+	    stack[lev+1] = stack[lev]+1;
+	}
+	if (!yield_indexed_values(values, n, stack+1)) {
+	    rb_raise(rb_eRuntimeError, "combination reentered");
+	}
+	do {
+	    if (lev == 0) return;
+	    stack[lev--]++;
+	} while (stack[lev+1]+n == len+lev+1);
+    }
+}
+
 static VALUE
 rb_ary_combination_size(VALUE ary, VALUE args, VALUE eobj)
 {
@@ -4921,7 +4942,7 @@ rb_ary_combination_size(VALUE ary, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L4942
 static VALUE
 rb_ary_combination(VALUE ary, VALUE num)
 {
-    long n, i, len;
+    long i, n, len;
 
     n = NUM2LONG(num);
     RETURN_SIZED_ENUMERATOR(ary, 1, &num, rb_ary_combination_size);
@@ -4941,24 +4962,9 @@ rb_ary_combination(VALUE ary, VALUE num) https://github.com/ruby/ruby/blob/trunk/array.c#L4962
 	VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
 	volatile VALUE t0;
 	long *stack = ALLOCV_N(long, t0, n+1);
-	long lev = 0;
 
 	RBASIC_CLEAR_CLASS(ary0);
-	MEMZERO(stack+1, long, n);
-	stack[0] = -1;
-	for (;;) {
-	    for (lev++; lev < n; lev++) {
-		stack[lev+1] = stack[lev]+1;
-	    }
-	    if (!yield_indexed_values(ary0, n, stack+1)) {
-		rb_raise(rb_eRuntimeError, "combination reentered");
-	    }
-	    do {
-		if (lev == 0) goto done;
-		stack[lev--]++;
-	    } while (stack[lev+1]+n == len+lev+1);
-	}
-    done:
+	combinate0(len, n, stack, ary0);
 	ALLOCV_END(t0);
 	RBASIC_SET_CLASS_RAW(ary0, rb_cArray);
     }

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

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