Call destroy notify when destroying the hash table in g_hash_table_unref.
[platform/upstream/glib.git] / glib / ghash.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 /*
28  * MT safe
29  */
30
31 #include "config.h"
32
33 #include "glib.h"
34 #include "galias.h"
35
36
37 #define HASH_TABLE_MIN_SIZE 11
38 #define HASH_TABLE_MAX_SIZE 13845163
39
40
41 typedef struct _GHashNode      GHashNode;
42
43 struct _GHashNode
44 {
45   gpointer   key;
46   gpointer   value;
47   GHashNode *next;
48   guint      key_hash;
49 };
50
51 struct _GHashTable
52 {
53   gint             size;
54   gint             nnodes;
55   GHashNode      **nodes;
56   GHashFunc        hash_func;
57   GEqualFunc       key_equal_func;
58   volatile gint    ref_count;
59   GDestroyNotify   key_destroy_func;
60   GDestroyNotify   value_destroy_func;
61 };
62
63 /*
64  * g_hash_table_lookup_node:
65  * @hash_table: our #GHashTable
66  * @key: the key to lookup against
67  * @hash_return: optional key hash return location
68  * Return value: a pointer to the described #GHashNode pointer
69  *
70  * Performs a lookup in the hash table.  Virtually all hash operations
71  * will use this function internally.
72  *
73  * This function first computes the hash value of the key using the
74  * user's hash function.
75  *
76  * If an entry in the table matching @key is found then this function
77  * returns a pointer to the pointer to that entry in the table.  In
78  * the case that the entry is at the head of a chain, this pointer
79  * will be an item in the nodes[] array.  In the case that the entry
80  * is not at the head of a chain, this pointer will be the ->next
81  * pointer on the node that preceeds it.
82  *
83  * In the case that no matching entry exists in the table, a pointer
84  * to a %NULL pointer will be returned.  To insert a item, this %NULL
85  * pointer should be updated to point to the new #GHashNode.
86  *
87  * If @hash_return is a pass-by-reference parameter.  If it is
88  * non-%NULL then the computed hash value is returned.  This is to
89  * save insertions from having to compute the hash record again for
90  * the new record.
91  */
92 static inline GHashNode **
93 g_hash_table_lookup_node (GHashTable    *hash_table,
94                           gconstpointer  key,
95                           guint         *hash_return)
96 {
97   GHashNode **node_ptr, *node;
98   guint hash_value;
99
100   hash_value = (* hash_table->hash_func) (key);
101   node_ptr = &hash_table->nodes[hash_value % hash_table->size];
102
103   if (hash_return)
104     *hash_return = hash_value;
105
106   /* Hash table lookup needs to be fast.
107    *  We therefore remove the extra conditional of testing
108    *  whether to call the key_equal_func or not from
109    *  the inner loop.
110    *
111    *  Additional optimisation: first check if our full hash
112    *  values are equal so we can avoid calling the full-blown
113    *  key equality function in most cases.
114    */
115   if (hash_table->key_equal_func)
116     {
117       while ((node = *node_ptr))
118         {
119           if (node->key_hash == hash_value &&
120               hash_table->key_equal_func (node->key, key))
121             break;
122
123           node_ptr = &(*node_ptr)->next;
124         }
125     }
126   else
127     {
128       while ((node = *node_ptr))
129         {
130           if (node->key == key)
131             break;
132
133           node_ptr = &(*node_ptr)->next;
134         }
135     }
136
137   return node_ptr;
138 }
139
140 /*
141  * g_hash_table_remove_node:
142  * @hash_table: our #GHashTable
143  * @node_ptr_ptr: a pointer to the return value from
144  *   g_hash_table_lookup_node()
145  * @notify: %TRUE if the destroy notify handlers are to be called
146  *
147  * Removes a node from the hash table and updates the node count.  The
148  * node is freed.  No table resize is performed.
149  *
150  * If @notify is %TRUE then the destroy notify functions are called
151  * for the key and value of the hash node.
152  *
153  * @node_ptr_ptr is a pass-by-reference in/out parameter.  When the
154  * function is called, it should point to the pointer to the node to
155  * remove.  This level of indirection is required so that the pointer
156  * may be updated appropriately once the node has been removed.
157  *
158  * Before the function returns, the pointer at @node_ptr_ptr will be
159  * updated to point to the position in the table that contains the
160  * pointer to the "next" node in the chain.  This makes this function
161  * convenient to use from functions that iterate over the entire
162  * table.  If there is no further item in the chain then the
163  * #GHashNode pointer will be %NULL (ie: **node_ptr_ptr == %NULL).
164  *
165  * Since the pointer in the table to the removed node is replaced with
166  * either a pointer to the next node or a %NULL pointer as
167  * appropriate, the pointer at the end of @node_ptr_ptr will never be
168  * modified at all.  Stay tuned. :)
169  */
170 static void
171 g_hash_table_remove_node (GHashTable   *hash_table,
172                           GHashNode  ***node_ptr_ptr,
173                           gboolean      notify)
174 {
175   GHashNode **node_ptr, *node;
176
177   node_ptr = *node_ptr_ptr;
178   node = *node_ptr;
179
180   *node_ptr = node->next;
181
182   if (notify && hash_table->key_destroy_func)
183     hash_table->key_destroy_func (node->key);
184
185   if (notify && hash_table->value_destroy_func)
186     hash_table->value_destroy_func (node->value);
187
188   g_slice_free (GHashNode, node);
189
190   hash_table->nnodes--;
191 }
192
193 /*
194  * g_hash_table_remove_all_nodes:
195  * @hash_table: our #GHashTable
196  * @notify: %TRUE if the destroy notify handlers are to be called
197  *
198  * Removes all nodes from the table.  Since this may be a precursor to
199  * freeing the table entirely, no resize is performed.
200  *
201  * If @notify is %TRUE then the destroy notify functions are called
202  * for the key and value of the hash node.
203  */
204 static void
205 g_hash_table_remove_all_nodes (GHashTable *hash_table,
206                                gboolean    notify)
207 {
208   GHashNode **node_ptr;
209   int i;
210
211   for (i = 0; i < hash_table->size; i++)
212     for (node_ptr = &hash_table->nodes[i]; *node_ptr != NULL;)
213       g_hash_table_remove_node (hash_table, &node_ptr, notify);
214
215   hash_table->nnodes = 0;
216 }
217
218 /*
219  * g_hash_table_resize:
220  * @hash_table: our #GHashTable
221  *
222  * Resizes the hash table to the optimal size based on the number of
223  * nodes currently held.  If you call this function then a resize will
224  * occur, even if one does not need to occur.  Use
225  * g_hash_table_maybe_resize() instead.
226  */
227 static void
228 g_hash_table_resize (GHashTable *hash_table)
229 {
230   GHashNode **new_nodes;
231   GHashNode *node;
232   GHashNode *next;
233   guint hash_val;
234   gint new_size;
235   gint i;
236
237   new_size = g_spaced_primes_closest (hash_table->nnodes);
238   new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
239
240   new_nodes = g_new0 (GHashNode*, new_size);
241
242   for (i = 0; i < hash_table->size; i++)
243     for (node = hash_table->nodes[i]; node; node = next)
244       {
245         next = node->next;
246
247         hash_val = node->key_hash % new_size;
248
249         node->next = new_nodes[hash_val];
250         new_nodes[hash_val] = node;
251       }
252
253   g_free (hash_table->nodes);
254   hash_table->nodes = new_nodes;
255   hash_table->size = new_size;
256 }
257
258 /*
259  * g_hash_table_maybe_resize:
260  * @hash_table: our #GHashTable
261  *
262  * Resizes the hash table, if needed.
263  *
264  * Essentially, calls g_hash_table_resize() if the table has strayed
265  * too far from its ideal size for its number of nodes.
266  */
267 static inline void
268 g_hash_table_maybe_resize (GHashTable *hash_table)
269 {
270   gint nnodes = hash_table->nnodes;
271   gint size = hash_table->size;
272
273   if ((size >= 3 * nnodes && size > HASH_TABLE_MIN_SIZE) ||
274       (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE))
275     g_hash_table_resize (hash_table);
276 }
277
278 /**
279  * g_hash_table_new:
280  * @hash_func: a function to create a hash value from a key.
281  *   Hash values are used to determine where keys are stored within the
282  *   #GHashTable data structure. The g_direct_hash(), g_int_hash() and
283  *   g_str_hash() functions are provided for some common types of keys.
284  *   If hash_func is %NULL, g_direct_hash() is used.
285  * @key_equal_func: a function to check two keys for equality.  This is
286  *   used when looking up keys in the #GHashTable.  The g_direct_equal(),
287  *   g_int_equal() and g_str_equal() functions are provided for the most
288  *   common types of keys. If @key_equal_func is %NULL, keys are compared
289  *   directly in a similar fashion to g_direct_equal(), but without the
290  *   overhead of a function call.
291  *
292  * Creates a new #GHashTable with a reference count of 1.
293  *
294  * Return value: a new #GHashTable.
295  **/
296 GHashTable*
297 g_hash_table_new (GHashFunc    hash_func,
298                   GEqualFunc   key_equal_func)
299 {
300   return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
301 }
302
303
304 /**
305  * g_hash_table_new_full:
306  * @hash_func: a function to create a hash value from a key.
307  * @key_equal_func: a function to check two keys for equality.
308  * @key_destroy_func: a function to free the memory allocated for the key
309  *   used when removing the entry from the #GHashTable or %NULL if you
310  *   don't want to supply such a function.
311  * @value_destroy_func: a function to free the memory allocated for the
312  *   value used when removing the entry from the #GHashTable or %NULL if
313  *   you don't want to supply such a function.
314  *
315  * Creates a new #GHashTable like g_hash_table_new() with a reference count
316  * of 1 and allows to specify functions to free the memory allocated for the
317  * key and value that get called when removing the entry from the #GHashTable.
318  *
319  * Return value: a new #GHashTable.
320  **/
321 GHashTable*
322 g_hash_table_new_full (GHashFunc       hash_func,
323                        GEqualFunc      key_equal_func,
324                        GDestroyNotify  key_destroy_func,
325                        GDestroyNotify  value_destroy_func)
326 {
327   GHashTable *hash_table;
328
329   hash_table = g_slice_new (GHashTable);
330   hash_table->size               = HASH_TABLE_MIN_SIZE;
331   hash_table->nnodes             = 0;
332   hash_table->hash_func          = hash_func ? hash_func : g_direct_hash;
333   hash_table->key_equal_func     = key_equal_func;
334   hash_table->ref_count          = 1;
335   hash_table->key_destroy_func   = key_destroy_func;
336   hash_table->value_destroy_func = value_destroy_func;
337   hash_table->nodes              = g_new0 (GHashNode*, hash_table->size);
338
339   return hash_table;
340 }
341
342
343 /**
344  * g_hash_table_ref:
345  * @hash_table: a valid #GHashTable.
346  *
347  * Atomically increments the reference count of @hash_table by one.
348  * This function is MT-safe and may be called from any thread.
349  *
350  * Return value: the passed in #GHashTable.
351  *
352  * Since: 2.10
353  **/
354 GHashTable*
355 g_hash_table_ref (GHashTable *hash_table)
356 {
357   g_return_val_if_fail (hash_table != NULL, NULL);
358   g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
359
360   g_atomic_int_add (&hash_table->ref_count, 1);
361   return hash_table;
362 }
363
364 /**
365  * g_hash_table_unref:
366  * @hash_table: a valid #GHashTable.
367  *
368  * Atomically decrements the reference count of @hash_table by one.
369  * If the reference count drops to 0, all keys and values will be
370  * destroyed, and all memory allocated by the hash table is released.
371  * This function is MT-safe and may be called from any thread.
372  *
373  * Since: 2.10
374  **/
375 void
376 g_hash_table_unref (GHashTable *hash_table)
377 {
378   g_return_if_fail (hash_table != NULL);
379   g_return_if_fail (hash_table->ref_count > 0);
380
381   if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
382     {
383       g_hash_table_remove_all_nodes (hash_table, TRUE);
384       g_free (hash_table->nodes);
385       g_slice_free (GHashTable, hash_table);
386     }
387 }
388
389 /**
390  * g_hash_table_destroy:
391  * @hash_table: a #GHashTable.
392  *
393  * Destroys all keys and values in the #GHashTable and decrements its
394  * reference count by 1. If keys and/or values are dynamically allocated,
395  * you should either free them first or create the #GHashTable with destroy
396  * notifiers using g_hash_table_new_full(). In the latter case the destroy
397  * functions you supplied will be called on all keys and values during the
398  * destruction phase.
399  **/
400 void
401 g_hash_table_destroy (GHashTable *hash_table)
402 {
403   g_return_if_fail (hash_table != NULL);
404   g_return_if_fail (hash_table->ref_count > 0);
405
406   g_hash_table_remove_all (hash_table);
407   g_hash_table_unref (hash_table);
408 }
409
410 /**
411  * g_hash_table_lookup:
412  * @hash_table: a #GHashTable.
413  * @key: the key to look up.
414  *
415  * Looks up a key in a #GHashTable. Note that this function cannot
416  * distinguish between a key that is not present and one which is present
417  * and has the value %NULL. If you need this distinction, use
418  * g_hash_table_lookup_extended().
419  *
420  * Return value: the associated value, or %NULL if the key is not found.
421  **/
422 gpointer
423 g_hash_table_lookup (GHashTable   *hash_table,
424                      gconstpointer key)
425 {
426   GHashNode *node;
427
428   g_return_val_if_fail (hash_table != NULL, NULL);
429
430   node = *g_hash_table_lookup_node (hash_table, key, NULL);
431
432   return node ? node->value : NULL;
433 }
434
435 /**
436  * g_hash_table_lookup_extended:
437  * @hash_table: a #GHashTable.
438  * @lookup_key: the key to look up.
439  * @orig_key: returns the original key.
440  * @value: returns the value associated with the key.
441  *
442  * Looks up a key in the #GHashTable, returning the original key and the
443  * associated value and a #gboolean which is %TRUE if the key was found. This
444  * is useful if you need to free the memory allocated for the original key,
445  * for example before calling g_hash_table_remove().
446  *
447  * Return value: %TRUE if the key was found in the #GHashTable.
448  **/
449 gboolean
450 g_hash_table_lookup_extended (GHashTable    *hash_table,
451                               gconstpointer  lookup_key,
452                               gpointer      *orig_key,
453                               gpointer      *value)
454 {
455   GHashNode *node;
456
457   g_return_val_if_fail (hash_table != NULL, FALSE);
458
459   node = *g_hash_table_lookup_node (hash_table, lookup_key, NULL);
460
461   if (node == NULL)
462     return FALSE;
463
464   if (orig_key)
465     *orig_key = node->key;
466
467   if (value)
468     *value = node->value;
469
470   return TRUE;
471 }
472
473 /*
474  * g_hash_table_insert_internal:
475  * @hash_table: our #GHashTable
476  * @key: the key to insert
477  * @value: the value to insert
478  * @keep_new_key: if %TRUE and this key already exists in the table
479  *   then call the destroy notify function on the old key.  If %FALSE
480  *   then call the destroy notify function on the new key.
481  *
482  * Implements the common logic for the g_hash_table_insert() and
483  * g_hash_table_replace() functions.
484  *
485  * Do a lookup of @key.  If it is found, replace it with the new
486  * @value (and perhaps the new @key).  If it is not found, create a
487  * new node.
488  */
489 static void
490 g_hash_table_insert_internal (GHashTable *hash_table,
491                               gpointer    key,
492                               gpointer    value,
493                               gboolean    keep_new_key)
494 {
495   GHashNode **node_ptr, *node;
496   guint key_hash;
497
498   g_return_if_fail (hash_table != NULL);
499   g_return_if_fail (hash_table->ref_count > 0);
500
501   node_ptr = g_hash_table_lookup_node (hash_table, key, &key_hash);
502
503   if ((node = *node_ptr))
504     {
505       if (keep_new_key)
506         {
507           if (hash_table->key_destroy_func)
508             hash_table->key_destroy_func (node->key);
509           node->key = key;
510         }
511       else
512         {
513           if (hash_table->key_destroy_func)
514             hash_table->key_destroy_func (key);
515         }
516
517       if (hash_table->value_destroy_func)
518         hash_table->value_destroy_func (node->value);
519
520       node->value = value;
521     }
522   else
523     {
524       node = g_slice_new (GHashNode);
525
526       node->key = key;
527       node->value = value;
528       node->key_hash = key_hash;
529       node->next = NULL;
530
531       *node_ptr = node;
532       hash_table->nnodes++;
533       g_hash_table_maybe_resize (hash_table);
534     }
535 }
536
537 /**
538  * g_hash_table_insert:
539  * @hash_table: a #GHashTable.
540  * @key: a key to insert.
541  * @value: the value to associate with the key.
542  *
543  * Inserts a new key and value into a #GHashTable.
544  *
545  * If the key already exists in the #GHashTable its current value is replaced
546  * with the new value. If you supplied a @value_destroy_func when creating the
547  * #GHashTable, the old value is freed using that function. If you supplied
548  * a @key_destroy_func when creating the #GHashTable, the passed key is freed
549  * using that function.
550  **/
551 void
552 g_hash_table_insert (GHashTable *hash_table,
553                      gpointer    key,
554                      gpointer    value)
555 {
556   return g_hash_table_insert_internal (hash_table, key, value, FALSE);
557 }
558
559 /**
560  * g_hash_table_replace:
561  * @hash_table: a #GHashTable.
562  * @key: a key to insert.
563  * @value: the value to associate with the key.
564  *
565  * Inserts a new key and value into a #GHashTable similar to
566  * g_hash_table_insert(). The difference is that if the key already exists
567  * in the #GHashTable, it gets replaced by the new key. If you supplied a
568  * @value_destroy_func when creating the #GHashTable, the old value is freed
569  * using that function. If you supplied a @key_destroy_func when creating the
570  * #GHashTable, the old key is freed using that function.
571  **/
572 void
573 g_hash_table_replace (GHashTable *hash_table,
574                       gpointer    key,
575                       gpointer    value)
576 {
577   return g_hash_table_insert_internal (hash_table, key, value, TRUE);
578 }
579
580 /*
581  * g_hash_table_remove_internal:
582  * @hash_table: our #GHashTable
583  * @key: the key to remove
584  * @notify: %TRUE if the destroy notify handlers are to be called
585  * Return value: %TRUE if a node was found and removed, else %FALSE
586  *
587  * Implements the common logic for the g_hash_table_remove() and
588  * g_hash_table_steal() functions.
589  *
590  * Do a lookup of @key and remove it if it is found, calling the
591  * destroy notify handlers only if @notify is %TRUE.
592  */
593 static gboolean
594 g_hash_table_remove_internal (GHashTable    *hash_table,
595                               gconstpointer  key,
596                               gboolean       notify)
597 {
598   GHashNode **node_ptr;
599
600   g_return_val_if_fail (hash_table != NULL, FALSE);
601
602   node_ptr = g_hash_table_lookup_node (hash_table, key, NULL);
603   if (*node_ptr == NULL)
604     return FALSE;
605
606   g_hash_table_remove_node (hash_table, &node_ptr, notify);
607   g_hash_table_maybe_resize (hash_table);
608
609   return TRUE;
610 }
611
612 /**
613  * g_hash_table_remove:
614  * @hash_table: a #GHashTable.
615  * @key: the key to remove.
616  *
617  * Removes a key and its associated value from a #GHashTable.
618  *
619  * If the #GHashTable was created using g_hash_table_new_full(), the
620  * key and value are freed using the supplied destroy functions, otherwise
621  * you have to make sure that any dynamically allocated values are freed
622  * yourself.
623  *
624  * Return value: %TRUE if the key was found and removed from the #GHashTable.
625  **/
626 gboolean
627 g_hash_table_remove (GHashTable    *hash_table,
628                      gconstpointer  key)
629 {
630   return g_hash_table_remove_internal (hash_table, key, TRUE);
631 }
632
633 /**
634  * g_hash_table_steal:
635  * @hash_table: a #GHashTable.
636  * @key: the key to remove.
637  *
638  * Removes a key and its associated value from a #GHashTable without
639  * calling the key and value destroy functions.
640  *
641  * Return value: %TRUE if the key was found and removed from the #GHashTable.
642  **/
643 gboolean
644 g_hash_table_steal (GHashTable    *hash_table,
645                     gconstpointer  key)
646 {
647   return g_hash_table_remove_internal (hash_table, key, FALSE);
648 }
649
650 /**
651  * g_hash_table_remove_all:
652  * @hash_table: a #GHashTable
653  *
654  * Removes all keys and their associated values from a #GHashTable.
655  *
656  * If the #GHashTable was created using g_hash_table_new_full(), the keys
657  * and values are freed using the supplied destroy functions, otherwise you
658  * have to make sure that any dynamically allocated values are freed
659  * yourself.
660  *
661  * Since: 2.12
662  **/
663 void
664 g_hash_table_remove_all (GHashTable *hash_table)
665 {
666   g_return_if_fail (hash_table != NULL);
667
668   g_hash_table_remove_all_nodes (hash_table, TRUE);
669   g_hash_table_maybe_resize (hash_table);
670 }
671
672 /**
673  * g_hash_table_steal_all:
674  * @hash_table: a #GHashTable.
675  *
676  * Removes all keys and their associated values from a #GHashTable
677  * without calling the key and value destroy functions.
678  *
679  * Since: 2.12
680  **/
681 void
682 g_hash_table_steal_all (GHashTable *hash_table)
683 {
684   g_return_if_fail (hash_table != NULL);
685
686   g_hash_table_remove_all_nodes (hash_table, FALSE);
687   g_hash_table_maybe_resize (hash_table);
688 }
689
690 /*
691  * g_hash_table_foreach_remove_or_steal:
692  * @hash_table: our #GHashTable
693  * @func: the user's callback function
694  * @user_data: data for @func
695  * @notify: %TRUE if the destroy notify handlers are to be called
696  *
697  * Implements the common logic for g_hash_table_foreach_remove() and
698  * g_hash_table_foreach_steal().
699  *
700  * Iterates over every node in the table, calling @func with the key
701  * and value of the node (and @user_data).  If @func returns %TRUE the
702  * node is removed from the table.
703  *
704  * If @notify is true then the destroy notify handlers will be called
705  * for each removed node.
706  */
707 static guint
708 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
709                                       GHRFunc     func,
710                                       gpointer    user_data,
711                                       gboolean    notify)
712 {
713   GHashNode *node, **node_ptr;
714   guint deleted = 0;
715   gint i;
716
717   for (i = 0; i < hash_table->size; i++)
718     for (node_ptr = &hash_table->nodes[i]; (node = *node_ptr) != NULL;)
719       if ((* func) (node->key, node->value, user_data))
720         {
721           g_hash_table_remove_node (hash_table, &node_ptr, notify);
722           deleted++;
723         }
724       else
725         node_ptr = &node->next;
726
727   g_hash_table_maybe_resize (hash_table);
728
729   return deleted;
730 }
731
732 /**
733  * g_hash_table_foreach_remove:
734  * @hash_table: a #GHashTable.
735  * @func: the function to call for each key/value pair.
736  * @user_data: user data to pass to the function.
737  *
738  * Calls the given function for each key/value pair in the #GHashTable.
739  * If the function returns %TRUE, then the key/value pair is removed from the
740  * #GHashTable. If you supplied key or value destroy functions when creating
741  * the #GHashTable, they are used to free the memory allocated for the removed
742  * keys and values.
743  *
744  * Return value: the number of key/value pairs removed.
745  **/
746 guint
747 g_hash_table_foreach_remove (GHashTable *hash_table,
748                              GHRFunc     func,
749                              gpointer    user_data)
750 {
751   g_return_val_if_fail (hash_table != NULL, 0);
752   g_return_val_if_fail (func != NULL, 0);
753
754   return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
755 }
756
757 /**
758  * g_hash_table_foreach_steal:
759  * @hash_table: a #GHashTable.
760  * @func: the function to call for each key/value pair.
761  * @user_data: user data to pass to the function.
762  *
763  * Calls the given function for each key/value pair in the #GHashTable.
764  * If the function returns %TRUE, then the key/value pair is removed from the
765  * #GHashTable, but no key or value destroy functions are called.
766  *
767  * Return value: the number of key/value pairs removed.
768  **/
769 guint
770 g_hash_table_foreach_steal (GHashTable *hash_table,
771                             GHRFunc     func,
772                             gpointer    user_data)
773 {
774   g_return_val_if_fail (hash_table != NULL, 0);
775   g_return_val_if_fail (func != NULL, 0);
776
777   return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
778 }
779
780 /**
781  * g_hash_table_foreach:
782  * @hash_table: a #GHashTable.
783  * @func: the function to call for each key/value pair.
784  * @user_data: user data to pass to the function.
785  *
786  * Calls the given function for each of the key/value pairs in the
787  * #GHashTable.  The function is passed the key and value of each
788  * pair, and the given @user_data parameter.  The hash table may not
789  * be modified while iterating over it (you can't add/remove
790  * items). To remove all items matching a predicate, use
791  * g_hash_table_foreach_remove().
792  *
793  * See g_hash_table_find() for performance caveats for linear
794  * order searches in contrast to g_hash_table_lookup().
795  **/
796 void
797 g_hash_table_foreach (GHashTable *hash_table,
798                       GHFunc      func,
799                       gpointer    user_data)
800 {
801   GHashNode *node;
802   gint i;
803
804   g_return_if_fail (hash_table != NULL);
805   g_return_if_fail (func != NULL);
806
807   for (i = 0; i < hash_table->size; i++)
808     for (node = hash_table->nodes[i]; node; node = node->next)
809       (* func) (node->key, node->value, user_data);
810 }
811
812 /**
813  * g_hash_table_find:
814  * @hash_table: a #GHashTable.
815  * @predicate:  function to test the key/value pairs for a certain property.
816  * @user_data:  user data to pass to the function.
817  *
818  * Calls the given function for key/value pairs in the #GHashTable until
819  * @predicate returns %TRUE.  The function is passed the key and value of
820  * each pair, and the given @user_data parameter. The hash table may not
821  * be modified while iterating over it (you can't add/remove items).
822  *
823  * Note, that hash tables are really only optimized for forward lookups,
824  * i.e. g_hash_table_lookup().
825  * So code that frequently issues g_hash_table_find() or
826  * g_hash_table_foreach() (e.g. in the order of once per every entry in a
827  * hash table) should probably be reworked to use additional or different
828  * data structures for reverse lookups (keep in mind that an O(n) find/foreach
829  * operation issued for all n values in a hash table ends up needing O(n*n)
830  * operations).
831  *
832  * Return value: The value of the first key/value pair is returned, for which
833  * func evaluates to %TRUE. If no pair with the requested property is found,
834  * %NULL is returned.
835  *
836  * Since: 2.4
837  **/
838 gpointer
839 g_hash_table_find (GHashTable      *hash_table,
840                    GHRFunc          predicate,
841                    gpointer         user_data)
842 {
843   GHashNode *node;
844   gint i;
845
846   g_return_val_if_fail (hash_table != NULL, NULL);
847   g_return_val_if_fail (predicate != NULL, NULL);
848
849   for (i = 0; i < hash_table->size; i++)
850     for (node = hash_table->nodes[i]; node; node = node->next)
851       if (predicate (node->key, node->value, user_data))
852         return node->value;
853   return NULL;
854 }
855
856 /**
857  * g_hash_table_size:
858  * @hash_table: a #GHashTable.
859  *
860  * Returns the number of elements contained in the #GHashTable.
861  *
862  * Return value: the number of key/value pairs in the #GHashTable.
863  **/
864 guint
865 g_hash_table_size (GHashTable *hash_table)
866 {
867   g_return_val_if_fail (hash_table != NULL, 0);
868
869   return hash_table->nnodes;
870 }
871
872 /**
873  * g_hash_table_get_keys:
874  * @hash_table: a #GHashTable
875  *
876  * Retrieves every key inside @hash_table. The returned data is valid
877  * until @hash_table is modified.
878  *
879  * Return value: a #GList containing all the keys inside the hash
880  *   table. The content of the list is owned by the hash table and
881  *   should not be modified or freed. Use g_list_free() when done
882  *   using the list.
883  *
884  * Since: 2.14
885  */
886 GList *
887 g_hash_table_get_keys (GHashTable *hash_table)
888 {
889   GHashNode *node;
890   gint i;
891   GList *retval;
892
893   g_return_val_if_fail (hash_table != NULL, NULL);
894
895   retval = NULL;
896   for (i = 0; i < hash_table->size; i++)
897     for (node = hash_table->nodes[i]; node; node = node->next)
898       retval = g_list_prepend (retval, node->key);
899
900   return retval;
901 }
902
903 /**
904  * g_hash_table_get_values:
905  * @hash_table: a #GHashTable
906  *
907  * Retrieves every value inside @hash_table. The returned data is
908  * valid until @hash_table is modified.
909  *
910  * Return value: a #GList containing all the values inside the hash
911  *   table. The content of the list is owned by the hash table and
912  *   should not be modified or freed. Use g_list_free() when done
913  *   using the list.
914  *
915  * Since: 2.14
916  */
917 GList *
918 g_hash_table_get_values (GHashTable *hash_table)
919 {
920   GHashNode *node;
921   gint i;
922   GList *retval;
923
924   g_return_val_if_fail (hash_table != NULL, NULL);
925
926   retval = NULL;
927   for (i = 0; i < hash_table->size; i++)
928     for (node = hash_table->nodes[i]; node; node = node->next)
929       retval = g_list_prepend (retval, node->value);
930
931   return retval;
932 }
933
934 #define __G_HASH_C__
935 #include "galiasdef.c"