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_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; }
guint g_hash_table_foreach_remove (GHashTable *hash_table, GHRFunc func, gpointer user_data) { 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, hash_table->key_destroy_func, hash_table->value_destroy_func); node = prev; } else { hash_table->nodes[i] = node->next; g_hash_node_destroy (node, hash_table->key_destroy_func, hash_table->value_destroy_func); goto restart; } } } } return deleted; }
/** * 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; }