only whitespace fixes, mostly removing spaces at ends of lines (and on empty lines)
[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 #define G_HASH_TABLE_RESIZE(hash_table)                         \
64    G_STMT_START {                                               \
65      if ((hash_table->size >= 3 * hash_table->nnodes &&         \
66           hash_table->size > HASH_TABLE_MIN_SIZE) ||            \
67          (3 * hash_table->size <= hash_table->nnodes &&         \
68           hash_table->size < HASH_TABLE_MAX_SIZE))              \
69            g_hash_table_resize (hash_table);                    \
70    } G_STMT_END
71
72 static void             g_hash_table_resize       (GHashTable     *hash_table);
73 static GHashNode**      g_hash_table_lookup_node  (GHashTable     *hash_table,
74                                                    gconstpointer   key,
75                                                    guint          *hash_return);
76 static GHashNode*       g_hash_node_new           (gpointer        key,
77                                                    gpointer        value,
78                                                    guint           key_hash);
79 static guint g_hash_table_foreach_remove_or_steal (GHashTable     *hash_table,
80                                                    GHRFunc         func,
81                                                    gpointer        user_data,
82                                                    gboolean        notify);
83 static void         g_hash_table_remove_all_nodes (GHashTable *hash_table,
84                                                    gboolean        notify);
85
86
87 /**
88  * g_hash_table_new:
89  * @hash_func: a function to create a hash value from a key.
90  *   Hash values are used to determine where keys are stored within the
91  *   #GHashTable data structure. The g_direct_hash(), g_int_hash() and
92  *   g_str_hash() functions are provided for some common types of keys.
93  *   If hash_func is %NULL, g_direct_hash() is used.
94  * @key_equal_func: a function to check two keys for equality.  This is
95  *   used when looking up keys in the #GHashTable.  The g_direct_equal(),
96  *   g_int_equal() and g_str_equal() functions are provided for the most
97  *   common types of keys. If @key_equal_func is %NULL, keys are compared
98  *   directly in a similar fashion to g_direct_equal(), but without the
99  *   overhead of a function call.
100  *
101  * Creates a new #GHashTable with a reference count of 1.
102  *
103  * Return value: a new #GHashTable.
104  **/
105 GHashTable*
106 g_hash_table_new (GHashFunc    hash_func,
107                   GEqualFunc   key_equal_func)
108 {
109   return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
110 }
111
112
113 /**
114  * g_hash_table_new_full:
115  * @hash_func: a function to create a hash value from a key.
116  * @key_equal_func: a function to check two keys for equality.
117  * @key_destroy_func: a function to free the memory allocated for the key
118  *   used when removing the entry from the #GHashTable or %NULL if you
119  *   don't want to supply such a function.
120  * @value_destroy_func: a function to free the memory allocated for the
121  *   value used when removing the entry from the #GHashTable or %NULL if
122  *   you don't want to supply such a function.
123  *
124  * Creates a new #GHashTable like g_hash_table_new() with a reference count
125  * of 1 and allows to specify functions to free the memory allocated for the
126  * key and value that get called when removing the entry from the #GHashTable.
127  *
128  * Return value: a new #GHashTable.
129  **/
130 GHashTable*
131 g_hash_table_new_full (GHashFunc       hash_func,
132                        GEqualFunc      key_equal_func,
133                        GDestroyNotify  key_destroy_func,
134                        GDestroyNotify  value_destroy_func)
135 {
136   GHashTable *hash_table;
137
138   hash_table = g_slice_new (GHashTable);
139   hash_table->size               = HASH_TABLE_MIN_SIZE;
140   hash_table->nnodes             = 0;
141   hash_table->hash_func          = hash_func ? hash_func : g_direct_hash;
142   hash_table->key_equal_func     = key_equal_func;
143   hash_table->ref_count          = 1;
144   hash_table->key_destroy_func   = key_destroy_func;
145   hash_table->value_destroy_func = value_destroy_func;
146   hash_table->nodes              = g_new0 (GHashNode*, hash_table->size);
147
148   return hash_table;
149 }
150
151
152 /**
153  * g_hash_table_ref:
154  * @hash_table: a valid #GHashTable.
155  *
156  * Atomically increments the reference count of @hash_table by one.
157  * This function is MT-safe and may be called from any thread.
158  *
159  * Return value: the passed in #GHashTable.
160  *
161  * Since: 2.10
162  **/
163 GHashTable*
164 g_hash_table_ref (GHashTable *hash_table)
165 {
166   g_return_val_if_fail (hash_table != NULL, NULL);
167   g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
168
169   g_atomic_int_add (&hash_table->ref_count, 1);
170   return hash_table;
171 }
172
173 /**
174  * g_hash_table_unref:
175  * @hash_table: a valid #GHashTable.
176  *
177  * Atomically decrements the reference count of @hash_table by one.
178  * If the reference count drops to 0, all keys and values will be
179  * destroyed, and all memory allocated by the hash table is released.
180  * This function is MT-safe and may be called from any thread.
181  *
182  * Since: 2.10
183  **/
184 void
185 g_hash_table_unref (GHashTable *hash_table)
186 {
187   g_return_if_fail (hash_table != NULL);
188   g_return_if_fail (hash_table->ref_count > 0);
189
190   if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
191     {
192       g_hash_table_remove_all_nodes (hash_table, FALSE);
193       g_free (hash_table->nodes);
194       g_slice_free (GHashTable, hash_table);
195     }
196 }
197
198 /**
199  * g_hash_table_destroy:
200  * @hash_table: a #GHashTable.
201  *
202  * Destroys all keys and values in the #GHashTable and decrements its
203  * reference count by 1. If keys and/or values are dynamically allocated,
204  * you should either free them first or create the #GHashTable with destroy
205  * notifiers using g_hash_table_new_full(). In the latter case the destroy
206  * functions you supplied will be called on all keys and values during the
207  * destruction phase.
208  **/
209 void
210 g_hash_table_destroy (GHashTable *hash_table)
211 {
212   g_return_if_fail (hash_table != NULL);
213   g_return_if_fail (hash_table->ref_count > 0);
214
215   g_hash_table_remove_all (hash_table);
216   g_hash_table_unref (hash_table);
217 }
218
219 static inline GHashNode**
220 g_hash_table_lookup_node (GHashTable    *hash_table,
221                           gconstpointer  key,
222                           guint         *hash_return)
223 {
224   GHashNode **node;
225   guint hash_value;
226
227   hash_value = (* hash_table->hash_func) (key);
228   node = &hash_table->nodes[hash_value % hash_table->size];
229
230   if (hash_return)
231     *hash_return = hash_value;
232
233   /* Hash table lookup needs to be fast.
234    *  We therefore remove the extra conditional of testing
235    *  whether to call the key_equal_func or not from
236    *  the inner loop.
237    *
238    *  Additional optimisation: first check if our full hash
239    *  values are equal so we can avoid calling the full-blown
240    *  key equality function in most cases.
241    */
242   if (hash_table->key_equal_func)
243     while (*node && (((*node)->key_hash != hash_value) ||
244                      !(*hash_table->key_equal_func) ((*node)->key, key)))
245       node = &(*node)->next;
246   else
247     while (*node && (*node)->key != key)
248       node = &(*node)->next;
249
250   return node;
251 }
252
253 /**
254  * g_hash_table_lookup:
255  * @hash_table: a #GHashTable.
256  * @key: the key to look up.
257  *
258  * Looks up a key in a #GHashTable. Note that this function cannot
259  * distinguish between a key that is not present and one which is present
260  * and has the value %NULL. If you need this distinction, use
261  * g_hash_table_lookup_extended().
262  *
263  * Return value: the associated value, or %NULL if the key is not found.
264  **/
265 gpointer
266 g_hash_table_lookup (GHashTable   *hash_table,
267                      gconstpointer key)
268 {
269   GHashNode *node;
270
271   g_return_val_if_fail (hash_table != NULL, NULL);
272
273   node = *g_hash_table_lookup_node (hash_table, key, NULL);
274
275   return node ? node->value : NULL;
276 }
277
278 /**
279  * g_hash_table_lookup_extended:
280  * @hash_table: a #GHashTable.
281  * @lookup_key: the key to look up.
282  * @orig_key: returns the original key.
283  * @value: returns the value associated with the key.
284  *
285  * Looks up a key in the #GHashTable, returning the original key and the
286  * associated value and a #gboolean which is %TRUE if the key was found. This
287  * is useful if you need to free the memory allocated for the original key,
288  * for example before calling g_hash_table_remove().
289  *
290  * Return value: %TRUE if the key was found in the #GHashTable.
291  **/
292 gboolean
293 g_hash_table_lookup_extended (GHashTable    *hash_table,
294                               gconstpointer  lookup_key,
295                               gpointer      *orig_key,
296                               gpointer      *value)
297 {
298   GHashNode *node;
299
300   g_return_val_if_fail (hash_table != NULL, FALSE);
301
302   node = *g_hash_table_lookup_node (hash_table, lookup_key, NULL);
303
304   if (node)
305     {
306       if (orig_key)
307         *orig_key = node->key;
308       if (value)
309         *value = node->value;
310       return TRUE;
311     }
312   else
313     return FALSE;
314 }
315
316 static void
317 g_hash_table_insert_internal (GHashTable *hash_table,
318                               gpointer    key,
319                               gpointer    value,
320                               gboolean    keep_new_key)
321 {
322   GHashNode **node;
323   guint key_hash;
324
325   g_return_if_fail (hash_table != NULL);
326   g_return_if_fail (hash_table->ref_count > 0);
327
328   node = g_hash_table_lookup_node (hash_table, key, &key_hash);
329
330   if (*node)
331     {
332       if (keep_new_key)
333         {
334           if (hash_table->key_destroy_func)
335             hash_table->key_destroy_func ((*node)->key);
336           (*node)->key = key;
337         }
338       else
339         {
340           if (hash_table->key_destroy_func)
341             hash_table->key_destroy_func (key);
342         }
343
344       if (hash_table->value_destroy_func)
345         hash_table->value_destroy_func ((*node)->value);
346
347       (*node)->value = value;
348     }
349   else
350     {
351       *node = g_hash_node_new (key, value, key_hash);
352       hash_table->nnodes++;
353       G_HASH_TABLE_RESIZE (hash_table);
354     }
355 }
356
357 /**
358  * g_hash_table_insert:
359  * @hash_table: a #GHashTable.
360  * @key: a key to insert.
361  * @value: the value to associate with the key.
362  *
363  * Inserts a new key and value into a #GHashTable.
364  *
365  * If the key already exists in the #GHashTable its current value is replaced
366  * with the new value. If you supplied a @value_destroy_func when creating the
367  * #GHashTable, the old value is freed using that function. If you supplied
368  * a @key_destroy_func when creating the #GHashTable, the passed key is freed
369  * using that function.
370  **/
371 void
372 g_hash_table_insert (GHashTable *hash_table,
373                      gpointer    key,
374                      gpointer    value)
375 {
376   return g_hash_table_insert_internal (hash_table, key, value, FALSE);
377 }
378
379 /**
380  * g_hash_table_replace:
381  * @hash_table: a #GHashTable.
382  * @key: a key to insert.
383  * @value: the value to associate with the key.
384  *
385  * Inserts a new key and value into a #GHashTable similar to
386  * g_hash_table_insert(). The difference is that if the key already exists
387  * in the #GHashTable, it gets replaced by the new key. If you supplied a
388  * @value_destroy_func when creating the #GHashTable, the old value is freed
389  * using that function. If you supplied a @key_destroy_func when creating the
390  * #GHashTable, the old key is freed using that function.
391  **/
392 void
393 g_hash_table_replace (GHashTable *hash_table,
394                       gpointer    key,
395                       gpointer    value)
396 {
397   return g_hash_table_insert_internal (hash_table, key, value, TRUE);
398 }
399
400 static void
401 g_hash_table_remove_node (GHashTable   *hash_table,
402                           GHashNode  ***node_ptr_ptr,
403                           gboolean      notify)
404 {
405   GHashNode **node_ptr, *node;
406
407   node_ptr = *node_ptr_ptr;
408   node = *node_ptr;
409
410   *node_ptr = node->next;
411
412   if (notify && hash_table->key_destroy_func)
413     hash_table->key_destroy_func (node->key);
414
415   if (notify && hash_table->value_destroy_func)
416     hash_table->value_destroy_func (node->value);
417
418   g_slice_free (GHashNode, node);
419
420   hash_table->nnodes--;
421 }
422
423 static void
424 g_hash_table_remove_all_nodes (GHashTable *hash_table,
425                                gboolean    notify)
426 {
427   GHashNode **node_ptr;
428   int i;
429
430   for (i = 0; i < hash_table->size; i++)
431     for (node_ptr = &hash_table->nodes[i]; *node_ptr != NULL;)
432       g_hash_table_remove_node (hash_table, &node_ptr, notify);
433
434   hash_table->nnodes = 0;
435 }
436
437 static gboolean
438 g_hash_table_remove_internal (GHashTable    *hash_table,
439                               gconstpointer  key,
440                               gboolean       notify)
441 {
442   GHashNode **node_ptr;
443
444   g_return_val_if_fail (hash_table != NULL, FALSE);
445
446   node_ptr = g_hash_table_lookup_node (hash_table, key, NULL);
447   if (*node_ptr == NULL)
448     return FALSE;
449
450   g_hash_table_remove_node (hash_table, &node_ptr, notify);
451   G_HASH_TABLE_RESIZE (hash_table);
452
453   return TRUE;
454 }
455
456 /**
457  * g_hash_table_remove:
458  * @hash_table: a #GHashTable.
459  * @key: the key to remove.
460  *
461  * Removes a key and its associated value from a #GHashTable.
462  *
463  * If the #GHashTable was created using g_hash_table_new_full(), the
464  * key and value are freed using the supplied destroy functions, otherwise
465  * you have to make sure that any dynamically allocated values are freed
466  * yourself.
467  *
468  * Return value: %TRUE if the key was found and removed from the #GHashTable.
469  **/
470 gboolean
471 g_hash_table_remove (GHashTable    *hash_table,
472                      gconstpointer  key)
473 {
474   return g_hash_table_remove_internal (hash_table, key, TRUE);
475 }
476
477 /**
478  * g_hash_table_remove_all:
479  * @hash_table: a #GHashTable
480  *
481  * Removes all keys and their associated values from a #GHashTable.
482  *
483  * If the #GHashTable was created using g_hash_table_new_full(), the keys
484  * and values are freed using the supplied destroy functions, otherwise you
485  * have to make sure that any dynamically allocated values are freed
486  * yourself.
487  *
488  * Since: 2.12
489  **/
490 void
491 g_hash_table_remove_all (GHashTable *hash_table)
492 {
493   g_return_if_fail (hash_table != NULL);
494
495   g_hash_table_remove_all_nodes (hash_table, TRUE);
496   G_HASH_TABLE_RESIZE (hash_table);
497 }
498
499 /**
500  * g_hash_table_steal:
501  * @hash_table: a #GHashTable.
502  * @key: the key to remove.
503  *
504  * Removes a key and its associated value from a #GHashTable without
505  * calling the key and value destroy functions.
506  *
507  * Return value: %TRUE if the key was found and removed from the #GHashTable.
508  **/
509 gboolean
510 g_hash_table_steal (GHashTable    *hash_table,
511                     gconstpointer  key)
512 {
513   return g_hash_table_remove_internal (hash_table, key, FALSE);
514 }
515
516 /**
517  * g_hash_table_steal_all:
518  * @hash_table: a #GHashTable.
519  *
520  * Removes all keys and their associated values from a #GHashTable
521  * without calling the key and value destroy functions.
522  *
523  * Since: 2.12
524  **/
525 void
526 g_hash_table_steal_all (GHashTable *hash_table)
527 {
528   g_return_if_fail (hash_table != NULL);
529
530   g_hash_table_remove_all_nodes (hash_table, FALSE);
531   G_HASH_TABLE_RESIZE (hash_table);
532 }
533
534 /**
535  * g_hash_table_foreach_remove:
536  * @hash_table: a #GHashTable.
537  * @func: the function to call for each key/value pair.
538  * @user_data: user data to pass to the function.
539  *
540  * Calls the given function for each key/value pair in the #GHashTable.
541  * If the function returns %TRUE, then the key/value pair is removed from the
542  * #GHashTable. If you supplied key or value destroy functions when creating
543  * the #GHashTable, they are used to free the memory allocated for the removed
544  * keys and values.
545  *
546  * Return value: the number of key/value pairs removed.
547  **/
548 guint
549 g_hash_table_foreach_remove (GHashTable *hash_table,
550                              GHRFunc     func,
551                              gpointer    user_data)
552 {
553   g_return_val_if_fail (hash_table != NULL, 0);
554   g_return_val_if_fail (func != NULL, 0);
555
556   return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
557 }
558
559 /**
560  * g_hash_table_foreach_steal:
561  * @hash_table: a #GHashTable.
562  * @func: the function to call for each key/value pair.
563  * @user_data: user data to pass to the function.
564  *
565  * Calls the given function for each key/value pair in the #GHashTable.
566  * If the function returns %TRUE, then the key/value pair is removed from the
567  * #GHashTable, but no key or value destroy functions are called.
568  *
569  * Return value: the number of key/value pairs removed.
570  **/
571 guint
572 g_hash_table_foreach_steal (GHashTable *hash_table,
573                             GHRFunc     func,
574                             gpointer    user_data)
575 {
576   g_return_val_if_fail (hash_table != NULL, 0);
577   g_return_val_if_fail (func != NULL, 0);
578
579   return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
580 }
581
582 static guint
583 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
584                                       GHRFunc     func,
585                                       gpointer    user_data,
586                                       gboolean    notify)
587 {
588   GHashNode *node, **node_ptr;
589   guint deleted = 0;
590   gint i;
591
592   for (i = 0; i < hash_table->size; i++)
593     for (node_ptr = &hash_table->nodes[i]; (node = *node_ptr) != NULL;)
594       if ((* func) (node->key, node->value, user_data))
595         {
596           g_hash_table_remove_node (hash_table, &node_ptr, notify);
597           deleted++;
598         }
599       else
600         node_ptr = &node->next;
601
602   G_HASH_TABLE_RESIZE (hash_table);
603
604   return deleted;
605 }
606
607 /**
608  * g_hash_table_foreach:
609  * @hash_table: a #GHashTable.
610  * @func: the function to call for each key/value pair.
611  * @user_data: user data to pass to the function.
612  *
613  * Calls the given function for each of the key/value pairs in the
614  * #GHashTable.  The function is passed the key and value of each
615  * pair, and the given @user_data parameter.  The hash table may not
616  * be modified while iterating over it (you can't add/remove
617  * items). To remove all items matching a predicate, use
618  * g_hash_table_foreach_remove().
619  *
620  * See g_hash_table_find() for performance caveats for linear
621  * order searches in contrast to g_hash_table_lookup().
622  **/
623 void
624 g_hash_table_foreach (GHashTable *hash_table,
625                       GHFunc      func,
626                       gpointer    user_data)
627 {
628   GHashNode *node;
629   gint i;
630
631   g_return_if_fail (hash_table != NULL);
632   g_return_if_fail (func != NULL);
633
634   for (i = 0; i < hash_table->size; i++)
635     for (node = hash_table->nodes[i]; node; node = node->next)
636       (* func) (node->key, node->value, user_data);
637 }
638
639 /**
640  * g_hash_table_find:
641  * @hash_table: a #GHashTable.
642  * @predicate:  function to test the key/value pairs for a certain property.
643  * @user_data:  user data to pass to the function.
644  *
645  * Calls the given function for key/value pairs in the #GHashTable until
646  * @predicate returns %TRUE.  The function is passed the key and value of
647  * each pair, and the given @user_data parameter. The hash table may not
648  * be modified while iterating over it (you can't add/remove items).
649  *
650  * Note, that hash tables are really only optimized for forward lookups,
651  * i.e. g_hash_table_lookup().
652  * So code that frequently issues g_hash_table_find() or
653  * g_hash_table_foreach() (e.g. in the order of once per every entry in a
654  * hash table) should probably be reworked to use additional or different
655  * data structures for reverse lookups (keep in mind that an O(n) find/foreach
656  * operation issued for all n values in a hash table ends up needing O(n*n)
657  * operations).
658  *
659  * Return value: The value of the first key/value pair is returned, for which
660  * func evaluates to %TRUE. If no pair with the requested property is found,
661  * %NULL is returned.
662  *
663  * Since: 2.4
664  **/
665 gpointer
666 g_hash_table_find (GHashTable      *hash_table,
667                    GHRFunc          predicate,
668                    gpointer         user_data)
669 {
670   GHashNode *node;
671   gint i;
672
673   g_return_val_if_fail (hash_table != NULL, NULL);
674   g_return_val_if_fail (predicate != NULL, NULL);
675
676   for (i = 0; i < hash_table->size; i++)
677     for (node = hash_table->nodes[i]; node; node = node->next)
678       if (predicate (node->key, node->value, user_data))
679         return node->value;
680   return NULL;
681 }
682
683 /**
684  * g_hash_table_size:
685  * @hash_table: a #GHashTable.
686  *
687  * Returns the number of elements contained in the #GHashTable.
688  *
689  * Return value: the number of key/value pairs in the #GHashTable.
690  **/
691 guint
692 g_hash_table_size (GHashTable *hash_table)
693 {
694   g_return_val_if_fail (hash_table != NULL, 0);
695
696   return hash_table->nnodes;
697 }
698
699 /**
700  * g_hash_table_get_keys:
701  * @hash_table: a #GHashTable
702  *
703  * Retrieves every key inside @hash_table. The returned data is valid
704  * until @hash_table is modified.
705  *
706  * Return value: a #GList containing all the keys inside the hash
707  *   table. The content of the list is owned by the hash table and
708  *   should not be modified or freed. Use g_list_free() when done
709  *   using the list.
710  *
711  * Since: 2.14
712  */
713 GList *
714 g_hash_table_get_keys (GHashTable *hash_table)
715 {
716   GHashNode *node;
717   gint i;
718   GList *retval;
719
720   g_return_val_if_fail (hash_table != NULL, NULL);
721
722   retval = NULL;
723   for (i = 0; i < hash_table->size; i++)
724     for (node = hash_table->nodes[i]; node; node = node->next)
725       retval = g_list_prepend (retval, node->key);
726
727   return retval;
728 }
729
730 /**
731  * g_hash_table_get_values:
732  * @hash_table: a #GHashTable
733  *
734  * Retrieves every value inside @hash_table. The returned data is
735  * valid until @hash_table is modified.
736  *
737  * Return value: a #GList containing all the values inside the hash
738  *   table. The content of the list is owned by the hash table and
739  *   should not be modified or freed. Use g_list_free() when done
740  *   using the list.
741  *
742  * Since: 2.14
743  */
744 GList *
745 g_hash_table_get_values (GHashTable *hash_table)
746 {
747   GHashNode *node;
748   gint i;
749   GList *retval;
750
751   g_return_val_if_fail (hash_table != NULL, NULL);
752
753   retval = NULL;
754   for (i = 0; i < hash_table->size; i++)
755     for (node = hash_table->nodes[i]; node; node = node->next)
756       retval = g_list_prepend (retval, node->value);
757
758   return retval;
759 }
760
761 static void
762 g_hash_table_resize (GHashTable *hash_table)
763 {
764   GHashNode **new_nodes;
765   GHashNode *node;
766   GHashNode *next;
767   guint hash_val;
768   gint new_size;
769   gint i;
770
771   new_size = g_spaced_primes_closest (hash_table->nnodes);
772   new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
773
774   new_nodes = g_new0 (GHashNode*, new_size);
775
776   for (i = 0; i < hash_table->size; i++)
777     for (node = hash_table->nodes[i]; node; node = next)
778       {
779         next = node->next;
780
781         hash_val = node->key_hash % new_size;
782
783         node->next = new_nodes[hash_val];
784         new_nodes[hash_val] = node;
785       }
786
787   g_free (hash_table->nodes);
788   hash_table->nodes = new_nodes;
789   hash_table->size = new_size;
790 }
791
792 static GHashNode*
793 g_hash_node_new (gpointer key,
794                  gpointer value,
795                  guint key_hash)
796 {
797   GHashNode *hash_node = g_slice_new (GHashNode);
798
799   hash_node->key = key;
800   hash_node->value = value;
801   hash_node->key_hash = key_hash;
802   hash_node->next = NULL;
803
804   return hash_node;
805 }
806
807 #define __G_HASH_C__
808 #include "galiasdef.c"