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

ruby-changes:4924

From: ko1@a...
Date: Wed, 14 May 2008 19:27:00 +0900 (JST)
Subject: [ruby-changes:4924] knu - Ruby:r16417 (ruby_1_8): * array.c (rb_ary_count): Override Enumerable#count for better

knu	2008-05-14 19:26:48 +0900 (Wed, 14 May 2008)

  New Revision: 16417

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/NEWS
    branches/ruby_1_8/array.c
    branches/ruby_1_8/enumerator.c
    branches/ruby_1_8/test/ruby/test_array.rb

  Log:
    * array.c (rb_ary_count): Override Enumerable#count for better
      performance.
      (rb_ary_nitems): Undo the backport.  Use #count {} instead.
    
    * enumerator.c (enumerator_iter_i): Remove an unused function.
      (enumerator_with_index, enumerator_each): Remove unused
      variables.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=16417&r2=16416&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/enumerator.c?r1=16417&r2=16416&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/array.c?r1=16417&r2=16416&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/test/ruby/test_array.rb?r1=16417&r2=16416&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/NEWS?r1=16417&r2=16416&diff_format=u

Index: ruby_1_8/array.c
===================================================================
--- ruby_1_8/array.c	(revision 16416)
+++ ruby_1_8/array.c	(revision 16417)
@@ -3032,16 +3032,12 @@
 /*
  *  call-seq:
  *     array.nitems -> int
- *     array.nitems { |item| block }  -> int
  *  
  *  Returns the number of non-<code>nil</code> elements in _self_.
- *  If a block is given, the elements yielding a true value are
- *  counted.
  *
  *  May be zero.
  *     
  *     [ 1, nil, 3, nil, 5 ].nitems   #=> 3
- *     [5,6,7,8,9].nitems { |x| x % 2 != 0 }  #=> 3
  */
 
 static VALUE
@@ -3049,24 +3045,54 @@
     VALUE ary;
 {
     long n = 0;
- 
-    if (rb_block_given_p()) {
-	long i;
+    VALUE *p, *pend;
 
-	for (i=0; i<RARRAY(ary)->len; i++) {
-	    VALUE v = RARRAY(ary)->ptr[i];
- 	    if (RTEST(rb_yield(v))) n++;
- 	}
+    for (p = RARRAY(ary)->ptr, pend = p + RARRAY(ary)->len; p < pend; p++) {
+	if (!NIL_P(*p)) n++;
     }
+    return LONG2NUM(n);
+}
+
+/*
+ *  call-seq:
+ *     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 tthe number of elements yielding a true value.
+ *
+ *     ary = [1, 2, 4, 2]
+ *     ary.count(2)          # => 2
+ *     ary.count{|x|x%2==0}  # => 3
+ *
+ */
+
+static VALUE
+rb_ary_count(int argc, VALUE *argv, VALUE ary)
+{
+    long n = 0;
+
+    if (argc == 0) {
+	VALUE *p, *pend;
+
+	RETURN_ENUMERATOR(ary, 0, 0);
+
+	for (p = RARRAY_PTR(ary), pend = p + RARRAY_LEN(ary); p < pend; p++) {
+	    if (RTEST(rb_yield(*p))) n++;
+	}
+    }
     else {
-	VALUE *p = RARRAY(ary)->ptr;
-	VALUE *pend = p + RARRAY(ary)->len;
+	VALUE obj, *p, *pend;
 
- 	while (p < pend) {
- 	    if (!NIL_P(*p)) n++;
- 	    p++;
- 	}
+	rb_scan_args(argc, argv, "1", &obj);
+	if (rb_block_given_p()) {
+	    rb_warn("given block not used");
+	}
+	for (p = RARRAY_PTR(ary), pend = p + RARRAY_LEN(ary); p < pend; p++) {
+	    if (rb_equal(*p, obj)) n++;
+	}
     }
+
     return LONG2NUM(n);
 }
 
@@ -3789,6 +3815,7 @@
     rb_define_method(rb_cArray, "flatten", rb_ary_flatten, -1);
     rb_define_method(rb_cArray, "flatten!", rb_ary_flatten_bang, -1);
     rb_define_method(rb_cArray, "nitems", rb_ary_nitems, 0);
+    rb_define_method(rb_cArray, "count", rb_ary_count, -1);
     rb_define_method(rb_cArray, "shuffle!", rb_ary_shuffle_bang, 0);
     rb_define_method(rb_cArray, "shuffle", rb_ary_shuffle, 0);
     rb_define_method(rb_cArray, "choice", rb_ary_choice, 0);
Index: ruby_1_8/NEWS
===================================================================
--- ruby_1_8/NEWS	(revision 16416)
+++ ruby_1_8/NEWS	(revision 16417)
@@ -17,10 +17,6 @@
 
 * builtin classes
 
-  * Array#nitems now takes a block optionally, which is used to
-    determine if each element should be counted instead of checking if
-    the element is non-nil.
-
   * Array#flatten
   * Array#flatten!
 
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 16416)
+++ ruby_1_8/ChangeLog	(revision 16417)
@@ -1,3 +1,13 @@
+Wed May 14 19:24:59 2008  Akinori MUSHA  <knu@i...>
+
+	* array.c (rb_ary_count): Override Enumerable#count for better
+	  performance.
+	  (rb_ary_nitems): Undo the backport.  Use #count {} instead.
+
+	* enumerator.c (enumerator_iter_i): Remove an unused function.
+	  (enumerator_with_index, enumerator_each): Remove unused
+	  variables.
+
 Wed May 14 17:15:11 2008  NAKAMURA Usaku  <usa@r...>
 
 	* ext/tk/tkutil/extronf.rb: check stdndup() because it's not standard
Index: ruby_1_8/enumerator.c
===================================================================
--- ruby_1_8/enumerator.c	(revision 16416)
+++ ruby_1_8/enumerator.c	(revision 16417)
@@ -72,16 +72,6 @@
     return ptr;
 }
 
-static VALUE enumerator_iter_i _((VALUE, VALUE));
-static VALUE
-enumerator_iter_i(i, enum_obj)
-    VALUE i;
-    VALUE enum_obj;
-{
-    struct enumerator *e = (struct enumerator *)enum_obj;
-    return rb_yield(proc_call(e->proc, i));
-}
-
 /*
  *  call-seq:
  *    obj.to_enum(method = :each, *args)
@@ -339,7 +329,6 @@
     struct enumerator *e;
     int argc = 0;
     VALUE *argv = 0;
-    VALUE method;
 
     if (!rb_block_given_p()) return obj;
     e = enumerator_ptr(obj);
@@ -377,7 +366,6 @@
     VALUE memo = 0;
     int argc = 0;
     VALUE *argv = 0;
-    VALUE method;
 
     RETURN_ENUMERATOR(obj, 0, 0);
     if (e->args) {
Index: ruby_1_8/test/ruby/test_array.rb
===================================================================
--- ruby_1_8/test/ruby/test_array.rb	(revision 16416)
+++ ruby_1_8/test/ruby/test_array.rb	(revision 16417)
@@ -528,6 +528,14 @@
     assert_equal([1, 2, 3, 1, 2, 3], a)
   end
 
+  def test_count
+    a = @cls[1, 2, 3, 1, 2]
+    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 })
+    assert_raise(ArgumentError) { a.count(0, 1) }
+  end
+
   def test_delete
     a = @cls[*('cab'..'cat').to_a]
     assert_equal('cap', a.delete('cap'))

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

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