1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
37 #define HASH_TABLE_MIN_SIZE 11
38 #define HASH_TABLE_MAX_SIZE 13845163
41 typedef struct _GHashNode GHashNode;
56 GEqualFunc key_equal_func;
57 volatile guint ref_count;
58 GDestroyNotify key_destroy_func;
59 GDestroyNotify value_destroy_func;
62 #define G_HASH_TABLE_RESIZE(hash_table) \
64 if ((hash_table->size >= 3 * hash_table->nnodes && \
65 hash_table->size > HASH_TABLE_MIN_SIZE) || \
66 (3 * hash_table->size <= hash_table->nnodes && \
67 hash_table->size < HASH_TABLE_MAX_SIZE)) \
68 g_hash_table_resize (hash_table); \
71 static void g_hash_table_resize (GHashTable *hash_table);
72 static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
74 static GHashNode* g_hash_node_new (gpointer key,
76 static void g_hash_node_destroy (GHashNode *hash_node,
77 GDestroyNotify key_destroy_func,
78 GDestroyNotify value_destroy_func);
79 static void g_hash_nodes_destroy (GHashNode *hash_node,
80 GDestroyNotify key_destroy_func,
81 GDestroyNotify value_destroy_func);
82 static guint g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
90 * @hash_func: a function to create a hash value from a key.
91 * Hash values are used to determine where keys are stored within the
92 * #GHashTable data structure. The g_direct_hash(), g_int_hash() and
93 * g_str_hash() functions are provided for some common types of keys.
94 * If hash_func is %NULL, g_direct_hash() is used.
95 * @key_equal_func: a function to check two keys for equality. This is
96 * used when looking up keys in the #GHashTable. The g_direct_equal(),
97 * g_int_equal() and g_str_equal() functions are provided for the most
98 * common types of keys. If @key_equal_func is %NULL, keys are compared
99 * directly in a similar fashion to g_direct_equal(), but without the
100 * overhead of a function call.
102 * Creates a new #GHashTable with a reference count of 1.
104 * Return value: a new #GHashTable.
107 g_hash_table_new (GHashFunc hash_func,
108 GEqualFunc key_equal_func)
110 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
115 * g_hash_table_new_full:
116 * @hash_func: a function to create a hash value from a key.
117 * @key_equal_func: a function to check two keys for equality.
118 * @key_destroy_func: a function to free the memory allocated for the key
119 * used when removing the entry from the #GHashTable or %NULL if you
120 * don't want to supply such a function.
121 * @value_destroy_func: a function to free the memory allocated for the
122 * value used when removing the entry from the #GHashTable or %NULL if
123 * you don't want to supply such a function.
125 * Creates a new #GHashTable like g_hash_table_new() with a reference count
126 * of 1 and allows to specify functions to free the memory allocated for the
127 * key and value that get called when removing the entry from the #GHashTable.
129 * Return value: a new #GHashTable.
132 g_hash_table_new_full (GHashFunc hash_func,
133 GEqualFunc key_equal_func,
134 GDestroyNotify key_destroy_func,
135 GDestroyNotify value_destroy_func)
137 GHashTable *hash_table;
139 hash_table = g_slice_new (GHashTable);
140 hash_table->size = HASH_TABLE_MIN_SIZE;
141 hash_table->nnodes = 0;
142 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
143 hash_table->key_equal_func = key_equal_func;
144 hash_table->ref_count = 1;
145 hash_table->key_destroy_func = key_destroy_func;
146 hash_table->value_destroy_func = value_destroy_func;
147 hash_table->nodes = g_new0 (GHashNode*, hash_table->size);
155 * @hash_table: a valid #GHashTable.
157 * Atomically increments the reference count of @hash_table by one.
158 * This function is MT-safe and may be called from any thread.
160 * Return value: the passed in #GHashTable.
165 g_hash_table_ref (GHashTable *hash_table)
167 g_return_val_if_fail (hash_table != NULL, NULL);
168 g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
170 g_atomic_int_add (&hash_table->ref_count, 1);
175 * g_hash_table_unref:
176 * @hash_table: a valid #GHashTable.
178 * Atomically decrements the reference count of @hash_table by one.
179 * If the reference count drops to 0, all keys and values will be
180 * destroyed, and all memory allocated by the hash table is released.
181 * This function is MT-safe and may be called from any thread.
186 g_hash_table_unref (GHashTable *hash_table)
188 g_return_if_fail (hash_table != NULL);
189 g_return_if_fail (hash_table->ref_count > 0);
191 if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
195 for (i = 0; i < hash_table->size; i++)
196 g_hash_nodes_destroy (hash_table->nodes[i],
197 hash_table->key_destroy_func,
198 hash_table->value_destroy_func);
199 g_free (hash_table->nodes);
200 g_slice_free (GHashTable, hash_table);
205 * g_hash_table_destroy:
206 * @hash_table: a #GHashTable.
208 * Destroys all keys and values in the #GHashTable and decrements its
209 * reference count by 1. If keys and/or values are dynamically allocated,
210 * you should either free them first or create the #GHashTable with destroy
211 * notifiers using g_hash_table_new_full(). In the latter case the destroy
212 * functions you supplied will be called on all keys and values during the
216 g_hash_table_destroy (GHashTable *hash_table)
220 g_return_if_fail (hash_table != NULL);
221 g_return_if_fail (hash_table->ref_count > 0);
223 for (i = 0; i < hash_table->size; i++)
225 g_hash_nodes_destroy (hash_table->nodes[i],
226 hash_table->key_destroy_func,
227 hash_table->value_destroy_func);
228 hash_table->nodes[i] = NULL;
230 hash_table->nnodes = 0;
231 hash_table->size = HASH_TABLE_MIN_SIZE;
233 g_hash_table_unref (hash_table);
236 static inline GHashNode**
237 g_hash_table_lookup_node (GHashTable *hash_table,
242 node = &hash_table->nodes
243 [(* hash_table->hash_func) (key) % hash_table->size];
245 /* Hash table lookup needs to be fast.
246 * We therefore remove the extra conditional of testing
247 * whether to call the key_equal_func or not from
250 if (hash_table->key_equal_func)
251 while (*node && !(*hash_table->key_equal_func) ((*node)->key, key))
252 node = &(*node)->next;
254 while (*node && (*node)->key != key)
255 node = &(*node)->next;
261 * g_hash_table_lookup:
262 * @hash_table: a #GHashTable.
263 * @key: the key to look up.
265 * Looks up a key in a #GHashTable. Note that this function cannot
266 * distinguish between a key that is not present and one which is present
267 * and has the value %NULL. If you need this distinction, use
268 * g_hash_table_lookup_extended().
270 * Return value: the associated value, or %NULL if the key is not found.
273 g_hash_table_lookup (GHashTable *hash_table,
278 g_return_val_if_fail (hash_table != NULL, NULL);
280 node = *g_hash_table_lookup_node (hash_table, key);
282 return node ? node->value : NULL;
286 * g_hash_table_lookup_extended:
287 * @hash_table: a #GHashTable.
288 * @lookup_key: the key to look up.
289 * @orig_key: returns the original key.
290 * @value: returns the value associated with the key.
292 * Looks up a key in the #GHashTable, returning the original key and the
293 * associated value and a #gboolean which is %TRUE if the key was found. This
294 * is useful if you need to free the memory allocated for the original key,
295 * for example before calling g_hash_table_remove().
297 * Return value: %TRUE if the key was found in the #GHashTable.
300 g_hash_table_lookup_extended (GHashTable *hash_table,
301 gconstpointer lookup_key,
307 g_return_val_if_fail (hash_table != NULL, FALSE);
309 node = *g_hash_table_lookup_node (hash_table, lookup_key);
314 *orig_key = node->key;
316 *value = node->value;
324 * g_hash_table_insert:
325 * @hash_table: a #GHashTable.
326 * @key: a key to insert.
327 * @value: the value to associate with the key.
329 * Inserts a new key and value into a #GHashTable.
331 * If the key already exists in the #GHashTable its current value is replaced
332 * with the new value. If you supplied a @value_destroy_func when creating the
333 * #GHashTable, the old value is freed using that function. If you supplied
334 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
335 * using that function.
338 g_hash_table_insert (GHashTable *hash_table,
344 g_return_if_fail (hash_table != NULL);
345 g_return_if_fail (hash_table->ref_count > 0);
347 node = g_hash_table_lookup_node (hash_table, key);
351 /* do not reset node->key in this place, keeping
352 * the old key is the intended behaviour.
353 * g_hash_table_replace() can be used instead.
356 /* free the passed key */
357 if (hash_table->key_destroy_func)
358 hash_table->key_destroy_func (key);
360 if (hash_table->value_destroy_func)
361 hash_table->value_destroy_func ((*node)->value);
363 (*node)->value = value;
367 *node = g_hash_node_new (key, value);
368 hash_table->nnodes++;
369 G_HASH_TABLE_RESIZE (hash_table);
374 * g_hash_table_replace:
375 * @hash_table: a #GHashTable.
376 * @key: a key to insert.
377 * @value: the value to associate with the key.
379 * Inserts a new key and value into a #GHashTable similar to
380 * g_hash_table_insert(). The difference is that if the key already exists
381 * in the #GHashTable, it gets replaced by the new key. If you supplied a
382 * @value_destroy_func when creating the #GHashTable, the old value is freed
383 * using that function. If you supplied a @key_destroy_func when creating the
384 * #GHashTable, the old key is freed using that function.
387 g_hash_table_replace (GHashTable *hash_table,
393 g_return_if_fail (hash_table != NULL);
394 g_return_if_fail (hash_table->ref_count > 0);
396 node = g_hash_table_lookup_node (hash_table, key);
400 if (hash_table->key_destroy_func)
401 hash_table->key_destroy_func ((*node)->key);
403 if (hash_table->value_destroy_func)
404 hash_table->value_destroy_func ((*node)->value);
407 (*node)->value = value;
411 *node = g_hash_node_new (key, value);
412 hash_table->nnodes++;
413 G_HASH_TABLE_RESIZE (hash_table);
418 * g_hash_table_remove:
419 * @hash_table: a #GHashTable.
420 * @key: the key to remove.
422 * Removes a key and its associated value from a #GHashTable.
424 * If the #GHashTable was created using g_hash_table_new_full(), the
425 * key and value are freed using the supplied destroy functions, otherwise
426 * you have to make sure that any dynamically allocated values are freed
429 * Return value: %TRUE if the key was found and removed from the #GHashTable.
432 g_hash_table_remove (GHashTable *hash_table,
435 GHashNode **node, *dest;
437 g_return_val_if_fail (hash_table != NULL, FALSE);
439 node = g_hash_table_lookup_node (hash_table, key);
443 (*node) = dest->next;
444 g_hash_node_destroy (dest,
445 hash_table->key_destroy_func,
446 hash_table->value_destroy_func);
447 hash_table->nnodes--;
449 G_HASH_TABLE_RESIZE (hash_table);
458 * g_hash_table_steal:
459 * @hash_table: a #GHashTable.
460 * @key: the key to remove.
462 * Removes a key and its associated value from a #GHashTable without
463 * calling the key and value destroy functions.
465 * Return value: %TRUE if the key was found and removed from the #GHashTable.
468 g_hash_table_steal (GHashTable *hash_table,
471 GHashNode **node, *dest;
473 g_return_val_if_fail (hash_table != NULL, FALSE);
475 node = g_hash_table_lookup_node (hash_table, key);
479 (*node) = dest->next;
480 g_hash_node_destroy (dest, NULL, NULL);
481 hash_table->nnodes--;
483 G_HASH_TABLE_RESIZE (hash_table);
492 * g_hash_table_foreach_remove:
493 * @hash_table: a #GHashTable.
494 * @func: the function to call for each key/value pair.
495 * @user_data: user data to pass to the function.
497 * Calls the given function for each key/value pair in the #GHashTable.
498 * If the function returns %TRUE, then the key/value pair is removed from the
499 * #GHashTable. If you supplied key or value destroy functions when creating
500 * the #GHashTable, they are used to free the memory allocated for the removed
503 * Return value: the number of key/value pairs removed.
506 g_hash_table_foreach_remove (GHashTable *hash_table,
510 g_return_val_if_fail (hash_table != NULL, 0);
511 g_return_val_if_fail (func != NULL, 0);
513 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
517 * g_hash_table_foreach_steal:
518 * @hash_table: a #GHashTable.
519 * @func: the function to call for each key/value pair.
520 * @user_data: user data to pass to the function.
522 * Calls the given function for each key/value pair in the #GHashTable.
523 * If the function returns %TRUE, then the key/value pair is removed from the
524 * #GHashTable, but no key or value destroy functions are called.
526 * Return value: the number of key/value pairs removed.
529 g_hash_table_foreach_steal (GHashTable *hash_table,
533 g_return_val_if_fail (hash_table != NULL, 0);
534 g_return_val_if_fail (func != NULL, 0);
536 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
540 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
545 GHashNode *node, *prev;
549 for (i = 0; i < hash_table->size; i++)
555 for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
557 if ((* func) (node->key, node->value, user_data))
561 hash_table->nnodes -= 1;
565 prev->next = node->next;
566 g_hash_node_destroy (node,
567 notify ? hash_table->key_destroy_func : NULL,
568 notify ? hash_table->value_destroy_func : NULL);
573 hash_table->nodes[i] = node->next;
574 g_hash_node_destroy (node,
575 notify ? hash_table->key_destroy_func : NULL,
576 notify ? hash_table->value_destroy_func : NULL);
583 G_HASH_TABLE_RESIZE (hash_table);
589 * g_hash_table_foreach:
590 * @hash_table: a #GHashTable.
591 * @func: the function to call for each key/value pair.
592 * @user_data: user data to pass to the function.
594 * Calls the given function for each of the key/value pairs in the
595 * #GHashTable. The function is passed the key and value of each
596 * pair, and the given @user_data parameter. The hash table may not
597 * be modified while iterating over it (you can't add/remove
598 * items). To remove all items matching a predicate, use
599 * g_hash_table_foreach_remove().
602 g_hash_table_foreach (GHashTable *hash_table,
609 g_return_if_fail (hash_table != NULL);
610 g_return_if_fail (func != NULL);
612 for (i = 0; i < hash_table->size; i++)
613 for (node = hash_table->nodes[i]; node; node = node->next)
614 (* func) (node->key, node->value, user_data);
619 * @hash_table: a #GHashTable.
620 * @predicate: function to test the key/value pairs for a certain property.
621 * @user_data: user data to pass to the function.
623 * Calls the given function for key/value pairs in the #GHashTable until
624 * @predicate returns %TRUE. The function is passed the key and value of
625 * each pair, and the given @user_data parameter. The hash table may not
626 * be modified while iterating over it (you can't add/remove items).
628 * Return value: The value of the first key/value pair is returned, for which
629 * func evaluates to %TRUE. If no pair with the requested property is found,
635 g_hash_table_find (GHashTable *hash_table,
642 g_return_val_if_fail (hash_table != NULL, NULL);
643 g_return_val_if_fail (predicate != NULL, NULL);
645 for (i = 0; i < hash_table->size; i++)
646 for (node = hash_table->nodes[i]; node; node = node->next)
647 if (predicate (node->key, node->value, user_data))
654 * @hash_table: a #GHashTable.
656 * Returns the number of elements contained in the #GHashTable.
658 * Return value: the number of key/value pairs in the #GHashTable.
661 g_hash_table_size (GHashTable *hash_table)
663 g_return_val_if_fail (hash_table != NULL, 0);
665 return hash_table->nnodes;
669 g_hash_table_resize (GHashTable *hash_table)
671 GHashNode **new_nodes;
678 new_size = g_spaced_primes_closest (hash_table->nnodes);
679 new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
681 new_nodes = g_new0 (GHashNode*, new_size);
683 for (i = 0; i < hash_table->size; i++)
684 for (node = hash_table->nodes[i]; node; node = next)
688 hash_val = (* hash_table->hash_func) (node->key) % new_size;
690 node->next = new_nodes[hash_val];
691 new_nodes[hash_val] = node;
694 g_free (hash_table->nodes);
695 hash_table->nodes = new_nodes;
696 hash_table->size = new_size;
700 g_hash_node_new (gpointer key,
703 GHashNode *hash_node = g_slice_new (GHashNode);
705 hash_node->key = key;
706 hash_node->value = value;
707 hash_node->next = NULL;
713 g_hash_node_destroy (GHashNode *hash_node,
714 GDestroyNotify key_destroy_func,
715 GDestroyNotify value_destroy_func)
717 if (key_destroy_func)
718 key_destroy_func (hash_node->key);
719 if (value_destroy_func)
720 value_destroy_func (hash_node->value);
722 #ifdef ENABLE_GC_FRIENDLY
723 hash_node->key = NULL;
724 hash_node->value = NULL;
725 #endif /* ENABLE_GC_FRIENDLY */
727 g_slice_free (GHashNode, hash_node);
731 g_hash_nodes_destroy (GHashNode *hash_node,
732 GFreeFunc key_destroy_func,
733 GFreeFunc value_destroy_func)
737 GHashNode *next = hash_node->next;
738 if (key_destroy_func)
739 key_destroy_func (hash_node->key);
740 if (value_destroy_func)
741 value_destroy_func (hash_node->value);
742 g_slice_free (GHashNode, hash_node);
749 #include "galiasdef.c"