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/.
34 #define HASH_TABLE_MIN_SIZE 11
35 #define HASH_TABLE_MAX_SIZE 13845163
38 typedef struct _GHashNode GHashNode;
53 GEqualFunc key_equal_func;
57 static void g_hash_table_resize (GHashTable *hash_table);
58 static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
60 static GHashNode* g_hash_node_new (gpointer key,
62 static void g_hash_node_destroy (GHashNode *hash_node);
63 static void g_hash_nodes_destroy (GHashNode *hash_node);
66 G_LOCK_DEFINE_STATIC (g_hash_global);
68 static GMemChunk *node_mem_chunk = NULL;
69 static GHashNode *node_free_list = NULL;
73 g_hash_table_new (GHashFunc hash_func,
74 GEqualFunc key_equal_func)
76 GHashTable *hash_table;
79 hash_table = g_new (GHashTable, 1);
80 hash_table->size = HASH_TABLE_MIN_SIZE;
81 hash_table->nnodes = 0;
82 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
83 hash_table->key_equal_func = key_equal_func;
84 hash_table->nodes = g_new (GHashNode*, hash_table->size);
86 for (i = 0; i < hash_table->size; i++)
87 hash_table->nodes[i] = NULL;
93 g_hash_table_destroy (GHashTable *hash_table)
97 g_return_if_fail (hash_table != NULL);
99 for (i = 0; i < hash_table->size; i++)
100 g_hash_nodes_destroy (hash_table->nodes[i]);
102 g_free (hash_table->nodes);
106 static inline GHashNode**
107 g_hash_table_lookup_node (GHashTable *hash_table,
112 node = &hash_table->nodes
113 [(* hash_table->hash_func) (key) % hash_table->size];
115 /* Hash table lookup needs to be fast.
116 * We therefore remove the extra conditional of testing
117 * whether to call the key_equal_func or not from
120 if (hash_table->key_equal_func)
121 while (*node && !(*hash_table->key_equal_func) ((*node)->key, key))
122 node = &(*node)->next;
124 while (*node && (*node)->key != key)
125 node = &(*node)->next;
131 g_hash_table_lookup (GHashTable *hash_table,
136 g_return_val_if_fail (hash_table != NULL, NULL);
138 node = *g_hash_table_lookup_node (hash_table, key);
140 return node ? node->value : NULL;
144 g_hash_table_insert (GHashTable *hash_table,
150 g_return_if_fail (hash_table != NULL);
152 node = g_hash_table_lookup_node (hash_table, key);
156 /* do not reset node->key in this place, keeping
157 * the old key might be intended.
158 * a g_hash_table_remove/g_hash_table_insert pair
159 * can be used otherwise.
161 * node->key = key; */
162 (*node)->value = value;
166 *node = g_hash_node_new (key, value);
167 hash_table->nnodes++;
168 g_hash_table_resize (hash_table);
173 g_hash_table_remove (GHashTable *hash_table,
176 GHashNode **node, *dest;
178 g_return_if_fail (hash_table != NULL);
180 node = g_hash_table_lookup_node (hash_table, key);
185 (*node) = dest->next;
186 g_hash_node_destroy (dest);
187 hash_table->nnodes--;
189 g_hash_table_resize (hash_table);
194 g_hash_table_lookup_extended (GHashTable *hash_table,
195 gconstpointer lookup_key,
201 g_return_val_if_fail (hash_table != NULL, FALSE);
203 node = *g_hash_table_lookup_node (hash_table, lookup_key);
208 *orig_key = node->key;
210 *value = node->value;
218 g_hash_table_freeze (GHashTable *hash_table)
220 #ifdef G_ENABLE_DEBUG
221 static gboolean first_call = TRUE;
225 g_warning("g_hash_table_freeze and g_hash_table_thaw are deprecated.");
228 #endif /* G_ENABLE_DEBUG */
232 g_hash_table_thaw (GHashTable *hash_table)
237 g_hash_table_foreach_remove (GHashTable *hash_table,
241 GHashNode *node, *prev;
245 g_return_val_if_fail (hash_table != NULL, 0);
246 g_return_val_if_fail (func != NULL, 0);
248 for (i = 0; i < hash_table->size; i++)
254 for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
256 if ((* func) (node->key, node->value, user_data))
260 hash_table->nnodes -= 1;
264 prev->next = node->next;
265 g_hash_node_destroy (node);
270 hash_table->nodes[i] = node->next;
271 g_hash_node_destroy (node);
278 g_hash_table_resize (hash_table);
284 g_hash_table_foreach (GHashTable *hash_table,
291 g_return_if_fail (hash_table != NULL);
292 g_return_if_fail (func != NULL);
294 for (i = 0; i < hash_table->size; i++)
295 for (node = hash_table->nodes[i]; node; node = node->next)
296 (* func) (node->key, node->value, user_data);
299 /* Returns the number of elements contained in the hash table. */
301 g_hash_table_size (GHashTable *hash_table)
303 g_return_val_if_fail (hash_table != NULL, 0);
305 return hash_table->nnodes;
309 g_hash_table_resize (GHashTable *hash_table)
311 GHashNode **new_nodes;
314 gfloat nodes_per_list;
319 nodes_per_list = (gfloat) hash_table->nnodes / (gfloat) hash_table->size;
321 if ((nodes_per_list > 0.3 || hash_table->size <= HASH_TABLE_MIN_SIZE) &&
322 (nodes_per_list < 3.0 || hash_table->size >= HASH_TABLE_MAX_SIZE))
325 new_size = CLAMP(g_spaced_primes_closest (hash_table->nnodes),
327 HASH_TABLE_MAX_SIZE);
328 new_nodes = g_new0 (GHashNode*, new_size);
330 for (i = 0; i < hash_table->size; i++)
331 for (node = hash_table->nodes[i]; node; node = next)
335 hash_val = (* hash_table->hash_func) (node->key) % new_size;
337 node->next = new_nodes[hash_val];
338 new_nodes[hash_val] = node;
341 g_free (hash_table->nodes);
342 hash_table->nodes = new_nodes;
343 hash_table->size = new_size;
347 g_hash_node_new (gpointer key,
350 GHashNode *hash_node;
352 G_LOCK (g_hash_global);
355 hash_node = node_free_list;
356 node_free_list = node_free_list->next;
361 node_mem_chunk = g_mem_chunk_new ("hash node mem chunk",
365 hash_node = g_chunk_new (GHashNode, node_mem_chunk);
367 G_UNLOCK (g_hash_global);
369 hash_node->key = key;
370 hash_node->value = value;
371 hash_node->next = NULL;
377 g_hash_node_destroy (GHashNode *hash_node)
380 #ifdef ENABLE_GC_FRIENDLY
381 hash_node->key = NULL;
382 hash_node->value = NULL;
383 #endif /* ENABLE_GC_FRIENDLY */
385 G_LOCK (g_hash_global);
386 hash_node->next = node_free_list;
387 node_free_list = hash_node;
388 G_UNLOCK (g_hash_global);
392 g_hash_nodes_destroy (GHashNode *hash_node)
396 GHashNode *node = hash_node;
400 #ifdef ENABLE_GC_FRIENDLY
403 #endif /* ENABLE_GC_FRIENDLY */
407 #ifdef ENABLE_GC_FRIENDLY
410 #endif /* ENABLE_GC_FRIENDLY */
412 G_LOCK (g_hash_global);
413 node->next = node_free_list;
414 node_free_list = hash_node;
415 G_UNLOCK (g_hash_global);