diff options
author | Emmanuele Bassi <ebassi@gnome.org> | 2007-04-11 13:09:38 +0000 |
---|---|---|
committer | Emmanuele Bassi <ebassi@src.gnome.org> | 2007-04-11 13:09:38 +0000 |
commit | db8642a56c0436aeb730ead593140bc01b82d4ac (patch) | |
tree | 0c7adfb54d0ec9af16aee6de6dcc3be660740342 | |
parent | e542f521efb15d9ed4aa776cccfab9a55c9e7922 (diff) |
Add g_hash_table_get_keys() and g_hash_table_get_values(), API to retrieve
2007-04-11 Emmanuele Bassi <ebassi@gnome.org>
* glib/ghash.[ch]: Add g_hash_table_get_keys() and
g_hash_table_get_values(), API to retrieve the keys
and values inside an hash table in list form. (#413133)
* glib/glib.symbols: Update symbols.
* tests/hash-test.c: Exercise newly added functions.
svn path=/trunk/; revision=5444
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | glib/ghash.c | 62 | ||||
-rw-r--r-- | glib/ghash.h | 3 | ||||
-rw-r--r-- | glib/glib.symbols | 2 | ||||
-rw-r--r-- | tests/hash-test.c | 20 |
5 files changed, 96 insertions, 1 deletions
@@ -1,3 +1,13 @@ +2007-04-11 Emmanuele Bassi <ebassi@gnome.org> + + * glib/ghash.[ch]: Add g_hash_table_get_keys() and + g_hash_table_get_values(), API to retrieve the keys + and values inside an hash table in list form. (#413133) + + * glib/glib.symbols: Update symbols. + + * tests/hash-test.c: Exercise newly added functions. + 2007-04-11 Matthias Clasen <mclasen@redhat.com> * configure.in: Use CFLAGS/LDFLAGS in addition to diff --git a/glib/ghash.c b/glib/ghash.c index aa0cc0a09..8eea09a26 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -728,6 +728,68 @@ g_hash_table_size (GHashTable *hash_table) return hash_table->nnodes; } +/** + * g_hash_table_get_keys: + * @hash_table: a #GHashTable + * + * Retrieves every key inside @hash_table. The returned data is valid + * until @hash_table is modified. + * + * Return value: a #GList containing all the keys inside the hash + * table. The content of the list is owned by the hash table and + * should not be modified or freed. Use g_list_free() when done + * using the list. + * + * Since: 2.14 + */ +GList * +g_hash_table_get_keys (GHashTable *hash_table) +{ + GHashNode *node; + gint i; + GList *retval; + + g_return_val_if_fail (hash_table != NULL, NULL); + + retval = NULL; + for (i = 0; i < hash_table->size; i++) + for (node = hash_table->nodes[i]; node; node = node->next) + retval = g_list_prepend (retval, node->key); + + return retval; +} + +/** + * g_hash_table_get_values: + * @hash_table: a #GHashTable + * + * Retrieves every value inside @hash_table. The returned data is + * valid until @hash_table is modified. + * + * Return value: a #GList containing all the values inside the hash + * table. The content of the list is owned by the hash table and + * should not be modified or freed. Use g_list_free() when done + * using the list. + * + * Since: 2.14 + */ +GList * +g_hash_table_get_values (GHashTable *hash_table) +{ + GHashNode *node; + gint i; + GList *retval; + + g_return_val_if_fail (hash_table != NULL, NULL); + + retval = NULL; + for (i = 0; i < hash_table->size; i++) + for (node = hash_table->nodes[i]; node; node = node->next) + retval = g_list_prepend (retval, node->value); + + return retval; +} + static void g_hash_table_resize (GHashTable *hash_table) { diff --git a/glib/ghash.h b/glib/ghash.h index 41813de2f..854266506 100644 --- a/glib/ghash.h +++ b/glib/ghash.h @@ -28,6 +28,7 @@ #define __G_HASH_H__ #include <glib/gtypes.h> +#include <glib/glist.h> G_BEGIN_DECLS @@ -77,6 +78,8 @@ guint g_hash_table_foreach_steal (GHashTable *hash_table, GHRFunc func, gpointer user_data); guint g_hash_table_size (GHashTable *hash_table); +GList * g_hash_table_get_keys (GHashTable *hash_table); +GList * g_hash_table_get_values (GHashTable *hash_table); /* keeping hash tables alive */ GHashTable* g_hash_table_ref (GHashTable *hash_table); diff --git a/glib/glib.symbols b/glib/glib.symbols index 5fc3163a5..bc6a30777 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -356,6 +356,8 @@ g_hash_table_find g_hash_table_foreach g_hash_table_foreach_remove g_hash_table_foreach_steal +g_hash_table_get_keys +g_hash_table_get_values g_hash_table_insert g_hash_table_lookup g_hash_table_lookup_extended diff --git a/tests/hash-test.c b/tests/hash-test.c index 00074fb1b..da77dd38b 100644 --- a/tests/hash-test.c +++ b/tests/hash-test.c @@ -342,7 +342,9 @@ main (int argc, GHashTable *hash_table; gint i; gint value = 120; - gint *pvalue; + gint *pvalue; + GList *keys, *values; + gint keys_len, values_len; hash_table = g_hash_table_new (my_hash, my_hash_equal); for (i = 0; i < 10000; i++) @@ -353,6 +355,22 @@ main (int argc, pvalue = g_hash_table_find (hash_table, find_first, &value); if (!pvalue || *pvalue != value) g_assert_not_reached(); + + keys = g_hash_table_get_keys (hash_table); + if (!keys) + g_assert_not_reached (); + + values = g_hash_table_get_values (hash_table); + if (!values) + g_assert_not_reached (); + + keys_len = g_list_length (keys); + values_len = g_list_length (values); + if (values_len != keys_len && keys_len != g_hash_table_size (hash_table)) + g_assert_not_reached (); + + g_list_free (keys); + g_list_free (values); g_hash_table_foreach (hash_table, my_hash_callback, NULL); |