From a5b4b8bfb18838454d99bdee60294da07e5ef0d2 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 1 Jun 2006 14:16:39 +0000 Subject: [PATCH] Add g_hash_table_{remove,steal}_all to remove all nodes from a hash table. 2006-06-01 Matthias Clasen * glib/glib.symbols: * glib/ghash.h: * glib/ghash.c: Add g_hash_table_{remove,steal}_all to remove all nodes from a hash table. (#168538, Matt Barnes) --- ChangeLog | 7 ++++ ChangeLog.pre-2-12 | 7 ++++ docs/reference/ChangeLog | 4 ++ docs/reference/glib/glib-sections.txt | 2 + glib/ghash.c | 72 +++++++++++++++++++++++++++++------ glib/ghash.h | 2 + glib/glib.symbols | 2 + 7 files changed, 84 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index dd8dd06..c782756 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2006-06-01 Matthias Clasen + + * glib/glib.symbols: + * glib/ghash.h: + * glib/ghash.c: Add g_hash_table_{remove,steal}_all to + remove all nodes from a hash table. (#168538, Matt Barnes) + 2006-06-01 Behdad Esfahbod * glib/gkeyfile.c (g_key_file_to_data), diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index dd8dd06..c782756 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,10 @@ +2006-06-01 Matthias Clasen + + * glib/glib.symbols: + * glib/ghash.h: + * glib/ghash.c: Add g_hash_table_{remove,steal}_all to + remove all nodes from a hash table. (#168538, Matt Barnes) + 2006-06-01 Behdad Esfahbod * glib/gkeyfile.c (g_key_file_to_data), diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index 8f920ee..5b3db99 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,7 @@ +2006-06-01 Matthias Clasen + + * glib/glib-sections.txt: Add new hash table functions. + Wed May 31 11:35:48 2006 Tim Janik * gobject/tmpl/gtype.sgml (Note): amend G_TYPE_CHAR according to #303622. diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index aa64591..14a5b96 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -1844,6 +1844,8 @@ g_hash_table_remove g_hash_table_steal g_hash_table_foreach_remove g_hash_table_foreach_steal +g_hash_table_remove_all +g_hash_table_steal_all GHRFunc g_hash_table_freeze g_hash_table_thaw diff --git a/glib/ghash.c b/glib/ghash.c index 2f4ac03..1069758 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -215,21 +215,10 @@ g_hash_table_unref (GHashTable *hash_table) void g_hash_table_destroy (GHashTable *hash_table) { - gint i; - g_return_if_fail (hash_table != NULL); g_return_if_fail (hash_table->ref_count > 0); - 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; - hash_table->size = HASH_TABLE_MIN_SIZE; - + g_hash_table_remove_all (hash_table); g_hash_table_unref (hash_table); } @@ -455,6 +444,38 @@ g_hash_table_remove (GHashTable *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. @@ -489,6 +510,33 @@ g_hash_table_steal (GHashTable *hash_table, } /** + * 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_foreach_remove: * @hash_table: a #GHashTable. * @func: the function to call for each key/value pair. diff --git a/glib/ghash.h b/glib/ghash.h index 6d301b2..41813de 100644 --- a/glib/ghash.h +++ b/glib/ghash.h @@ -54,8 +54,10 @@ void g_hash_table_replace (GHashTable *hash_table, gpointer value); gboolean g_hash_table_remove (GHashTable *hash_table, gconstpointer key); +void g_hash_table_remove_all (GHashTable *hash_table); gboolean g_hash_table_steal (GHashTable *hash_table, gconstpointer key); +void g_hash_table_steal_all (GHashTable *hash_table); gpointer g_hash_table_lookup (GHashTable *hash_table, gconstpointer key); gboolean g_hash_table_lookup_extended (GHashTable *hash_table, diff --git a/glib/glib.symbols b/glib/glib.symbols index 62e2a10..4c7b7c1 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -362,9 +362,11 @@ g_hash_table_lookup_extended g_hash_table_new g_hash_table_new_full g_hash_table_remove +g_hash_table_remove_all g_hash_table_replace g_hash_table_size g_hash_table_steal +g_hash_table_steal_all #endif #endif -- 2.7.4