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.
22 #define HASH_TABLE_MIN_SIZE 11
23 #define HASH_TABLE_MAX_SIZE 13845163
26 typedef struct _GHashNode GHashNode;
42 GCompareFunc key_compare_func;
46 static void g_hash_table_resize (GHashTable *hash_table);
47 static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
49 static GHashNode* g_hash_node_new (gpointer key,
51 static void g_hash_node_destroy (GHashNode *hash_node);
52 static void g_hash_nodes_destroy (GHashNode *hash_node);
55 static GMemChunk *node_mem_chunk = NULL;
56 static GHashNode *node_free_list = NULL;
60 g_hash_table_new (GHashFunc hash_func,
61 GCompareFunc key_compare_func)
63 GHashTable *hash_table;
66 hash_table = g_new (GHashTable, 1);
67 hash_table->size = HASH_TABLE_MIN_SIZE;
68 hash_table->nnodes = 0;
69 hash_table->frozen = FALSE;
70 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
71 hash_table->key_compare_func = key_compare_func;
72 hash_table->nodes = g_new (GHashNode*, hash_table->size);
74 for (i = 0; i < hash_table->size; i++)
75 hash_table->nodes[i] = NULL;
81 g_hash_table_destroy (GHashTable *hash_table)
85 g_return_if_fail (hash_table != NULL);
87 for (i = 0; i < hash_table->size; i++)
88 g_hash_nodes_destroy (hash_table->nodes[i]);
90 g_free (hash_table->nodes);
94 static inline GHashNode**
95 g_hash_table_lookup_node (GHashTable *hash_table,
100 node = &hash_table->nodes
101 [(* hash_table->hash_func) (key) % hash_table->size];
103 /* Hash table lookup needs to be fast.
104 * We therefore remove the extra conditional of testing
105 * whether to call the key_compare_func or not from
108 if (hash_table->key_compare_func)
109 while (*node && !(*hash_table->key_compare_func) ((*node)->key, key))
110 node = &(*node)->next;
112 while (*node && (*node)->key != key)
113 node = &(*node)->next;
119 g_hash_table_lookup (GHashTable *hash_table,
124 g_return_val_if_fail (hash_table != NULL, NULL);
126 node = *g_hash_table_lookup_node (hash_table, key);
128 return node ? node->value : NULL;
132 g_hash_table_insert (GHashTable *hash_table,
138 g_return_if_fail (hash_table != NULL);
140 node = g_hash_table_lookup_node (hash_table, key);
144 /* do not reset node->key in this place, keeping
145 * the old key might be intended.
146 * a g_hash_table_remove/g_hash_table_insert pair
147 * can be used otherwise.
149 * node->key = key; */
150 (*node)->value = value;
154 *node = g_hash_node_new (key, value);
155 hash_table->nnodes++;
156 if (!hash_table->frozen)
157 g_hash_table_resize (hash_table);
162 g_hash_table_remove (GHashTable *hash_table,
165 GHashNode **node, *dest;
167 g_return_if_fail (hash_table != NULL);
169 while (*(node = g_hash_table_lookup_node (hash_table, key)))
172 (*node) = dest->next;
173 g_hash_node_destroy (dest);
174 hash_table->nnodes--;
177 if (!hash_table->frozen)
178 g_hash_table_resize (hash_table);
182 g_hash_table_lookup_extended (GHashTable *hash_table,
183 gconstpointer lookup_key,
189 g_return_val_if_fail (hash_table != NULL, FALSE);
191 node = *g_hash_table_lookup_node (hash_table, lookup_key);
196 *orig_key = node->key;
198 *value = node->value;
206 g_hash_table_freeze (GHashTable *hash_table)
208 g_return_if_fail (hash_table != NULL);
210 hash_table->frozen++;
214 g_hash_table_thaw (GHashTable *hash_table)
216 g_return_if_fail (hash_table != NULL);
218 if (hash_table->frozen)
219 if (!(--hash_table->frozen))
220 g_hash_table_resize (hash_table);
224 g_hash_table_foreach_remove (GHashTable *hash_table,
228 GHashNode *node, *prev;
232 g_return_val_if_fail (hash_table != NULL, 0);
233 g_return_val_if_fail (func != NULL, 0);
235 for (i = 0; i < hash_table->size; i++)
241 for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
243 if ((* func) (node->key, node->value, user_data))
247 hash_table->nnodes -= 1;
251 prev->next = node->next;
252 g_hash_node_destroy (node);
257 hash_table->nodes[i] = node->next;
258 g_hash_node_destroy (node);
265 if (!hash_table->frozen)
266 g_hash_table_resize (hash_table);
272 g_hash_table_foreach (GHashTable *hash_table,
279 g_return_if_fail (hash_table != NULL);
280 g_return_if_fail (func != NULL);
282 for (i = 0; i < hash_table->size; i++)
283 for (node = hash_table->nodes[i]; node; node = node->next)
284 (* func) (node->key, node->value, user_data);
287 /* Returns the number of elements contained in the hash table. */
289 g_hash_table_size (GHashTable *hash_table)
291 g_return_val_if_fail (hash_table != NULL, 0);
293 return hash_table->nnodes;
297 g_hash_table_resize (GHashTable *hash_table)
299 GHashNode **new_nodes;
302 gfloat nodes_per_list;
307 nodes_per_list = (gfloat) hash_table->nnodes / (gfloat) hash_table->size;
309 if ((nodes_per_list > 0.3 || hash_table->size <= HASH_TABLE_MIN_SIZE) &&
310 (nodes_per_list < 3.0 || hash_table->size >= HASH_TABLE_MAX_SIZE))
313 new_size = CLAMP(g_spaced_primes_closest (hash_table->nnodes),
315 HASH_TABLE_MAX_SIZE);
316 new_nodes = g_new (GHashNode*, new_size);
318 for (i = 0; i < new_size; i++)
321 for (i = 0; i < hash_table->size; i++)
322 for (node = hash_table->nodes[i]; node; node = next)
325 hash_val = (* hash_table->hash_func) (node->key) % new_size;
326 node->next = new_nodes[hash_val];
327 new_nodes[hash_val] = node;
330 g_free (hash_table->nodes);
331 hash_table->nodes = new_nodes;
332 hash_table->size = new_size;
336 g_hash_node_new (gpointer key,
339 GHashNode *hash_node;
343 hash_node = node_free_list;
344 node_free_list = node_free_list->next;
349 node_mem_chunk = g_mem_chunk_new ("hash node mem chunk",
353 hash_node = g_chunk_new (GHashNode, node_mem_chunk);
356 hash_node->key = key;
357 hash_node->value = value;
358 hash_node->next = NULL;
364 g_hash_node_destroy (GHashNode *hash_node)
366 hash_node->next = node_free_list;
367 node_free_list = hash_node;
371 g_hash_nodes_destroy (GHashNode *hash_node)
383 node->next = node_free_list;
384 node_free_list = hash_node;