summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-05-18 15:02:36 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-05-18 15:02:36 +0000
commit8480bcc8d5c72b61055cfa98e80f37fd62ae7ad4 (patch)
treef404254dbcee2a59866dbb3f7baab4df8e4b23ce /array.c
parent32378c5abea38a8278dae28eae9abcd547ac8a95 (diff)
Merge -r16241:16456 from ruby_1_8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@16458 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c61
1 files changed, 44 insertions, 17 deletions
diff --git a/array.c b/array.c
index 423030a..1121b0f 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-<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;
-
- 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 @@ 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);