1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16694 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2008-05-29 19:35:04 +00:00
parent 019864bfc1
commit 14dd72b35a
6 changed files with 72 additions and 38 deletions

View file

@ -1,3 +1,10 @@
Fri May 30 04:32:07 2008 Akinori MUSHA <knu@iDaemons.org>
* 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@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):

15
array.c
View file

@ -3064,27 +3064,34 @@ rb_ary_nitems(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
* *
*/ */
static VALUE 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; long n = 0;
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++;

80
enum.c
View file

@ -15,7 +15,7 @@
#include "util.h" #include "util.h"
VALUE rb_mEnumerable; 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 { struct iter_method_arg {
VALUE obj; VALUE obj;
@ -114,36 +114,53 @@ enum_grep(obj, pat)
} }
static VALUE static VALUE
count_i(i, arg) count_i(i, memop)
VALUE i; VALUE i, memop;
VALUE *arg;
{ {
if (rb_equal(i, arg[0])) { VALUE *memo = (VALUE*)memop;
arg[1]++;
if (rb_equal(i, memo[1])) {
memo[0]++;
} }
return Qnil; return Qnil;
} }
static VALUE static VALUE
count_iter_i(i, n) count_iter_i(i, memop)
VALUE i; VALUE i, memop;
long *n;
{ {
VALUE *memo = (VALUE*)memop;
if (RTEST(rb_yield(i))) { if (RTEST(rb_yield(i))) {
(*n)++; memo[0]++;
} }
return Qnil; return Qnil;
} }
static VALUE
count_all_i(i, memop)
VALUE i, memop;
{
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
* *
@ -155,31 +172,31 @@ enum_count(argc, argv, obj)
VALUE *argv; VALUE *argv;
VALUE obj; VALUE obj;
{ {
if (argc == 1) { VALUE memo[2]; /* [count, condition value] */
VALUE item, args[2]; 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()) { if (rb_block_given_p()) {
rb_warn("given block not used"); rb_warn("given block not used");
} }
rb_scan_args(argc, argv, "1", &item); func = count_i;
args[0] = item;
args[1] = 0;
rb_block_call(obj, id_each, 0, 0, count_i, (VALUE)&args);
return INT2NUM(args[1]);
} }
else if (argc == 0) {
long n;
RETURN_ENUMERATOR(obj, 0, 0); memo[0] = 0;
n = 0; rb_block_call(obj, id_each, 0, 0, func, (VALUE)&memo);
rb_block_call(obj, id_each, 0, 0, count_iter_i, (VALUE)&n); return INT2NUM(memo[0]);
return INT2NUM(n);
}
else {
VALUE v;
rb_scan_args(argc, argv, "1", &v);
return Qnil; /* not reached */
}
} }
static VALUE static VALUE
@ -1866,5 +1883,6 @@ Init_Enumerable()
id_eqq = rb_intern("==="); id_eqq = rb_intern("===");
id_each = rb_intern("each"); id_each = rb_intern("each");
id_cmp = rb_intern("<=>"); id_cmp = rb_intern("<=>");
id_size = rb_intern("size");
} }

View file

@ -530,6 +530,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 })

View file

@ -29,6 +29,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 })

View file

@ -1,7 +1,7 @@
#define RUBY_VERSION "1.8.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_VERSION_CODE 187
#define RUBY_RELEASE_CODE 20080528 #define RUBY_RELEASE_CODE 20080530
#define RUBY_PATCHLEVEL 5000 #define RUBY_PATCHLEVEL 5000
#define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MAJOR 1
@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 7 #define RUBY_VERSION_TEENY 7
#define RUBY_RELEASE_YEAR 2008 #define RUBY_RELEASE_YEAR 2008
#define RUBY_RELEASE_MONTH 5 #define RUBY_RELEASE_MONTH 5
#define RUBY_RELEASE_DAY 28 #define RUBY_RELEASE_DAY 30
#ifdef RUBY_EXTERN #ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[]; RUBY_EXTERN const char ruby_version[];