summaryrefslogtreecommitdiff
path: root/hash.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 /hash.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 'hash.c')
-rw-r--r--hash.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/hash.c b/hash.c
index c600fd11ab..2e51a08d27 100644
--- a/hash.c
+++ b/hash.c
@@ -2463,6 +2463,52 @@ rb_hash_compare_by_id_p(VALUE hash)
return Qfalse;
}
+static int
+any_p_i(VALUE key, VALUE value, VALUE arg)
+{
+ VALUE ret = rb_yield(rb_assoc_new(key, value));
+ if (RTEST(ret)) {
+ *(VALUE *)arg = Qtrue;
+ return ST_STOP;
+ }
+ return ST_CONTINUE;
+}
+
+static int
+any_p_i_fast(VALUE key, VALUE value, VALUE arg)
+{
+ VALUE ret = rb_yield_values(2, key, value);
+ if (RTEST(ret)) {
+ *(VALUE *)arg = Qtrue;
+ return ST_STOP;
+ }
+ return ST_CONTINUE;
+}
+
+/*
+ * call-seq:
+ * hsh.any? [{ |(key, value)| block }] -> true or false
+ *
+ * See also Enumerable#any?
+ */
+
+static VALUE
+rb_hash_any_p(VALUE hash)
+{
+ VALUE ret = Qfalse;
+
+ if (RHASH_EMPTY_P(hash)) return Qfalse;
+ if (!rb_block_given_p()) {
+ /* yields pairs, never false */
+ return Qtrue;
+ }
+ if (rb_block_arity() > 1)
+ rb_hash_foreach(hash, any_p_i_fast, (VALUE)&ret);
+ else
+ rb_hash_foreach(hash, any_p_i, (VALUE)&ret);
+ return ret;
+}
+
static int path_tainted = -1;
static char **origenviron;
@@ -3861,6 +3907,8 @@ Init_Hash(void)
rb_define_method(rb_cHash,"compare_by_identity", rb_hash_compare_by_id, 0);
rb_define_method(rb_cHash,"compare_by_identity?", rb_hash_compare_by_id_p, 0);
+ rb_define_method(rb_cHash, "any?", rb_hash_any_p, 0);
+
/* Document-class: ENV
*
* ENV is a hash-like accessor for environment variables.