From 19ed71c8d035e14d6ca39af4e52fb6630cb56c23 Mon Sep 17 00:00:00 2001 From: marcandre Date: Tue, 6 Nov 2012 17:13:33 +0000 Subject: [PATCH] * hash.c: Support for enumerators created by Hash: delete_if, reject!, ... [Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37511 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- hash.c | 18 ++++++++++-------- test/ruby/test_enumerator.rb | 7 +++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/hash.c b/hash.c index db83da0eca..126fd41dc8 100644 --- a/hash.c +++ b/hash.c @@ -911,6 +911,8 @@ delete_if_i(VALUE key, VALUE value, VALUE hash) return ST_CONTINUE; } +static VALUE rb_hash_size(VALUE hash); + /* * call-seq: * hsh.delete_if {| key, value | block } -> hsh @@ -929,7 +931,7 @@ delete_if_i(VALUE key, VALUE value, VALUE hash) VALUE rb_hash_delete_if(VALUE hash) { - RETURN_ENUMERATOR(hash, 0, 0); + RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size); rb_hash_modify_check(hash); if (RHASH(hash)->ntbl) rb_hash_foreach(hash, delete_if_i, hash); @@ -950,7 +952,7 @@ rb_hash_reject_bang(VALUE hash) { st_index_t n; - RETURN_ENUMERATOR(hash, 0, 0); + RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size); rb_hash_modify(hash); if (!RHASH(hash)->ntbl) return Qnil; @@ -1027,7 +1029,7 @@ rb_hash_select(VALUE hash) { VALUE result; - RETURN_ENUMERATOR(hash, 0, 0); + RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size); result = rb_hash_new(); rb_hash_foreach(hash, select_i, result); return result; @@ -1056,7 +1058,7 @@ rb_hash_select_bang(VALUE hash) { st_index_t n; - RETURN_ENUMERATOR(hash, 0, 0); + RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size); rb_hash_modify_check(hash); if (!RHASH(hash)->ntbl) return Qnil; @@ -1081,7 +1083,7 @@ rb_hash_select_bang(VALUE hash) VALUE rb_hash_keep_if(VALUE hash) { - RETURN_ENUMERATOR(hash, 0, 0); + RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size); rb_hash_modify_check(hash); if (RHASH(hash)->ntbl) rb_hash_foreach(hash, keep_if_i, hash); @@ -1310,7 +1312,7 @@ each_value_i(VALUE key, VALUE value) static VALUE rb_hash_each_value(VALUE hash) { - RETURN_ENUMERATOR(hash, 0, 0); + RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size); rb_hash_foreach(hash, each_value_i, 0); return hash; } @@ -1343,7 +1345,7 @@ each_key_i(VALUE key, VALUE value) static VALUE rb_hash_each_key(VALUE hash) { - RETURN_ENUMERATOR(hash, 0, 0); + RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size); rb_hash_foreach(hash, each_key_i, 0); return hash; } @@ -1380,7 +1382,7 @@ each_pair_i(VALUE key, VALUE value) static VALUE rb_hash_each_pair(VALUE hash) { - RETURN_ENUMERATOR(hash, 0, 0); + RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size); rb_hash_foreach(hash, each_pair_i, 0); return hash; } diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index 66d5e28a14..af41341227 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -443,6 +443,13 @@ class TestEnumerator < Test::Unit::TestCase assert_equal 42, @sized.each_with_object(nil).size end + def test_size_for_enum_created_from_hash + h = {a: 1, b: 2, c: 3} + %i[delete_if reject! select select! keep_if each each_key each_pair].each do |method| + assert_equal 3, h.send(method).size + end + end + def check_consistency_for_combinatorics(method) [ [], [:a, :b, :c, :d, :e] ].product([-2, 0, 2, 5, 6]) do |array, arg| assert_equal array.send(method, arg).to_a.size, array.send(method, arg).size,