1 /*-----------------------------------------------------------------------------
2 * $Id: Hash.c,v 1.6 2001/08/14 13:40:09 sewardj Exp $
4 * (c) The AQUA Project, Glasgow University, 1995-1998
5 * (c) The GHC Team, 1999
7 * Dynamically expanding linear hash tables, as described in
8 * Per-\AAke Larson, ``Dynamic Hash Tables,'' CACM 31(4), April 1988,
10 * -------------------------------------------------------------------------- */
12 #include "PosixSource.h"
17 #define HSEGSIZE 1024 /* Size of a single hash table segment */
18 /* Also the minimum size of a hash table */
19 #define HDIRSIZE 1024 /* Size of the segment directory */
20 /* Maximum hash table size is HSEGSIZE * HDIRSIZE */
21 #define HLOAD 5 /* Maximum average load of a single hash bucket */
23 #define HCHUNK (1024 * sizeof(W_) / sizeof(HashList))
24 /* Number of HashList cells to allocate in one go */
27 /* Linked list of (key, data) pairs for separate chaining */
31 struct hashlist *next; /* Next cell in bucket chain (same hash value) */
34 typedef struct hashlist HashList;
36 typedef int HashFunction(HashTable *table, StgWord key);
37 typedef int CompareFunction(StgWord key1, StgWord key2);
40 int split; /* Next bucket to split when expanding */
41 int max; /* Max bucket of smaller table */
42 int mask1; /* Mask for doing the mod of h_1 (smaller table) */
43 int mask2; /* Mask for doing the mod of h_2 (larger table) */
44 int kcount; /* Number of keys */
45 int bcount; /* Number of buckets */
46 HashList **dir[HDIRSIZE]; /* Directory of segments */
47 HashFunction *hash; /* hash function */
48 CompareFunction *compare; /* key comparison function */
51 /* -----------------------------------------------------------------------------
52 * Hash first using the smaller table. If the bucket is less than the
53 * next bucket to be split, re-hash using the larger table.
54 * -------------------------------------------------------------------------- */
57 hashWord(HashTable *table, StgWord key)
61 /* Strip the boring zero bits */
62 key /= sizeof(StgWord);
64 /* Mod the size of the hash table (a power of 2) */
65 bucket = key & table->mask1;
67 if (bucket < table->split) {
68 /* Mod the size of the expanded hash table (also a power of 2) */
69 bucket = key & table->mask2;
75 hashStr(HashTable *table, char *key)
84 h = h % 1048583; /* some random large prime */
87 /* Mod the size of the hash table (a power of 2) */
88 bucket = h & table->mask1;
90 if (bucket < table->split) {
91 /* Mod the size of the expanded hash table (also a power of 2) */
92 bucket = h & table->mask2;
99 compareWord(StgWord key1, StgWord key2)
101 return (key1 == key2);
105 compareStr(StgWord key1, StgWord key2)
107 return (strcmp((char *)key1, (char *)key2) == 0);
111 /* -----------------------------------------------------------------------------
112 * Allocate a new segment of the dynamically growing hash table.
113 * -------------------------------------------------------------------------- */
116 allocSegment(HashTable *table, int segment)
118 table->dir[segment] = stgMallocBytes(HSEGSIZE * sizeof(HashList *),
123 /* -----------------------------------------------------------------------------
124 * Expand the larger hash table by one bucket, and split one bucket
125 * from the smaller table into two parts. Only the bucket referenced
126 * by @table->split@ is affected by the expansion.
127 * -------------------------------------------------------------------------- */
130 expand(HashTable *table)
141 if (table->split + table->max >= HDIRSIZE * HSEGSIZE)
142 /* Wow! That's big. Too big, so don't expand. */
145 /* Calculate indices of bucket to split */
146 oldsegment = table->split / HSEGSIZE;
147 oldindex = table->split % HSEGSIZE;
149 newbucket = table->max + table->split;
151 /* And the indices of the new bucket */
152 newsegment = newbucket / HSEGSIZE;
153 newindex = newbucket % HSEGSIZE;
156 allocSegment(table, newsegment);
158 if (++table->split == table->max) {
161 table->mask1 = table->mask2;
162 table->mask2 = table->mask2 << 1 | 1;
166 /* Split the bucket, paying no attention to the original order */
169 for (hl = table->dir[oldsegment][oldindex]; hl != NULL; hl = next) {
171 if (table->hash(table, hl->key) == newbucket) {
179 table->dir[oldsegment][oldindex] = old;
180 table->dir[newsegment][newindex] = new;
186 lookupHashTable(HashTable *table, StgWord key)
193 bucket = table->hash(table, key);
194 segment = bucket / HSEGSIZE;
195 index = bucket % HSEGSIZE;
197 for (hl = table->dir[segment][index]; hl != NULL; hl = hl->next)
198 if (table->compare(hl->key, key))
205 /* -----------------------------------------------------------------------------
206 * We allocate the hashlist cells in large chunks to cut down on malloc
207 * overhead. Although we keep a free list of hashlist cells, we make
208 * no effort to actually return the space to the malloc arena.
209 * -------------------------------------------------------------------------- */
211 static HashList *freeList = NULL;
218 if ((hl = freeList) != NULL) {
221 hl = stgMallocBytes(HCHUNK * sizeof(HashList), "allocHashList");
224 for (p = freeList; p < hl + HCHUNK - 1; p++)
232 freeHashList(HashList *hl)
239 insertHashTable(HashTable *table, StgWord key, void *data)
246 /* We want no duplicates */
247 ASSERT(lookupHashTable(table, key) == NULL);
249 /* When the average load gets too high, we expand the table */
250 if (++table->kcount >= HLOAD * table->bcount)
253 bucket = table->hash(table, key);
254 segment = bucket / HSEGSIZE;
255 index = bucket % HSEGSIZE;
257 hl = allocHashList();
261 hl->next = table->dir[segment][index];
262 table->dir[segment][index] = hl;
267 removeHashTable(HashTable *table, StgWord key, void *data)
273 HashList *prev = NULL;
275 bucket = table->hash(table, key);
276 segment = bucket / HSEGSIZE;
277 index = bucket % HSEGSIZE;
279 for (hl = table->dir[segment][index]; hl != NULL; hl = hl->next) {
280 if (table->compare(hl->key,key) && (data == NULL || hl->data == data)) {
282 table->dir[segment][index] = hl->next;
284 prev->next = hl->next;
292 ASSERT(data == NULL);
296 /* -----------------------------------------------------------------------------
297 * When we free a hash table, we are also good enough to free the
298 * data part of each (key, data) pair, as long as our caller can tell
300 * -------------------------------------------------------------------------- */
303 freeHashTable(HashTable *table, void (*freeDataFun)(void *) )
310 /* The last bucket with something in it is table->max + table->split - 1 */
311 segment = (table->max + table->split - 1) / HSEGSIZE;
312 index = (table->max + table->split - 1) % HSEGSIZE;
314 while (segment >= 0) {
316 for (hl = table->dir[segment][index]; hl != NULL; hl = next) {
318 if (freeDataFun != NULL)
319 (*freeDataFun)(hl->data);
324 free(table->dir[segment]);
326 index = HSEGSIZE - 1;
331 /* -----------------------------------------------------------------------------
332 * When we initialize a hash table, we set up the first segment as well,
333 * initializing all of the first segment's hash buckets to NULL.
334 * -------------------------------------------------------------------------- */
337 allocHashTable_(HashFunction *hash, CompareFunction *compare)
342 table = stgMallocBytes(sizeof(HashTable),"allocHashTable");
344 allocSegment(table, 0);
346 for (hb = table->dir[0]; hb < table->dir[0] + HSEGSIZE; hb++)
350 table->max = HSEGSIZE;
351 table->mask1 = HSEGSIZE - 1;
352 table->mask2 = 2 * HSEGSIZE - 1;
354 table->bcount = HSEGSIZE;
356 table->compare = compare;
364 return allocHashTable_(hashWord, compareWord);
368 allocStrHashTable(void)
370 return allocHashTable_((HashFunction *)hashStr,
371 (CompareFunction *)compareStr);