X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fadt%2Fset.c;h=f22c897804a27de59771c335f7f3cc676b7da58a;hb=6b7483c417524023d2675981f07e6c59e955428f;hp=9c4470382b4b95a48aa4b7bee68889732f6c9591;hpb=efbeaff549fcc6015da255ed4d453a95937ff0fd;p=libfirm diff --git a/ir/adt/set.c b/ir/adt/set.c index 9c4470382..f22c89780 100644 --- a/ir/adt/set.c +++ b/ir/adt/set.c @@ -1,5 +1,14 @@ -/* Set --- collection of entries that are unique wrt to a key. - Copyright (C) 1995, 1996 Markus Armbruster */ +/* + * Project: libFIRM + * File name: ir/adt/set.c + * Purpose: Set --- collection of entries that are unique wrt to a key. + * Author: Markus Armbruster + * Modified by: + * Created: 1999 by getting from fiasco + * CVS-ID: $Id$ + * Copyright: (c) 1995, 1996 Markus Armbruster + * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. + */ /* This code is derived from: @@ -14,17 +23,23 @@ Submitted-By: Esmond Pitt Archive-name: dynamic-hash - ** Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson. - ** Coded into C, with minor code improvements, and with hsearch(3) interface, - ** by ejp@ausmelb.oz, Jul 26, 1988: 13:16; + * Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson. + * Coded into C, with minor code improvements, and with hsearch(3) interface, + * by ejp@ausmelb.oz, Jul 26, 1988: 13:16; TODO: Fix Esmond's ugly MixedCapsIdentifiers ;-> */ +/* $Id$ */ + #ifdef HAVE_CONFIG_H # include #endif +/* bcopy is not ISO C * +#define bcopy(X, Y, Z) memcpy((Y), (X), (Z)) +*/ + #ifdef PSET # define SET pset # define PMANGLE(pre) pre##_pset @@ -44,13 +59,14 @@ #include #include #include -#include "misc.h" +#include "xmalloc.h" #ifdef PSET # include "pset.h" #else # include "set.h" #endif + #define TOBSTACK_ID MANGLEP(tag) #include "obst.h" @@ -59,28 +75,28 @@ #define SEGMENT_SIZE (1 << SEGMENT_SIZE_SHIFT) #define DIRECTORY_SIZE_SHIFT 8 #define DIRECTORY_SIZE (1 << DIRECTORY_SIZE_SHIFT) -#define MAX_LOAD_FACTOR 4 +#define MAX_LOAD_FACTOR 4 typedef struct element { - struct element *chain; + struct element *chain; /* for chaining Elements */ MANGLEP (entry) entry; } Element, *Segment; struct SET { - short p; /* Next bucket to be split */ - short maxp; /* upper bound on p during expansion */ - int nkey; /* current # keys */ - short nseg; /* current # segments */ + unsigned p; /* Next bucket to be split */ + unsigned maxp; /* upper bound on p during expansion */ + unsigned nkey; /* current # keys */ + unsigned nseg; /* current # segments */ Segment *dir[DIRECTORY_SIZE]; MANGLEP(cmp_fun) cmp; /* function comparing entries */ - int iter_i, iter_j; + unsigned iter_i, iter_j; Element *iter_tail; /* non-NULL while iterating over elts */ #ifdef PSET - Element *free_list; + Element *free_list; /* list of free Elements */ #endif - struct obstack obst; + struct obstack obst; /* obstack for allocation all data */ #ifdef STATS int naccess, ncollision, ndups; int max_chain_len; @@ -105,7 +121,7 @@ MANGLEP(stats) (SET *table) table->naccess, table->ncollision, table->nkey, table->ndups, table->max_chain_len, nfree); } -static inline void +static INLINE void stat_chain_len (SET *table, int chain_len) { table->ncollision += chain_len; @@ -131,11 +147,11 @@ const char *MANGLEP(tag); void MANGLEP(describe) (SET *table) { - int i, j, collide; + unsigned i, j, collide; Element *ptr; Segment *seg; - printf ("p=%d maxp=%d nkey=%d nseg=%d\n", + printf ("p=%u maxp=%u nkey=%u nseg=%u\n", table->p, table->maxp, table->nkey, table->nseg); for (i = 0; i < table->nseg; i++) { seg = table->dir[i]; @@ -145,7 +161,7 @@ MANGLEP(describe) (SET *table) while (ptr) { if (collide) printf ("<%3d>", collide); else printf ("table"); - printf ("[%d][%3d]: %u %p\n", i, j, ptr->entry.hash, ptr->entry.dptr); + printf ("[%d][%3d]: %u %p\n", i, j, ptr->entry.hash, (void *)ptr->entry.dptr); ptr = ptr->chain; collide++; } @@ -162,10 +178,14 @@ SET * int i; SET *table = xmalloc (sizeof (SET)); - /* Adjust nslots up to next power of 2, minimum SEGMENT_SIZE */ - assert (nslots >= 0); - for (i = SEGMENT_SIZE; i < nslots; i <<= 1) assert (i < (i << 1)); - nslots = i >> SEGMENT_SIZE_SHIFT; + if (nslots > SEGMENT_SIZE * DIRECTORY_SIZE) + n_slots = DIRECTORY_SIZE; + else { + assert (nslots >= 0); + /* Adjust nslots up to next power of 2, minimum SEGMENT_SIZE */ + for (i = SEGMENT_SIZE; i < nslots; i <<= 1); + nslots = i >> SEGMENT_SIZE_SHIFT; + } table->nseg = table->p = table->nkey = 0; table->maxp = nslots << SEGMENT_SIZE_SHIFT; @@ -181,7 +201,7 @@ SET * table->dir[i] = (Segment *)obstack_alloc (&table->obst, sizeof (Segment) * SEGMENT_SIZE); - memset (table->dir[i], 0, sizeof (Segment) * SEGMENT_SIZE); + memset(table->dir[i], 0, sizeof (Segment) * SEGMENT_SIZE); table->nseg++; } @@ -206,8 +226,17 @@ PMANGLE(del) (SET *table) xfree (table); } +int +MANGLEP(count) (SET *table) +{ + return table->nkey; +} -static inline int +/* + * do one iteration step, return 1 + * if still data in the set, 0 else + */ +static INLINE int iter_step (SET *table) { if (++table->iter_j >= SEGMENT_SIZE) { @@ -220,7 +249,9 @@ iter_step (SET *table) return 1; } - +/* + * finds the first entry in the table + */ void * MANGLEP(first) (SET *table) { @@ -235,13 +266,19 @@ MANGLEP(first) (SET *table) return table->iter_tail->entry.dptr; } - +/* + * returns next entry in the table + */ void * MANGLEP(next) (SET *table) { - assert (table->iter_tail); + if (!table->iter_tail) + return NULL; + + /* follow collision chain */ table->iter_tail = table->iter_tail->chain; if (!table->iter_tail) { + /* go to next segment */ do { if (!iter_step (table)) return NULL; } while (!table->dir[table->iter_i][table->iter_j]); @@ -254,31 +291,42 @@ MANGLEP(next) (SET *table) void MANGLEP(break) (SET *table) { - assert (table->iter_tail); table->iter_tail = NULL; } - -static inline unsigned +/* + * limit the hash value + */ +static INLINE unsigned Hash (SET *table, unsigned h) { unsigned address; - address = h & (table->maxp - 1); + address = h & (table->maxp - 1); /* h % table->maxp */ if (address < (unsigned)table->p) address = h & ((table->maxp << 1) - 1); /* h % (2*table->maxp) */ return address; } - -static inline int +/* + * returns non-zero if the number of elements in + * the set is greater then number of segments * MAX_LOAD_FACTOR + */ +static INLINE int loaded (SET *table) { return ( ++table->nkey > (table->nseg << SEGMENT_SIZE_SHIFT) * MAX_LOAD_FACTOR); } - +/* + * expand the hash-table: the algorithm is split, so on every + * insert, only ONE segment is rehashed! + * + * table->p contains the current segment to split + * after all segments were split, table->p is set to zero and + * table->maxp is duplicated. + */ static void expand_table (SET *table) { @@ -293,18 +341,20 @@ expand_table (SET *table) if (table->maxp + table->p < (DIRECTORY_SIZE << SEGMENT_SIZE_SHIFT)) { /* Locate the bucket to be split */ - OldSegmentDir = table->p >> SEGMENT_SIZE_SHIFT; - OldSegment = table->dir[OldSegmentDir]; + OldSegmentDir = table->p >> SEGMENT_SIZE_SHIFT; + OldSegment = table->dir[OldSegmentDir]; OldSegmentIndex = table->p & (SEGMENT_SIZE-1); /* Expand address space; if necessary create a new segment */ - NewAddress = table->maxp + table->p; - NewSegmentDir = NewAddress >> SEGMENT_SIZE_SHIFT; + NewAddress = table->maxp + table->p; + NewSegmentDir = NewAddress >> SEGMENT_SIZE_SHIFT; NewSegmentIndex = NewAddress & (SEGMENT_SIZE-1); if (NewSegmentIndex == 0) { table->dir[NewSegmentDir] = (Segment *)obstack_alloc (&table->obst, sizeof(Segment) * SEGMENT_SIZE); + memset(table->dir[NewSegmentDir], 0, sizeof(Segment) * SEGMENT_SIZE); + table->nseg++; } NewSegment = table->dir[NewSegmentDir]; @@ -314,7 +364,6 @@ expand_table (SET *table) table->maxp <<= 1; /* table->maxp *= 2 */ table->p = 0; } - table->nseg++; /* Relocate records to the new bucket */ Previous = &OldSegment[OldSegmentIndex]; @@ -325,9 +374,9 @@ expand_table (SET *table) if (Hash (table, Current->entry.hash) == NewAddress) { /* move to new chain */ *LastOfNew = Current; - *Previous = Current->chain; - LastOfNew = &Current->chain; - Current = Current->chain; + *Previous = Current->chain; + LastOfNew = &Current->chain; + Current = Current->chain; *LastOfNew = NULL; } else { /* leave on old chain */ @@ -365,7 +414,7 @@ MANGLE(_,_search) (SET *table, /* Find collision chain */ h = Hash (table, hash); - SegmentIndex = h & (SEGMENT_SIZE-1); + SegmentIndex = h & (SEGMENT_SIZE-1); CurrentSegment = table->dir[h >> SEGMENT_SIZE_SHIFT]; assert (CurrentSegment != NULL); q = CurrentSegment[SegmentIndex]; @@ -391,7 +440,10 @@ MANGLE(_,_search) (SET *table, q->entry.dptr = (void *)key; #else obstack_blank (&table->obst, offsetof (Element, entry.dptr)); - obstack_grow (&table->obst, key, size); + if (action == _set_hinsert0) + obstack_grow0 (&table->obst, key, size); + else + obstack_grow (&table->obst, key, size); q = obstack_finish (&table->obst); q->entry.size = size; #endif @@ -405,7 +457,11 @@ MANGLE(_,_search) (SET *table, } if (!q) return NULL; - if (action == MANGLE(_,_hinsert)) return &q->entry; +#ifdef PSET + if (action == _pset_hinsert) return &q->entry; +#else + if (action == _set_hinsert || action == _set_hinsert0) return &q->entry; +#endif return q->entry.dptr; } @@ -443,9 +499,24 @@ pset_remove (SET *table, const void *key, unsigned hash) stat_chain_len (table, chain_len); q = *p; + + if (q == table->iter_tail) { + /* removing current element */ + table->iter_tail = q->chain; + if (!table->iter_tail) { + /* go to next segment */ + do { + if (!iter_step (table)) + break; + } while (!table->dir[table->iter_i][table->iter_j]); + table->iter_tail = table->dir[table->iter_i][table->iter_j]; + } + } + *p = (*p)->chain; q->chain = table->free_list; table->free_list = q; + --table->nkey; return q->entry.dptr; }