g_hash_table_steal_extended()
- From: Ken Ishii <keni4i yahoo com>
- To: gtk-devel-list gnome org
- Subject: g_hash_table_steal_extended()
- Date: Sat, 18 Nov 2006 10:51:18 -0800 (PST)
Hi,
When you want to steal a key/value pair from a GHashTable and at the same time
retrieve the pair's value, doing something like
value = g_hash_table_lookup (hash_table, key);
g_hash_table_steal (hash_table, key);
requires hashing the same key twice. So instead, something like
if (g_hash_table_steal_extended (hash_table, key, &value))
{
/* pair stolen, value retrieved */
}
would be more efficient. The behavior of this new function is similar to that
of Java's Hashtable::remove(). The implementation is pasted below.
Just my two cents,
Ken
Index: glib/ghash.c
===================================================================
RCS file: /cvs/gnome/glib/glib/ghash.c,v
retrieving revision 1.47
diff -u -r1.47 ghash.c
--- glib/ghash.c 1 Jun 2006 14:16:39 -0000 1.47
+++ glib/ghash.c 18 Nov 2006 18:22:49 -0000
@@ -509,6 +509,35 @@
return FALSE;
}
+gboolean
+g_hash_table_steal_extended (GHashTable *hash_table,
+ gconstpointer key,
+ gpointer *value)
+{
+ 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;
+
+ if (value != NULL)
+ *value = dest->value;
+
+ (*node) = dest->next;
+ g_hash_node_destroy (dest, NULL, NULL);
+ hash_table->nnodes--;
+
+ G_HASH_TABLE_RESIZE (hash_table);
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
/**
* g_hash_table_steal_all:
* @hash_table: a #GHashTable.
____________________________________________________________________________________
Sponsored Link
$420k for $1,399/mo.
Think You Pay Too Much For Your Mortgage?
Find Out! www.LowerMyBills.com/lre
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]