1 /*-----------------------------------------------------------------------------
2 * $Id: Hash.c,v 1.1 1999/01/27 12:11:25 simonm 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 * -------------------------------------------------------------------------- */
16 #define HSEGSIZE 1024 /* Size of a single hash table segment */
17 /* Also the minimum size of a hash table */
18 #define HDIRSIZE 1024 /* Size of the segment directory */
19 /* Maximum hash table size is HSEGSIZE * HDIRSIZE */
20 #define HLOAD 5 /* Maximum average load of a single hash bucket */
22 #define HCHUNK (1024 * sizeof(W_) / sizeof(HashList))
23 /* Number of HashList cells to allocate in one go */
26 /* Linked list of (key, data) pairs for separate chaining */
30 struct hashlist *next; /* Next cell in bucket chain (same hash value) */
33 typedef struct hashlist HashList;
36 int split; /* Next bucket to split when expanding */
37 int max; /* Max bucket of smaller table */
38 int mask1; /* Mask for doing the mod of h_1 (smaller table) */
39 int mask2; /* Mask for doing the mod of h_2 (larger table) */
40 int kcount; /* Number of keys */
41 int bcount; /* Number of buckets */
42 HashList **dir[HDIRSIZE]; /* Directory of segments */
45 /* -----------------------------------------------------------------------------
46 * Hash first using the smaller table. If the bucket is less than the
47 * next bucket to be split, re-hash using the larger table.
48 * -------------------------------------------------------------------------- */
51 hash(HashTable *table, W_ key)
55 /* Strip the boring zero bits */
56 key /= sizeof(StgWord);
58 /* Mod the size of the hash table (a power of 2) */
59 bucket = key & table->mask1;
61 if (bucket < table->split) {
62 /* Mod the size of the expanded hash table (also a power of 2) */
63 bucket = key & table->mask2;
68 /* -----------------------------------------------------------------------------
69 * Allocate a new segment of the dynamically growing hash table.
70 * -------------------------------------------------------------------------- */
73 allocSegment(HashTable *table, int segment)
75 table->dir[segment] = stgMallocBytes(HSEGSIZE * sizeof(HashList *),
80 /* -----------------------------------------------------------------------------
81 * Expand the larger hash table by one bucket, and split one bucket
82 * from the smaller table into two parts. Only the bucket referenced
83 * by @table->split@ is affected by the expansion.
84 * -------------------------------------------------------------------------- */
87 expand(HashTable *table)
98 if (table->split + table->max >= HDIRSIZE * HSEGSIZE)
99 /* Wow! That's big. Too big, so don't expand. */
102 /* Calculate indices of bucket to split */
103 oldsegment = table->split / HSEGSIZE;
104 oldindex = table->split % HSEGSIZE;
106 newbucket = table->max + table->split;
108 /* And the indices of the new bucket */
109 newsegment = newbucket / HSEGSIZE;
110 newindex = newbucket % HSEGSIZE;
113 allocSegment(table, newsegment);
115 if (++table->split == table->max) {
118 table->mask1 = table->mask2;
119 table->mask2 = table->mask2 << 1 | 1;
123 /* Split the bucket, paying no attention to the original order */
126 for (hl = table->dir[oldsegment][oldindex]; hl != NULL; hl = next) {
128 if (hash(table, hl->key) == newbucket) {
136 table->dir[oldsegment][oldindex] = old;
137 table->dir[newsegment][newindex] = new;
143 lookupHashTable(HashTable *table, StgWord key)
150 bucket = hash(table, key);
151 segment = bucket / HSEGSIZE;
152 index = bucket % HSEGSIZE;
154 for (hl = table->dir[segment][index]; hl != NULL; hl = hl->next)
162 /* -----------------------------------------------------------------------------
163 * We allocate the hashlist cells in large chunks to cut down on malloc
164 * overhead. Although we keep a free list of hashlist cells, we make
165 * no effort to actually return the space to the malloc arena.
166 * -------------------------------------------------------------------------- */
168 static HashList *freeList = NULL;
175 if ((hl = freeList) != NULL) {
178 hl = stgMallocBytes(HCHUNK * sizeof(HashList), "allocHashList");
181 for (p = freeList; p < hl + HCHUNK - 1; p++)
189 freeHashList(HashList *hl)
196 insertHashTable(HashTable *table, StgWord key, void *data)
203 /* We want no duplicates */
204 ASSERT(lookupHashTable(table, key) == NULL);
206 /* When the average load gets too high, we expand the table */
207 if (++table->kcount >= HLOAD * table->bcount)
210 bucket = hash(table, key);
211 segment = bucket / HSEGSIZE;
212 index = bucket % HSEGSIZE;
214 hl = allocHashList();
218 hl->next = table->dir[segment][index];
219 table->dir[segment][index] = hl;
224 removeHashTable(HashTable *table, StgWord key, void *data)
230 HashList *prev = NULL;
232 bucket = hash(table, key);
233 segment = bucket / HSEGSIZE;
234 index = bucket % HSEGSIZE;
236 for (hl = table->dir[segment][index]; hl != NULL; hl = hl->next) {
237 if (hl->key == key && (data == NULL || hl->data == data)) {
239 table->dir[segment][index] = hl->next;
241 prev->next = hl->next;
249 ASSERT(data == NULL);
253 /* -----------------------------------------------------------------------------
254 * When we free a hash table, we are also good enough to free the
255 * data part of each (key, data) pair, as long as our caller can tell
257 * -------------------------------------------------------------------------- */
260 freeHashTable(HashTable *table, void (*freeDataFun)(void *) )
267 /* The last bucket with something in it is table->max + table->split - 1 */
268 segment = (table->max + table->split - 1) / HSEGSIZE;
269 index = (table->max + table->split - 1) % HSEGSIZE;
271 while (segment >= 0) {
273 for (hl = table->dir[segment][index]; hl != NULL; hl = next) {
275 if (freeDataFun != NULL)
276 (*freeDataFun)(hl->data);
281 free(table->dir[segment]);
283 index = HSEGSIZE - 1;
288 /* -----------------------------------------------------------------------------
289 * When we initialize a hash table, we set up the first segment as well,
290 * initializing all of the first segment's hash buckets to NULL.
291 * -------------------------------------------------------------------------- */
299 table = stgMallocBytes(sizeof(HashTable),"allocHashTable");
301 allocSegment(table, 0);
303 for (hb = table->dir[0]; hb < table->dir[0] + HSEGSIZE; hb++)
307 table->max = HSEGSIZE;
308 table->mask1 = HSEGSIZE - 1;
309 table->mask2 = 2 * HSEGSIZE - 1;
311 table->bcount = HSEGSIZE;