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 gint g_hash_closest_prime (gint num);
50 static GHashNode* g_hash_node_new (gpointer key,
52 static void g_hash_node_destroy (GHashNode *hash_node);
53 static void g_hash_nodes_destroy (GHashNode *hash_node);
56 static GMemChunk *node_mem_chunk = NULL;
57 static GHashNode *node_free_list = NULL;
61 g_hash_table_new (GHashFunc hash_func,
62 GCompareFunc key_compare_func)
64 GHashTable *hash_table;
67 hash_table = g_new (GHashTable, 1);
68 hash_table->size = HASH_TABLE_MIN_SIZE;
69 hash_table->nnodes = 0;
70 hash_table->frozen = FALSE;
71 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
72 hash_table->key_compare_func = key_compare_func;
73 hash_table->nodes = g_new (GHashNode*, hash_table->size);
75 for (i = 0; i < hash_table->size; i++)
76 hash_table->nodes[i] = NULL;
82 g_hash_table_destroy (GHashTable *hash_table)
86 g_return_if_fail (hash_table);
88 for (i = 0; i < hash_table->size; i++)
89 g_hash_nodes_destroy (hash_table->nodes[i]);
91 g_free (hash_table->nodes);
96 g_hash_table_insert (GHashTable *hash_table,
102 g_return_if_fail (hash_table);
104 node = g_hash_table_lookup_node (hash_table, key);
108 /* do not reset node->key in this place, keeping
109 * the old key might be intended.
110 * a g_hash_table_remove/g_hash_table_insert pair
111 * can be used otherwise.
113 * node->key = key; */
114 (*node)->value = value;
118 *node = g_hash_node_new (key, value);
119 hash_table->nnodes++;
120 if (!hash_table->frozen)
121 g_hash_table_resize (hash_table);
126 g_hash_table_remove (GHashTable *hash_table,
129 GHashNode **node, *dest;
131 g_return_if_fail (hash_table);
133 while (*(node = g_hash_table_lookup_node (hash_table, key)))
136 (*node) = dest->next;
137 g_hash_node_destroy (dest);
138 hash_table->nnodes--;
140 if (!hash_table->frozen)
141 g_hash_table_resize (hash_table);
145 g_hash_table_lookup (GHashTable *hash_table,
150 g_return_val_if_fail (hash_table, NULL);
152 node = *g_hash_table_lookup_node (hash_table, key);
153 return node ? node->value : NULL;
157 g_hash_table_lookup_extended (GHashTable *hash_table,
158 gconstpointer lookup_key,
164 g_return_val_if_fail (hash_table, FALSE);
166 node = *g_hash_table_lookup_node (hash_table, lookup_key);
171 *orig_key = node->key;
173 *value = node->value;
181 g_hash_table_freeze (GHashTable *hash_table)
183 g_return_if_fail (hash_table);
185 hash_table->frozen = TRUE;
189 g_hash_table_thaw (GHashTable *hash_table)
191 g_return_if_fail (hash_table);
193 hash_table->frozen = FALSE;
195 g_hash_table_resize (hash_table);
199 g_hash_table_foreach (GHashTable *hash_table,
206 g_return_if_fail (hash_table);
208 for (i = 0; i < hash_table->size; i++)
209 for (node = hash_table->nodes[i]; node; node = node->next)
210 (* func) (node->key, node->value, user_data);
213 /* Returns the number of elements contained in the hash table. */
214 gint g_hash_table_size (GHashTable *hash_table)
216 g_return_val_if_fail (hash_table, 0);
218 return hash_table->nnodes;
222 g_hash_table_resize (GHashTable *hash_table)
224 GHashNode **new_nodes;
227 gfloat nodes_per_list;
232 g_return_if_fail (hash_table);
234 nodes_per_list = (gfloat) hash_table->nnodes / (gfloat) hash_table->size;
236 if ((nodes_per_list > 0.3 || hash_table->size <= HASH_TABLE_MIN_SIZE) &&
237 (nodes_per_list < 3.0 || hash_table->size >= HASH_TABLE_MAX_SIZE))
240 new_size = CLAMP(g_hash_closest_prime (hash_table->nnodes),
242 HASH_TABLE_MAX_SIZE);
243 new_nodes = g_new (GHashNode*, new_size);
245 for (i = 0; i < new_size; i++)
248 for (i = 0; i < hash_table->size; i++)
249 for (node = hash_table->nodes[i]; node; node = next)
252 hash_val = (* hash_table->hash_func) (node->key) % new_size;
253 node->next = new_nodes[hash_val];
254 new_nodes[hash_val] = node;
257 g_free (hash_table->nodes);
258 hash_table->nodes = new_nodes;
259 hash_table->size = new_size;
263 g_hash_table_lookup_node (GHashTable *hash_table,
268 g_return_val_if_fail (hash_table, NULL);
270 node = &hash_table->nodes
271 [(* hash_table->hash_func) (key) % hash_table->size];
273 /* Hash table lookup needs to be fast.
274 * We therefore remove the extra conditional of testing
275 * whether to call the key_compare_func or not from
278 if (hash_table->key_compare_func)
279 while (*node && !(*hash_table->key_compare_func) ((*node)->key, key))
280 node = &(*node)->next;
282 while (*node && (*node)->key != key)
283 node = &(*node)->next;
289 g_hash_closest_prime (gint num)
291 extern gint g_primes[];
292 extern gint g_nprimes;
295 for (i = 0; i < g_nprimes; i++)
296 if ((g_primes[i] - num) > 0)
299 return g_primes[g_nprimes - 1];
303 g_hash_node_new (gpointer key,
306 GHashNode *hash_node;
310 hash_node = node_free_list;
311 node_free_list = node_free_list->next;
316 node_mem_chunk = g_mem_chunk_new ("hash node mem chunk",
320 hash_node = g_chunk_new (GHashNode, node_mem_chunk);
323 hash_node->key = key;
324 hash_node->value = value;
325 hash_node->next = NULL;
331 g_hash_node_destroy (GHashNode *hash_node)
333 g_return_if_fail (hash_node);
335 hash_node->next = node_free_list;
336 node_free_list = hash_node;
340 g_hash_nodes_destroy (GHashNode *hash_node)
352 node->next = node_free_list;
353 node_free_list = hash_node;