3 * File name: ir/adt/set.c
4 * Purpose: Set --- collection of entries that are unique wrt to a key.
5 * Author: Markus Armbruster
7 * Created: 1999 by getting from fiasco
9 * Copyright: (c) 1995, 1996 Markus Armbruster
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
13 /* This code is derived from:
15 From: ejp@ausmelb.oz.AU (Esmond Pitt)
16 Date: Tue, 7 Mar 1989 22:06:26 GMT
17 Subject: v06i042: dynamic hashing version of hsearch(3)
18 Message-ID: <1821@basser.oz>
19 Newsgroups: comp.sources.misc
20 Sender: msgs@basser.oz
22 Posting-number: Volume 6, Issue 42
23 Submitted-By: Esmond Pitt <ejp@ausmelb.oz.AU>
24 Archive-name: dynamic-hash
26 * Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson.
27 * Coded into C, with minor code improvements, and with hsearch(3) interface,
28 * by ejp@ausmelb.oz, Jul 26, 1988: 13:16;
30 TODO: Fix Esmond's ugly MixedCapsIdentifiers ;->
39 /* bcopy is not ISO C *
40 #define bcopy(X, Y, Z) memcpy((Y), (X), (Z))
45 # define PMANGLE(pre) pre##_pset
46 # define MANGLEP(post) pset_##post
47 # define MANGLE(pre, post) pre##pset##post
48 # define EQUAL(cmp, elt, key, siz) (!(cmp) ((elt)->entry.dptr, (key)))
51 # define PMANGLE(pre) pre##_set
52 # define MANGLEP(post) set_##post
53 # define MANGLE(pre, post) pre##set##post
54 # define EQUAL(cmp, elt, key, siz) \
55 (((elt)->entry.size == (siz)) && !(cmp) ((elt)->entry.dptr, (key), (siz)))
70 #define TOBSTACK_ID MANGLEP(tag)
74 #define SEGMENT_SIZE_SHIFT 8
75 #define SEGMENT_SIZE (1 << SEGMENT_SIZE_SHIFT)
76 #define DIRECTORY_SIZE_SHIFT 8
77 #define DIRECTORY_SIZE (1 << DIRECTORY_SIZE_SHIFT)
78 #define MAX_LOAD_FACTOR 4
81 typedef struct element {
82 struct element *chain; /* for chaining Elements */
83 MANGLEP (entry) entry;
88 short p; /* Next bucket to be split */
89 short maxp; /* upper bound on p during expansion */
90 int nkey; /* current # keys */
91 short nseg; /* current # segments */
92 Segment *dir[DIRECTORY_SIZE];
93 MANGLEP(cmp_fun) cmp; /* function comparing entries */
95 Element *iter_tail; /* non-NULL while iterating over elts */
97 Element *free_list; /* list of free Elements */
99 struct obstack obst; /* obstack for allocation all data */
101 int naccess, ncollision, ndups;
113 MANGLEP(stats) (SET *table)
117 Element *q = table->free_list;
118 while (q) { q = q->chain; ++nfree; }
120 printf (" accesses collisions keys duplicates longest wasted\n%12d%12d%12d%12d%12d%12d\n",
121 table->naccess, table->ncollision, table->nkey, table->ndups, table->max_chain_len, nfree);
125 stat_chain_len (SET *table, int chain_len)
127 table->ncollision += chain_len;
128 if (table->max_chain_len < chain_len) table->max_chain_len = chain_len;
131 # define stat_access(table) (++(table)->naccess)
132 # define stat_dup(table) (++(table)->ndups)
136 # define stat_chain_len(table, chain_len) ((void)0)
137 # define stat_access(table) ((void)0)
138 # define stat_dup(table) ((void)0)
144 const char *MANGLEP(tag);
148 MANGLEP(describe) (SET *table)
154 printf ("p=%d maxp=%d nkey=%d nseg=%d\n",
155 table->p, table->maxp, table->nkey, table->nseg);
156 for (i = 0; i < table->nseg; i++) {
158 for (j = 0; j < SEGMENT_SIZE; j++) {
162 if (collide) printf ("<%3d>", collide);
163 else printf ("table");
164 printf ("[%d][%3d]: %u %p\n", i, j, ptr->entry.hash, ptr->entry.dptr);
176 (PMANGLE(new)) (MANGLEP(cmp_fun) cmp, int nslots)
179 SET *table = xmalloc (sizeof (SET));
181 /* Adjust nslots up to next power of 2, minimum SEGMENT_SIZE */
182 assert (nslots >= 0);
183 for (i = SEGMENT_SIZE; i < nslots; i <<= 1) assert (i < (i << 1));
184 nslots = i >> SEGMENT_SIZE_SHIFT;
186 table->nseg = table->p = table->nkey = 0;
187 table->maxp = nslots << SEGMENT_SIZE_SHIFT;
189 table->iter_tail = NULL;
191 table->free_list = NULL;
193 obstack_init (&table->obst);
196 for (i = 0; i < nslots; ++i) {
197 table->dir[i] = (Segment *)obstack_alloc (&table->obst,
198 sizeof (Segment) * SEGMENT_SIZE);
200 memset(table->dir[i], 0, sizeof (Segment) * SEGMENT_SIZE);
205 table->naccess = table->ncollision = table->ndups = 0;
206 table->max_chain_len = 0;
209 table->tag = MANGLEP(tag);
216 PMANGLE(del) (SET *table)
219 MANGLEP(tag) = table->tag;
221 obstack_free (&table->obst, NULL);
226 MANGLEP(count) (SET *table)
232 * do one iteration step, return 1
233 * if still data in the set, 0 else
236 iter_step (SET *table)
238 if (++table->iter_j >= SEGMENT_SIZE) {
240 if (++table->iter_i >= table->nseg) {
249 * finds the first entry in the table
252 MANGLEP(first) (SET *table)
254 assert (!table->iter_tail);
257 while (!table->dir[table->iter_i][table->iter_j]) {
258 if (!iter_step (table)) return NULL;
260 table->iter_tail = table->dir[table->iter_i][table->iter_j];
261 assert (table->iter_tail->entry.dptr);
262 return table->iter_tail->entry.dptr;
266 * returns next entry in the table
269 MANGLEP(next) (SET *table)
271 if (!table->iter_tail)
274 /* follow collision chain */
275 table->iter_tail = table->iter_tail->chain;
276 if (!table->iter_tail) {
277 /* go to next segment */
279 if (!iter_step (table)) return NULL;
280 } while (!table->dir[table->iter_i][table->iter_j]);
281 table->iter_tail = table->dir[table->iter_i][table->iter_j];
283 assert (table->iter_tail->entry.dptr);
284 return table->iter_tail->entry.dptr;
288 MANGLEP(break) (SET *table)
290 table->iter_tail = NULL;
294 * limit the hash value
296 static INLINE unsigned
297 Hash (SET *table, unsigned h)
301 address = h & (table->maxp - 1); /* h % table->maxp */
302 if (address < (unsigned)table->p)
303 address = h & ((table->maxp << 1) - 1); /* h % (2*table->maxp) */
308 * returns non-zero if the number of elements in
309 * the set is greater then number of segments * MAX_LOAD_FACTOR
314 return ( ++table->nkey
315 > (table->nseg << SEGMENT_SIZE_SHIFT) * MAX_LOAD_FACTOR);
319 * expand the hash-table: the algorithm is split, so on every
320 * insert, only ONE segment is rehashed!
322 * table->p contains the current segment to split
323 * after all segments were split, table->p is set to zero and
324 * table->maxp is duplicated.
327 expand_table (SET *table)
330 int OldSegmentIndex, NewSegmentIndex;
331 int OldSegmentDir, NewSegmentDir;
338 if (table->maxp + table->p < (DIRECTORY_SIZE << SEGMENT_SIZE_SHIFT)) {
339 /* Locate the bucket to be split */
340 OldSegmentDir = table->p >> SEGMENT_SIZE_SHIFT;
341 OldSegment = table->dir[OldSegmentDir];
342 OldSegmentIndex = table->p & (SEGMENT_SIZE-1);
344 /* Expand address space; if necessary create a new segment */
345 NewAddress = table->maxp + table->p;
346 NewSegmentDir = NewAddress >> SEGMENT_SIZE_SHIFT;
347 NewSegmentIndex = NewAddress & (SEGMENT_SIZE-1);
348 if (NewSegmentIndex == 0) {
349 table->dir[NewSegmentDir] =
350 (Segment *)obstack_alloc (&table->obst,
351 sizeof(Segment) * SEGMENT_SIZE);
352 memset(table->dir[NewSegmentDir], 0, sizeof(Segment) * SEGMENT_SIZE);
355 NewSegment = table->dir[NewSegmentDir];
357 /* Adjust state variables */
359 if (table->p == table->maxp) {
360 table->maxp <<= 1; /* table->maxp *= 2 */
364 /* Relocate records to the new bucket */
365 Previous = &OldSegment[OldSegmentIndex];
367 LastOfNew = &NewSegment[NewSegmentIndex];
369 while (Current != NULL) {
370 if (Hash (table, Current->entry.hash) == NewAddress) {
371 /* move to new chain */
372 *LastOfNew = Current;
373 *Previous = Current->chain;
374 LastOfNew = &Current->chain;
375 Current = Current->chain;
378 /* leave on old chain */
379 Previous = &Current->chain;
380 Current = Current->chain;
388 MANGLE(_,_search) (SET *table,
394 MANGLE(_,_action) action)
397 Segment *CurrentSegment;
399 MANGLEP(cmp_fun) cmp = table->cmp;
404 assert (!table->iter_tail);
407 MANGLEP(tag) = table->tag;
411 /* Find collision chain */
412 h = Hash (table, hash);
413 SegmentIndex = h & (SEGMENT_SIZE-1);
414 CurrentSegment = table->dir[h >> SEGMENT_SIZE_SHIFT];
415 assert (CurrentSegment != NULL);
416 q = CurrentSegment[SegmentIndex];
418 /* Follow collision chain */
419 while (q && !EQUAL (cmp, q, key, size)) {
424 stat_chain_len (table, chain_len);
426 if (!q && (action != MANGLE(_,_find))) { /* not found, insert */
427 if (CurrentSegment[SegmentIndex]) stat_dup (table);
430 if (table->free_list) {
431 q = table->free_list;
432 table->free_list = table->free_list->chain;
434 q = obstack_alloc (&table->obst, sizeof (Element));
436 q->entry.dptr = (void *)key;
438 obstack_blank (&table->obst, offsetof (Element, entry.dptr));
439 if (action == _set_hinsert0)
440 obstack_grow0 (&table->obst, key, size);
442 obstack_grow (&table->obst, key, size);
443 q = obstack_finish (&table->obst);
444 q->entry.size = size;
446 q->chain = CurrentSegment[SegmentIndex];
447 q->entry.hash = hash;
448 CurrentSegment[SegmentIndex] = q;
450 if (loaded (table)) {
451 expand_table(table); /* doesn't affect q */
457 if (action == _pset_hinsert) return &q->entry;
459 if (action == _set_hinsert || action == _set_hinsert0) return &q->entry;
461 return q->entry.dptr;
468 pset_remove (SET *table, const void *key, unsigned hash)
471 Segment *CurrentSegment;
473 pset_cmp_fun cmp = table->cmp;
478 assert (table && !table->iter_tail);
481 /* Find collision chain */
482 h = Hash (table, hash);
483 SegmentIndex = h & (SEGMENT_SIZE-1);
484 CurrentSegment = table->dir[h >> SEGMENT_SIZE_SHIFT];
485 assert (CurrentSegment != NULL);
486 p = &CurrentSegment[SegmentIndex];
488 /* Follow collision chain */
489 while (!EQUAL (cmp, *p, key, size)) {
495 stat_chain_len (table, chain_len);
499 if (q == table->iter_tail) {
500 /* removing current element */
501 table->iter_tail = q->chain;
502 if (!table->iter_tail) {
503 /* go to next segment */
505 if (!iter_step (table))
507 } while (!table->dir[table->iter_i][table->iter_j]);
508 table->iter_tail = table->dir[table->iter_i][table->iter_j];
513 q->chain = table->free_list;
514 table->free_list = q;
517 return q->entry.dptr;
522 (pset_find) (SET *se, const void *key, unsigned hash)
524 return pset_find (se, key, hash);
529 (pset_insert) (SET *se, const void *key, unsigned hash)
531 return pset_insert (se, key, hash);
536 (pset_hinsert) (SET *se, const void *key, unsigned hash)
538 return pset_hinsert (se, key, hash);
544 (set_find) (set *se, const void *key, size_t size, unsigned hash)
546 return set_find (se, key, size, hash);
551 (set_insert) (set *se, const void *key, size_t size, unsigned hash)
553 return set_insert (se, key, size, hash);
558 (set_hinsert) (set *se, const void *key, size_t size, unsigned hash)
560 return set_hinsert (se, key, size, hash);