2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief implementation of set
23 * @author Markus Armbruster
27 /* This code is derived from:
29 From: ejp@ausmelb.oz.AU (Esmond Pitt)
30 Date: Tue, 7 Mar 1989 22:06:26 GMT
31 Subject: v06i042: dynamic hashing version of hsearch(3)
32 Message-ID: <1821@basser.oz>
33 Newsgroups: comp.sources.misc
34 Sender: msgs@basser.oz
36 Posting-number: Volume 6, Issue 42
37 Submitted-By: Esmond Pitt <ejp@ausmelb.oz.AU>
38 Archive-name: dynamic-hash
40 * Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson.
41 * Coded into C, with minor code improvements, and with hsearch(3) interface,
42 * by ejp@ausmelb.oz, Jul 26, 1988: 13:16;
44 TODO: Fix Esmond's ugly MixedCapsIdentifiers ;->
50 # define PMANGLE(pre) pre##_pset
51 # define MANGLEP(post) pset_##post
52 # define MANGLE(pre, post) pre##pset##post
53 # define EQUAL(cmp, elt, key, siz) (!(cmp) ((elt)->entry.dptr, (key)))
56 # define PMANGLE(pre) pre##_set
57 # define MANGLEP(post) set_##post
58 # define MANGLE(pre, post) pre##set##post
59 # define EQUAL(cmp, elt, key, siz) \
60 (((elt)->entry.size == (siz)) && !(cmp) ((elt)->entry.dptr, (key), (siz)))
75 #define TOBSTACK_ID MANGLEP(tag)
79 #define SEGMENT_SIZE_SHIFT 8
80 #define SEGMENT_SIZE (1 << SEGMENT_SIZE_SHIFT)
81 #define DIRECTORY_SIZE_SHIFT 8
82 #define DIRECTORY_SIZE (1 << DIRECTORY_SIZE_SHIFT)
83 #define MAX_LOAD_FACTOR 4
86 typedef struct element {
87 struct element *chain; /**< for chaining Elements */
88 MANGLEP (entry) entry;
93 unsigned p; /**< Next bucket to be split */
94 unsigned maxp; /**< upper bound on p during expansion */
95 unsigned nkey; /**< current # keys */
96 unsigned nseg; /**< current # segments */
97 Segment *dir[DIRECTORY_SIZE];
98 MANGLEP(cmp_fun) cmp; /**< function comparing entries */
99 unsigned iter_i, iter_j;
100 Element *iter_tail; /**< non-NULL while iterating over elts */
102 Element *free_list; /**< list of free Elements */
104 struct obstack obst; /**< obstack for allocation all data */
106 int naccess, ncollision, ndups;
110 const char *tag; /**< an optionally tag for distinguishing sets */
118 MANGLEP(stats) (SET *table)
122 Element *q = table->free_list;
123 while (q) { q = q->chain; ++nfree; }
125 printf (" accesses collisions keys duplicates longest wasted\n%12d%12d%12d%12d%12d%12d\n",
126 table->naccess, table->ncollision, table->nkey, table->ndups, table->max_chain_len, nfree);
130 stat_chain_len (SET *table, int chain_len)
132 table->ncollision += chain_len;
133 if (table->max_chain_len < chain_len) table->max_chain_len = chain_len;
136 # define stat_access(table) (++(table)->naccess)
137 # define stat_dup(table) (++(table)->ndups)
141 # define stat_chain_len(table, chain_len) ((void)0)
142 # define stat_access(table) ((void)0)
143 # define stat_dup(table) ((void)0)
149 const char *MANGLEP(tag);
153 MANGLEP(describe) (SET *table)
155 unsigned i, j, collide;
159 printf ("p=%u maxp=%u nkey=%u nseg=%u\n",
160 table->p, table->maxp, table->nkey, table->nseg);
161 for (i = 0; i < table->nseg; i++) {
163 for (j = 0; j < SEGMENT_SIZE; j++) {
167 if (collide) printf ("<%3d>", collide);
168 else printf ("table");
169 printf ("[%d][%3d]: %u %p\n", i, j, ptr->entry.hash, (void *)ptr->entry.dptr);
176 MANGLEP(stats)(table);
184 (PMANGLE(new)) (MANGLEP(cmp_fun) cmp, int nslots)
187 SET *table = XMALLOC(SET);
189 if (nslots > SEGMENT_SIZE * DIRECTORY_SIZE)
190 nslots = DIRECTORY_SIZE;
192 assert (nslots >= 0);
193 /* Adjust nslots up to next power of 2, minimum SEGMENT_SIZE */
194 for (i = SEGMENT_SIZE; i < nslots; i <<= 1);
195 nslots = i >> SEGMENT_SIZE_SHIFT;
198 table->nseg = table->p = table->nkey = 0;
199 table->maxp = nslots << SEGMENT_SIZE_SHIFT;
201 table->iter_tail = NULL;
203 table->free_list = NULL;
205 obstack_init (&table->obst);
208 for (i = 0; i < nslots; ++i) {
209 table->dir[i] = (Segment *)obstack_alloc (&table->obst,
210 sizeof (Segment) * SEGMENT_SIZE);
212 memset(table->dir[i], 0, sizeof (Segment) * SEGMENT_SIZE);
217 table->naccess = table->ncollision = table->ndups = 0;
218 table->max_chain_len = 0;
221 table->tag = MANGLEP(tag);
228 PMANGLE(del) (SET *table)
231 MANGLEP(tag) = table->tag;
233 obstack_free (&table->obst, NULL);
238 MANGLEP(count) (SET *table)
244 * do one iteration step, return 1
245 * if still data in the set, 0 else
248 iter_step (SET *table)
250 if (++table->iter_j >= SEGMENT_SIZE) {
252 if (++table->iter_i >= table->nseg) {
261 * finds the first entry in the table
264 MANGLEP(first) (SET *table)
266 assert (!table->iter_tail);
269 while (!table->dir[table->iter_i][table->iter_j]) {
270 if (!iter_step (table)) return NULL;
272 table->iter_tail = table->dir[table->iter_i][table->iter_j];
273 assert (table->iter_tail->entry.dptr);
274 return table->iter_tail->entry.dptr;
278 * returns next entry in the table
281 MANGLEP(next) (SET *table)
283 if (!table->iter_tail)
286 /* follow collision chain */
287 table->iter_tail = table->iter_tail->chain;
288 if (!table->iter_tail) {
289 /* go to next segment */
291 if (!iter_step (table)) return NULL;
292 } while (!table->dir[table->iter_i][table->iter_j]);
293 table->iter_tail = table->dir[table->iter_i][table->iter_j];
295 assert (table->iter_tail->entry.dptr);
296 return table->iter_tail->entry.dptr;
300 MANGLEP(break) (SET *table)
302 table->iter_tail = NULL;
306 * limit the hash value
308 static inline unsigned
309 Hash (SET *table, unsigned h)
312 address = h & (table->maxp - 1); /* h % table->maxp */
313 if (address < (unsigned)table->p)
314 address = h & ((table->maxp << 1) - 1); /* h % (2*table->maxp) */
319 * returns non-zero if the number of elements in
320 * the set is greater then number of segments * MAX_LOAD_FACTOR
325 return ( ++table->nkey
326 > (table->nseg << SEGMENT_SIZE_SHIFT) * MAX_LOAD_FACTOR);
330 * expand the hash-table: the algorithm is split, so on every
331 * insert, only ONE segment is rehashed!
333 * table->p contains the current segment to split
334 * after all segments were split, table->p is set to zero and
335 * table->maxp is duplicated.
338 expand_table (SET *table)
341 int OldSegmentIndex, NewSegmentIndex;
342 int OldSegmentDir, NewSegmentDir;
349 if (table->maxp + table->p < (DIRECTORY_SIZE << SEGMENT_SIZE_SHIFT)) {
350 /* Locate the bucket to be split */
351 OldSegmentDir = table->p >> SEGMENT_SIZE_SHIFT;
352 OldSegment = table->dir[OldSegmentDir];
353 OldSegmentIndex = table->p & (SEGMENT_SIZE-1);
355 /* Expand address space; if necessary create a new segment */
356 NewAddress = table->maxp + table->p;
357 NewSegmentDir = NewAddress >> SEGMENT_SIZE_SHIFT;
358 NewSegmentIndex = NewAddress & (SEGMENT_SIZE-1);
359 if (NewSegmentIndex == 0) {
360 table->dir[NewSegmentDir] =
361 (Segment *)obstack_alloc (&table->obst,
362 sizeof(Segment) * SEGMENT_SIZE);
363 memset(table->dir[NewSegmentDir], 0, sizeof(Segment) * SEGMENT_SIZE);
366 NewSegment = table->dir[NewSegmentDir];
368 /* Adjust state variables */
370 if (table->p == table->maxp) {
371 table->maxp <<= 1; /* table->maxp *= 2 */
375 /* Relocate records to the new bucket */
376 Previous = &OldSegment[OldSegmentIndex];
378 LastOfNew = &NewSegment[NewSegmentIndex];
380 while (Current != NULL) {
381 if (Hash (table, Current->entry.hash) == NewAddress) {
382 /* move to new chain */
383 *LastOfNew = Current;
384 *Previous = Current->chain;
385 LastOfNew = &Current->chain;
386 Current = Current->chain;
389 /* leave on old chain */
390 Previous = &Current->chain;
391 Current = Current->chain;
399 MANGLE(_,_search) (SET *table,
405 MANGLE(_,_action) action)
408 Segment *CurrentSegment;
410 MANGLEP(cmp_fun) cmp = table->cmp;
417 MANGLEP(tag) = table->tag;
421 /* Find collision chain */
422 h = Hash (table, hash);
423 SegmentIndex = h & (SEGMENT_SIZE-1);
424 CurrentSegment = table->dir[h >> SEGMENT_SIZE_SHIFT];
425 assert (CurrentSegment != NULL);
426 q = CurrentSegment[SegmentIndex];
428 /* Follow collision chain */
429 while (q && !EQUAL (cmp, q, key, size)) {
434 stat_chain_len (table, chain_len);
436 if (!q && (action != MANGLE(_,_find))) { /* not found, insert */
437 assert (!table->iter_tail && "insert an element into a set that is iterated");
439 if (CurrentSegment[SegmentIndex]) stat_dup (table);
442 if (table->free_list) {
443 q = table->free_list;
444 table->free_list = table->free_list->chain;
446 q = obstack_alloc (&table->obst, sizeof (Element));
448 q->entry.dptr = (void *)key;
450 obstack_blank (&table->obst, offsetof (Element, entry.dptr));
451 if (action == _set_hinsert0)
452 obstack_grow0 (&table->obst, key, size);
454 obstack_grow (&table->obst, key, size);
455 q = obstack_finish (&table->obst);
456 q->entry.size = size;
458 q->chain = CurrentSegment[SegmentIndex];
459 q->entry.hash = hash;
460 CurrentSegment[SegmentIndex] = q;
462 if (loaded (table)) {
463 expand_table(table); /* doesn't affect q */
469 if (action == _pset_hinsert) return &q->entry;
471 if (action == _set_hinsert || action == _set_hinsert0) return &q->entry;
473 return q->entry.dptr;
479 int pset_default_ptr_cmp(const void *x, const void *y)
485 pset_remove (SET *table, const void *key, unsigned hash)
488 Segment *CurrentSegment;
490 pset_cmp_fun cmp = table->cmp;
495 assert (table && !table->iter_tail);
498 /* Find collision chain */
499 h = Hash (table, hash);
500 SegmentIndex = h & (SEGMENT_SIZE-1);
501 CurrentSegment = table->dir[h >> SEGMENT_SIZE_SHIFT];
502 assert (CurrentSegment != NULL);
503 p = &CurrentSegment[SegmentIndex];
505 /* Follow collision chain */
506 while (!EQUAL (cmp, *p, key, size)) {
512 stat_chain_len (table, chain_len);
516 if (q == table->iter_tail) {
517 /* removing current element */
518 table->iter_tail = q->chain;
519 if (!table->iter_tail) {
520 /* go to next segment */
522 if (!iter_step (table))
524 } while (!table->dir[table->iter_i][table->iter_j]);
525 table->iter_tail = table->dir[table->iter_i][table->iter_j];
530 q->chain = table->free_list;
531 table->free_list = q;
534 return q->entry.dptr;
539 (pset_find) (SET *se, const void *key, unsigned hash)
541 return pset_find (se, key, hash);
546 (pset_insert) (SET *se, const void *key, unsigned hash)
548 return pset_insert (se, key, hash);
553 (pset_hinsert) (SET *se, const void *key, unsigned hash)
555 return pset_hinsert (se, key, hash);
558 void pset_insert_pset_ptr(pset *target, pset *src) {
560 for (elt = pset_first(src); elt; elt = pset_next(src)) {
561 pset_insert_ptr(target, elt);
568 (set_find) (set *se, const void *key, size_t size, unsigned hash)
570 return set_find (se, key, size, hash);
575 (set_insert) (set *se, const void *key, size_t size, unsigned hash)
577 return set_insert (se, key, size, hash);
582 (set_hinsert) (set *se, const void *key, size_t size, unsigned hash)
584 return set_hinsert (se, key, size, hash);