summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-07-18 13:16:48 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-07-18 13:16:48 +0000
commitd738e3e15533e0f500789faaedcef9ed9ca362b9 (patch)
treeae914e671da780385594a4efe45df56cc01412af /array.c
parente23d1736797f9b7170c80a7470b8e5cecd308105 (diff)
optimized any? methods
* array.c (rb_ary_any_p), hash.c (rb_hash_any_p): optimized versions. these are bit faster than optimization in Enumerable#any?. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46866 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/array.c b/array.c
index 0bfbfb4..92897c4 100644
--- a/array.c
+++ b/array.c
@@ -5409,6 +5409,29 @@ rb_ary_drop_while(VALUE ary)
}
/*
+ * call-seq:
+ * ary.any? [{ |obj| block }] -> true or false
+ *
+ * See also Enumerable#any?
+ */
+
+static VALUE
+rb_ary_any_p(VALUE ary)
+{
+ long i, len = RARRAY_LEN(ary);
+ const VALUE *ptr = RARRAY_CONST_PTR(ary);
+
+ if (!len) return Qfalse;
+ if (!rb_block_given_p()) {
+ for (i = 0; i < len; ++i) if (RTEST(ptr[i])) return Qtrue;
+ }
+ else {
+ for (i = 0; i < len; ++i) if (RTEST(rb_yield(ptr[i]))) return Qtrue;
+ }
+ return Qfalse;
+}
+
+/*
* Arrays are ordered, integer-indexed collections of any object.
*
* Array indexing starts at 0, as in C or Java. A negative index is assumed
@@ -5757,6 +5780,7 @@ Init_Array(void)
rb_define_method(rb_cArray, "drop", rb_ary_drop, 1);
rb_define_method(rb_cArray, "drop_while", rb_ary_drop_while, 0);
rb_define_method(rb_cArray, "bsearch", rb_ary_bsearch, 0);
+ rb_define_method(rb_cArray, "any?", rb_ary_any_p, 0);
id_cmp = rb_intern("<=>");
id_random = rb_intern("random");