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

ruby-changes:5200

From: knu <ko1@a...>
Date: Fri, 30 May 2008 04:36:03 +0900 (JST)
Subject: [ruby-changes:5200] Ruby:r16695 (ruby_1_8_7): Merge from ruby_1_8.

knu	2008-05-30 04:35:50 +0900 (Fri, 30 May 2008)

  New Revision: 16695

  Modified files:
    branches/ruby_1_8_7/ChangeLog
    branches/ruby_1_8_7/array.c
    branches/ruby_1_8_7/enum.c
    branches/ruby_1_8_7/test/ruby/test_array.rb
    branches/ruby_1_8_7/test/ruby/test_enum.rb
    branches/ruby_1_8_7/version.h

  Log:
    Merge from ruby_1_8.

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/enum.c?r1=16695&r2=16694&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/test/ruby/test_enum.rb?r1=16695&r2=16694&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/ChangeLog?r1=16695&r2=16694&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/test/ruby/test_array.rb?r1=16695&r2=16694&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/version.h?r1=16695&r2=16694&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/array.c?r1=16695&r2=16694&diff_format=u

Index: ruby_1_8_7/array.c
===================================================================
--- ruby_1_8_7/array.c	(revision 16694)
+++ ruby_1_8_7/array.c	(revision 16695)
@@ -3064,27 +3064,34 @@
 
 /*
  *  call-seq:
+ *     array.count      -> int
  *     array.count(obj) -> int
  *     array.count { |item| block }  -> int
  *  
- *  Returns the number of elements which equals to <i>obj</i>.
- *  If a block is given, counts the number of elements yielding a true value.
+ *  Returns the number of elements.  If an argument is given, counts
+ *  the number of elements which equals to <i>obj</i>.  If a block is
+ *  given, counts the number of elements yielding a true value.
  *
  *     ary = [1, 2, 4, 2]
+ *     ary.count             # => 4
  *     ary.count(2)          # => 2
  *     ary.count{|x|x%2==0}  # => 3
  *
  */
 
 static VALUE
