From ce079f165176d168ced2c81c79c9d1f889ab173a Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 20 Sep 2018 03:18:54 +0000 Subject: Introduce rb_ary_union_hash method in Array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid repeating code and improve readability in `rb_ary_or` and `rb_ary_union_multi`. Similaty as done with `rb_ary_union`. [Fix GH-1747] [Feature #14097] From: Ana María Martínez Gómez git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64790 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- array.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'array.c') diff --git a/array.c b/array.c index 5202eb9947..4b62394daf 100644 --- a/array.c +++ b/array.c @@ -4282,6 +4282,18 @@ rb_ary_union(VALUE ary_union, VALUE ary) } } +static void +rb_ary_union_hash(VALUE hash, VALUE ary2) +{ + long i; + for (i = 0; i < RARRAY_LEN(ary2); i++) { + VALUE elt = RARRAY_AREF(ary2, i); + if (!st_update(RHASH_TBL_RAW(hash), (st_data_t)elt, ary_hash_orset, (st_data_t)elt)) { + RB_OBJ_WRITTEN(hash, Qundef, elt); + } + } +} + /* * call-seq: * ary | other_ary -> new_ary @@ -4301,7 +4313,6 @@ static VALUE rb_ary_or(VALUE ary1, VALUE ary2) { VALUE hash, ary3; - long i; ary2 = to_ary(ary2); if (RARRAY_LEN(ary1) + RARRAY_LEN(ary2) <= SMALL_ARRAY_LEN) { @@ -4312,12 +4323,8 @@ rb_ary_or(VALUE ary1, VALUE ary2) } hash = ary_make_hash(ary1); - for (i=0; i