Add boolean returns to some hash functions

The functions g_hash_table_insert, g_hash_table_replace
and g_hash_table_add now return TRUE if they inserted a
new key/value pair.

https://bugzilla.gnome.org/show_bug.cgi?id=697828
This commit is contained in:
Matthias Clasen 2013-11-24 01:22:44 -05:00
parent 616af3b80e
commit 910191597a
3 changed files with 34 additions and 16 deletions

View File

@ -829,8 +829,10 @@ g_hash_table_iter_remove (GHashTableIter *iter)
* If @key has been taken out of the existing node (ie it is not
* passed in via a g_hash_table_insert/replace) call, then @reusing_key
* should be %TRUE.
*
* Returns: %TRUE if the key did not exist yet
*/
static void
static gboolean
g_hash_table_insert_node (GHashTable *hash_table,
guint node_index,
guint key_hash,
@ -921,6 +923,8 @@ g_hash_table_insert_node (GHashTable *hash_table,
if (hash_table->value_destroy_func)
(* hash_table->value_destroy_func) (value_to_free);
}
return !already_exists;
}
/**
@ -1138,8 +1142,10 @@ g_hash_table_lookup_extended (GHashTable *hash_table,
* Do a lookup of @key. If it is found, replace it with the new
* @value (and perhaps the new @key). If it is not found, create
* a new node.
*
* Returns: %TRUE if the key did not exist yet
*/
static void
static gboolean
g_hash_table_insert_internal (GHashTable *hash_table,
gpointer key,
gpointer value,
@ -1148,11 +1154,11 @@ g_hash_table_insert_internal (GHashTable *hash_table,
guint key_hash;
guint node_index;
g_return_if_fail (hash_table != NULL);
g_return_val_if_fail (hash_table != NULL, FALSE);
node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
return g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
}
/**
@ -1169,13 +1175,15 @@ g_hash_table_insert_internal (GHashTable *hash_table,
* value is freed using that function. If you supplied a
* @key_destroy_func when creating the #GHashTable, the passed
* key is freed using that function.
*
* Returns: %TRUE if the key did not exist yet
*/
void
gboolean
g_hash_table_insert (GHashTable *hash_table,
gpointer key,
gpointer value)
{
g_hash_table_insert_internal (hash_table, key, value, FALSE);
return g_hash_table_insert_internal (hash_table, key, value, FALSE);
}
/**
@ -1191,13 +1199,15 @@ g_hash_table_insert (GHashTable *hash_table,
* the #GHashTable, the old value is freed using that function.
* If you supplied a @key_destroy_func when creating the
* #GHashTable, the old key is freed using that function.
*
* Returns: %TRUE of the key did not exist yet
*/
void
gboolean
g_hash_table_replace (GHashTable *hash_table,
gpointer key,
gpointer value)
{
g_hash_table_insert_internal (hash_table, key, value, TRUE);
return g_hash_table_insert_internal (hash_table, key, value, TRUE);
}
/**
@ -1213,13 +1223,15 @@ g_hash_table_replace (GHashTable *hash_table,
* corresponding value it is able to be stored more efficiently. See
* the discussion in the section description.
*
* Returns: %TRUE if the key did not exist yet
*
* Since: 2.32
**/
void
*/
gboolean
g_hash_table_add (GHashTable *hash_table,
gpointer key)
{
g_hash_table_insert_internal (hash_table, key, key, TRUE);
return g_hash_table_insert_internal (hash_table, key, key, TRUE);
}
/**

View File

@ -66,15 +66,15 @@ GHashTable* g_hash_table_new_full (GHashFunc hash_func,
GLIB_AVAILABLE_IN_ALL
void g_hash_table_destroy (GHashTable *hash_table);
GLIB_AVAILABLE_IN_ALL
void g_hash_table_insert (GHashTable *hash_table,
gboolean g_hash_table_insert (GHashTable *hash_table,
gpointer key,
gpointer value);
GLIB_AVAILABLE_IN_ALL
void g_hash_table_replace (GHashTable *hash_table,
gboolean g_hash_table_replace (GHashTable *hash_table,
gpointer key,
gpointer value);
GLIB_AVAILABLE_IN_ALL
void g_hash_table_add (GHashTable *hash_table,
gboolean g_hash_table_add (GHashTable *hash_table,
gpointer key);
GLIB_AVAILABLE_IN_ALL
gboolean g_hash_table_remove (GHashTable *hash_table,

View File

@ -508,9 +508,11 @@ set_hash_test (void)
for (i = 2; i < 5000; i += 7)
{
char *s = g_strdup_printf ("%d", i);
g_hash_table_add (hash_table, s);
g_assert (g_hash_table_add (hash_table, s));
}
g_assert (!g_hash_table_add (hash_table, g_strdup_printf ("%d", 2)));
i = 0;
g_hash_table_foreach (hash_table, set_check, &i);
g_assert_cmpint (i, ==, g_hash_table_size (hash_table));
@ -520,7 +522,11 @@ set_hash_test (void)
g_assert (!g_hash_table_contains (hash_table, "a"));
/* this will cause the hash table to loose set nature */
g_hash_table_insert (hash_table, g_strdup ("a"), "b");
g_assert (g_hash_table_insert (hash_table, g_strdup ("a"), "b"));
g_assert (!g_hash_table_insert (hash_table, g_strdup ("a"), "b"));
g_assert (g_hash_table_replace (hash_table, g_strdup ("c"), "d"));
g_assert (!g_hash_table_replace (hash_table, g_strdup ("c"), "d"));
g_assert_cmpstr (g_hash_table_lookup (hash_table, "2"), ==, "2");
g_assert_cmpstr (g_hash_table_lookup (hash_table, "a"), ==, "b");