diff options
author | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-29 11:24:08 +0000 |
---|---|---|
committer | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-29 11:24:08 +0000 |
commit | ef01a5a6490472e00cad6562074e0eab77aa4f22 (patch) | |
tree | 8de894f4f2f91bc99120fa5ea45c2e869896347b /hash.c | |
parent | 7440815128e50103688fe369713f3e609d127a02 (diff) | |
download | ruby-ef01a5a6490472e00cad6562074e0eab77aa4f22.tar.gz |
* hash.c (rb_hash_assoc): revert r42224. table->type->compare is
called only if hashes are matched.
* test/ruby/test_hash.rb: add a test to check using #== to compare.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r-- | hash.c | 58 |
1 files changed, 14 insertions, 44 deletions
@@ -2123,34 +2123,15 @@ rb_hash_merge(VALUE hash1, VALUE hash2) } static int -assoc_cmp(VALUE a, VALUE b) +assoc_i(VALUE key, VALUE val, VALUE arg) { - return !RTEST(rb_equal(a, b)); -} - -struct lookup2_arg { - VALUE hash; - VALUE key; -}; - -static VALUE -lookup2_call(VALUE arg) -{ - struct lookup2_arg *p = (struct lookup2_arg *)arg; - return rb_hash_lookup2(p->hash, p->key, Qundef); -} - -struct reset_hash_type_arg { - VALUE hash; - const struct st_hash_type *orighash; -}; + VALUE *args = (VALUE *)arg; -static VALUE -reset_hash_type(VALUE arg) -{ - struct reset_hash_type_arg *p = (struct reset_hash_type_arg *)arg; - RHASH(p->hash)->ntbl->type = p->orighash; - return Qundef; + if (RTEST(rb_equal(args[0], key))) { + args[1] = rb_assoc_new(key, val); + return ST_STOP; + } + return ST_CONTINUE; } /* @@ -2168,25 +2149,14 @@ reset_hash_type(VALUE arg) */ VALUE -rb_hash_assoc(VALUE hash, VALUE key) +rb_hash_assoc(VALUE hash, VALUE obj) { - VALUE value; - st_table *table = hash_tbl(hash); - struct lookup2_arg arg; - struct reset_hash_type_arg ensure_arg; - const struct st_hash_type *orighash = table->type; - struct st_hash_type assochash; - - assochash.compare = assoc_cmp; - assochash.hash = orighash->hash; - table->type = &assochash; - arg.hash = hash; - arg.key = key; - ensure_arg.hash = hash; - ensure_arg.orighash = orighash; - value = rb_ensure(lookup2_call, (VALUE)&arg, reset_hash_type, (VALUE)&ensure_arg); - if (value == Qundef) return Qnil; - return rb_assoc_new(key, value); + VALUE args[2]; + + args[0] = obj; + args[1] = Qnil; + rb_hash_foreach(hash, assoc_i, (VALUE)args); + return args[1]; } static int |