[前][次][番号順一覧][スレッド一覧]

ruby-changes:43635

From: nobu <ko1@a...>
Date: Wed, 20 Jul 2016 17:35:31 +0900 (JST)
Subject: [ruby-changes:43635] nobu:r55707 (trunk): hash.c: rb_hash_add_new_element

nobu	2016-07-20 17:35:25 +0900 (Wed, 20 Jul 2016)

  New Revision: 55707

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55707

  Log:
    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.

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/hash.c
    trunk/internal.h
Index: internal.h
===================================================================
--- internal.h	(revision 55706)
+++ internal.h	(revision 55707)
@@ -1049,6 +1049,7 @@ st_table *rb_init_identtable_with_size(s https://github.com/ruby/ruby/blob/trunk/internal.h#L1049
 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
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55706)
+++ ChangeLog	(revision 55707)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jul 20 17:35:23 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@i...>
 
 	* lib/unicode_normalize/tables.rb: Remove
Index: array.c
===================================================================
--- array.c	(revision 55706)
+++ array.c	(revision 55707)
@@ -4008,9 +4008,7 @@ ary_add_hash(VALUE hash, VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L4008
 
     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) https://github.com/ruby/ruby/blob/trunk/array.c#L4036
 
     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;
 }
Index: hash.c
===================================================================
--- hash.c	(revision 55706)
+++ hash.c	(revision 55707)
@@ -2862,6 +2862,30 @@ rb_hash_to_proc(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2862
     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;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]