-rb_ary_count(int argc, VALUE *argv, VALUE ary)
+rb_ary_count(argc, argv, ary)
+    int argc;
+    VALUE *argv;
+    VALUE ary;
 {
     long n = 0;
 
     if (argc == 0) {
 	VALUE *p, *pend;
 
-	RETURN_ENUMERATOR(ary, 0, 0);
+	if (!rb_block_given_p())
+	    return LONG2NUM(RARRAY_LEN(ary));
 
 	for (p = RARRAY_PTR(ary), pend = p + RARRAY_LEN(ary); p < pend; p++) {
 	    if (RTEST(rb_yield(*p))) n++;
Index: ruby_1_8_7/ChangeLog
===================================================================
--- ruby_1_8_7/ChangeLog	(revision 16694)
+++ ruby_1_8_7/ChangeLog	(revision 16695)
@@ -1,3 +1,10 @@
+Fri May 30 04:32:07 2008  Akinori MUSHA  <knu@i...>
+
+	* enum.c (count_i, count_iter_i): Sync with trunk.
+	  enum.c (enum_count, count_all_i, Init_Enumerable),
+	  array.c (rb_ary_count): Sync with trunk.  If no argument or
+	  block is given, count the number of all elements.
+
 Fri May 30 03:12:18 2008  Akinori MUSHA  <knu@i...>
 
 	* ext/openssl/ossl_bn.c (ossl_bn_s_rand, ossl_bn_s_pseudo_rand):
Index: ruby_1_8_7/version.h
===================================================================
--- ruby_1_8_7/version.h	(revision 16694)
+++ ruby_1_8_7/version.h	(revision 16695)
@@ -1,7 +1,7 @@
 #define RUBY_VERSION "1.8.7"
-#define RUBY_RELEASE_DATE "2008-05-28"
+#define RUBY_RELEASE_DATE "2008-05-30"
 #define RUBY_VERSION_CODE 187
-#define RUBY_RELEASE_CODE 20080528
+#define RUBY_RELEASE_CODE 20080530
 #define RUBY_PATCHLEVEL 0
 
 #define RUBY_VERSION_MAJOR 1
@@ -9,7 +9,7 @@
 #define RUBY_VERSION_TEENY 7
 #define RUBY_RELEASE_YEAR 2008
 #define RUBY_RELEASE_MONTH 5
-#define RUBY_RELEASE_DAY 28
+#define RUBY_RELEASE_DAY 30
 
 #ifdef RUBY_EXTERN
 RUBY_EXTERN const char ruby_version[];
Index: ruby_1_8_7/enum.c
===================================================================
--- ruby_1_8_7/enum.c	(revision 16694)
+++ ruby_1_8_7/enum.c	(revision 16695)
@@ -15,7 +15,7 @@
 #include "util.h"
 
 VALUE rb_mEnumerable;
-static ID id_each, id_eqq, id_cmp;
+static ID id_each, id_eqq, id_cmp, id_size;
 
 struct iter_method_arg {
     VALUE obj;
@@ -114,36 +114,53 @@
 }
 
 static VALUE
-count_i(i, arg)
-     VALUE i;
-     VALUE *arg;
+count_i(i, memop)
+    VALUE i, 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(i, n)
-     VALUE i;
-     long *n;
+count_iter_i(i, memop)
+    VALUE i, memop;
 {
+    VALUE *memo = (VALUE*)memop;
+
     if (RTEST(rb_yield(i))) {
-	(*n)++;
+	memo[0]++;
     }
     return Qnil;
 }
 
+static VALUE
+count_all_i(i, memop)
+    VALUE i, memop;
+{
+    VALUE *memo = (VALUE*)memop;
+
+    memo[0]++;
+    return Qnil;
+}
+
 /*
  *  call-seq:
+ *     enum.count                   => int
  *     enum.count(item)             => int
  *     enum.count {| obj | block }  => int
  *
- *  Returns the number of items in <i>enum</i> for which equals to <i>item</i>.
- *  If a block is given, counts the number of elements yielding a true value.
+ *  Returns the number of items in <i>enum</i>, where #size is called
+ *  if it responds to it, otherwise the items are counted through
+ *  enumeration.  If an argument is given, counts the number of items
+ *  in <i>enum</i>, for which equals to <i>item</i>.  If a block is
+ *  given, counts the number of elements yielding a true value.
  *
  *     ary = [1, 2, 4, 2]
+ *     ary.count             # => 4
  *     ary.count(2)          # => 2
  *     ary.count{|x|x%2==0}  # => 3
  *
@@ -155,31 +172,31 @@
     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) {
 	if (rb_block_given_p()) {
+	    func = count_iter_i;
+	}
+	else {
+	    if (rb_respond_to(obj, id_size)) {
+		return rb_funcall(obj, id_size, 0, 0);
+	    }
+	    func = count_all_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
@@ -1866,5 +1883,6 @@
     id_eqq  = rb_intern("===");
     id_each = rb_intern("each");
     id_cmp  = rb_intern("<=>");
+    id_size = rb_intern("size");
 }
 
Index: ruby_1_8_7/test/ruby/test_array.rb
===================================================================
--- ruby_1_8_7/test/ruby/test_array.rb	(revision 16694)
+++ ruby_1_8_7/test/ruby/test_array.rb	(revision 16695)
@@ -530,6 +530,7 @@
 
   def test_count
     a = @cls[1, 2, 3, 1, 2]
+    assert_equal(5, a.count)
     assert_equal(2, a.count(1))
     assert_equal(3, a.count {|x| x % 2 == 1 })
     assert_equal(2, a.count(1) {|x| x % 2 == 1 })
Index: ruby_1_8_7/test/ruby/test_enum.rb
===================================================================
--- ruby_1_8_7/test/ruby/test_enum.rb	(revision 16694)
+++ ruby_1_8_7/test/ruby/test_enum.rb	(revision 16695)
@@ -29,6 +29,7 @@
   end
 
   def test_count
+    assert_equal(5, @obj.count)
     assert_equal(2, @obj.count(1))
     assert_equal(3, @obj.count {|x| x % 2 == 1 })
     assert_equal(2, @obj.count(1) {|x| x % 2 == 1 })

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

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