Add g_hash_table_get_keys() and g_hash_table_get_values(), API to retrieve
authorEmmanuele Bassi <ebassi@gnome.org>
Wed, 11 Apr 2007 13:09:38 +0000 (13:09 +0000)
committerEmmanuele Bassi <ebassi@src.gnome.org>
Wed, 11 Apr 2007 13:09:38 +0000 (13:09 +0000)
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

ChangeLog
glib/ghash.c
glib/ghash.h
glib/glib.symbols
tests/hash-test.c

index d40d321..9fcb3c9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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 
index aa0cc0a..8eea09a 100644 (file)
@@ -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)
 {
index 41813de..8542665 100644 (file)
@@ -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);
index 5fc3163..bc6a307 100644 (file)
@@ -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
index 00074fb..da77dd3 100644 (file)
@@ -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);