* eina: add a way to move a key to another without triggering the Free_Cb.
authorcedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 30 Jul 2010 09:06:58 +0000 (09:06 +0000)
committercedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 30 Jul 2010 09:06:58 +0000 (09:06 +0000)
git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@50683 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

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

index 6774f63..959b286 100644 (file)
@@ -122,6 +122,12 @@ eina_hash_set(
    Eina_Hash *hash,
    const void *key,
    const void *data) EINA_ARG_NONNULL(1, 2, 3);
+EAPI Eina_Bool
+eina_hash_move(
+   Eina_Hash *hash,
+   const void *old_key,
+   const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
+
 EAPI void
 eina_hash_free(
    Eina_Hash *hash) EINA_ARG_NONNULL(1);
index 35a0511..490f11e 100644 (file)
@@ -1483,6 +1483,40 @@ eina_hash_modify(Eina_Hash *hash, const void *key, const void *data)
    return eina_hash_modify_by_hash(hash, key, key_length, hash_num, data);
 }
 
+/**
+ * Change the key associated with a data without trigerring the del callback.
+ * @param hash    The given hash table.
+ * @param old_key The current key associated with the data
+ * @param new_key The new key to associate data with
+ * @return EINA_FALSE if somethings goes wrong.
+ */
+EAPI Eina_Bool
+eina_hash_move(Eina_Hash *hash, const void *old_key, const void *new_key)
+{
+   Eina_Free_Cb hash_free_cb;
+   const void *data;
+   Eina_Bool result;
+
+   EINA_MAGIC_CHECK_HASH(hash);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(hash,              EINA_FALSE);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, EINA_FALSE);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(old_key,           EINA_FALSE);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(new_key,           EINA_FALSE);
+
+   data = eina_hash_find(hash, old_key);
+   if (!data) return EINA_FALSE;
+
+   hash_free_cb = hash->data_free_cb;
+   hash->data_free_cb = NULL;
+
+   eina_hash_del(hash, old_key, data);
+   result = eina_hash_add(hash, new_key, data);
+
+   hash->data_free_cb = hash_free_cb;
+
+   return result;
+}
+
 /*============================================================================*
 *                                Iterator                                    *
 *============================================================================*/