From d007e7d20f0b2c76b2c638f9a1a3844a86ddeb62 Mon Sep 17 00:00:00 2001 From: knu Date: Wed, 14 May 2008 10:26:48 +0000 Subject: [PATCH] * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16417 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 +++++++ NEWS | 4 --- array.c | 61 +++++++++++++++++++++++++++++------------ enumerator.c | 12 -------- test/ruby/test_array.rb | 8 ++++++ 5 files changed, 62 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index 161baa7f84..c3c3281514 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Wed May 14 19:24:59 2008 Akinori MUSHA + + * 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 * ext/tk/tkutil/extronf.rb: check stdndup() because it's not standard diff --git a/NEWS b/NEWS index ec2e32e1f4..190c15fec7 100644 --- a/NEWS +++ b/NEWS @@ -17,10 +17,6 @@ with all sufficient information, see the ChangeLog file. * 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! diff --git a/array.c b/array.c index 423030ac41..1121b0f193 100644 --- a/array.c +++ b/array.c @@ -3032,16 +3032,12 @@ rb_ary_compact(ary) /* * call-seq: * array.nitems -> int - * array.nitems { |item| block } -> int * * Returns the number of non-nil 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 @@ rb_ary_nitems(ary) VALUE ary; { long n = 0; - - if (rb_block_given_p()) { - long i; + VALUE *p, *pend; - for (i=0; ilen; 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 obj. + * 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 @@ Init_Array() 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); diff --git a/enumerator.c b/enumerator.c index 8ec8b2f60b..d7a79c3c5a 100644 --- a/enumerator.c +++ b/enumerator.c @@ -72,16 +72,6 @@ enumerator_ptr(obj) 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 @@ enumerator_each(obj) 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 @@ enumerator_with_index(obj) VALUE memo = 0; int argc = 0; VALUE *argv = 0; - VALUE method; RETURN_ENUMERATOR(obj, 0, 0); if (e->args) { diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 3cb1ec174e..6a816a2c85 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -528,6 +528,14 @@ class TestArray < Test::Unit::TestCase 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'))