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 inline GHashNode **
64 g_hash_table_lookup_node (GHashTable *hash_table,
68 GHashNode **node_ptr, *node;
71 hash_value = (* hash_table->hash_func) (key);
72 node_ptr = &hash_table->nodes[hash_value % hash_table->size];
75 *hash_return = hash_value;
77 /* Hash table lookup needs to be fast.
78 * We therefore remove the extra conditional of testing
79 * whether to call the key_equal_func or not from
82 * Additional optimisation: first check if our full hash
83 * values are equal so we can avoid calling the full-blown
84 * key equality function in most cases.
86 if (hash_table->key_equal_func)
88 while ((node = *node_ptr))
90 if (node->key_hash == hash_value &&
91 hash_table->key_equal_func (node->key, key))
94 node_ptr = &(*node_ptr)->next;
99 while ((node = *node_ptr))
101 if (node->key == key)
104 node_ptr = &(*node_ptr)->next;
112 g_hash_table_remove_node (GHashTable *hash_table,
113 GHashNode ***node_ptr_ptr,
116 GHashNode **node_ptr, *node;
118 node_ptr = *node_ptr_ptr;
121 *node_ptr = node->next;
123 if (notify && hash_table->key_destroy_func)
124 hash_table->key_destroy_func (node->key);
126 if (notify && hash_table->value_destroy_func)
127 hash_table->value_destroy_func (node->value);
129 g_slice_free (GHashNode, node);
131 hash_table->nnodes--;
135 g_hash_table_remove_all_nodes (GHashTable *hash_table,
138 GHashNode **node_ptr;
141 for (i = 0; i < hash_table->size; i++)
142 for (node_ptr = &hash_table->nodes[i]; *node_ptr != NULL;)
143 g_hash_table_remove_node (hash_table, &node_ptr, notify);
145 hash_table->nnodes = 0;
149 g_hash_table_resize (GHashTable *hash_table)
151 GHashNode **new_nodes;
158 new_size = g_spaced_primes_closest (hash_table->nnodes);
159 new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
161 new_nodes = g_new0 (GHashNode*, new_size);
163 for (i = 0; i < hash_table->size; i++)
164 for (node = hash_table->nodes[i]; node; node = next)
168 hash_val = node->key_hash % new_size;
170 node->next = new_nodes[hash_val];
171 new_nodes[hash_val] = node;
174 g_free (hash_table->nodes);
175 hash_table->nodes = new_nodes;
176 hash_table->size = new_size;
180 g_hash_table_maybe_resize (GHashTable *hash_table)
182 gint nnodes = hash_table->nnodes;
183 gint size = hash_table->size;
185 if ((size >= 3 * nnodes && size > HASH_TABLE_MIN_SIZE) ||
186 (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE))
187 g_hash_table_resize (hash_table);
192 * @hash_func: a function to create a hash value from a key.
193 * Hash values are used to determine where keys are stored within the
194 * #GHashTable data structure. The g_direct_hash(), g_int_hash() and
195 * g_str_hash() functions are provided for some common types of keys.
196 * If hash_func is %NULL, g_direct_hash() is used.
197 * @key_equal_func: a function to check two keys for equality. This is
198 * used when looking up keys in the #GHashTable. The g_direct_equal(),
199 * g_int_equal() and g_str_equal() functions are provided for the most
200 * common types of keys. If @key_equal_func is %NULL, keys are compared
201 * directly in a similar fashion to g_direct_equal(), but without the
202 * overhead of a function call.
204 * Creates a new #GHashTable with a reference count of 1.
206 * Return value: a new #GHashTable.
209 g_hash_table_new (GHashFunc hash_func,
210 GEqualFunc key_equal_func)
212 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
217 * g_hash_table_new_full:
218 * @hash_func: a function to create a hash value from a key.
219 * @key_equal_func: a function to check two keys for equality.
220 * @key_destroy_func: a function to free the memory allocated for the key
221 * used when removing the entry from the #GHashTable or %NULL if you
222 * don't want to supply such a function.
223 * @value_destroy_func: a function to free the memory allocated for the
224 * value used when removing the entry from the #GHashTable or %NULL if
225 * you don't want to supply such a function.
227 * Creates a new #GHashTable like g_hash_table_new() with a reference count
228 * of 1 and allows to specify functions to free the memory allocated for the
229 * key and value that get called when removing the entry from the #GHashTable.
231 * Return value: a new #GHashTable.
234 g_hash_table_new_full (GHashFunc hash_func,
235 GEqualFunc key_equal_func,
236 GDestroyNotify key_destroy_func,
237 GDestroyNotify value_destroy_func)
239 GHashTable *hash_table;
241 hash_table = g_slice_new (GHashTable);
242 hash_table->size = HASH_TABLE_MIN_SIZE;
243 hash_table->nnodes = 0;
244 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
245 hash_table->key_equal_func = key_equal_func;
246 hash_table->ref_count = 1;
247 hash_table->key_destroy_func = key_destroy_func;
248 hash_table->value_destroy_func = value_destroy_func;
249 hash_table->nodes = g_new0 (GHashNode*, hash_table->size);
257 * @hash_table: a valid #GHashTable.
259 * Atomically increments the reference count of @hash_table by one.
260 * This function is MT-safe and may be called from any thread.
262 * Return value: the passed in #GHashTable.
267 g_hash_table_ref (GHashTable *hash_table)
269 g_return_val_if_fail (hash_table != NULL, NULL);
270 g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
272 g_atomic_int_add (&hash_table->ref_count, 1);
277 * g_hash_table_unref:
278 * @hash_table: a valid #GHashTable.
280 * Atomically decrements the reference count of @hash_table by one.
281 * If the reference count drops to 0, all keys and values will be
282 * destroyed, and all memory allocated by the hash table is released.
283 * This function is MT-safe and may be called from any thread.
288 g_hash_table_unref (GHashTable *hash_table)
290 g_return_if_fail (hash_table != NULL);
291 g_return_if_fail (hash_table->ref_count > 0);
293 if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
295 g_hash_table_remove_all_nodes (hash_table, FALSE);
296 g_free (hash_table->nodes);
297 g_slice_free (GHashTable, hash_table);
302 * g_hash_table_destroy:
303 * @hash_table: a #GHashTable.
305 * Destroys all keys and values in the #GHashTable and decrements its
306 * reference count by 1. If keys and/or values are dynamically allocated,
307 * you should either free them first or create the #GHashTable with destroy
308 * notifiers using g_hash_table_new_full(). In the latter case the destroy
309 * functions you supplied will be called on all keys and values during the
313 g_hash_table_destroy (GHashTable *hash_table)
315 g_return_if_fail (hash_table != NULL);
316 g_return_if_fail (hash_table->ref_count > 0);
318 g_hash_table_remove_all (hash_table);
319 g_hash_table_unref (hash_table);
323 * g_hash_table_lookup:
324 * @hash_table: a #GHashTable.
325 * @key: the key to look up.
327 * Looks up a key in a #GHashTable. Note that this function cannot
328 * distinguish between a key that is not present and one which is present
329 * and has the value %NULL. If you need this distinction, use
330 * g_hash_table_lookup_extended().
332 * Return value: the associated value, or %NULL if the key is not found.
335 g_hash_table_lookup (GHashTable *hash_table,
340 g_return_val_if_fail (hash_table != NULL, NULL);
342 node = *g_hash_table_lookup_node (hash_table, key, NULL);
344 return node ? node->value : NULL;
348 * g_hash_table_lookup_extended:
349 * @hash_table: a #GHashTable.
350 * @lookup_key: the key to look up.
351 * @orig_key: returns the original key.
352 * @value: returns the value associated with the key.
354 * Looks up a key in the #GHashTable, returning the original key and the
355 * associated value and a #gboolean which is %TRUE if the key was found. This
356 * is useful if you need to free the memory allocated for the original key,
357 * for example before calling g_hash_table_remove().
359 * Return value: %TRUE if the key was found in the #GHashTable.
362 g_hash_table_lookup_extended (GHashTable *hash_table,
363 gconstpointer lookup_key,
369 g_return_val_if_fail (hash_table != NULL, FALSE);
371 node = *g_hash_table_lookup_node (hash_table, lookup_key, NULL);
377 *orig_key = node->key;
380 *value = node->value;
386 g_hash_table_insert_internal (GHashTable *hash_table,
389 gboolean keep_new_key)
391 GHashNode **node_ptr, *node;
394 g_return_if_fail (hash_table != NULL);
395 g_return_if_fail (hash_table->ref_count > 0);
397 node_ptr = g_hash_table_lookup_node (hash_table, key, &key_hash);
399 if ((node = *node_ptr))
403 if (hash_table->key_destroy_func)
404 hash_table->key_destroy_func (node->key);
409 if (hash_table->key_destroy_func)
410 hash_table->key_destroy_func (key);
413 if (hash_table->value_destroy_func)
414 hash_table->value_destroy_func (node->value);
420 node = g_slice_new (GHashNode);
424 node->key_hash = key_hash;
428 hash_table->nnodes++;
429 g_hash_table_maybe_resize (hash_table);
434 * g_hash_table_insert:
435 * @hash_table: a #GHashTable.
436 * @key: a key to insert.
437 * @value: the value to associate with the key.
439 * Inserts a new key and value into a #GHashTable.
441 * If the key already exists in the #GHashTable its current value is replaced
442 * with the new value. If you supplied a @value_destroy_func when creating the
443 * #GHashTable, the old value is freed using that function. If you supplied
444 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
445 * using that function.
448 g_hash_table_insert (GHashTable *hash_table,
452 return g_hash_table_insert_internal (hash_table, key, value, FALSE);
456 * g_hash_table_replace:
457 * @hash_table: a #GHashTable.
458 * @key: a key to insert.
459 * @value: the value to associate with the key.
461 * Inserts a new key and value into a #GHashTable similar to
462 * g_hash_table_insert(). The difference is that if the key already exists
463 * in the #GHashTable, it gets replaced by the new key. If you supplied a
464 * @value_destroy_func when creating the #GHashTable, the old value is freed
465 * using that function. If you supplied a @key_destroy_func when creating the
466 * #GHashTable, the old key is freed using that function.
469 g_hash_table_replace (GHashTable *hash_table,
473 return g_hash_table_insert_internal (hash_table, key, value, TRUE);
477 g_hash_table_remove_internal (GHashTable *hash_table,
481 GHashNode **node_ptr;
483 g_return_val_if_fail (hash_table != NULL, FALSE);
485 node_ptr = g_hash_table_lookup_node (hash_table, key, NULL);
486 if (*node_ptr == NULL)
489 g_hash_table_remove_node (hash_table, &node_ptr, notify);
490 g_hash_table_maybe_resize (hash_table);
496 * g_hash_table_remove:
497 * @hash_table: a #GHashTable.
498 * @key: the key to remove.
500 * Removes a key and its associated value from a #GHashTable.
502 * If the #GHashTable was created using g_hash_table_new_full(), the
503 * key and value are freed using the supplied destroy functions, otherwise
504 * you have to make sure that any dynamically allocated values are freed
507 * Return value: %TRUE if the key was found and removed from the #GHashTable.
510 g_hash_table_remove (GHashTable *hash_table,
513 return g_hash_table_remove_internal (hash_table, key, TRUE);
517 * g_hash_table_steal:
518 * @hash_table: a #GHashTable.
519 * @key: the key to remove.
521 * Removes a key and its associated value from a #GHashTable without
522 * calling the key and value destroy functions.
524 * Return value: %TRUE if the key was found and removed from the #GHashTable.
527 g_hash_table_steal (GHashTable *hash_table,
530 return g_hash_table_remove_internal (hash_table, key, FALSE);
534 * g_hash_table_remove_all:
535 * @hash_table: a #GHashTable
537 * Removes all keys and their associated values from a #GHashTable.
539 * If the #GHashTable was created using g_hash_table_new_full(), the keys
540 * and values are freed using the supplied destroy functions, otherwise you
541 * have to make sure that any dynamically allocated values are freed
547 g_hash_table_remove_all (GHashTable *hash_table)
549 g_return_if_fail (hash_table != NULL);
551 g_hash_table_remove_all_nodes (hash_table, TRUE);
552 g_hash_table_maybe_resize (hash_table);
556 * g_hash_table_steal_all:
557 * @hash_table: a #GHashTable.
559 * Removes all keys and their associated values from a #GHashTable
560 * without calling the key and value destroy functions.
565 g_hash_table_steal_all (GHashTable *hash_table)
567 g_return_if_fail (hash_table != NULL);
569 g_hash_table_remove_all_nodes (hash_table, FALSE);
570 g_hash_table_maybe_resize (hash_table);
574 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
579 GHashNode *node, **node_ptr;
583 for (i = 0; i < hash_table->size; i++)
584 for (node_ptr = &hash_table->nodes[i]; (node = *node_ptr) != NULL;)
585 if ((* func) (node->key, node->value, user_data))
587 g_hash_table_remove_node (hash_table, &node_ptr, notify);
591 node_ptr = &node->next;
593 g_hash_table_maybe_resize (hash_table);
599 * g_hash_table_foreach_remove:
600 * @hash_table: a #GHashTable.
601 * @func: the function to call for each key/value pair.
602 * @user_data: user data to pass to the function.
604 * Calls the given function for each key/value pair in the #GHashTable.
605 * If the function returns %TRUE, then the key/value pair is removed from the
606 * #GHashTable. If you supplied key or value destroy functions when creating
607 * the #GHashTable, they are used to free the memory allocated for the removed
610 * Return value: the number of key/value pairs removed.
613 g_hash_table_foreach_remove (GHashTable *hash_table,
617 g_return_val_if_fail (hash_table != NULL, 0);
618 g_return_val_if_fail (func != NULL, 0);
620 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
624 * g_hash_table_foreach_steal:
625 * @hash_table: a #GHashTable.
626 * @func: the function to call for each key/value pair.
627 * @user_data: user data to pass to the function.
629 * Calls the given function for each key/value pair in the #GHashTable.
630 * If the function returns %TRUE, then the key/value pair is removed from the
631 * #GHashTable, but no key or value destroy functions are called.
633 * Return value: the number of key/value pairs removed.
636 g_hash_table_foreach_steal (GHashTable *hash_table,
640 g_return_val_if_fail (hash_table != NULL, 0);
641 g_return_val_if_fail (func != NULL, 0);
643 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
647 * g_hash_table_foreach:
648 * @hash_table: a #GHashTable.
649 * @func: the function to call for each key/value pair.
650 * @user_data: user data to pass to the function.
652 * Calls the given function for each of the key/value pairs in the
653 * #GHashTable. The function is passed the key and value of each
654 * pair, and the given @user_data parameter. The hash table may not
655 * be modified while iterating over it (you can't add/remove
656 * items). To remove all items matching a predicate, use
657 * g_hash_table_foreach_remove().
659 * See g_hash_table_find() for performance caveats for linear
660 * order searches in contrast to g_hash_table_lookup().
663 g_hash_table_foreach (GHashTable *hash_table,
670 g_return_if_fail (hash_table != NULL);
671 g_return_if_fail (func != NULL);
673 for (i = 0; i < hash_table->size; i++)
674 for (node = hash_table->nodes[i]; node; node = node->next)
675 (* func) (node->key, node->value, user_data);
680 * @hash_table: a #GHashTable.
681 * @predicate: function to test the key/value pairs for a certain property.
682 * @user_data: user data to pass to the function.
684 * Calls the given function for key/value pairs in the #GHashTable until
685 * @predicate returns %TRUE. The function is passed the key and value of
686 * each pair, and the given @user_data parameter. The hash table may not
687 * be modified while iterating over it (you can't add/remove items).
689 * Note, that hash tables are really only optimized for forward lookups,
690 * i.e. g_hash_table_lookup().
691 * So code that frequently issues g_hash_table_find() or
692 * g_hash_table_foreach() (e.g. in the order of once per every entry in a
693 * hash table) should probably be reworked to use additional or different
694 * data structures for reverse lookups (keep in mind that an O(n) find/foreach
695 * operation issued for all n values in a hash table ends up needing O(n*n)
698 * Return value: The value of the first key/value pair is returned, for which
699 * func evaluates to %TRUE. If no pair with the requested property is found,
705 g_hash_table_find (GHashTable *hash_table,
712 g_return_val_if_fail (hash_table != NULL, NULL);
713 g_return_val_if_fail (predicate != NULL, NULL);
715 for (i = 0; i < hash_table->size; i++)
716 for (node = hash_table->nodes[i]; node; node = node->next)
717 if (predicate (node->key, node->value, user_data))
724 * @hash_table: a #GHashTable.
726 * Returns the number of elements contained in the #GHashTable.
728 * Return value: the number of key/value pairs in the #GHashTable.
731 g_hash_table_size (GHashTable *hash_table)
733 g_return_val_if_fail (hash_table != NULL, 0);
735 return hash_table->nnodes;
739 * g_hash_table_get_keys:
740 * @hash_table: a #GHashTable
742 * Retrieves every key inside @hash_table. The returned data is valid
743 * until @hash_table is modified.
745 * Return value: a #GList containing all the keys inside the hash
746 * table. The content of the list is owned by the hash table and
747 * should not be modified or freed. Use g_list_free() when done
753 g_hash_table_get_keys (GHashTable *hash_table)
759 g_return_val_if_fail (hash_table != NULL, NULL);
762 for (i = 0; i < hash_table->size; i++)
763 for (node = hash_table->nodes[i]; node; node = node->next)
764 retval = g_list_prepend (retval, node->key);
770 * g_hash_table_get_values:
771 * @hash_table: a #GHashTable
773 * Retrieves every value inside @hash_table. The returned data is
774 * valid until @hash_table is modified.
776 * Return value: a #GList containing all the values inside the hash
777 * table. The content of the list is owned by the hash table and
778 * should not be modified or freed. Use g_list_free() when done
784 g_hash_table_get_values (GHashTable *hash_table)
790 g_return_val_if_fail (hash_table != NULL, NULL);
793 for (i = 0; i < hash_table->size; i++)
794 for (node = hash_table->nodes[i]; node; node = node->next)
795 retval = g_list_prepend (retval, node->value);
801 #include "galiasdef.c"