/** * g_hash_table_replace: * @hash_table: a #GHashTable. * @key: a key to insert. * @value: the value to associate with the key. * * Inserts a new key and value into a #GHashTable similar to * g_hash_table_insert(). The difference is that if the key already exists * in the #GHashTable, it gets replaced by the new key. If you supplied a * @value_destroy_func when creating 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. **/ void g_hash_table_replace (GHashTable *hash_table, gpointer key, gpointer value) { GHashNode **node; g_return_if_fail (hash_table != NULL); node = g_hash_table_lookup_node (hash_table, key); if (*node) { if (hash_table->key_destroy_func) hash_table->key_destroy_func ((*node)->key); if (hash_table->value_destroy_func) hash_table->value_destroy_func ((*node)->value); (*node)->key = key; (*node)->value = value; } else { *node = g_hash_node_new (key, value); hash_table->nnodes++; G_HASH_TABLE_RESIZE (hash_table); } }
/** * g_hash_table_remove: * @hash_table: a #GHashTable. * @key: the key to remove. * * Removes a key and its associated value from a #GHashTable. * * If the #GHashTable was created using g_hash_table_new_full(), the * key and value are freed using the supplied destroy functions, otherwise * you have to make sure that any dynamically allocated values are freed * yourself. * * Return value: %TRUE if the key was found and removed from the #GHashTable. **/ gboolean g_hash_table_remove (GHashTable *hash_table, gconstpointer key) { GHashNode **node, *dest; g_return_val_if_fail (hash_table != NULL, FALSE); node = g_hash_table_lookup_node (hash_table, key); if (*node) { dest = *node; (*node) = dest->next; g_hash_node_destroy (dest, hash_table->key_destroy_func, hash_table->value_destroy_func); hash_table->nnodes--; G_HASH_TABLE_RESIZE (hash_table); return TRUE; } return FALSE; }
/** * g_hash_table_insert: * @hash_table: a #GHashTable. * @key: a key to insert. * @value: the value to associate with the key. * * Inserts a new key and value into a #GHashTable. * * If the key already exists in the #GHashTable its current value is replaced * with the new value. If you supplied a @value_destroy_func when creating the * #GHashTable, the old 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. **/ void g_hash_table_insert (GHashTable *hash_table, gpointer key, gpointer value) { GHashNode **node; g_return_if_fail (hash_table != NULL); node = g_hash_table_lookup_node (hash_table, key); if (*node) { /* do not reset node->key in this place, keeping * the old key is the intended behaviour. * g_hash_table_replace() can be used instead. */ /* free the passed key */ if (hash_table->key_destroy_func) hash_table->key_destroy_func (key); if (hash_table->value_destroy_func) hash_table->value_destroy_func ((*node)->value); (*node)->value = value; } else { *node = g_hash_node_new (key, value); hash_table->nnodes++; G_HASH_TABLE_RESIZE (hash_table); } }
/** * g_hash_table_replace: * @hash_table: a #GHashTable. * @key: a key to insert. * @value: the value to associate with the key. * * Inserts a new key and value into a #GHashTable similar to * g_hash_table_insert(). The difference is that if the key already exists * in the #GHashTable, it gets replaced by the new key. If you supplied a * @value_destroy_func when creating 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. **/ void mono_g_hash_table_replace (MonoGHashTable *hash_table, gpointer key, gpointer value) { MonoGHashNode **node; g_return_if_fail (hash_table != NULL); node = g_hash_table_lookup_node (hash_table, key); if (*node) { if (hash_table->key_destroy_func) hash_table->key_destroy_func ((*node)->key); if (hash_table->value_destroy_func) hash_table->value_destroy_func ((*node)->value); SET_NODE_KEY ((*node), hash_table->gc_type, key); SET_NODE_VALUE ((*node), hash_table->gc_type, value); } else { *node = g_hash_node_new (key, value, hash_table->gc_type); hash_table->nnodes++; G_HASH_TABLE_RESIZE (hash_table); } }
static guint g_hash_table_foreach_remove_or_steal (GHashTable *hash_table, GHRFunc func, gpointer user_data, gboolean notify) { GHashNode *node, *prev; guint i; guint deleted = 0; for (i = 0; i < hash_table->size; i++) { restart: prev = NULL; for (node = hash_table->nodes[i]; node; prev = node, node = node->next) { if ((* func) (node->key, node->value, user_data)) { deleted += 1; hash_table->nnodes -= 1; if (prev) { prev->next = node->next; g_hash_node_destroy (node, notify ? hash_table->key_destroy_func : NULL, notify ? hash_table->value_destroy_func : NULL); node = prev; } else { hash_table->nodes[i] = node->next; g_hash_node_destroy (node, notify ? hash_table->key_destroy_func : NULL, notify ? hash_table->value_destroy_func : NULL); goto restart; } } } } G_HASH_TABLE_RESIZE (hash_table); return deleted; }
/** * g_hash_table_steal_all: * @hash_table: a #GHashTable. * * Removes all keys and their associated values from a #GHashTable * without calling the key and value destroy functions. * * Since: 2.12 **/ void g_hash_table_steal_all (GHashTable *hash_table) { guint i; g_return_if_fail (hash_table != NULL); for (i = 0; i < hash_table->size; i++) { g_hash_nodes_destroy (hash_table->nodes[i], NULL, NULL); hash_table->nodes[i] = NULL; } hash_table->nnodes = 0; G_HASH_TABLE_RESIZE (hash_table); }
/** * g_hash_table_remove_all: * @hash_table: a #GHashTable * * Removes all keys and their associated values from a #GHashTable. * * If the #GHashTable was created using g_hash_table_new_full(), the keys * and values are freed using the supplied destroy functions, otherwise you * have to make sure that any dynamically allocated values are freed * yourself. * * Since: 2.12 **/ void g_hash_table_remove_all (GHashTable *hash_table) { guint i; g_return_if_fail (hash_table != NULL); for (i = 0; i < hash_table->size; i++) { g_hash_nodes_destroy (hash_table->nodes[i], hash_table->key_destroy_func, hash_table->value_destroy_func); hash_table->nodes[i] = NULL; } hash_table->nnodes = 0; G_HASH_TABLE_RESIZE (hash_table); }
/** * g_hash_table_steal: * @hash_table: a #GHashTable. * @key: the key to remove. * * Removes a key and its associated value from a #GHashTable without * calling the key and value destroy functions. * * Return value: %TRUE if the key was found and removed from the #GHashTable. **/ gboolean mono_g_hash_table_steal (MonoGHashTable *hash_table, gconstpointer key) { MonoGHashNode **node, *dest; g_return_val_if_fail (hash_table != NULL, FALSE); node = g_hash_table_lookup_node (hash_table, key); if (*node) { dest = *node; (*node) = dest->next; g_hash_node_destroy (dest, hash_table->gc_type, NULL, NULL); hash_table->nnodes--; G_HASH_TABLE_RESIZE (hash_table); return TRUE; } return FALSE; }