1 /*-----------------------------------------------------------------------------
3 * (c) The AQUA Project, Glasgow University, 1995-1998
4 * (c) The GHC Team, 1999
6 * Dynamically expanding linear hash tables, as described in
7 * Per-\AAke Larson, ``Dynamic Hash Tables,'' CACM 31(4), April 1988,
9 * -------------------------------------------------------------------------- */
11 #include "PosixSource.h"
19 #define HSEGSIZE 1024 /* Size of a single hash table segment */
20 /* Also the minimum size of a hash table */
21 #define HDIRSIZE 1024 /* Size of the segment directory */
22 /* Maximum hash table size is HSEGSIZE * HDIRSIZE */
23 #define HLOAD 5 /* Maximum average load of a single hash bucket */
25 #define HCHUNK (1024 * sizeof(W_) / sizeof(HashList))
26 /* Number of HashList cells to allocate in one go */
29 /* Linked list of (key, data) pairs for separate chaining */
33 struct hashlist *next; /* Next cell in bucket chain (same hash value) */
36 typedef struct hashlist HashList;
38 typedef int HashFunction(HashTable *table, StgWord key);
39 typedef int CompareFunction(StgWord key1, StgWord key2);
42 int split; /* Next bucket to split when expanding */
43 int max; /* Max bucket of smaller table */
44 int mask1; /* Mask for doing the mod of h_1 (smaller table) */
45 int mask2; /* Mask for doing the mod of h_2 (larger table) */
46 int kcount; /* Number of keys */
47 int bcount; /* Number of buckets */
48 HashList **dir[HDIRSIZE]; /* Directory of segments */
49 HashFunction *hash; /* hash function */
50 CompareFunction *compare; /* key comparison function */
53 /* -----------------------------------------------------------------------------
54 * Hash first using the smaller table. If the bucket is less than the
55 * next bucket to be split, re-hash using the larger table.
56 * -------------------------------------------------------------------------- */
59 hashWord(HashTable *table, StgWord key)
63 /* Strip the boring zero bits */
64 key /= sizeof(StgWord);
66 /* Mod the size of the hash table (a power of 2) */
67 bucket = key & table->mask1;
69 if (bucket < table->split) {
70 /* Mod the size of the expanded hash table (also a power of 2) */
71 bucket = key & table->mask2;
77 hashStr(HashTable *table, char *key)
86 h = h % 1048583; /* some random large prime */
89 /* Mod the size of the hash table (a power of 2) */
90 bucket = h & table->mask1;
92 if (bucket < table->split) {
93 /* Mod the size of the expanded hash table (also a power of 2) */
94 bucket = h & table->mask2;
101 compareWord(StgWord key1, StgWord key2)
103 return (key1 == key2);
107 compareStr(StgWord key1, StgWord key2)
109 return (strcmp((char *)key1, (char *)key2) == 0);
113 /* -----------------------------------------------------------------------------
114 * Allocate a new segment of the dynamically growing hash table.
115 * -------------------------------------------------------------------------- */
118 allocSegment(HashTable *table, int segment)
120 table->dir[segment] = stgMallocBytes(HSEGSIZE * sizeof(HashList *),
125 /* -----------------------------------------------------------------------------
126 * Expand the larger hash table by one bucket, and split one bucket
127 * from the smaller table into two parts. Only the bucket referenced
128 * by @table->split@ is affected by the expansion.
129 * -------------------------------------------------------------------------- */
132 expand(HashTable *table)
143 if (table->split + table->max >= HDIRSIZE * HSEGSIZE)
144 /* Wow! That's big. Too big, so don't expand. */
147 /* Calculate indices of bucket to split */
148 oldsegment = table->split / HSEGSIZE;
149 oldindex = table->split % HSEGSIZE;
151 newbucket = table->max + table->split;
153 /* And the indices of the new bucket */
154 newsegment = newbucket / HSEGSIZE;
155 newindex = newbucket % HSEGSIZE;
158 allocSegment(table, newsegment);
160 if (++table->split == table->max) {
163 table->mask1 = table->mask2;
164 table->mask2 = table->mask2 << 1 | 1;
168 /* Split the bucket, paying no attention to the original order */
171 for (hl = table->dir[oldsegment][oldindex]; hl != NULL; hl = next) {
173 if (table->hash(table, hl->key) == newbucket) {
181 table->dir[oldsegment][oldindex] = old;
182 table->dir[newsegment][newindex] = new;
188 lookupHashTable(HashTable *table, StgWord key)
195 bucket = table->hash(table, key);
196 segment = bucket / HSEGSIZE;
197 index = bucket % HSEGSIZE;
199 for (hl = table->dir[segment][index]; hl != NULL; hl = hl->next)
200 if (table->compare(hl->key, key))
207 /* -----------------------------------------------------------------------------
208 * We allocate the hashlist cells in large chunks to cut down on malloc
209 * overhead. Although we keep a free list of hashlist cells, we make
210 * no effort to actually return the space to the malloc arena.
211 * -------------------------------------------------------------------------- */
213 static HashList *freeList = NULL;
220 if ((hl = freeList) != NULL) {
223 hl = stgMallocBytes(HCHUNK * sizeof(HashList), "allocHashList");
226 for (p = freeList; p < hl + HCHUNK - 1; p++)
234 freeHashList(HashList *hl)
241 insertHashTable(HashTable *table, StgWord key, void *data)
248 /* We want no duplicates */
249 ASSERT(lookupHashTable(table, key) == NULL);
251 /* When the average load gets too high, we expand the table */
252 if (++table->kcount >= HLOAD * table->bcount)
255 bucket = table->hash(table, key);
256 segment = bucket / HSEGSIZE;
257 index = bucket % HSEGSIZE;
259 hl = allocHashList();
263 hl->next = table->dir[segment][index];
264 table->dir[segment][index] = hl;
269 removeHashTable(HashTable *table, StgWord key, void *data)
275 HashList *prev = NULL;
277 bucket = table->hash(table, key);
278 segment = bucket / HSEGSIZE;
279 index = bucket % HSEGSIZE;
281 for (hl = table->dir[segment][index]; hl != NULL; hl = hl->next) {
282 if (table->compare(hl->key,key) && (data == NULL || hl->data == data)) {
284 table->dir[segment][index] = hl->next;
286 prev->next = hl->next;
295 ASSERT(data == NULL);
299 /* -----------------------------------------------------------------------------
300 * When we free a hash table, we are also good enough to free the
301 * data part of each (key, data) pair, as long as our caller can tell
303 * -------------------------------------------------------------------------- */
306 freeHashTable(HashTable *table, void (*freeDataFun)(void *) )
313 /* The last bucket with something in it is table->max + table->split - 1 */
314 segment = (table->max + table->split - 1) / HSEGSIZE;
315 index = (table->max + table->split - 1) % HSEGSIZE;
317 while (segment >= 0) {
319 for (hl = table->dir[segment][index]; hl != NULL; hl = next) {
321 if (freeDataFun != NULL)
322 (*freeDataFun)(hl->data);
327 stgFree(table->dir[segment]);
329 index = HSEGSIZE - 1;
334 /* -----------------------------------------------------------------------------
335 * When we initialize a hash table, we set up the first segment as well,
336 * initializing all of the first segment's hash buckets to NULL.
337 * -------------------------------------------------------------------------- */
340 allocHashTable_(HashFunction *hash, CompareFunction *compare)
345 table = stgMallocBytes(sizeof(HashTable),"allocHashTable");
347 allocSegment(table, 0);
349 for (hb = table->dir[0]; hb < table->dir[0] + HSEGSIZE; hb++)
353 table->max = HSEGSIZE;
354 table->mask1 = HSEGSIZE - 1;
355 table->mask2 = 2 * HSEGSIZE - 1;
357 table->bcount = HSEGSIZE;
359 table->compare = compare;
367 return allocHashTable_(hashWord, compareWord);
371 allocStrHashTable(void)
373 return allocHashTable_((HashFunction *)hashStr,
374 (CompareFunction *)compareStr);