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 == g_direct_equal ? NULL : 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;
284 static inline MonoGHashNode*
285 g_hash_node_new (gpointer key,
288 MonoGHashNode *hash_node = NULL;
291 if (node_free_list) {
292 G_LOCK (g_hash_global);
294 if (node_free_list) {
295 hash_node = node_free_list;
296 node_free_list = node_free_list->next;
298 G_UNLOCK (g_hash_global);
301 hash_node = GC_MALLOC (sizeof (MonoGHashNode));
303 G_LOCK (g_hash_global);
306 hash_node = node_free_list;
307 node_free_list = node_free_list->next;
312 node_mem_chunk = g_mem_chunk_new ("hash node mem chunk",
313 sizeof (MonoGHashNode),
316 hash_node = g_chunk_new (MonoGHashNode, node_mem_chunk);
318 G_UNLOCK (g_hash_global);
321 hash_node->key = key;
322 hash_node->value = value;
323 hash_node->next = NULL;
329 * g_hash_table_insert:
330 * @hash_table: a #GHashTable.
331 * @key: a key to insert.
332 * @value: the value to associate with the key.
334 * Inserts a new key and value into a #GHashTable.
336 * If the key already exists in the #GHashTable its current value is replaced
337 * with the new value. If you supplied a @value_destroy_func when creating the
338 * #GHashTable, the old value is freed using that function. If you supplied
339 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
340 * using that function.
343 mono_g_hash_table_insert (MonoGHashTable *hash_table,
347 MonoGHashNode **node;
349 g_return_if_fail (hash_table != NULL);
351 node = g_hash_table_lookup_node (hash_table, key);
355 /* do not reset node->key in this place, keeping
356 * the old key is the intended behaviour.
357 * g_hash_table_replace() can be used instead.
360 /* free the passed key */
361 if (hash_table->key_destroy_func)
362 hash_table->key_destroy_func (key);
364 if (hash_table->value_destroy_func)
365 hash_table->value_destroy_func ((*node)->value);
367 (*node)->value = value;
371 *node = g_hash_node_new (key, value);
372 hash_table->nnodes++;
373 G_HASH_TABLE_RESIZE (hash_table);
378 * g_hash_table_replace:
379 * @hash_table: a #GHashTable.
380 * @key: a key to insert.
381 * @value: the value to associate with the key.
383 * Inserts a new key and value into a #GHashTable similar to
384 * g_hash_table_insert(). The difference is that if the key already exists
385 * in the #GHashTable, it gets replaced by the new key. If you supplied a
386 * @value_destroy_func when creating the #GHashTable, the old value is freed
387 * using that function. If you supplied a @key_destroy_func when creating the
388 * #GHashTable, the old key is freed using that function.
391 mono_g_hash_table_replace (MonoGHashTable *hash_table,
395 MonoGHashNode **node;
397 g_return_if_fail (hash_table != NULL);
399 node = g_hash_table_lookup_node (hash_table, key);
403 if (hash_table->key_destroy_func)
404 hash_table->key_destroy_func ((*node)->key);
406 if (hash_table->value_destroy_func)
407 hash_table->value_destroy_func ((*node)->value);
410 (*node)->value = value;
414 *node = g_hash_node_new (key, value);
415 hash_table->nnodes++;
416 G_HASH_TABLE_RESIZE (hash_table);
421 * g_hash_table_remove:
422 * @hash_table: a #GHashTable.
423 * @key: the key to remove.
425 * Removes a key and its associated value from a #GHashTable.
427 * If the #GHashTable was created using g_hash_table_new_full(), the
428 * key and value are freed using the supplied destroy functions, otherwise
429 * you have to make sure that any dynamically allocated values are freed
432 * Return value: %TRUE if the key was found and removed from the #GHashTable.
435 mono_g_hash_table_remove (MonoGHashTable *hash_table,
438 MonoGHashNode **node, *dest;
440 g_return_val_if_fail (hash_table != NULL, FALSE);
442 node = g_hash_table_lookup_node (hash_table, key);
446 (*node) = dest->next;
447 g_hash_node_destroy (dest,
448 hash_table->key_destroy_func,
449 hash_table->value_destroy_func);
450 hash_table->nnodes--;
452 G_HASH_TABLE_RESIZE (hash_table);
461 * g_hash_table_steal:
462 * @hash_table: a #GHashTable.
463 * @key: the key to remove.
465 * Removes a key and its associated value from a #GHashTable without
466 * calling the key and value destroy functions.
468 * Return value: %TRUE if the key was found and removed from the #GHashTable.
471 mono_g_hash_table_steal (MonoGHashTable *hash_table,
474 MonoGHashNode **node, *dest;
476 g_return_val_if_fail (hash_table != NULL, FALSE);
478 node = g_hash_table_lookup_node (hash_table, key);
482 (*node) = dest->next;
483 g_hash_node_destroy (dest, NULL, NULL);
484 hash_table->nnodes--;
486 G_HASH_TABLE_RESIZE (hash_table);
495 * g_hash_table_foreach_remove:
496 * @hash_table: a #GHashTable.
497 * @func: the function to call for each key/value pair.
498 * @user_data: user data to pass to the function.
500 * Calls the given function for each key/value pair in the #GHashTable.
501 * If the function returns %TRUE, then the key/value pair is removed from the
502 * #GHashTable. If you supplied key or value destroy functions when creating
503 * the #GHashTable, they are used to free the memory allocated for the removed
506 * Return value: the number of key/value pairs removed.
509 mono_g_hash_table_foreach_remove (MonoGHashTable *hash_table,
513 g_return_val_if_fail (hash_table != NULL, 0);
514 g_return_val_if_fail (func != NULL, 0);
516 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
520 * g_hash_table_foreach_steal:
521 * @hash_table: a #GHashTable.
522 * @func: the function to call for each key/value pair.
523 * @user_data: user data to pass to the function.
525 * Calls the given function for each key/value pair in the #GHashTable.
526 * If the function returns %TRUE, then the key/value pair is removed from the
527 * #GHashTable, but no key or value destroy functions are called.
529 * Return value: the number of key/value pairs removed.
532 mono_g_hash_table_foreach_steal (MonoGHashTable *hash_table,
536 g_return_val_if_fail (hash_table != NULL, 0);
537 g_return_val_if_fail (func != NULL, 0);
539 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
543 g_hash_table_foreach_remove_or_steal (MonoGHashTable *hash_table,
548 MonoGHashNode *node, *prev;
552 for (i = 0; i < hash_table->size; i++)
558 for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
560 if ((* func) (node->key, node->value, user_data))
564 hash_table->nnodes -= 1;
568 prev->next = node->next;
569 g_hash_node_destroy (node,
570 notify ? hash_table->key_destroy_func : NULL,
571 notify ? hash_table->value_destroy_func : NULL);
576 hash_table->nodes[i] = node->next;
577 g_hash_node_destroy (node,
578 notify ? hash_table->key_destroy_func : NULL,
579 notify ? hash_table->value_destroy_func : NULL);
586 G_HASH_TABLE_RESIZE (hash_table);
592 * g_hash_table_foreach:
593 * @hash_table: a #GHashTable.
594 * @func: the function to call for each key/value pair.
595 * @user_data: user data to pass to the function.
597 * Calls the given function for each of the key/value pairs in the
598 * #GHashTable. The function is passed the key and value of each
599 * pair, and the given @user_data parameter. The hash table may not
600 * be modified while iterating over it (you can't add/remove
601 * items). To remove all items matching a predicate, use
602 * g_hash_table_remove().
605 mono_g_hash_table_foreach (MonoGHashTable *hash_table,
612 g_return_if_fail (hash_table != NULL);
613 g_return_if_fail (func != NULL);
615 for (i = 0; i < hash_table->size; i++)
616 for (node = hash_table->nodes[i]; node; node = node->next)
617 (* func) (node->key, node->value, user_data);
622 * @hash_table: a #GHashTable.
624 * Returns the number of elements contained in the #GHashTable.
626 * Return value: the number of key/value pairs in the #GHashTable.
629 mono_g_hash_table_size (MonoGHashTable *hash_table)
631 g_return_val_if_fail (hash_table != NULL, 0);
633 return hash_table->nnodes;
637 * mono_g_hash_table_remap:
639 * Calls the given function for each key-value pair in the hash table,
640 * and replaces the value stored in the hash table by the value returned by
645 mono_g_hash_table_remap (MonoGHashTable *hash_table,
646 MonoGRemapperFunc func,
652 g_return_if_fail (hash_table != NULL);
653 g_return_if_fail (func != NULL);
655 for (i = 0; i < hash_table->size; i++)
656 for (node = hash_table->nodes[i]; node; node = node->next)
657 node->value = (* func) (node->key, node->value, user_data);
661 g_hash_table_resize (MonoGHashTable *hash_table)
663 MonoGHashNode **new_nodes;
670 new_size = g_spaced_primes_closest (hash_table->nnodes);
671 new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
674 new_nodes = GC_MALLOC (sizeof (MonoGHashNode*) * new_size);
676 new_nodes = g_new0 (MonoGHashNode*, new_size);
679 for (i = 0; i < hash_table->size; i++)
680 for (node = hash_table->nodes[i]; node; node = next)
684 hash_val = (* hash_table->hash_func) (node->key) % new_size;
686 node->next = new_nodes[hash_val];
687 new_nodes[hash_val] = node;
692 g_free (hash_table->nodes);
694 hash_table->nodes = new_nodes;
695 hash_table->size = new_size;
699 g_hash_node_destroy (MonoGHashNode *hash_node,
700 GDestroyNotify key_destroy_func,
701 GDestroyNotify value_destroy_func)
703 if (key_destroy_func)
704 key_destroy_func (hash_node->key);
705 if (value_destroy_func)
706 value_destroy_func (hash_node->value);
708 hash_node->key = NULL;
709 hash_node->value = NULL;
711 G_LOCK (g_hash_global);
712 hash_node->next = node_free_list;
713 node_free_list = hash_node;
714 G_UNLOCK (g_hash_global);
718 g_hash_nodes_destroy (MonoGHashNode *hash_node,
719 GFreeFunc key_destroy_func,
720 GFreeFunc value_destroy_func)
724 MonoGHashNode *node = hash_node;
728 if (key_destroy_func)
729 key_destroy_func (node->key);
730 if (value_destroy_func)
731 value_destroy_func (node->value);
739 if (key_destroy_func)
740 key_destroy_func (node->key);
741 if (value_destroy_func)
742 value_destroy_func (node->value);
747 G_LOCK (g_hash_global);
748 node->next = node_free_list;
749 node_free_list = hash_node;
750 G_UNLOCK (g_hash_global);