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/.
28 * Modified by Bruno Haible for use as a gnulib module.
43 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
46 #define HASH_TABLE_MIN_SIZE 11
47 #define HASH_TABLE_MAX_SIZE 13845163
50 typedef struct _GHashNode GHashNode;
65 GEqualFunc key_equal_func;
66 volatile guint ref_count;
67 GDestroyNotify key_destroy_func;
68 GDestroyNotify value_destroy_func;
71 #define G_HASH_TABLE_RESIZE(hash_table) \
73 if ((hash_table->size >= 3 * hash_table->nnodes && \
74 hash_table->size > HASH_TABLE_MIN_SIZE) || \
75 (3 * hash_table->size <= hash_table->nnodes && \
76 hash_table->size < HASH_TABLE_MAX_SIZE)) \
77 g_hash_table_resize (hash_table); \
80 static void g_hash_table_resize (GHashTable *hash_table);
81 static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
83 static GHashNode* g_hash_node_new (gpointer key,
86 static void g_hash_node_destroy (GHashNode *hash_node,
87 GDestroyNotify key_destroy_func,
88 GDestroyNotify value_destroy_func);
89 static void g_hash_nodes_destroy (GHashNode *hash_node,
90 GDestroyNotify key_destroy_func,
91 GDestroyNotify value_destroy_func);
92 static guint g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
101 * @hash_func: a function to create a hash value from a key.
102 * Hash values are used to determine where keys are stored within the
103 * #GHashTable data structure. The g_direct_hash(), g_int_hash() and
104 * g_str_hash() functions are provided for some common types of keys.
105 * If hash_func is %NULL, g_direct_hash() is used.
106 * @key_equal_func: a function to check two keys for equality. This is
107 * used when looking up keys in the #GHashTable. The g_direct_equal(),
108 * g_int_equal() and g_str_equal() functions are provided for the most
109 * common types of keys. If @key_equal_func is %NULL, keys are compared
110 * directly in a similar fashion to g_direct_equal(), but without the
111 * overhead of a function call.
113 * Creates a new #GHashTable with a reference count of 1.
115 * Return value: a new #GHashTable.
118 g_hash_table_new (GHashFunc hash_func,
119 GEqualFunc key_equal_func)
121 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
126 * g_hash_table_new_full:
127 * @hash_func: a function to create a hash value from a key.
128 * @key_equal_func: a function to check two keys for equality.
129 * @key_destroy_func: a function to free the memory allocated for the key
130 * used when removing the entry from the #GHashTable or %NULL if you
131 * don't want to supply such a function.
132 * @value_destroy_func: a function to free the memory allocated for the
133 * value used when removing the entry from the #GHashTable or %NULL if
134 * you don't want to supply such a function.
136 * Creates a new #GHashTable like g_hash_table_new() with a reference count
137 * of 1 and allows to specify functions to free the memory allocated for the
138 * key and value that get called when removing the entry from the #GHashTable.
140 * Return value: a new #GHashTable.
143 g_hash_table_new_full (GHashFunc hash_func,
144 GEqualFunc key_equal_func,
145 GDestroyNotify key_destroy_func,
146 GDestroyNotify value_destroy_func)
148 GHashTable *hash_table;
150 hash_table = g_slice_new (GHashTable);
151 hash_table->size = HASH_TABLE_MIN_SIZE;
152 hash_table->nnodes = 0;
153 hash_table->hash_func = hash_func;
154 hash_table->key_equal_func = key_equal_func;
155 hash_table->ref_count = 1;
156 hash_table->key_destroy_func = key_destroy_func;
157 hash_table->value_destroy_func = value_destroy_func;
158 hash_table->nodes = g_new0 (GHashNode*, hash_table->size);
167 * @hash_table: a valid #GHashTable.
169 * Atomically increments the reference count of @hash_table by one.
170 * This function is MT-safe and may be called from any thread.
172 * Return value: the passed in #GHashTable.
177 g_hash_table_ref (GHashTable *hash_table)
179 g_return_val_if_fail (hash_table != NULL, NULL);
180 g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
182 g_atomic_int_add (&hash_table->ref_count, 1);
187 * g_hash_table_unref:
188 * @hash_table: a valid #GHashTable.
190 * Atomically decrements the reference count of @hash_table by one.
191 * If the reference count drops to 0, all keys and values will be
192 * destroyed, and all memory allocated by the hash table is released.
193 * This function is MT-safe and may be called from any thread.
198 g_hash_table_unref (GHashTable *hash_table)
200 g_return_if_fail (hash_table != NULL);
201 g_return_if_fail (hash_table->ref_count > 0);
203 if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
207 for (i = 0; i < hash_table->size; i++)
208 g_hash_nodes_destroy (hash_table->nodes[i],
209 hash_table->key_destroy_func,
210 hash_table->value_destroy_func);
211 g_free (hash_table->nodes);
212 g_slice_free (GHashTable, hash_table);
217 * g_hash_table_destroy:
218 * @hash_table: a #GHashTable.
220 * Destroys all keys and values in the #GHashTable and decrements its
221 * reference count by 1. If keys and/or values are dynamically allocated,
222 * you should either free them first or create the #GHashTable with destroy
223 * notifiers using g_hash_table_new_full(). In the latter case the destroy
224 * functions you supplied will be called on all keys and values during the
228 g_hash_table_destroy (GHashTable *hash_table)
230 g_return_if_fail (hash_table != NULL);
231 g_return_if_fail (hash_table->ref_count > 0);
233 g_hash_table_remove_all (hash_table);
234 g_hash_table_unref (hash_table);
239 static inline GHashNode**
240 g_hash_table_lookup_node (GHashTable *hash_table,
245 node = &hash_table->nodes
246 [(* hash_table->hash_func) (key) % hash_table->size];
248 /* Hash table lookup needs to be fast.
249 * We therefore remove the extra conditional of testing
250 * whether to call the key_equal_func or not from
253 if (hash_table->key_equal_func)
254 while (*node && !(*hash_table->key_equal_func) ((*node)->key, key))
255 node = &(*node)->next;
257 while (*node && (*node)->key != key)
258 node = &(*node)->next;
264 * g_hash_table_lookup:
265 * @hash_table: a #GHashTable.
266 * @key: the key to look up.
268 * Looks up a key in a #GHashTable. Note that this function cannot
269 * distinguish between a key that is not present and one which is present
270 * and has the value %NULL. If you need this distinction, use
271 * g_hash_table_lookup_extended().
273 * Return value: the associated value, or %NULL if the key is not found.
276 g_hash_table_lookup (GHashTable *hash_table,
281 g_return_val_if_fail (hash_table != NULL, NULL);
283 node = *g_hash_table_lookup_node (hash_table, key);
285 return node ? node->value : NULL;
291 * g_hash_table_lookup_extended:
292 * @hash_table: a #GHashTable.
293 * @lookup_key: the key to look up.
294 * @orig_key: returns the original key.
295 * @value: returns the value associated with the key.
297 * Looks up a key in the #GHashTable, returning the original key and the
298 * associated value and a #gboolean which is %TRUE if the key was found. This
299 * is useful if you need to free the memory allocated for the original key,
300 * for example before calling g_hash_table_remove().
302 * Return value: %TRUE if the key was found in the #GHashTable.
305 g_hash_table_lookup_extended (GHashTable *hash_table,
306 gconstpointer lookup_key,
312 g_return_val_if_fail (hash_table != NULL, FALSE);
314 node = *g_hash_table_lookup_node (hash_table, lookup_key);
319 *orig_key = node->key;
321 *value = node->value;
331 * g_hash_table_insert:
332 * @hash_table: a #GHashTable.
333 * @key: a key to insert.
334 * @value: the value to associate with the key.
336 * Inserts a new key and value into a #GHashTable.
338 * If the key already exists in the #GHashTable its current value is replaced
339 * with the new value. If you supplied a @value_destroy_func when creating the
340 * #GHashTable, the old value is freed using that function. If you supplied
341 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
342 * using that function.
345 g_hash_table_insert (GHashTable *hash_table,
351 g_return_if_fail (hash_table != NULL);
352 g_return_if_fail (hash_table->ref_count > 0);
354 node = g_hash_table_lookup_node (hash_table, key);
358 /* do not reset node->key in this place, keeping
359 * the old key is the intended behaviour.
360 * g_hash_table_replace() can be used instead.
363 /* free the passed key */
364 if (hash_table->key_destroy_func)
365 hash_table->key_destroy_func (key);
367 if (hash_table->value_destroy_func)
368 hash_table->value_destroy_func ((*node)->value);
370 (*node)->value = value;
374 *node = g_hash_node_new (key, value);
375 hash_table->nnodes++;
376 G_HASH_TABLE_RESIZE (hash_table);
383 * g_hash_table_replace:
384 * @hash_table: a #GHashTable.
385 * @key: a key to insert.
386 * @value: the value to associate with the key.
388 * Inserts a new key and value into a #GHashTable similar to
389 * g_hash_table_insert(). The difference is that if the key already exists
390 * in the #GHashTable, it gets replaced by the new key. If you supplied a
391 * @value_destroy_func when creating the #GHashTable, the old value is freed
392 * using that function. If you supplied a @key_destroy_func when creating the
393 * #GHashTable, the old key is freed using that function.
396 g_hash_table_replace (GHashTable *hash_table,
402 g_return_if_fail (hash_table != NULL);
403 g_return_if_fail (hash_table->ref_count > 0);
405 node = g_hash_table_lookup_node (hash_table, key);
409 if (hash_table->key_destroy_func)
410 hash_table->key_destroy_func ((*node)->key);
412 if (hash_table->value_destroy_func)
413 hash_table->value_destroy_func ((*node)->value);
416 (*node)->value = value;
420 *node = g_hash_node_new (key, value);
421 hash_table->nnodes++;
422 G_HASH_TABLE_RESIZE (hash_table);
427 * g_hash_table_remove:
428 * @hash_table: a #GHashTable.
429 * @key: the key to remove.
431 * Removes a key and its associated value from a #GHashTable.
433 * If the #GHashTable was created using g_hash_table_new_full(), the
434 * key and value are freed using the supplied destroy functions, otherwise
435 * you have to make sure that any dynamically allocated values are freed
438 * Return value: %TRUE if the key was found and removed from the #GHashTable.
441 g_hash_table_remove (GHashTable *hash_table,
444 GHashNode **node, *dest;
446 g_return_val_if_fail (hash_table != NULL, FALSE);
448 node = g_hash_table_lookup_node (hash_table, key);
452 (*node) = dest->next;
453 g_hash_node_destroy (dest,
454 hash_table->key_destroy_func,
455 hash_table->value_destroy_func);
456 hash_table->nnodes--;
458 G_HASH_TABLE_RESIZE (hash_table);
467 * g_hash_table_remove_all:
468 * @hash_table: a #GHashTable
470 * Removes all keys and their associated values from a #GHashTable.
472 * If the #GHashTable was created using g_hash_table_new_full(), the keys
473 * and values are freed using the supplied destroy functions, otherwise you
474 * have to make sure that any dynamically allocated values are freed
480 g_hash_table_remove_all (GHashTable *hash_table)
484 g_return_if_fail (hash_table != NULL);
486 for (i = 0; i < hash_table->size; i++)
488 g_hash_nodes_destroy (hash_table->nodes[i],
489 hash_table->key_destroy_func,
490 hash_table->value_destroy_func);
491 hash_table->nodes[i] = NULL;
493 hash_table->nnodes = 0;
495 G_HASH_TABLE_RESIZE (hash_table);
499 * g_hash_table_steal:
500 * @hash_table: a #GHashTable.
501 * @key: the key to remove.
503 * Removes a key and its associated value from a #GHashTable without
504 * calling the key and value destroy functions.
506 * Return value: %TRUE if the key was found and removed from the #GHashTable.
509 g_hash_table_steal (GHashTable *hash_table,
512 GHashNode **node, *dest;
514 g_return_val_if_fail (hash_table != NULL, FALSE);
516 node = g_hash_table_lookup_node (hash_table, key);
520 (*node) = dest->next;
521 g_hash_node_destroy (dest, NULL, NULL);
522 hash_table->nnodes--;
524 G_HASH_TABLE_RESIZE (hash_table);
533 * g_hash_table_steal_all:
534 * @hash_table: a #GHashTable.
536 * Removes all keys and their associated values from a #GHashTable
537 * without calling the key and value destroy functions.
542 g_hash_table_steal_all (GHashTable *hash_table)
546 g_return_if_fail (hash_table != NULL);
548 for (i = 0; i < hash_table->size; i++)
550 g_hash_nodes_destroy (hash_table->nodes[i], NULL, NULL);
551 hash_table->nodes[i] = NULL;
554 hash_table->nnodes = 0;
556 G_HASH_TABLE_RESIZE (hash_table);
560 * g_hash_table_foreach_remove:
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.
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. If you supplied key or value destroy functions when creating
568 * the #GHashTable, they are used to free the memory allocated for the removed
571 * Return value: the number of key/value pairs removed.
574 g_hash_table_foreach_remove (GHashTable *hash_table,
578 g_return_val_if_fail (hash_table != NULL, 0);
579 g_return_val_if_fail (func != NULL, 0);
581 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
585 * g_hash_table_foreach_steal:
586 * @hash_table: a #GHashTable.
587 * @func: the function to call for each key/value pair.
588 * @user_data: user data to pass to the function.
590 * Calls the given function for each key/value pair in the #GHashTable.
591 * If the function returns %TRUE, then the key/value pair is removed from the
592 * #GHashTable, but no key or value destroy functions are called.
594 * Return value: the number of key/value pairs removed.
597 g_hash_table_foreach_steal (GHashTable *hash_table,
601 g_return_val_if_fail (hash_table != NULL, 0);
602 g_return_val_if_fail (func != NULL, 0);
604 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
608 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
613 GHashNode *node, *prev;
617 for (i = 0; i < hash_table->size; i++)
623 for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
625 if ((* func) (node->key, node->value, user_data))
629 hash_table->nnodes -= 1;
633 prev->next = node->next;
634 g_hash_node_destroy (node,
635 notify ? hash_table->key_destroy_func : NULL,
636 notify ? hash_table->value_destroy_func : NULL);
641 hash_table->nodes[i] = node->next;
642 g_hash_node_destroy (node,
643 notify ? hash_table->key_destroy_func : NULL,
644 notify ? hash_table->value_destroy_func : NULL);
651 G_HASH_TABLE_RESIZE (hash_table);
657 * g_hash_table_foreach:
658 * @hash_table: a #GHashTable.
659 * @func: the function to call for each key/value pair.
660 * @user_data: user data to pass to the function.
662 * Calls the given function for each of the key/value pairs in the
663 * #GHashTable. The function is passed the key and value of each
664 * pair, and the given @user_data parameter. The hash table may not
665 * be modified while iterating over it (you can't add/remove
666 * items). To remove all items matching a predicate, use
667 * g_hash_table_foreach_remove().
670 g_hash_table_foreach (GHashTable *hash_table,
677 g_return_if_fail (hash_table != NULL);
678 g_return_if_fail (func != NULL);
680 for (i = 0; i < hash_table->size; i++)
681 for (node = hash_table->nodes[i]; node; node = node->next)
682 (* func) (node->key, node->value, user_data);
687 * @hash_table: a #GHashTable.
688 * @predicate: function to test the key/value pairs for a certain property.
689 * @user_data: user data to pass to the function.
691 * Calls the given function for key/value pairs in the #GHashTable until
692 * @predicate returns %TRUE. The function is passed the key and value of
693 * each pair, and the given @user_data parameter. The hash table may not
694 * be modified while iterating over it (you can't add/remove items).
696 * Return value: The value of the first key/value pair is returned, for which
697 * func evaluates to %TRUE. If no pair with the requested property is found,
703 g_hash_table_find (GHashTable *hash_table,
710 g_return_val_if_fail (hash_table != NULL, NULL);
711 g_return_val_if_fail (predicate != NULL, NULL);
713 for (i = 0; i < hash_table->size; i++)
714 for (node = hash_table->nodes[i]; node; node = node->next)
715 if (predicate (node->key, node->value, user_data))
722 * @hash_table: a #GHashTable.
724 * Returns the number of elements contained in the #GHashTable.
726 * Return value: the number of key/value pairs in the #GHashTable.
729 g_hash_table_size (GHashTable *hash_table)
731 g_return_val_if_fail (hash_table != NULL, 0);
733 return hash_table->nnodes;
739 g_hash_table_resize (GHashTable *hash_table)
741 GHashNode **new_nodes;
748 new_size = g_spaced_primes_closest (hash_table->nnodes);
749 new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
751 new_nodes = g_new0 (GHashNode*, new_size);
753 for (i = 0; i < hash_table->size; i++)
754 for (node = hash_table->nodes[i]; node; node = next)
758 hash_val = (* hash_table->hash_func) (node->key) % new_size;
760 node->next = new_nodes[hash_val];
761 new_nodes[hash_val] = node;
764 g_free (hash_table->nodes);
765 hash_table->nodes = new_nodes;
766 hash_table->size = new_size;
770 g_hash_node_new (gpointer key,
773 GHashNode *hash_node = g_slice_new (GHashNode);
775 hash_node->key = key;
776 hash_node->value = value;
777 hash_node->next = NULL;
785 g_hash_node_destroy (GHashNode *hash_node,
786 GDestroyNotify key_destroy_func,
787 GDestroyNotify value_destroy_func)
789 if (key_destroy_func)
790 key_destroy_func (hash_node->key);
791 if (value_destroy_func)
792 value_destroy_func (hash_node->value);
793 g_slice_free (GHashNode, hash_node);
797 g_hash_nodes_destroy (GHashNode *hash_node,
798 GFreeFunc key_destroy_func,
799 GFreeFunc value_destroy_func)
803 GHashNode *next = hash_node->next;
804 if (key_destroy_func)
805 key_destroy_func (hash_node->key);
806 if (value_destroy_func)
807 value_destroy_func (hash_node->value);
808 g_slice_free (GHashNode, hash_node);
815 #include "galiasdef.c"