mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
e9d69ca0ce
commit
d007e7d20f
5 changed files with 62 additions and 33 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
|||
Wed May 14 19:24:59 2008 Akinori MUSHA <knu@iDaemons.org>
|
||||
|
||||
* 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@ruby-lang.org>
|
||||
|
||||
* ext/tk/tkutil/extronf.rb: check stdndup() because it's not standard
|
||||
|
|
4
NEWS
4
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!
|
||||
|
||||
|
|
59
array.c
59
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-<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 @@ rb_ary_nitems(ary)
|
|||
VALUE ary;
|
||||
{
|
||||
long n = 0;
|
||||
VALUE *p, *pend;
|
||||
|
||||
if (rb_block_given_p()) {
|
||||
long i;
|
||||
for (p = RARRAY(ary)->ptr, pend = p + RARRAY(ary)->len; p < pend; p++) {
|
||||
if (!NIL_P(*p)) n++;
|
||||
}
|
||||
return LONG2NUM(n);
|
||||
}
|
||||
|
||||
for (i=0; i<RARRAY(ary)->len; i++) {
|
||||
VALUE v = RARRAY(ary)->ptr[i];
|
||||
if (RTEST(rb_yield(v))) 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 @@ 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);
|
||||
|
|
12
enumerator.c
12
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) {
|
||||
|
|
|
@ -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'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue