Patch to do reverse lookups in g_hash_table



g_hash_table_lookup_reverse()


This function is nearly the same as g_hash_table_find(),
but the key is returned instead of the value.


diff -apru glib/docs/reference/glib/glib-sections.txt glib-new/docs/reference/glib/glib-sections.txt
--- glib/docs/reference/glib/glib-sections.txt  2004-06-24 14:28:02.000000000 +1000
+++ glib-new/docs/reference/glib/glib-sections.txt 2004-06-21 13:19:31.000000000 +1000
@@ -1584,6 +1584,7 @@ g_hash_table_lookup
 g_hash_table_lookup_extended
 g_hash_table_foreach
 g_hash_table_find
+g_hash_table_lookup_reverse
 GHFunc
 g_hash_table_remove
 g_hash_table_steal



diff -apru glib/glib/ghash.h glib-new/glib/ghash.h
--- glib/glib/ghash.h   2004-06-24 14:28:07.000000000 +1000
+++ glib-new/glib/ghash.h       2004-06-21 01:32:40.000000000 +1000
@@ -58,6 +58,9 @@ gboolean    g_hash_table_steal
                                            gconstpointer   key);
 gpointer    g_hash_table_lookup                   (GHashTable     *hash_table,
                                            gconstpointer   key);
+gpointer    g_hash_table_lookup_reverse           (GHashTable     *hash_table,
+                                           GHRFunc         predicate,
+                                           gpointer        user_data);
 gboolean    g_hash_table_lookup_extended   (GHashTable    *hash_table,
                                            gconstpointer   lookup_key,
                                            gpointer       *orig_key,



diff -apru glib/glib/ghash.c glib-new/glib/ghash.c
--- glib/glib/ghash.c   2004-06-24 14:28:07.000000000 +1000
+++ glib-new/glib/ghash.c       2004-06-21 01:32:37.000000000 +1000
@@ -595,6 +595,39 @@ g_hash_table_find (GHashTable         *hash_t
 }

 /**
+ * g_hash_table_lookup_reverse:
+ * @hash_table: a #GHashTable.
+ * @predicate:  function to test the key/value pairs for a certain property.
+ * @user_data:  user data to pass to the function.
+ *
+ * Calls the given function for key/value pairs in the #GHashTable until
+ * @predicate returns %TRUE.  The function is passed the key and value of
+ * each pair, and the given @user_data parameter. The hash table may not
+ * be modified while iterating over it (you can't add/remove items).
+ *
+ * Return value: The value of the matching key is returned, for which
+ * func evaluates to %TRUE. If no match is found, %NULL is returned.
+ *
+ **/
+gpointer
+g_hash_table_lookup_reverse (GHashTable           *hash_table,
+                            GHRFunc        predicate,
+                            gpointer       user_data)
+{
+  GHashNode *node;
+  gint i;
+
+  g_return_val_if_fail (hash_table != NULL, NULL);
+  g_return_val_if_fail (predicate != NULL, NULL);
+
+  for (i = 0; i < hash_table->size; i++)
+    for (node = hash_table->nodes[i]; node; node = node->next)
+      if (predicate (node->key, node->value, user_data))
+        return node->key;
+  return NULL;
+}
+
+/**
  * g_hash_table_size:
  * @hash_table: a #GHashTable.
  *



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]