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
80 #define HASH_MAGIC 0xdeadbeef
82 #define HASH_SIZE 512 /* Good for about 100 entries */
83 /* If you change this value, you probably
84 have to change the HashHash hashing
87 #define HASH_ALLOC malloc
88 #define HASH_FREE free
89 #define HASH_RANDOM_DECL
90 #define HASH_RANDOM_INIT(seed) srandom(seed)
91 #define HASH_RANDOM random()
92 #define HASH_RANDOM_DESTROY
94 typedef struct __glxHashBucket
{
97 struct __glxHashBucket
*next
;
98 } __glxHashBucket
, *__glxHashBucketPtr
;
100 typedef struct __glxHashTable
*__glxHashTablePtr
;
101 struct __glxHashTable
{
103 unsigned long entries
;
104 unsigned long hits
; /* At top of linked list */
105 unsigned long partials
; /* Not at top of linked list */
106 unsigned long misses
; /* Not in table */
107 __glxHashBucketPtr buckets
[HASH_SIZE
];
109 __glxHashBucketPtr p1
;
112 static unsigned long HashHash(unsigned long key
)
114 unsigned long hash
= 0;
115 unsigned long tmp
= key
;
117 static unsigned long scatter
[256];
122 HASH_RANDOM_INIT(37);
123 for (i
= 0; i
< 256; i
++) scatter
[i
] = HASH_RANDOM
;
129 hash
= (hash
<< 1) + scatter
[tmp
& 0xff];
135 printf( "Hash(%d) = %d\n", key
, hash
);
140 __glxHashTable
*__glxHashCreate(void)
142 __glxHashTablePtr table
;
145 table
= HASH_ALLOC(sizeof(*table
));
146 if (!table
) return NULL
;
147 table
->magic
= HASH_MAGIC
;
153 for (i
= 0; i
< HASH_SIZE
; i
++) table
->buckets
[i
] = NULL
;
157 int __glxHashDestroy(__glxHashTable
*t
)
159 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
160 __glxHashBucketPtr bucket
;
161 __glxHashBucketPtr next
;
164 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
166 for (i
= 0; i
< HASH_SIZE
; i
++) {
167 for (bucket
= table
->buckets
[i
]; bucket
;) {
177 /* Find the bucket and organize the list so that this bucket is at the
180 static __glxHashBucketPtr
HashFind(__glxHashTablePtr table
,
181 unsigned long key
, unsigned long *h
)
183 unsigned long hash
= HashHash(key
);
184 __glxHashBucketPtr prev
= NULL
;
185 __glxHashBucketPtr bucket
;
189 for (bucket
= table
->buckets
[hash
]; bucket
; bucket
= bucket
->next
) {
190 if (bucket
->key
== key
) {
193 prev
->next
= bucket
->next
;
194 bucket
->next
= table
->buckets
[hash
];
195 table
->buckets
[hash
] = bucket
;
208 int __glxHashLookup(__glxHashTable
*t
, unsigned long key
, void **value
)
210 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
211 __glxHashBucketPtr bucket
;
213 if (!table
|| table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
215 bucket
= HashFind(table
, key
, NULL
);
216 if (!bucket
) return 1; /* Not found */
217 *value
= bucket
->value
;
218 return 0; /* Found */
221 int __glxHashInsert(__glxHashTable
*t
, unsigned long key
, void *value
)
223 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
224 __glxHashBucketPtr bucket
;
227 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
229 if (HashFind(table
, key
, &hash
)) return 1; /* Already in table */
231 bucket
= HASH_ALLOC(sizeof(*bucket
));
232 if (!bucket
) return -1; /* Error */
234 bucket
->value
= value
;
235 bucket
->next
= table
->buckets
[hash
];
236 table
->buckets
[hash
] = bucket
;
238 printf("Inserted %d at %d/%p\n", key
, hash
, bucket
);
240 return 0; /* Added to table */
243 int __glxHashDelete(__glxHashTable
*t
, unsigned long key
)
245 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
247 __glxHashBucketPtr bucket
;
249 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
251 bucket
= HashFind(table
, key
, &hash
);
253 if (!bucket
) return 1; /* Not found */
255 table
->buckets
[hash
] = bucket
->next
;
260 int __glxHashNext(__glxHashTable
*t
, unsigned long *key
, void **value
)
262 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
264 while (table
->p0
< HASH_SIZE
) {
266 *key
= table
->p1
->key
;
267 *value
= table
->p1
->value
;
268 table
->p1
= table
->p1
->next
;
271 table
->p1
= table
->buckets
[table
->p0
];
277 int __glxHashFirst(__glxHashTable
*t
, unsigned long *key
, void **value
)
279 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
281 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
284 table
->p1
= table
->buckets
[0];
285 return __glxHashNext(table
, key
, value
);
289 #define DIST_LIMIT 10
290 static int dist
[DIST_LIMIT
];
292 static void clear_dist(void) {
295 for (i
= 0; i
< DIST_LIMIT
; i
++) dist
[i
] = 0;
298 static int count_entries(__glxHashBucketPtr bucket
)
302 for (; bucket
; bucket
= bucket
->next
) ++count
;
306 static void update_dist(int count
)
308 if (count
>= DIST_LIMIT
) ++dist
[DIST_LIMIT
-1];
312 static void compute_dist(__glxHashTablePtr table
)
315 __glxHashBucketPtr bucket
;
317 printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
318 table
->entries
, table
->hits
, table
->partials
, table
->misses
);
320 for (i
= 0; i
< HASH_SIZE
; i
++) {
321 bucket
= table
->buckets
[i
];
322 update_dist(count_entries(bucket
));
324 for (i
= 0; i
< DIST_LIMIT
; i
++) {
325 if (i
!= DIST_LIMIT
-1) printf("%5d %10d\n", i
, dist
[i
]);
326 else printf("other %10d\n", dist
[i
]);
330 static void check_table(__glxHashTablePtr table
,
331 unsigned long key
, unsigned long value
)
333 unsigned long retval
= 0;
334 int retcode
= __glxHashLookup(table
, key
, &retval
);
338 printf("Bad magic = 0x%08lx:"
339 " key = %lu, expected = %lu, returned = %lu\n",
340 table
->magic
, key
, value
, retval
);
343 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
348 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
352 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
353 retcode
, key
, value
, retval
);
360 __glxHashTablePtr table
;
363 printf("\n***** 256 consecutive integers ****\n");
364 table
= __glxHashCreate();
365 for (i
= 0; i
< 256; i
++) __glxHashInsert(table
, i
, i
);
366 for (i
= 0; i
< 256; i
++) check_table(table
, i
, i
);
367 for (i
= 256; i
>= 0; i
--) check_table(table
, i
, i
);
369 __glxHashDestroy(table
);
371 printf("\n***** 1024 consecutive integers ****\n");
372 table
= __glxHashCreate();
373 for (i
= 0; i
< 1024; i
++) __glxHashInsert(table
, i
, i
);
374 for (i
= 0; i
< 1024; i
++) check_table(table
, i
, i
);
375 for (i
= 1024; i
>= 0; i
--) check_table(table
, i
, i
);
377 __glxHashDestroy(table
);
379 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
380 table
= __glxHashCreate();
381 for (i
= 0; i
< 1024; i
++) __glxHashInsert(table
, i
*4096, i
);
382 for (i
= 0; i
< 1024; i
++) check_table(table
, i
*4096, i
);
383 for (i
= 1024; i
>= 0; i
--) check_table(table
, i
*4096, i
);
385 __glxHashDestroy(table
);
387 printf("\n***** 1024 random integers ****\n");
388 table
= __glxHashCreate();
390 for (i
= 0; i
< 1024; i
++) __glxHashInsert(table
, random(), i
);
392 for (i
= 0; i
< 1024; i
++) check_table(table
, random(), i
);
394 for (i
= 0; i
< 1024; i
++) check_table(table
, random(), i
);
396 __glxHashDestroy(table
);
398 printf("\n***** 5000 random integers ****\n");
399 table
= __glxHashCreate();
401 for (i
= 0; i
< 5000; i
++) __glxHashInsert(table
, random(), i
);
403 for (i
= 0; i
< 5000; i
++) check_table(table
, random(), i
);
405 for (i
= 0; i
< 5000; i
++) check_table(table
, random(), i
);
407 __glxHashDestroy(table
);