1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1999 The Free Software Foundation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
41 my_hash_callback_remove (gpointer key,
54 my_hash_callback_remove_test (gpointer key,
61 g_assert_not_reached ();
65 my_hash_callback (gpointer key,
74 my_hash (gconstpointer key)
76 return (guint) *((const gint*) key);
80 my_hash_compare (gconstpointer a,
83 return *((const gint*) a) == *((const gint*) b);
89 * This is a simplified version of the pathalias hashing function.
90 * Thanks to Steve Belovin and Peter Honeyman
92 * hash a string into a long int. 31 bit crc (from andrew appel).
93 * the crc table is computed at run time by crcinit() -- we could
94 * precompute, but it takes 1 clock tick on a 750.
96 * This fast table calculation works only if POLY is a prime polynomial
97 * in the field of integers modulo 2. Since the coefficients of a
98 * 32-bit polynomial won't fit in a 32-bit word, the high-order bit is
99 * implicit. IT MUST ALSO BE THE CASE that the coefficients of orders
100 * 31 down to 25 are zero. Happily, we have candidates, from
101 * E. J. Watson, "Primitive Polynomials (Mod 2)", Math. Comp. 16 (1962):
102 * x^32 + x^7 + x^5 + x^3 + x^2 + x^1 + x^0
105 * We reverse the bits to get:
106 * 111101010000000000000000000000001 but drop the last 1
108 * 010010000000000000000000000000001 ditto, for 31-bit crc
112 #define POLY 0x48000000L /* 31-bit polynomial (avoids sign problems) */
114 static guint CrcTable[128];
117 - crcinit - initialize tables for hash function
119 static void crcinit(void)
124 for (i = 0; i < 128; ++i) {
126 for (j = 7 - 1; j >= 0; --j)
134 - hash - Honeyman's nice hashing function
136 static guint honeyman_hash(gconstpointer key)
138 const gchar *name = (const gchar *) key;
142 g_assert (name != NULL);
143 g_assert (*name != 0);
148 sum = (sum >> 7) ^ CrcTable[(sum ^ (*name++)) & 0x7f];
155 static gint second_hash_cmp (gconstpointer a, gconstpointer b)
157 gint rc = (strcmp (a, b) == 0);
164 static guint one_hash(gconstpointer key)
170 static void not_even_foreach (gpointer key,
174 const char *_key = (const char *) key;
175 const char *_value = (const char *) value;
179 g_assert (_key != NULL);
180 g_assert (*_key != 0);
181 g_assert (_value != NULL);
182 g_assert (*_value != 0);
185 g_assert (atoi (_key) > 0);
187 sprintf (val, "%d value", i);
188 g_assert (strcmp (_value, val) == 0);
190 g_assert ((i % 2) != 0);
195 static gboolean remove_even_foreach (gpointer key,
199 const char *_key = (const char *) key;
200 const char *_value = (const char *) value;
204 g_assert (_key != NULL);
205 g_assert (*_key != 0);
206 g_assert (_value != NULL);
207 g_assert (*_value != 0);
212 sprintf (val, "%d value", i);
213 g_assert (strcmp (_value, val) == 0);
215 return ((i % 2) == 0) ? TRUE : FALSE;
221 static void second_hash_test (gboolean simple_hash)
224 char key[20] = "", val[20]="", *v, *orig_key, *orig_val;
230 h = g_hash_table_new (simple_hash ? one_hash : honeyman_hash,
232 g_assert (h != NULL);
235 sprintf (key, "%d", i);
236 g_assert (atoi (key) == i);
238 sprintf (val, "%d value", i);
239 g_assert (atoi (val) == i);
241 g_hash_table_insert (h, g_strdup (key), g_strdup (val));
244 g_assert (g_hash_table_size (h) == 20);
248 sprintf (key, "%d", i);
249 g_assert (atoi(key) == i);
251 v = (char *) g_hash_table_lookup (h, key);
253 g_assert (v != NULL);
255 g_assert (atoi (v) == i);
258 /**** future test stuff, yet to be debugged
259 sprintf (key, "%d", 3);
260 g_hash_table_remove (h, key);
261 g_hash_table_foreach_remove (h, remove_even_foreach, NULL);
262 g_hash_table_foreach (h, not_even_foreach, NULL);
267 if (((i % 2) == 0) || (i == 3))
270 sprintf (key, "%d", i);
271 g_assert (atoi(key) == i);
273 sprintf (val, "%d value", i);
274 g_assert (atoi (val) == i);
276 orig_key = orig_val = NULL;
277 found = g_hash_table_lookup_extended (h, key,
279 (gpointer)&orig_val);
282 g_assert (orig_key != NULL);
283 g_assert (strcmp (key, orig_key) == 0);
286 g_assert (orig_val != NULL);
287 g_assert (strcmp (val, orig_val) == 0);
291 g_hash_table_destroy (h);
295 static void direct_hash_test (void)
300 h = g_hash_table_new (NULL, NULL);
301 g_assert (h != NULL);
302 for (i=1; i<=20; i++)
304 g_hash_table_insert (h, GINT_TO_POINTER (i),
305 GINT_TO_POINTER (i + 42));
308 g_assert (g_hash_table_size (h) == 20);
310 for (i=1; i<=20; i++)
312 rc = GPOINTER_TO_INT (
313 g_hash_table_lookup (h, GINT_TO_POINTER (i)));
316 g_assert ((rc - 42) == i);
319 g_hash_table_destroy (h);
328 GHashTable *hash_table;
331 hash_table = g_hash_table_new (my_hash, my_hash_compare);
332 for (i = 0; i < 10000; i++)
335 g_hash_table_insert (hash_table, &array[i], &array[i]);
337 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
339 for (i = 0; i < 10000; i++)
341 g_assert_not_reached();
343 for (i = 0; i < 10000; i++)
344 g_hash_table_remove (hash_table, &array[i]);
346 for (i = 0; i < 10000; i++)
349 g_hash_table_insert (hash_table, &array[i], &array[i]);
352 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
353 g_hash_table_size (hash_table) != 5000)
354 g_assert_not_reached();
356 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
359 g_hash_table_destroy (hash_table);
361 second_hash_test (TRUE);
362 second_hash_test (FALSE);