* 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,
if (hash_table->value_destroy_func)
(* hash_table->value_destroy_func) (value_to_free);
}
+
+ return !already_exists;
}
/**
* 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,
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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
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,
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));
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");