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>
81 #define HASH_MAGIC 0xdeadbeef
83 #define HASH_SIZE 512 /* Good for about 100 entries */
84 /* If you change this value, you probably
85 have to change the HashHash hashing
88 #define HASH_ALLOC malloc
89 #define HASH_FREE free
90 #define HASH_RANDOM_DECL
91 #define HASH_RANDOM_INIT(seed) srandom(seed)
92 #define HASH_RANDOM random()
93 #define HASH_RANDOM_DESTROY
95 typedef struct __glxHashBucket
99 struct __glxHashBucket
*next
;
100 } __glxHashBucket
, *__glxHashBucketPtr
;
102 typedef struct __glxHashTable
*__glxHashTablePtr
;
103 struct __glxHashTable
106 unsigned long hits
; /* At top of linked list */
107 unsigned long partials
; /* Not at top of linked list */
108 unsigned long misses
; /* Not in table */
109 __glxHashBucketPtr buckets
[HASH_SIZE
];
111 __glxHashBucketPtr p1
;
115 HashHash(unsigned long key
)
117 unsigned long hash
= 0;
118 unsigned long tmp
= key
;
120 static unsigned long scatter
[256];
125 HASH_RANDOM_INIT(37);
126 for (i
= 0; i
< 256; i
++)
127 scatter
[i
] = HASH_RANDOM
;
133 hash
= (hash
<< 1) + scatter
[tmp
& 0xff];
139 printf("Hash(%d) = %d\n", key
, hash
);
144 _X_HIDDEN __glxHashTable
*
145 __glxHashCreate(void)
147 __glxHashTablePtr table
;
150 table
= HASH_ALLOC(sizeof(*table
));
153 table
->magic
= HASH_MAGIC
;
158 for (i
= 0; i
< HASH_SIZE
; i
++)
159 table
->buckets
[i
] = NULL
;
164 __glxHashDestroy(__glxHashTable
* t
)
166 __glxHashTablePtr table
= (__glxHashTablePtr
) t
;
167 __glxHashBucketPtr bucket
;
168 __glxHashBucketPtr next
;
171 if (table
->magic
!= HASH_MAGIC
)
172 return -1; /* Bad magic */
174 for (i
= 0; i
< HASH_SIZE
; i
++) {
175 for (bucket
= table
->buckets
[i
]; bucket
;) {
185 /* Find the bucket and organize the list so that this bucket is at the
188 static __glxHashBucketPtr
189 HashFind(__glxHashTablePtr table
, unsigned long key
, unsigned long *h
)
191 unsigned long hash
= HashHash(key
);
192 __glxHashBucketPtr prev
= NULL
;
193 __glxHashBucketPtr bucket
;
198 for (bucket
= table
->buckets
[hash
]; bucket
; bucket
= bucket
->next
) {
199 if (bucket
->key
== key
) {
202 prev
->next
= bucket
->next
;
203 bucket
->next
= table
->buckets
[hash
];
204 table
->buckets
[hash
] = bucket
;
219 __glxHashLookup(__glxHashTable
* t
, unsigned long key
, void **value
)
221 __glxHashTablePtr table
= (__glxHashTablePtr
) t
;
222 __glxHashBucketPtr bucket
;
224 if (!table
|| table
->magic
!= HASH_MAGIC
)
225 return -1; /* Bad magic */
227 bucket
= HashFind(table
, key
, NULL
);
229 return 1; /* Not found */
230 *value
= bucket
->value
;
231 return 0; /* Found */
235 __glxHashInsert(__glxHashTable
* t
, unsigned long key
, void *value
)
237 __glxHashTablePtr table
= (__glxHashTablePtr
) t
;
238 __glxHashBucketPtr bucket
;
241 if (table
->magic
!= HASH_MAGIC
)
242 return -1; /* Bad magic */
244 if (HashFind(table
, key
, &hash
))
245 return 1; /* Already in table */
247 bucket
= HASH_ALLOC(sizeof(*bucket
));
249 return -1; /* Error */
251 bucket
->value
= value
;
252 bucket
->next
= table
->buckets
[hash
];
253 table
->buckets
[hash
] = bucket
;
255 printf("Inserted %d at %d/%p\n", key
, hash
, bucket
);
257 return 0; /* Added to table */
261 __glxHashDelete(__glxHashTable
* t
, unsigned long key
)
263 __glxHashTablePtr table
= (__glxHashTablePtr
) t
;
265 __glxHashBucketPtr bucket
;
267 if (table
->magic
!= HASH_MAGIC
)
268 return -1; /* Bad magic */
270 bucket
= HashFind(table
, key
, &hash
);
273 return 1; /* Not found */
275 table
->buckets
[hash
] = bucket
->next
;
281 __glxHashNext(__glxHashTable
* t
, unsigned long *key
, void **value
)
283 __glxHashTablePtr table
= (__glxHashTablePtr
) t
;
285 while (table
->p0
< HASH_SIZE
) {
287 *key
= table
->p1
->key
;
288 *value
= table
->p1
->value
;
289 table
->p1
= table
->p1
->next
;
292 table
->p1
= table
->buckets
[table
->p0
];
299 __glxHashFirst(__glxHashTable
* t
, unsigned long *key
, void **value
)
301 __glxHashTablePtr table
= (__glxHashTablePtr
) t
;
303 if (table
->magic
!= HASH_MAGIC
)
304 return -1; /* Bad magic */
307 table
->p1
= table
->buckets
[0];
308 return __glxHashNext(table
, key
, value
);
312 #define DIST_LIMIT 10
313 static int dist
[DIST_LIMIT
];
320 for (i
= 0; i
< DIST_LIMIT
; i
++)
325 count_entries(__glxHashBucketPtr bucket
)
329 for (; bucket
; bucket
= bucket
->next
)
335 update_dist(int count
)
337 if (count
>= DIST_LIMIT
)
338 ++dist
[DIST_LIMIT
- 1];
344 compute_dist(__glxHashTablePtr table
)
347 __glxHashBucketPtr bucket
;
349 printf("Hits = %ld, partials = %ld, misses = %ld\n",
350 table
->hits
, table
->partials
, table
->misses
);
352 for (i
= 0; i
< HASH_SIZE
; i
++) {
353 bucket
= table
->buckets
[i
];
354 update_dist(count_entries(bucket
));
356 for (i
= 0; i
< DIST_LIMIT
; i
++) {
357 if (i
!= DIST_LIMIT
- 1)
358 printf("%5d %10d\n", i
, dist
[i
]);
360 printf("other %10d\n", dist
[i
]);
365 check_table(__glxHashTablePtr table
, unsigned long key
, unsigned long value
)
367 unsigned long retval
= 0;
368 int retcode
= __glxHashLookup(table
, key
, &retval
);
372 printf("Bad magic = 0x%08lx:"
373 " key = %lu, expected = %lu, returned = %lu\n",
374 table
->magic
, key
, value
, retval
);
377 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
382 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
386 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
387 retcode
, key
, value
, retval
);
395 __glxHashTablePtr table
;
398 printf("\n***** 256 consecutive integers ****\n");
399 table
= __glxHashCreate();
400 for (i
= 0; i
< 256; i
++)
401 __glxHashInsert(table
, i
, i
);
402 for (i
= 0; i
< 256; i
++)
403 check_table(table
, i
, i
);
404 for (i
= 256; i
>= 0; i
--)
405 check_table(table
, i
, i
);
407 __glxHashDestroy(table
);
409 printf("\n***** 1024 consecutive integers ****\n");
410 table
= __glxHashCreate();
411 for (i
= 0; i
< 1024; i
++)
412 __glxHashInsert(table
, i
, i
);
413 for (i
= 0; i
< 1024; i
++)
414 check_table(table
, i
, i
);
415 for (i
= 1024; i
>= 0; i
--)
416 check_table(table
, i
, i
);
418 __glxHashDestroy(table
);
420 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
421 table
= __glxHashCreate();
422 for (i
= 0; i
< 1024; i
++)
423 __glxHashInsert(table
, i
* 4096, i
);
424 for (i
= 0; i
< 1024; i
++)
425 check_table(table
, i
* 4096, i
);
426 for (i
= 1024; i
>= 0; i
--)
427 check_table(table
, i
* 4096, i
);
429 __glxHashDestroy(table
);
431 printf("\n***** 1024 random integers ****\n");
432 table
= __glxHashCreate();
434 for (i
= 0; i
< 1024; i
++)
435 __glxHashInsert(table
, random(), i
);
437 for (i
= 0; i
< 1024; i
++)
438 check_table(table
, random(), i
);
440 for (i
= 0; i
< 1024; i
++)
441 check_table(table
, random(), i
);
443 __glxHashDestroy(table
);
445 printf("\n***** 5000 random integers ****\n");
446 table
= __glxHashCreate();
448 for (i
= 0; i
< 5000; i
++)
449 __glxHashInsert(table
, random(), i
);
451 for (i
= 0; i
< 5000; i
++)
452 check_table(table
, random(), i
);
454 for (i
= 0; i
< 5000; i
++)
455 check_table(table
, random(), i
);
457 __glxHashDestroy(table
);