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;
57 GEqualFunc key_equal_func;
58 volatile gint ref_count;
59 GDestroyNotify key_destroy_func;
60 GDestroyNotify value_destroy_func;
63 static void g_hash_table_resize (GHashTable *hash_table);
64 static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
67 static GHashNode* g_hash_node_new (gpointer key,
70 static guint g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
74 static void g_hash_table_remove_all_nodes (GHashTable *hash_table,
78 g_hash_table_maybe_resize (GHashTable *hash_table)
80 gint nnodes = hash_table->nnodes;
81 gint size = hash_table->size;
83 if ((size >= 3 * nnodes && size > HASH_TABLE_MIN_SIZE) ||
84 (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE))
85 g_hash_table_resize (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)
193 g_hash_table_remove_all_nodes (hash_table, FALSE);
194 g_free (hash_table->nodes);
195 g_slice_free (GHashTable, hash_table);
200 * g_hash_table_destroy:
201 * @hash_table: a #GHashTable.
203 * Destroys all keys and values in the #GHashTable and decrements its
204 * reference count by 1. If keys and/or values are dynamically allocated,
205 * you should either free them first or create the #GHashTable with destroy
206 * notifiers using g_hash_table_new_full(). In the latter case the destroy
207 * functions you supplied will be called on all keys and values during the
211 g_hash_table_destroy (GHashTable *hash_table)
213 g_return_if_fail (hash_table != NULL);
214 g_return_if_fail (hash_table->ref_count > 0);
216 g_hash_table_remove_all (hash_table);
217 g_hash_table_unref (hash_table);
220 static inline GHashNode **
221 g_hash_table_lookup_node (GHashTable *hash_table,
225 GHashNode **node_ptr, *node;
228 hash_value = (* hash_table->hash_func) (key);
229 node_ptr = &hash_table->nodes[hash_value % hash_table->size];
232 *hash_return = hash_value;
234 /* Hash table lookup needs to be fast.
235 * We therefore remove the extra conditional of testing
236 * whether to call the key_equal_func or not from
239 * Additional optimisation: first check if our full hash
240 * values are equal so we can avoid calling the full-blown
241 * key equality function in most cases.
243 if (hash_table->key_equal_func)
245 while ((node = *node_ptr))
247 if (node->key_hash == hash_value &&
248 hash_table->key_equal_func (node->key, key))
251 node_ptr = &(*node_ptr)->next;
256 while ((node = *node_ptr))
258 if (node->key == key)
261 node_ptr = &(*node_ptr)->next;
269 * g_hash_table_lookup:
270 * @hash_table: a #GHashTable.
271 * @key: the key to look up.
273 * Looks up a key in a #GHashTable. Note that this function cannot
274 * distinguish between a key that is not present and one which is present
275 * and has the value %NULL. If you need this distinction, use
276 * g_hash_table_lookup_extended().
278 * Return value: the associated value, or %NULL if the key is not found.
281 g_hash_table_lookup (GHashTable *hash_table,
286 g_return_val_if_fail (hash_table != NULL, NULL);
288 node = *g_hash_table_lookup_node (hash_table, key, NULL);
290 return node ? node->value : NULL;
294 * g_hash_table_lookup_extended:
295 * @hash_table: a #GHashTable.
296 * @lookup_key: the key to look up.
297 * @orig_key: returns the original key.
298 * @value: returns the value associated with the key.
300 * Looks up a key in the #GHashTable, returning the original key and the
301 * associated value and a #gboolean which is %TRUE if the key was found. This
302 * is useful if you need to free the memory allocated for the original key,
303 * for example before calling g_hash_table_remove().
305 * Return value: %TRUE if the key was found in the #GHashTable.
308 g_hash_table_lookup_extended (GHashTable *hash_table,
309 gconstpointer lookup_key,
315 g_return_val_if_fail (hash_table != NULL, FALSE);
317 node = *g_hash_table_lookup_node (hash_table, lookup_key, NULL);
323 *orig_key = node->key;
326 *value = node->value;
332 g_hash_table_insert_internal (GHashTable *hash_table,
335 gboolean keep_new_key)
337 GHashNode **node_ptr, *node;
340 g_return_if_fail (hash_table != NULL);
341 g_return_if_fail (hash_table->ref_count > 0);
343 node_ptr = g_hash_table_lookup_node (hash_table, key, &key_hash);
345 if ((node = *node_ptr))
349 if (hash_table->key_destroy_func)
350 hash_table->key_destroy_func (node->key);
355 if (hash_table->key_destroy_func)
356 hash_table->key_destroy_func (key);
359 if (hash_table->value_destroy_func)
360 hash_table->value_destroy_func (node->value);
366 node = g_slice_new (GHashNode);
370 node->key_hash = key_hash;
374 hash_table->nnodes++;
375 g_hash_table_maybe_resize (hash_table);
380 * g_hash_table_insert:
381 * @hash_table: a #GHashTable.
382 * @key: a key to insert.
383 * @value: the value to associate with the key.
385 * Inserts a new key and value into a #GHashTable.
387 * If the key already exists in the #GHashTable its current value is replaced
388 * with the new value. If you supplied a @value_destroy_func when creating the
389 * #GHashTable, the old value is freed using that function. If you supplied
390 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
391 * using that function.
394 g_hash_table_insert (GHashTable *hash_table,
398 return g_hash_table_insert_internal (hash_table, key, value, FALSE);
402 * g_hash_table_replace:
403 * @hash_table: a #GHashTable.
404 * @key: a key to insert.
405 * @value: the value to associate with the key.
407 * Inserts a new key and value into a #GHashTable similar to
408 * g_hash_table_insert(). The difference is that if the key already exists
409 * in the #GHashTable, it gets replaced by the new key. If you supplied a
410 * @value_destroy_func when creating the #GHashTable, the old value is freed
411 * using that function. If you supplied a @key_destroy_func when creating the
412 * #GHashTable, the old key is freed using that function.
415 g_hash_table_replace (GHashTable *hash_table,
419 return g_hash_table_insert_internal (hash_table, key, value, TRUE);
423 g_hash_table_remove_node (GHashTable *hash_table,
424 GHashNode ***node_ptr_ptr,
427 GHashNode **node_ptr, *node;
429 node_ptr = *node_ptr_ptr;
432 *node_ptr = node->next;
434 if (notify && hash_table->key_destroy_func)
435 hash_table->key_destroy_func (node->key);
437 if (notify && hash_table->value_destroy_func)
438 hash_table->value_destroy_func (node->value);
440 g_slice_free (GHashNode, node);
442 hash_table->nnodes--;
446 g_hash_table_remove_all_nodes (GHashTable *hash_table,
449 GHashNode **node_ptr;
452 for (i = 0; i < hash_table->size; i++)
453 for (node_ptr = &hash_table->nodes[i]; *node_ptr != NULL;)
454 g_hash_table_remove_node (hash_table, &node_ptr, notify);
456 hash_table->nnodes = 0;
460 g_hash_table_remove_internal (GHashTable *hash_table,
464 GHashNode **node_ptr;
466 g_return_val_if_fail (hash_table != NULL, FALSE);
468 node_ptr = g_hash_table_lookup_node (hash_table, key, NULL);
469 if (*node_ptr == NULL)
472 g_hash_table_remove_node (hash_table, &node_ptr, notify);
473 g_hash_table_maybe_resize (hash_table);
479 * g_hash_table_remove:
480 * @hash_table: a #GHashTable.
481 * @key: the key to remove.
483 * Removes a key and its associated value from a #GHashTable.
485 * If the #GHashTable was created using g_hash_table_new_full(), the
486 * key and value are freed using the supplied destroy functions, otherwise
487 * you have to make sure that any dynamically allocated values are freed
490 * Return value: %TRUE if the key was found and removed from the #GHashTable.
493 g_hash_table_remove (GHashTable *hash_table,
496 return g_hash_table_remove_internal (hash_table, key, TRUE);
500 * g_hash_table_remove_all:
501 * @hash_table: a #GHashTable
503 * Removes all keys and their associated values from a #GHashTable.
505 * If the #GHashTable was created using g_hash_table_new_full(), the keys
506 * and values are freed using the supplied destroy functions, otherwise you
507 * have to make sure that any dynamically allocated values are freed
513 g_hash_table_remove_all (GHashTable *hash_table)
515 g_return_if_fail (hash_table != NULL);
517 g_hash_table_remove_all_nodes (hash_table, TRUE);
518 g_hash_table_maybe_resize (hash_table);
522 * g_hash_table_steal:
523 * @hash_table: a #GHashTable.
524 * @key: the key to remove.
526 * Removes a key and its associated value from a #GHashTable without
527 * calling the key and value destroy functions.
529 * Return value: %TRUE if the key was found and removed from the #GHashTable.
532 g_hash_table_steal (GHashTable *hash_table,
535 return g_hash_table_remove_internal (hash_table, key, FALSE);
539 * g_hash_table_steal_all:
540 * @hash_table: a #GHashTable.
542 * Removes all keys and their associated values from a #GHashTable
543 * without calling the key and value destroy functions.
548 g_hash_table_steal_all (GHashTable *hash_table)
550 g_return_if_fail (hash_table != NULL);
552 g_hash_table_remove_all_nodes (hash_table, FALSE);
553 g_hash_table_maybe_resize (hash_table);
557 * g_hash_table_foreach_remove:
558 * @hash_table: a #GHashTable.
559 * @func: the function to call for each key/value pair.
560 * @user_data: user data to pass to the function.
562 * Calls the given function for each key/value pair in the #GHashTable.
563 * If the function returns %TRUE, then the key/value pair is removed from the
564 * #GHashTable. If you supplied key or value destroy functions when creating
565 * the #GHashTable, they are used to free the memory allocated for the removed
568 * Return value: the number of key/value pairs removed.
571 g_hash_table_foreach_remove (GHashTable *hash_table,
575 g_return_val_if_fail (hash_table != NULL, 0);
576 g_return_val_if_fail (func != NULL, 0);
578 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
582 * g_hash_table_foreach_steal:
583 * @hash_table: a #GHashTable.
584 * @func: the function to call for each key/value pair.
585 * @user_data: user data to pass to the function.
587 * Calls the given function for each key/value pair in the #GHashTable.
588 * If the function returns %TRUE, then the key/value pair is removed from the
589 * #GHashTable, but no key or value destroy functions are called.
591 * Return value: the number of key/value pairs removed.
594 g_hash_table_foreach_steal (GHashTable *hash_table,
598 g_return_val_if_fail (hash_table != NULL, 0);
599 g_return_val_if_fail (func != NULL, 0);
601 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
605 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
610 GHashNode *node, **node_ptr;
614 for (i = 0; i < hash_table->size; i++)
615 for (node_ptr = &hash_table->nodes[i]; (node = *node_ptr) != NULL;)
616 if ((* func) (node->key, node->value, user_data))
618 g_hash_table_remove_node (hash_table, &node_ptr, notify);
622 node_ptr = &node->next;
624 g_hash_table_maybe_resize (hash_table);
630 * g_hash_table_foreach:
631 * @hash_table: a #GHashTable.
632 * @func: the function to call for each key/value pair.
633 * @user_data: user data to pass to the function.
635 * Calls the given function for each of the key/value pairs in the
636 * #GHashTable. The function is passed the key and value of each
637 * pair, and the given @user_data parameter. The hash table may not
638 * be modified while iterating over it (you can't add/remove
639 * items). To remove all items matching a predicate, use
640 * g_hash_table_foreach_remove().
642 * See g_hash_table_find() for performance caveats for linear
643 * order searches in contrast to g_hash_table_lookup().
646 g_hash_table_foreach (GHashTable *hash_table,
653 g_return_if_fail (hash_table != NULL);
654 g_return_if_fail (func != NULL);
656 for (i = 0; i < hash_table->size; i++)
657 for (node = hash_table->nodes[i]; node; node = node->next)
658 (* func) (node->key, node->value, user_data);
663 * @hash_table: a #GHashTable.
664 * @predicate: function to test the key/value pairs for a certain property.
665 * @user_data: user data to pass to the function.
667 * Calls the given function for key/value pairs in the #GHashTable until
668 * @predicate returns %TRUE. The function is passed the key and value of
669 * each pair, and the given @user_data parameter. The hash table may not
670 * be modified while iterating over it (you can't add/remove items).
672 * Note, that hash tables are really only optimized for forward lookups,
673 * i.e. g_hash_table_lookup().
674 * So code that frequently issues g_hash_table_find() or
675 * g_hash_table_foreach() (e.g. in the order of once per every entry in a
676 * hash table) should probably be reworked to use additional or different
677 * data structures for reverse lookups (keep in mind that an O(n) find/foreach
678 * operation issued for all n values in a hash table ends up needing O(n*n)
681 * Return value: The value of the first key/value pair is returned, for which
682 * func evaluates to %TRUE. If no pair with the requested property is found,
688 g_hash_table_find (GHashTable *hash_table,
695 g_return_val_if_fail (hash_table != NULL, NULL);
696 g_return_val_if_fail (predicate != NULL, NULL);
698 for (i = 0; i < hash_table->size; i++)
699 for (node = hash_table->nodes[i]; node; node = node->next)
700 if (predicate (node->key, node->value, user_data))
707 * @hash_table: a #GHashTable.
709 * Returns the number of elements contained in the #GHashTable.
711 * Return value: the number of key/value pairs in the #GHashTable.
714 g_hash_table_size (GHashTable *hash_table)
716 g_return_val_if_fail (hash_table != NULL, 0);
718 return hash_table->nnodes;
722 * g_hash_table_get_keys:
723 * @hash_table: a #GHashTable
725 * Retrieves every key inside @hash_table. The returned data is valid
726 * until @hash_table is modified.
728 * Return value: a #GList containing all the keys inside the hash
729 * table. The content of the list is owned by the hash table and
730 * should not be modified or freed. Use g_list_free() when done
736 g_hash_table_get_keys (GHashTable *hash_table)
742 g_return_val_if_fail (hash_table != NULL, NULL);
745 for (i = 0; i < hash_table->size; i++)
746 for (node = hash_table->nodes[i]; node; node = node->next)
747 retval = g_list_prepend (retval, node->key);
753 * g_hash_table_get_values:
754 * @hash_table: a #GHashTable
756 * Retrieves every value inside @hash_table. The returned data is
757 * valid until @hash_table is modified.
759 * Return value: a #GList containing all the values inside the hash
760 * table. The content of the list is owned by the hash table and
761 * should not be modified or freed. Use g_list_free() when done
767 g_hash_table_get_values (GHashTable *hash_table)
773 g_return_val_if_fail (hash_table != NULL, NULL);
776 for (i = 0; i < hash_table->size; i++)
777 for (node = hash_table->nodes[i]; node; node = node->next)
778 retval = g_list_prepend (retval, node->value);
784 g_hash_table_resize (GHashTable *hash_table)
786 GHashNode **new_nodes;
793 new_size = g_spaced_primes_closest (hash_table->nnodes);
794 new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
796 new_nodes = g_new0 (GHashNode*, new_size);
798 for (i = 0; i < hash_table->size; i++)
799 for (node = hash_table->nodes[i]; node; node = next)
803 hash_val = node->key_hash % new_size;
805 node->next = new_nodes[hash_val];
806 new_nodes[hash_val] = node;
809 g_free (hash_table->nodes);
810 hash_table->nodes = new_nodes;
811 hash_table->size = new_size;
815 #include "galiasdef.c"