summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-20 03:18:53 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-20 03:18:53 (GMT)
commitd0f9184f8be497313e3b26911c99fa0d23a8ebed (patch)
treef027e90529ece0d180714dd5192e1e6a15860752 /array.c
parent744e816f55757df92caa49b4787cf06af6120a20 (diff)
Introduce rb_ary_union method in Array
Avoid repeating code and improve readability in `rb_ary_or` and `rb_ary_union_multi`. [Fix GH-1747] [Feature #14097] From: Ana María Martínez Gómez <ammartinez@suse.de> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64788 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c39
1 files changed, 15 insertions, 24 deletions
diff --git a/array.c b/array.c
index ae3cc49..e49071d 100644
--- a/array.c
+++ b/array.c
@@ -4271,6 +4271,17 @@ ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
return ST_CONTINUE;
}
+static void
+rb_ary_union(VALUE ary_union, VALUE ary)
+{
+ long i;
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
+ VALUE elt = rb_ary_elt(ary, i);
+ if (rb_ary_includes_by_eql(ary_union, elt)) continue;
+ rb_ary_push(ary_union, elt);
+ }
+}
+
/*
* call-seq:
* ary | other_ary -> new_ary
@@ -4295,16 +4306,8 @@ rb_ary_or(VALUE ary1, VALUE ary2)
ary2 = to_ary(ary2);
if (RARRAY_LEN(ary1) + RARRAY_LEN(ary2) <= SMALL_ARRAY_LEN) {
ary3 = rb_ary_new();
- for (i=0; i<RARRAY_LEN(ary1); i++) {
- VALUE elt = rb_ary_elt(ary1, i);
- if (rb_ary_includes_by_eql(ary3, elt)) continue;
- rb_ary_push(ary3, elt);
- }
- for (i=0; i<RARRAY_LEN(ary2); i++) {
- VALUE elt = rb_ary_elt(ary2, i);
- if (rb_ary_includes_by_eql(ary3, elt)) continue;
- rb_ary_push(ary3, elt);
- }
+ rb_ary_union(ary3, ary1);
+ rb_ary_union(ary3, ary2);
return ary3;
}
@@ -4353,21 +4356,9 @@ rb_ary_union_multi(int argc, VALUE *argv, VALUE ary)
if (sum <= SMALL_ARRAY_LEN) {
ary_union = rb_ary_new();
- for (j = 0; j < RARRAY_LEN(ary); j++) {
- VALUE elt = rb_ary_elt(ary, j);
- if (rb_ary_includes_by_eql(ary_union, elt)) continue;
- rb_ary_push(ary_union, elt);
- }
-
- for (i = 0; i < argc; i++) {
- VALUE argv_i = argv[i];
+ rb_ary_union(ary_union, ary);
+ for (i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]);
- for (j = 0; j < RARRAY_LEN(argv_i); j++) {
- VALUE elt = rb_ary_elt(argv_i, j);
- if (rb_ary_includes_by_eql(ary_union, elt)) continue;
- rb_ary_push(ary_union, elt);
- }
- }
return ary_union;
}