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/.
32 * Imported in mono cvs from version 1.32 of gnome cvs.
40 #include <mono/os/gc_wrapper.h>
41 #include "mono-hash.h"
43 #define HASH_TABLE_MIN_SIZE 11
44 #define HASH_TABLE_MAX_SIZE 13845163
47 typedef struct _MonoGHashNode MonoGHashNode;
56 struct _MonoGHashTable
60 MonoGHashNode **nodes;
62 GEqualFunc key_equal_func;
63 GDestroyNotify key_destroy_func;
64 GDestroyNotify value_destroy_func;
67 #define G_HASH_TABLE_RESIZE(hash_table) \
69 if ((hash_table->size >= 3 * hash_table->nnodes && \
70 hash_table->size > HASH_TABLE_MIN_SIZE) || \
71 (3 * hash_table->size <= hash_table->nnodes && \
72 hash_table->size < HASH_TABLE_MAX_SIZE)) \
73 g_hash_table_resize (hash_table); \
76 static void g_hash_table_resize (MonoGHashTable *hash_table);
77 static MonoGHashNode** g_hash_table_lookup_node (MonoGHashTable *hash_table,
79 static MonoGHashNode* g_hash_node_new (gpointer key,
81 static void g_hash_node_destroy (MonoGHashNode *hash_node,
82 GDestroyNotify key_destroy_func,
83 GDestroyNotify value_destroy_func);
84 static void g_hash_nodes_destroy (MonoGHashNode *hash_node,
85 GDestroyNotify key_destroy_func,
86 GDestroyNotify value_destroy_func);
87 static guint g_hash_table_foreach_remove_or_steal (MonoGHashTable *hash_table,
93 G_LOCK_DEFINE_STATIC (g_hash_global);
96 static GMemChunk *node_mem_chunk = NULL;
98 static MonoGHashNode *node_free_list = NULL;
102 * @hash_func: a function to create a hash value from a key.
103 * Hash values are used to determine where keys are stored within the
104 * #GHashTable data structure. The g_direct_hash(), g_int_hash() and
105 * g_str_hash() functions are provided for some common types of keys.
106 * If hash_func is %NULL, g_direct_hash() is used.
107 * @key_equal_func: a function to check two keys for equality. This is
108 * used when looking up keys in the #GHashTable. The g_direct_equal(),
109 * g_int_equal() and g_str_equal() functions are provided for the most
110 * common types of keys. If @key_equal_func is %NULL, keys are compared
111 * directly in a similar fashion to g_direct_equal(), but without the
112 * overhead of a function call.
114 * Creates a new #GHashTable.
116 * Return value: a new #GHashTable.
119 mono_g_hash_table_new (GHashFunc hash_func,
120 GEqualFunc key_equal_func)
122 return mono_g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
127 * g_hash_table_new_full:
128 * @hash_func: a function to create a hash value from a key.
129 * @key_equal_func: a function to check two keys for equality.
130 * @key_destroy_func: a function to free the memory allocated for the key
131 * used when removing the entry from the #GHashTable or %NULL if you
132 * don't want to supply such a function.
133 * @value_destroy_func: a function to free the memory allocated for the
134 * value used when removing the entry from the #GHashTable or %NULL if
135 * you don't want to supply such a function.
137 * Creates a new #GHashTable like g_hash_table_new() and allows to specify
138 * functions to free the memory allocated for the key and value that get
139 * called when removing the entry from the #GHashTable.
141 * Return value: a new #GHashTable.
144 mono_g_hash_table_new_full (GHashFunc hash_func,
145 GEqualFunc key_equal_func,
146 GDestroyNotify key_destroy_func,
147 GDestroyNotify value_destroy_func)
149 MonoGHashTable *hash_table;
152 hash_table = GC_MALLOC (sizeof (MonoGHashTable));
154 hash_table = g_new (MonoGHashTable, 1);
156 hash_table->size = HASH_TABLE_MIN_SIZE;
157 hash_table->nnodes = 0;
158 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
159 hash_table->key_equal_func = key_equal_func;
160 hash_table->key_destroy_func = key_destroy_func;
161 hash_table->value_destroy_func = value_destroy_func;
163 hash_table->nodes = GC_MALLOC (sizeof (MonoGHashNode*) * hash_table->size);
165 hash_table->nodes = g_new0 (MonoGHashNode*, hash_table->size);
172 * g_hash_table_destroy:
173 * @hash_table: a #GHashTable.
175 * Destroys the #GHashTable. If keys and/or values are dynamically
176 * allocated, you should either free them first or create the #GHashTable
177 * using g_hash_table_new_full(). In the latter case the destroy functions
178 * you supplied will be called on all keys and values before destroying
182 mono_g_hash_table_destroy (MonoGHashTable *hash_table)
186 g_return_if_fail (hash_table != NULL);
188 for (i = 0; i < hash_table->size; i++)
189 g_hash_nodes_destroy (hash_table->nodes[i],
190 hash_table->key_destroy_func,
191 hash_table->value_destroy_func);
195 g_free (hash_table->nodes);
200 static inline MonoGHashNode**
201 g_hash_table_lookup_node (MonoGHashTable *hash_table,
204 MonoGHashNode **node;
206 node = &hash_table->nodes
207 [(* hash_table->hash_func) (key) % hash_table->size];
209 /* Hash table lookup needs to be fast.
210 * We therefore remove the extra conditional of testing
211 * whether to call the key_equal_func or not from
214 if (hash_table->key_equal_func)
215 while (*node && !(*hash_table->key_equal_func) ((*node)->key, key))
216 node = &(*node)->next;
218 while (*node && (*node)->key != key)
219 node = &(*node)->next;
225 * g_hash_table_lookup:
226 * @hash_table: a #GHashTable.
227 * @key: the key to look up.
229 * Looks up a key in a #GHashTable.
231 * Return value: the associated value, or %NULL if the key is not found.
234 mono_g_hash_table_lookup (MonoGHashTable *hash_table,
239 g_return_val_if_fail (hash_table != NULL, NULL);
241 node = *g_hash_table_lookup_node (hash_table, key);
243 return node ? node->value : NULL;
247 * g_hash_table_lookup_extended:
248 * @hash_table: a #GHashTable.
249 * @lookup_key: the key to look up.
250 * @orig_key: returns the original key.
251 * @value: returns the value associated with the key.
253 * Looks up a key in the #GHashTable, returning the original key and the
254 * associated value and a #gboolean which is %TRUE if the key was found. This
255 * is useful if you need to free the memory allocated for the original key,
256 * for example before calling g_hash_table_remove().
258 * Return value: %TRUE if the key was found in the #GHashTable.
261 mono_g_hash_table_lookup_extended (MonoGHashTable *hash_table,
262 gconstpointer lookup_key,
268 g_return_val_if_fail (hash_table != NULL, FALSE);
270 node = *g_hash_table_lookup_node (hash_table, lookup_key);
275 *orig_key = node->key;
277 *value = node->value;
285 * g_hash_table_insert:
286 * @hash_table: a #GHashTable.
287 * @key: a key to insert.
288 * @value: the value to associate with the key.
290 * Inserts a new key and value into a #GHashTable.
292 * If the key already exists in the #GHashTable its current value is replaced
293 * with the new value. If you supplied a @value_destroy_func when creating the
294 * #GHashTable, the old value is freed using that function. If you supplied
295 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
296 * using that function.
299 mono_g_hash_table_insert (MonoGHashTable *hash_table,
303 MonoGHashNode **node;
305 g_return_if_fail (hash_table != NULL);
307 node = g_hash_table_lookup_node (hash_table, key);
311 /* do not reset node->key in this place, keeping
312 * the old key is the intended behaviour.
313 * g_hash_table_replace() can be used instead.
316 /* free the passed key */
317 if (hash_table->key_destroy_func)
318 hash_table->key_destroy_func (key);
320 if (hash_table->value_destroy_func)
321 hash_table->value_destroy_func ((*node)->value);
323 (*node)->value = value;
327 *node = g_hash_node_new (key, value);
328 hash_table->nnodes++;
329 G_HASH_TABLE_RESIZE (hash_table);
334 * g_hash_table_replace:
335 * @hash_table: a #GHashTable.
336 * @key: a key to insert.
337 * @value: the value to associate with the key.
339 * Inserts a new key and value into a #GHashTable similar to
340 * g_hash_table_insert(). The difference is that if the key already exists
341 * in the #GHashTable, it gets replaced by the new key. If you supplied a
342 * @value_destroy_func when creating the #GHashTable, the old value is freed
343 * using that function. If you supplied a @key_destroy_func when creating the
344 * #GHashTable, the old key is freed using that function.
347 mono_g_hash_table_replace (MonoGHashTable *hash_table,
351 MonoGHashNode **node;
353 g_return_if_fail (hash_table != NULL);
355 node = g_hash_table_lookup_node (hash_table, key);
359 if (hash_table->key_destroy_func)
360 hash_table->key_destroy_func ((*node)->key);
362 if (hash_table->value_destroy_func)
363 hash_table->value_destroy_func ((*node)->value);
366 (*node)->value = value;
370 *node = g_hash_node_new (key, value);
371 hash_table->nnodes++;
372 G_HASH_TABLE_RESIZE (hash_table);
377 * g_hash_table_remove:
378 * @hash_table: a #GHashTable.
379 * @key: the key to remove.
381 * Removes a key and its associated value from a #GHashTable.
383 * If the #GHashTable was created using g_hash_table_new_full(), the
384 * key and value are freed using the supplied destroy functions, otherwise
385 * you have to make sure that any dynamically allocated values are freed
388 * Return value: %TRUE if the key was found and removed from the #GHashTable.
391 mono_g_hash_table_remove (MonoGHashTable *hash_table,
394 MonoGHashNode **node, *dest;
396 g_return_val_if_fail (hash_table != NULL, FALSE);
398 node = g_hash_table_lookup_node (hash_table, key);
402 (*node) = dest->next;
403 g_hash_node_destroy (dest,
404 hash_table->key_destroy_func,
405 hash_table->value_destroy_func);
406 hash_table->nnodes--;
408 G_HASH_TABLE_RESIZE (hash_table);
417 * g_hash_table_steal:
418 * @hash_table: a #GHashTable.
419 * @key: the key to remove.
421 * Removes a key and its associated value from a #GHashTable without
422 * calling the key and value destroy functions.
424 * Return value: %TRUE if the key was found and removed from the #GHashTable.
427 mono_g_hash_table_steal (MonoGHashTable *hash_table,
430 MonoGHashNode **node, *dest;
432 g_return_val_if_fail (hash_table != NULL, FALSE);
434 node = g_hash_table_lookup_node (hash_table, key);
438 (*node) = dest->next;
439 g_hash_node_destroy (dest, NULL, NULL);
440 hash_table->nnodes--;
442 G_HASH_TABLE_RESIZE (hash_table);
451 * g_hash_table_foreach_remove:
452 * @hash_table: a #GHashTable.
453 * @func: the function to call for each key/value pair.
454 * @user_data: user data to pass to the function.
456 * Calls the given function for each key/value pair in the #GHashTable.
457 * If the function returns %TRUE, then the key/value pair is removed from the
458 * #GHashTable. If you supplied key or value destroy functions when creating
459 * the #GHashTable, they are used to free the memory allocated for the removed
462 * Return value: the number of key/value pairs removed.
465 mono_g_hash_table_foreach_remove (MonoGHashTable *hash_table,
469 g_return_val_if_fail (hash_table != NULL, 0);
470 g_return_val_if_fail (func != NULL, 0);
472 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
476 * g_hash_table_foreach_steal:
477 * @hash_table: a #GHashTable.
478 * @func: the function to call for each key/value pair.
479 * @user_data: user data to pass to the function.
481 * Calls the given function for each key/value pair in the #GHashTable.
482 * If the function returns %TRUE, then the key/value pair is removed from the
483 * #GHashTable, but no key or value destroy functions are called.
485 * Return value: the number of key/value pairs removed.
488 mono_g_hash_table_foreach_steal (MonoGHashTable *hash_table,
492 g_return_val_if_fail (hash_table != NULL, 0);
493 g_return_val_if_fail (func != NULL, 0);
495 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
499 g_hash_table_foreach_remove_or_steal (MonoGHashTable *hash_table,
504 MonoGHashNode *node, *prev;
508 for (i = 0; i < hash_table->size; i++)
514 for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
516 if ((* func) (node->key, node->value, user_data))
520 hash_table->nnodes -= 1;
524 prev->next = node->next;
525 g_hash_node_destroy (node,
526 notify ? hash_table->key_destroy_func : NULL,
527 notify ? hash_table->value_destroy_func : NULL);
532 hash_table->nodes[i] = node->next;
533 g_hash_node_destroy (node,
534 notify ? hash_table->key_destroy_func : NULL,
535 notify ? hash_table->value_destroy_func : NULL);
542 G_HASH_TABLE_RESIZE (hash_table);
548 * g_hash_table_foreach:
549 * @hash_table: a #GHashTable.
550 * @func: the function to call for each key/value pair.
551 * @user_data: user data to pass to the function.
553 * Calls the given function for each of the key/value pairs in the
554 * #GHashTable. The function is passed the key and value of each
555 * pair, and the given @user_data parameter. The hash table may not
556 * be modified while iterating over it (you can't add/remove
557 * items). To remove all items matching a predicate, use
558 * g_hash_table_remove().
561 mono_g_hash_table_foreach (MonoGHashTable *hash_table,
568 g_return_if_fail (hash_table != NULL);
569 g_return_if_fail (func != NULL);
571 for (i = 0; i < hash_table->size; i++)
572 for (node = hash_table->nodes[i]; node; node = node->next)
573 (* func) (node->key, node->value, user_data);
578 * @hash_table: a #GHashTable.
580 * Returns the number of elements contained in the #GHashTable.
582 * Return value: the number of key/value pairs in the #GHashTable.
585 mono_g_hash_table_size (MonoGHashTable *hash_table)
587 g_return_val_if_fail (hash_table != NULL, 0);
589 return hash_table->nnodes;
593 g_hash_table_resize (MonoGHashTable *hash_table)
595 MonoGHashNode **new_nodes;
602 new_size = g_spaced_primes_closest (hash_table->nnodes);
603 new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
606 new_nodes = GC_MALLOC (sizeof (MonoGHashNode*) * new_size);
608 new_nodes = g_new0 (MonoGHashNode*, new_size);
611 for (i = 0; i < hash_table->size; i++)
612 for (node = hash_table->nodes[i]; node; node = next)
616 hash_val = (* hash_table->hash_func) (node->key) % new_size;
618 node->next = new_nodes[hash_val];
619 new_nodes[hash_val] = node;
624 g_free (hash_table->nodes);
626 hash_table->nodes = new_nodes;
627 hash_table->size = new_size;
630 static MonoGHashNode*
631 g_hash_node_new (gpointer key,
634 MonoGHashNode *hash_node;
636 G_LOCK (g_hash_global);
639 hash_node = node_free_list;
640 node_free_list = node_free_list->next;
645 hash_node = GC_MALLOC (sizeof (MonoGHashNode));
648 node_mem_chunk = g_mem_chunk_new ("hash node mem chunk",
649 sizeof (MonoGHashNode),
652 hash_node = g_chunk_new (MonoGHashNode, node_mem_chunk);
655 G_UNLOCK (g_hash_global);
657 hash_node->key = key;
658 hash_node->value = value;
659 hash_node->next = NULL;
665 g_hash_node_destroy (MonoGHashNode *hash_node,
666 GDestroyNotify key_destroy_func,
667 GDestroyNotify value_destroy_func)
669 if (key_destroy_func)
670 key_destroy_func (hash_node->key);
671 if (value_destroy_func)
672 value_destroy_func (hash_node->value);
674 hash_node->key = NULL;
675 hash_node->value = NULL;
677 G_LOCK (g_hash_global);
678 hash_node->next = node_free_list;
679 node_free_list = hash_node;
680 G_UNLOCK (g_hash_global);
684 g_hash_nodes_destroy (MonoGHashNode *hash_node,
685 GFreeFunc key_destroy_func,
686 GFreeFunc value_destroy_func)
690 MonoGHashNode *node = hash_node;
694 if (key_destroy_func)
695 key_destroy_func (node->key);
696 if (value_destroy_func)
697 value_destroy_func (node->value);
705 if (key_destroy_func)
706 key_destroy_func (node->key);
707 if (value_destroy_func)
708 value_destroy_func (node->value);
713 G_LOCK (g_hash_global);
714 node->next = node_free_list;
715 node_free_list = hash_node;
716 G_UNLOCK (g_hash_global);