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

ruby-changes:4482

From: ko1@a...
Date: Fri, 11 Apr 2008 17:27:28 +0900 (JST)
Subject: [ruby-changes:4482] knu - Ruby:r15974 (trunk): * enum.c (count_i, count_iter_i, enum_count, enum_find_index):

knu	2008-04-11 17:26:45 +0900 (Fri, 11 Apr 2008)

  New Revision: 15974

  Modified files:
    trunk/ChangeLog
    trunk/enum.c

  Log:
    * enum.c (count_i, count_iter_i, enum_count, enum_find_index):
      Reduce code.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15974&r2=15973&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/enum.c?r1=15974&r2=15973&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15973)
+++ ChangeLog	(revision 15974)
@@ -1,3 +1,8 @@
+Fri Apr 11 17:25:09 2008  Akinori MUSHA  <knu@i...>
+
+	* enum.c (count_i, count_iter_i, enum_count, enum_find_index):
+	  Reduce code.
+
 Fri Apr 11 17:06:01 2008  Yukihiro Matsumoto  <matz@r...>
 
 	* enum.c (find_index_i): modified to shut warning up.
Index: enum.c
===================================================================
--- enum.c	(revision 15973)
+++ enum.c	(revision 15974)
@@ -81,19 +81,23 @@
 }
 
 static VALUE
-count_i(VALUE i, VALUE *arg)
+count_i(VALUE i, VALUE memop)
 {
-    if (rb_equal(i, arg[0])) {
-	arg[1]++;
+    VALUE *memo = (VALUE*)memop;
+
+    if (rb_equal(i, memo[1])) {
+	memo[0]++;
     }
     return Qnil;
 }
 
 static VALUE
-count_iter_i(VALUE i, long *n, int argc, VALUE *argv)
+count_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv)
 {
+    VALUE *memo = (VALUE*)memop;
+
     if (RTEST(enum_yield(argc, argv))) {
-	(*n)++;
+	memo[0]++;
     }
     return Qnil;
 }
@@ -115,31 +119,24 @@
 static VALUE
 enum_count(int argc, VALUE *argv, VALUE obj)
 {
-    if (argc == 1) {
-	VALUE item, args[2];
+    VALUE memo[2];	/* [count, condition value] */
+    rb_block_call_func *func;
 
+    if (argc == 0) {
+	RETURN_ENUMERATOR(obj, 0, 0);
+        func = count_iter_i;
+    }
+    else {
+	rb_scan_args(argc, argv, "1", &memo[1]);
 	if (rb_block_given_p()) {
 	    rb_warn("given block not used");
 	}
-	rb_scan_args(argc, argv, "1", &item);
-	args[0] = item;
-	args[1] = 0;
-	rb_block_call(obj, id_each, 0, 0, count_i, (VALUE)&args);
-	return INT2NUM(args[1]);
+        func = count_i;
     }
-    else if (argc == 0) {
-	long n;
 
-	RETURN_ENUMERATOR(obj, 0, 0);
-	n = 0;
-	rb_block_call(obj, id_each, 0, 0, count_iter_i, (VALUE)&n);
-	return INT2NUM(n);
-    }
-    else {
-        VALUE v;
-	rb_scan_args(argc, argv, "1", &v);
-        return Qnil; /* not reached */
-    }
+    memo[0] = 0;
+    rb_block_call(obj, id_each, 0, 0, func, (VALUE)&memo);
+    return INT2NUM(memo[0]);
 }
 
 static VALUE
@@ -202,6 +199,7 @@
 find_index_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv)
 {
     VALUE *memo = (VALUE*)memop;
+
     if (RTEST(enum_yield(argc, argv))) {
 	memo[0] = UINT2NUM(memo[1]);
 	rb_iter_break();
@@ -234,24 +232,18 @@
 
     if (argc == 0) {
         RETURN_ENUMERATOR(obj, 0, 0);
-        memo[0] = Qnil;
-        memo[1] = 0;
-        memo[2] = Qundef;
         func = find_index_iter_i;
     }
     else {
-        VALUE item;
-
-	rb_scan_args(argc, argv, "1", &item);
+	rb_scan_args(argc, argv, "1", &memo[2]);
 	if (rb_block_given_p()) {
 	    rb_warn("given block not used");
 	}
-        memo[0] = Qnil;
-        memo[1] = 0;
-        memo[2] = item;
         func = find_index_i;
     }
 
+    memo[0] = Qnil;
+    memo[1] = 0;
     rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
     return memo[0];
 }

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

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