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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
27 #define HASH_TABLE_MIN_SIZE 11
28 #define HASH_TABLE_MAX_SIZE 13845163
31 typedef struct _GHashNode GHashNode;
47 GCompareFunc key_compare_func;
51 static void g_hash_table_resize (GHashTable *hash_table);
52 static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
54 static GHashNode* g_hash_node_new (gpointer key,
56 static void g_hash_node_destroy (GHashNode *hash_node);
57 static void g_hash_nodes_destroy (GHashNode *hash_node);
60 G_LOCK_DECLARE_STATIC (g_hash_global);
62 static GMemChunk *node_mem_chunk = NULL;
63 static GHashNode *node_free_list = NULL;
67 g_hash_table_new (GHashFunc hash_func,
68 GCompareFunc key_compare_func)
70 GHashTable *hash_table;
73 hash_table = g_new (GHashTable, 1);
74 hash_table->size = HASH_TABLE_MIN_SIZE;
75 hash_table->nnodes = 0;
76 hash_table->frozen = FALSE;
77 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
78 hash_table->key_compare_func = key_compare_func;
79 hash_table->nodes = g_new (GHashNode*, hash_table->size);
81 for (i = 0; i < hash_table->size; i++)
82 hash_table->nodes[i] = NULL;
88 g_hash_table_destroy (GHashTable *hash_table)
92 g_return_if_fail (hash_table != NULL);
94 for (i = 0; i < hash_table->size; i++)
95 g_hash_nodes_destroy (hash_table->nodes[i]);
97 g_free (hash_table->nodes);
101 static inline GHashNode**
102 g_hash_table_lookup_node (GHashTable *hash_table,
107 node = &hash_table->nodes
108 [(* hash_table->hash_func) (key) % hash_table->size];
110 /* Hash table lookup needs to be fast.
111 * We therefore remove the extra conditional of testing
112 * whether to call the key_compare_func or not from
115 if (hash_table->key_compare_func)
116 while (*node && !(*hash_table->key_compare_func) ((*node)->key, key))
117 node = &(*node)->next;
119 while (*node && (*node)->key != key)
120 node = &(*node)->next;
126 g_hash_table_lookup (GHashTable *hash_table,
131 g_return_val_if_fail (hash_table != NULL, NULL);
133 node = *g_hash_table_lookup_node (hash_table, key);
135 return node ? node->value : NULL;
139 g_hash_table_insert (GHashTable *hash_table,
145 g_return_if_fail (hash_table != NULL);
147 node = g_hash_table_lookup_node (hash_table, key);
151 /* do not reset node->key in this place, keeping
152 * the old key might be intended.
153 * a g_hash_table_remove/g_hash_table_insert pair
154 * can be used otherwise.
156 * node->key = key; */
157 (*node)->value = value;
161 *node = g_hash_node_new (key, value);
162 hash_table->nnodes++;
163 if (!hash_table->frozen)
164 g_hash_table_resize (hash_table);
169 g_hash_table_remove (GHashTable *hash_table,
172 GHashNode **node, *dest;
174 g_return_if_fail (hash_table != NULL);
176 while (*(node = g_hash_table_lookup_node (hash_table, key)))
179 (*node) = dest->next;
180 g_hash_node_destroy (dest);
181 hash_table->nnodes--;
184 if (!hash_table->frozen)
185 g_hash_table_resize (hash_table);
189 g_hash_table_lookup_extended (GHashTable *hash_table,
190 gconstpointer lookup_key,
196 g_return_val_if_fail (hash_table != NULL, FALSE);
198 node = *g_hash_table_lookup_node (hash_table, lookup_key);
203 *orig_key = node->key;
205 *value = node->value;
213 g_hash_table_freeze (GHashTable *hash_table)
215 g_return_if_fail (hash_table != NULL);
217 hash_table->frozen++;
221 g_hash_table_thaw (GHashTable *hash_table)
223 g_return_if_fail (hash_table != NULL);
225 if (hash_table->frozen)
226 if (!(--hash_table->frozen))
227 g_hash_table_resize (hash_table);
231 g_hash_table_foreach_remove (GHashTable *hash_table,
235 GHashNode *node, *prev;
239 g_return_val_if_fail (hash_table != NULL, 0);
240 g_return_val_if_fail (func != NULL, 0);
242 for (i = 0; i < hash_table->size; i++)
248 for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
250 if ((* func) (node->key, node->value, user_data))
254 hash_table->nnodes -= 1;
258 prev->next = node->next;
259 g_hash_node_destroy (node);
264 hash_table->nodes[i] = node->next;
265 g_hash_node_destroy (node);
272 if (!hash_table->frozen)
273 g_hash_table_resize (hash_table);
279 g_hash_table_foreach (GHashTable *hash_table,
286 g_return_if_fail (hash_table != NULL);
287 g_return_if_fail (func != NULL);
289 for (i = 0; i < hash_table->size; i++)
290 for (node = hash_table->nodes[i]; node; node = node->next)
291 (* func) (node->key, node->value, user_data);
294 /* Returns the number of elements contained in the hash table. */
296 g_hash_table_size (GHashTable *hash_table)
298 g_return_val_if_fail (hash_table != NULL, 0);
300 return hash_table->nnodes;
304 g_hash_table_resize (GHashTable *hash_table)
306 GHashNode **new_nodes;
309 gfloat nodes_per_list;
314 nodes_per_list = (gfloat) hash_table->nnodes / (gfloat) hash_table->size;
316 if ((nodes_per_list > 0.3 || hash_table->size <= HASH_TABLE_MIN_SIZE) &&
317 (nodes_per_list < 3.0 || hash_table->size >= HASH_TABLE_MAX_SIZE))
320 new_size = CLAMP(g_spaced_primes_closest (hash_table->nnodes),
322 HASH_TABLE_MAX_SIZE);
323 new_nodes = g_new (GHashNode*, new_size);
325 for (i = 0; i < new_size; i++)
328 for (i = 0; i < hash_table->size; i++)
329 for (node = hash_table->nodes[i]; node; node = next)
332 hash_val = (* hash_table->hash_func) (node->key) % new_size;
333 node->next = new_nodes[hash_val];
334 new_nodes[hash_val] = node;
337 g_free (hash_table->nodes);
338 hash_table->nodes = new_nodes;
339 hash_table->size = new_size;
343 g_hash_node_new (gpointer key,
346 GHashNode *hash_node;
348 G_LOCK (g_hash_global);
351 hash_node = node_free_list;
352 node_free_list = node_free_list->next;
357 node_mem_chunk = g_mem_chunk_new ("hash node mem chunk",
361 hash_node = g_chunk_new (GHashNode, node_mem_chunk);
363 G_UNLOCK (g_hash_global);
365 hash_node->key = key;
366 hash_node->value = value;
367 hash_node->next = NULL;
373 g_hash_node_destroy (GHashNode *hash_node)
375 G_LOCK (g_hash_global);
376 hash_node->next = node_free_list;
377 node_free_list = hash_node;
378 G_UNLOCK (g_hash_global);
382 g_hash_nodes_destroy (GHashNode *hash_node)
394 G_LOCK (g_hash_global);
395 node->next = node_free_list;
396 node_free_list = hash_node;
397 G_UNLOCK (g_hash_global);