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
{
98 struct __glxHashBucket
*next
;
99 } __glxHashBucket
, *__glxHashBucketPtr
;
101 typedef struct __glxHashTable
*__glxHashTablePtr
;
102 struct __glxHashTable
{
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 _X_HIDDEN __glxHashTable
*__glxHashCreate(void)
142 __glxHashTablePtr table
;
145 table
= HASH_ALLOC(sizeof(*table
));
146 if (!table
) return NULL
;
147 table
->magic
= HASH_MAGIC
;
152 for (i
= 0; i
< HASH_SIZE
; i
++) table
->buckets
[i
] = NULL
;
156 _X_HIDDEN
int __glxHashDestroy(__glxHashTable
*t
)
158 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
159 __glxHashBucketPtr bucket
;
160 __glxHashBucketPtr next
;
163 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
165 for (i
= 0; i
< HASH_SIZE
; i
++) {
166 for (bucket
= table
->buckets
[i
]; bucket
;) {
176 /* Find the bucket and organize the list so that this bucket is at the
179 static __glxHashBucketPtr
HashFind(__glxHashTablePtr table
,
180 unsigned long key
, unsigned long *h
)
182 unsigned long hash
= HashHash(key
);
183 __glxHashBucketPtr prev
= NULL
;
184 __glxHashBucketPtr bucket
;
188 for (bucket
= table
->buckets
[hash
]; bucket
; bucket
= bucket
->next
) {
189 if (bucket
->key
== key
) {
192 prev
->next
= bucket
->next
;
193 bucket
->next
= table
->buckets
[hash
];
194 table
->buckets
[hash
] = bucket
;
207 _X_HIDDEN
int __glxHashLookup(__glxHashTable
*t
,
208 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 _X_HIDDEN
int __glxHashInsert(__glxHashTable
*t
,
222 unsigned long key
, void *value
)
224 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
225 __glxHashBucketPtr bucket
;
228 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
230 if (HashFind(table
, key
, &hash
)) return 1; /* Already in table */
232 bucket
= HASH_ALLOC(sizeof(*bucket
));
233 if (!bucket
) return -1; /* Error */
235 bucket
->value
= value
;
236 bucket
->next
= table
->buckets
[hash
];
237 table
->buckets
[hash
] = bucket
;
239 printf("Inserted %d at %d/%p\n", key
, hash
, bucket
);
241 return 0; /* Added to table */
244 _X_HIDDEN
int __glxHashDelete(__glxHashTable
*t
, unsigned long key
)
246 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
248 __glxHashBucketPtr bucket
;
250 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
252 bucket
= HashFind(table
, key
, &hash
);
254 if (!bucket
) return 1; /* Not found */
256 table
->buckets
[hash
] = bucket
->next
;
261 _X_HIDDEN
int __glxHashNext(__glxHashTable
*t
,
262 unsigned long *key
, void **value
)
264 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
266 while (table
->p0
< HASH_SIZE
) {
268 *key
= table
->p1
->key
;
269 *value
= table
->p1
->value
;
270 table
->p1
= table
->p1
->next
;
273 table
->p1
= table
->buckets
[table
->p0
];
279 _X_HIDDEN
int __glxHashFirst(__glxHashTable
*t
,
280 unsigned long *key
, void **value
)
282 __glxHashTablePtr table
= (__glxHashTablePtr
)t
;
284 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
287 table
->p1
= table
->buckets
[0];
288 return __glxHashNext(table
, key
, value
);
292 #define DIST_LIMIT 10
293 static int dist
[DIST_LIMIT
];
295 static void clear_dist(void) {
298 for (i
= 0; i
< DIST_LIMIT
; i
++) dist
[i
] = 0;
301 static int count_entries(__glxHashBucketPtr bucket
)
305 for (; bucket
; bucket
= bucket
->next
) ++count
;
309 static void update_dist(int count
)
311 if (count
>= DIST_LIMIT
) ++dist
[DIST_LIMIT
-1];
315 static void compute_dist(__glxHashTablePtr table
)
318 __glxHashBucketPtr bucket
;
320 printf("Hits = %ld, partials = %ld, misses = %ld\n",
321 table
->hits
, table
->partials
, table
->misses
);
323 for (i
= 0; i
< HASH_SIZE
; i
++) {
324 bucket
= table
->buckets
[i
];
325 update_dist(count_entries(bucket
));
327 for (i
= 0; i
< DIST_LIMIT
; i
++) {
328 if (i
!= DIST_LIMIT
-1) printf("%5d %10d\n", i
, dist
[i
]);
329 else printf("other %10d\n", dist
[i
]);
333 static void check_table(__glxHashTablePtr table
,
334 unsigned long key
, unsigned long value
)
336 unsigned long retval
= 0;
337 int retcode
= __glxHashLookup(table
, key
, &retval
);
341 printf("Bad magic = 0x%08lx:"
342 " key = %lu, expected = %lu, returned = %lu\n",
343 table
->magic
, key
, value
, retval
);
346 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
351 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
355 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
356 retcode
, key
, value
, retval
);
363 __glxHashTablePtr table
;
366 printf("\n***** 256 consecutive integers ****\n");
367 table
= __glxHashCreate();
368 for (i
= 0; i
< 256; i
++) __glxHashInsert(table
, i
, i
);
369 for (i
= 0; i
< 256; i
++) check_table(table
, i
, i
);
370 for (i
= 256; i
>= 0; i
--) check_table(table
, i
, i
);
372 __glxHashDestroy(table
);
374 printf("\n***** 1024 consecutive integers ****\n");
375 table
= __glxHashCreate();
376 for (i
= 0; i
< 1024; i
++) __glxHashInsert(table
, i
, i
);
377 for (i
= 0; i
< 1024; i
++) check_table(table
, i
, i
);
378 for (i
= 1024; i
>= 0; i
--) check_table(table
, i
, i
);
380 __glxHashDestroy(table
);
382 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
383 table
= __glxHashCreate();
384 for (i
= 0; i
< 1024; i
++) __glxHashInsert(table
, i
*4096, i
);
385 for (i
= 0; i
< 1024; i
++) check_table(table
, i
*4096, i
);
386 for (i
= 1024; i
>= 0; i
--) check_table(table
, i
*4096, i
);
388 __glxHashDestroy(table
);
390 printf("\n***** 1024 random integers ****\n");
391 table
= __glxHashCreate();
393 for (i
= 0; i
< 1024; i
++) __glxHashInsert(table
, random(), i
);
395 for (i
= 0; i
< 1024; i
++) check_table(table
, random(), i
);
397 for (i
= 0; i
< 1024; i
++) check_table(table
, random(), i
);
399 __glxHashDestroy(table
);
401 printf("\n***** 5000 random integers ****\n");
402 table
= __glxHashCreate();
404 for (i
= 0; i
< 5000; i
++) __glxHashInsert(table
, random(), i
);
406 for (i
= 0; i
< 5000; i
++) check_table(table
, random(), i
);
408 for (i
= 0; i
< 5000; i
++) check_table(table
, random(), i
);
410 __glxHashDestroy(table
);