summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-20 08:35:25 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-20 08:35:25 +0000
commitaa0e51b529f4f06c0d44c4323b65655a024643f9 (patch)
tree6f2f7540de09d53478ed71e7469c9766d0145080
parent306b64bd1a05473319ca57e543e53dc56dca4c1c (diff)
hash.c: rb_hash_add_new_element
* hash.c (rb_hash_add_new_element): add new element or do nothing if it is contained already. * array.c (ary_add_hash, ary_add_hash_by): use rb_hash_add_new_element. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55707 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--array.c8
-rw-r--r--hash.c24
-rw-r--r--internal.h1
4 files changed, 35 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index db8bf648a6a..f6834c8f13d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Wed Jul 20 17:35:23 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * hash.c (rb_hash_add_new_element): add new element or do nothing
+ if it is contained already.
+
+ * array.c (ary_add_hash, ary_add_hash_by): use
+ rb_hash_add_new_element.
+
Tue Jul 19 18:21:17 2016 Martin Duerst <duerst@it.aoyama.ac.jp>
* lib/unicode_normalize/tables.rb: Remove
diff --git a/array.c b/array.c
index 6f1d65a88ee..5ca7a939721 100644
--- a/array.c
+++ b/array.c
@@ -4008,9 +4008,7 @@ ary_add_hash(VALUE hash, VALUE ary)
for (i=0; i<RARRAY_LEN(ary); i++) {
VALUE elt = RARRAY_AREF(ary, i);
- if (rb_hash_lookup2(hash, elt, Qundef) == Qundef) {
- rb_hash_aset(hash, elt, elt);
- }
+ rb_hash_add_new_element(hash, elt, elt);
}
return hash;
}
@@ -4038,9 +4036,7 @@ ary_add_hash_by(VALUE hash, VALUE ary)
for (i = 0; i < RARRAY_LEN(ary); ++i) {
VALUE v = rb_ary_elt(ary, i), k = rb_yield(v);
- if (rb_hash_lookup2(hash, k, Qundef) == Qundef) {
- rb_hash_aset(hash, k, v);
- }
+ rb_hash_add_new_element(hash, k, v);
}
return hash;
}
diff --git a/hash.c b/hash.c
index 561aa904008..cab0b9eaa2c 100644
--- a/hash.c
+++ b/hash.c
@@ -2862,6 +2862,30 @@ rb_hash_to_proc(VALUE hash)
return rb_func_proc_new(hash_proc_call, hash);
}
+static int
+add_new_i(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
+{
+ VALUE *args = (VALUE *)arg;
+ if (existing) return ST_STOP;
+ RB_OBJ_WRITTEN(args[0], Qundef, (VALUE)*key);
+ RB_OBJ_WRITE(args[0], (VALUE *)val, args[1]);
+ return ST_CONTINUE;
+}
+
+/*
+ * add +key+ to +val+ pair if +hash+ does not contain +key+.
+ * returns non-zero if +key+ was contained.
+ */
+int
+rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val)
+{
+ st_table *tbl = rb_hash_tbl_raw(hash);
+ VALUE args[2];
+ args[0] = hash;
+ args[1] = val;
+ return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
+}
+
static int path_tainted = -1;
static char **origenviron;
diff --git a/internal.h b/internal.h
index b47430cd7da..c59ccc5e19f 100644
--- a/internal.h
+++ b/internal.h
@@ -1049,6 +1049,7 @@ st_table *rb_init_identtable_with_size(st_index_t size);
VALUE rb_hash_keys(VALUE hash);
VALUE rb_hash_values(VALUE hash);
VALUE rb_hash_rehash(VALUE hash);
+int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val);
#define HASH_DELETED FL_USER1
#define HASH_PROC_DEFAULT FL_USER2