+eina_hash_modify_or_add to always add a value to the hash at the specified key
authordiscomfitor <discomfitor>
Wed, 21 Jul 2010 03:15:39 +0000 (03:15 +0000)
committerdiscomfitor <discomfitor@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Wed, 21 Jul 2010 03:15:39 +0000 (03:15 +0000)
git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@50402 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/include/eina_hash.h
src/lib/eina_hash.c

index 218784c..0f2b5e7 100644 (file)
@@ -78,6 +78,7 @@ EAPI Eina_Bool   eina_hash_direct_add(Eina_Hash *hash, const void *key, const vo
 EAPI Eina_Bool   eina_hash_del(Eina_Hash *hash, const void *key, const void *data) EINA_ARG_NONNULL(1);
 EAPI void      * eina_hash_find(const Eina_Hash *hash, const void *key) EINA_ARG_NONNULL(1, 2);
 EAPI void      * eina_hash_modify(Eina_Hash *hash, const void *key, const void *data) EINA_ARG_NONNULL(1, 2, 3);
+EAPI void      * eina_hash_modify_or_add(Eina_Hash *hash, const void *key, const void *data) EINA_ARG_NONNULL(1, 2, 3);
 EAPI void        eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
 EAPI void        eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
 EAPI int         eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
index 357dd7f..d477a05 100644 (file)
@@ -1228,14 +1228,62 @@ eina_hash_modify_by_hash(Eina_Hash *hash, const void *key, int key_length, int k
    el = _eina_hash_find_by_hash(hash, &tuple, key_hash, &eh);
    if (el)
      {
-       old_data = el->tuple.data;
-       el->tuple.data = (void *) data;
+        old_data = el->tuple.data;
+        el->tuple.data = (void *) data;
      }
 
    return old_data;
 }
 
 /**
+ * Modifies the entry pointer at the specified key and returns the old entry or
+ * adds the entry if not found
+ * @param   hash The given hash table.
+ * @param   key  The key of the entry to modify.
+ * @param   data The data to replace the old entry
+ * @return  The data pointer for the old stored entry, or @c NULL if not
+ *          found. If an existing entry is not found, the entry is added to the hash.
+ *
+ * This function adds the specified data to the table at with the key regardless
+ * of whether it is there.  To check for errors, use @ref eina_error_get
+ */
+EAPI void *
+eina_hash_modify_or_add(Eina_Hash *hash, const void *key, const void *data)
+{
+   Eina_Hash_Tuple tuple;
+   Eina_Hash_Head *eh;
+   Eina_Hash_El *el;
+   int key_length;
+   int key_hash;
+
+   EINA_MAGIC_CHECK_HASH(hash);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, NULL);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(data, NULL);
+
+   key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
+   key_hash = hash->key_hash_cb(key, key_length);
+
+   tuple.key = key;
+   tuple.key_length = key_length;
+   tuple.data = NULL;
+
+   el = _eina_hash_find_by_hash(hash, &tuple, key_hash, &eh);
+   if (el)
+     {
+        void *old_data = NULL;
+
+        old_data = el->tuple.data;
+        el->tuple.data = (void *) data;
+        return old_data;
+     }
+
+   eina_hash_add_alloc_by_hash(hash, key, key_length, key_length, key_hash, data);
+
+   return NULL;
+}
+/**
  * Modifies the entry pointer at the specified key and returns the old entry
  * @param   hash The given hash table.
  * @param   key  The key of the entry to modify.