mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enum.c (enum_count, count_all_i, Init_Enumerable),
array.c (rb_ary_count): If no argument or block is given, count the number of all elements. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16693 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
cd5eb03b58
commit
3a3e250975
5 changed files with 42 additions and 8 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Fri May 30 04:17:13 2008 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* enum.c (enum_count, count_all_i, Init_Enumerable),
|
||||||
|
array.c (rb_ary_count): If no argument or block is given, count
|
||||||
|
the number of all elements.
|
||||||
|
|
||||||
Fri May 30 03:12:18 2008 Akinori MUSHA <knu@iDaemons.org>
|
Fri May 30 03:12:18 2008 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
* ext/openssl/ossl_bn.c (ossl_bn_s_rand, ossl_bn_s_pseudo_rand):
|
* ext/openssl/ossl_bn.c (ossl_bn_s_rand, ossl_bn_s_pseudo_rand):
|
||||||
|
|
10
array.c
10
array.c
|
@ -2747,13 +2747,16 @@ rb_ary_compact(VALUE ary)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
|
* array.count -> int
|
||||||
* array.count(obj) -> int
|
* array.count(obj) -> int
|
||||||
* array.count { |item| block } -> int
|
* array.count { |item| block } -> int
|
||||||
*
|
*
|
||||||
* Returns the number of elements which equals to <i>obj</i>.
|
* Returns the number of elements. If an argument is given, counts
|
||||||
* If a block is given, counts the number of elements yielding a true value.
|
* 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 = [1, 2, 4, 2]
|
||||||
|
* ary.count # => 4
|
||||||
* ary.count(2) # => 2
|
* ary.count(2) # => 2
|
||||||
* ary.count{|x|x%2==0} # => 3
|
* ary.count{|x|x%2==0} # => 3
|
||||||
*
|
*
|
||||||
|
@ -2767,7 +2770,8 @@ rb_ary_count(int argc, VALUE *argv, VALUE ary)
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
VALUE *p, *pend;
|
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++) {
|
for (p = RARRAY_PTR(ary), pend = p + RARRAY_LEN(ary); p < pend; p++) {
|
||||||
if (RTEST(rb_yield(*p))) n++;
|
if (RTEST(rb_yield(*p))) n++;
|
||||||
|
|
32
enum.c
32
enum.c
|
@ -14,7 +14,7 @@
|
||||||
#include "ruby/util.h"
|
#include "ruby/util.h"
|
||||||
|
|
||||||
VALUE rb_mEnumerable;
|
VALUE rb_mEnumerable;
|
||||||
static ID id_each, id_eqq, id_cmp, id_next;
|
static ID id_each, id_eqq, id_cmp, id_next, id_size;
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
enum_values_pack(int argc, VALUE *argv)
|
enum_values_pack(int argc, VALUE *argv)
|
||||||
|
@ -108,15 +108,29 @@ count_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv)
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
count_all_i(VALUE i, VALUE memop, int argc, VALUE *argv)
|
||||||
|
{
|
||||||
|
VALUE *memo = (VALUE*)memop;
|
||||||
|
|
||||||
|
memo[0]++;
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
|
* enum.count => int
|
||||||
* enum.count(item) => int
|
* enum.count(item) => int
|
||||||
* enum.count {| obj | block } => int
|
* enum.count {| obj | block } => int
|
||||||
*
|
*
|
||||||
* Returns the number of items in <i>enum</i> for which equals to <i>item</i>.
|
* Returns the number of items in <i>enum</i>, where #size is called
|
||||||
* If a block is given, counts the number of elements yielding a true value.
|
* 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 = [1, 2, 4, 2]
|
||||||
|
* ary.count # => 4
|
||||||
* ary.count(2) # => 2
|
* ary.count(2) # => 2
|
||||||
* ary.count{|x|x%2==0} # => 3
|
* ary.count{|x|x%2==0} # => 3
|
||||||
*
|
*
|
||||||
|
@ -129,8 +143,15 @@ enum_count(int argc, VALUE *argv, VALUE obj)
|
||||||
rb_block_call_func *func;
|
rb_block_call_func *func;
|
||||||
|
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
RETURN_ENUMERATOR(obj, 0, 0);
|
if (rb_block_given_p()) {
|
||||||
func = count_iter_i;
|
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 {
|
else {
|
||||||
rb_scan_args(argc, argv, "1", &memo[1]);
|
rb_scan_args(argc, argv, "1", &memo[1]);
|
||||||
|
@ -1826,5 +1847,6 @@ Init_Enumerable(void)
|
||||||
id_each = rb_intern("each");
|
id_each = rb_intern("each");
|
||||||
id_cmp = rb_intern("<=>");
|
id_cmp = rb_intern("<=>");
|
||||||
id_next = rb_intern("next");
|
id_next = rb_intern("next");
|
||||||
|
id_size = rb_intern("size");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -539,6 +539,7 @@ class TestArray < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_count
|
def test_count
|
||||||
a = @cls[1, 2, 3, 1, 2]
|
a = @cls[1, 2, 3, 1, 2]
|
||||||
|
assert_equal(5, a.count)
|
||||||
assert_equal(2, a.count(1))
|
assert_equal(2, a.count(1))
|
||||||
assert_equal(3, a.count {|x| x % 2 == 1 })
|
assert_equal(3, a.count {|x| x % 2 == 1 })
|
||||||
assert_equal(2, a.count(1) {|x| x % 2 == 1 })
|
assert_equal(2, a.count(1) {|x| x % 2 == 1 })
|
||||||
|
|
|
@ -30,6 +30,7 @@ class TestEnumerable < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_count
|
def test_count
|
||||||
|
assert_equal(5, @obj.count)
|
||||||
assert_equal(2, @obj.count(1))
|
assert_equal(2, @obj.count(1))
|
||||||
assert_equal(3, @obj.count {|x| x % 2 == 1 })
|
assert_equal(3, @obj.count {|x| x % 2 == 1 })
|
||||||
assert_equal(2, @obj.count(1) {|x| x % 2 == 1 })
|
assert_equal(2, @obj.count(1) {|x| x % 2 == 1 })
|
||||||
|
|
Loading…
Add table
Reference in a new issue