1 /* glxhash.c -- Small hash table support for integer -> integer mapping
4 * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
6 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
28 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
32 * This file contains a straightforward implementation of a fixed-sized
33 * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
34 * collision resolution. There are two potentially interesting things
35 * about this implementation:
37 * 1) The table is power-of-two sized. Prime sized tables are more
38 * traditional, but do not have a significant advantage over power-of-two
39 * sized table, especially when double hashing is not used for collision
42 * 2) The hash computation uses a table of random integers [Hanson97,
47 * With a table size of 512, the current implementation is sufficient for a
48 * few hundred keys. Since this is well above the expected size of the
49 * tables for which this implementation was designed, the implementation of
50 * dynamic hash tables was postponed until the need arises. A common (and
51 * naive) approach to dynamic hash table implementation simply creates a
52 * new hash table when necessary, rehashes all the data into the new table,
53 * and destroys the old table. The approach in [Larson88] is superior in
54 * two ways: 1) only a portion of the table is expanded when needed,
55 * distributing the expansion cost over several insertions, and 2) portions
56 * of the table can be locked, enabling a scalable thread-safe
61 * [Hanson97] David R. Hanson. C Interfaces and Implementations:
62 * Techniques for Creating Reusable Software. Reading, Massachusetts:
63 * Addison-Wesley, 1997.
65 * [Knuth73] Donald E. Knuth. The Art of Computer Programming. Volume 3:
66 * Sorting and Searching. Reading, Massachusetts: Addison-Wesley, 1973.
68 * [Larson88] Per-Ake Larson. "Dynamic Hash Tables". CACM 31(4), April
74 #include <X11/Xfuncproto.h>
82 #define HASH_MAGIC 0xdeadbeef
84 #define HASH_SIZE 512 /* Good for about 100 entries */
85 /* If you change this value, you probably
86 have to change the HashHash hashing
89 #define HASH_ALLOC malloc
90 #define HASH_FREE free
92 #define HASH_RANDOM_DECL char *ps, rs[256]
93 #define HASH_RANDOM_INIT(seed) ps = initstate(seed, rs, sizeof(rs))
94 #define HASH_RANDOM random()
95 #define HASH_RANDOM_DESTROY setstate(ps)
97 #define HASH_RANDOM_DECL struct random_data rd; int32_t rv; char rs[256]
98 #define HASH_RANDOM_INIT(seed) \
100 (void) memset(&rd, 0, sizeof(rd)); \
101 (void) initstate_r(seed, rs, sizeof(rs), &rd); \
103 #define HASH_RANDOM ((void) random_r(&rd, &rv), rv)
104 #define HASH_RANDOM_DESTROY
107 typedef struct __glxHashBucket
111 struct __glxHashBucket *next;
112 } __glxHashBucket, *__glxHashBucketPtr;
114 typedef struct __glxHashTable *__glxHashTablePtr;
115 struct __glxHashTable
118 unsigned long hits; /* At top of linked list */
119 unsigned long partials; /* Not at top of linked list */
120 unsigned long misses; /* Not in table */
121 __glxHashBucketPtr buckets[HASH_SIZE];
123 __glxHashBucketPtr p1;
127 HashHash(unsigned long key)
129 unsigned long hash = 0;
130 unsigned long tmp = key;
132 static unsigned long scatter[256];
137 HASH_RANDOM_INIT(37);
138 for (i = 0; i < 256; i++)
139 scatter[i] = HASH_RANDOM;
145 hash = (hash << 1) + scatter[tmp & 0xff];
151 printf("Hash(%d) = %d\n", key, hash);
156 _X_HIDDEN __glxHashTable *
157 __glxHashCreate(void)
159 __glxHashTablePtr table;
162 table = HASH_ALLOC(sizeof(*table));
165 table->magic = HASH_MAGIC;
170 for (i = 0; i < HASH_SIZE; i++)
171 table->buckets[i] = NULL;
176 __glxHashDestroy(__glxHashTable * t)
178 __glxHashTablePtr table = (__glxHashTablePtr) t;
179 __glxHashBucketPtr bucket;
180 __glxHashBucketPtr next;
183 if (table->magic != HASH_MAGIC)
184 return -1; /* Bad magic */
186 for (i = 0; i < HASH_SIZE; i++) {
187 for (bucket = table->buckets[i]; bucket;) {
197 /* Find the bucket and organize the list so that this bucket is at the
200 static __glxHashBucketPtr
201 HashFind(__glxHashTablePtr table, unsigned long key, unsigned long *h)
203 unsigned long hash = HashHash(key);
204 __glxHashBucketPtr prev = NULL;
205 __glxHashBucketPtr bucket;
210 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
211 if (bucket->key == key) {
214 prev->next = bucket->next;
215 bucket->next = table->buckets[hash];
216 table->buckets[hash] = bucket;
231 __glxHashLookup(__glxHashTable * t, unsigned long key, void **value)
233 __glxHashTablePtr table = (__glxHashTablePtr) t;
234 __glxHashBucketPtr bucket;
236 if (!table || table->magic != HASH_MAGIC)
237 return -1; /* Bad magic */
239 bucket = HashFind(table, key, NULL);
241 return 1; /* Not found */
242 *value = bucket->value;
243 return 0; /* Found */
247 __glxHashInsert(__glxHashTable * t, unsigned long key, void *value)
249 __glxHashTablePtr table = (__glxHashTablePtr) t;
250 __glxHashBucketPtr bucket;
253 if (table->magic != HASH_MAGIC)
254 return -1; /* Bad magic */
256 if (HashFind(table, key, &hash))
257 return 1; /* Already in table */
259 bucket = HASH_ALLOC(sizeof(*bucket));
261 return -1; /* Error */
263 bucket->value = value;
264 bucket->next = table->buckets[hash];
265 table->buckets[hash] = bucket;
267 printf("Inserted %d at %d/%p\n", key, hash, bucket);
269 return 0; /* Added to table */
273 __glxHashDelete(__glxHashTable * t, unsigned long key)
275 __glxHashTablePtr table = (__glxHashTablePtr) t;
277 __glxHashBucketPtr bucket;
279 if (table->magic != HASH_MAGIC)
280 return -1; /* Bad magic */
282 bucket = HashFind(table, key, &hash);
285 return 1; /* Not found */
287 table->buckets[hash] = bucket->next;
293 __glxHashNext(__glxHashTable * t, unsigned long *key, void **value)
295 __glxHashTablePtr table = (__glxHashTablePtr) t;
297 while (table->p0 < HASH_SIZE) {
299 *key = table->p1->key;
300 *value = table->p1->value;
301 table->p1 = table->p1->next;
304 table->p1 = table->buckets[table->p0];
311 __glxHashFirst(__glxHashTable * t, unsigned long *key, void **value)
313 __glxHashTablePtr table = (__glxHashTablePtr) t;
315 if (table->magic != HASH_MAGIC)
316 return -1; /* Bad magic */
319 table->p1 = table->buckets[0];
320 return __glxHashNext(table, key, value);
324 #define DIST_LIMIT 10
325 static int dist[DIST_LIMIT];
332 for (i = 0; i < DIST_LIMIT; i++)
337 count_entries(__glxHashBucketPtr bucket)
341 for (; bucket; bucket = bucket->next)
347 update_dist(int count)
349 if (count >= DIST_LIMIT)
350 ++dist[DIST_LIMIT - 1];
356 compute_dist(__glxHashTablePtr table)
359 __glxHashBucketPtr bucket;
361 printf("Hits = %ld, partials = %ld, misses = %ld\n",
362 table->hits, table->partials, table->misses);
364 for (i = 0; i < HASH_SIZE; i++) {
365 bucket = table->buckets[i];
366 update_dist(count_entries(bucket));
368 for (i = 0; i < DIST_LIMIT; i++) {
369 if (i != DIST_LIMIT - 1)
370 printf("%5d %10d\n", i, dist[i]);
372 printf("other %10d\n", dist[i]);
377 check_table(__glxHashTablePtr table, unsigned long key, unsigned long value)
379 unsigned long retval = 0;
380 int retcode = __glxHashLookup(table, key, &retval);
384 printf("Bad magic = 0x%08lx:"
385 " key = %lu, expected = %lu, returned = %lu\n",
386 table->magic, key, value, retval);
389 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
394 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
398 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
399 retcode, key, value, retval);
407 __glxHashTablePtr table;
410 printf("\n***** 256 consecutive integers ****\n");
411 table = __glxHashCreate();
412 for (i = 0; i < 256; i++)
413 __glxHashInsert(table, i, i);
414 for (i = 0; i < 256; i++)
415 check_table(table, i, i);
416 for (i = 256; i >= 0; i--)
417 check_table(table, i, i);
419 __glxHashDestroy(table);
421 printf("\n***** 1024 consecutive integers ****\n");
422 table = __glxHashCreate();
423 for (i = 0; i < 1024; i++)
424 __glxHashInsert(table, i, i);
425 for (i = 0; i < 1024; i++)
426 check_table(table, i, i);
427 for (i = 1024; i >= 0; i--)
428 check_table(table, i, i);
430 __glxHashDestroy(table);
432 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
433 table = __glxHashCreate();
434 for (i = 0; i < 1024; i++)
435 __glxHashInsert(table, i * 4096, i);
436 for (i = 0; i < 1024; i++)
437 check_table(table, i * 4096, i);
438 for (i = 1024; i >= 0; i--)
439 check_table(table, i * 4096, i);
441 __glxHashDestroy(table);
443 printf("\n***** 1024 random integers ****\n");
444 table = __glxHashCreate();
446 for (i = 0; i < 1024; i++)
447 __glxHashInsert(table, random(), i);
449 for (i = 0; i < 1024; i++)
450 check_table(table, random(), i);
452 for (i = 0; i < 1024; i++)
453 check_table(table, random(), i);
455 __glxHashDestroy(table);
457 printf("\n***** 5000 random integers ****\n");
458 table = __glxHashCreate();
460 for (i = 0; i < 5000; i++)
461 __glxHashInsert(table, random(), i);
463 for (i = 0; i < 5000; i++)
464 check_table(table, random(), i);
466 for (i = 0; i < 5000; i++)
467 check_table(table, random(), i);
469 __glxHashDestroy(table);