dict2.cpp revision 0:a61af66fc99e
1238384Sjkim/*
2238384Sjkim * Copyright 1998-2002 Sun Microsystems, Inc.  All Rights Reserved.
3238384Sjkim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4238384Sjkim *
5238384Sjkim * This code is free software; you can redistribute it and/or modify it
6238384Sjkim * under the terms of the GNU General Public License version 2 only, as
7238384Sjkim * published by the Free Software Foundation.
8238384Sjkim *
9238384Sjkim * This code is distributed in the hope that it will be useful, but WITHOUT
10238384Sjkim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11238384Sjkim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12238384Sjkim * version 2 for more details (a copy is included in the LICENSE file that
13238384Sjkim * accompanied this code).
14238384Sjkim *
15238384Sjkim * You should have received a copy of the GNU General Public License version
16238384Sjkim * 2 along with this work; if not, write to the Free Software Foundation,
17238384Sjkim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18238384Sjkim *
19238384Sjkim * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20238384Sjkim * CA 95054 USA or visit www.sun.com if you need additional information or
21238384Sjkim * have any questions.
22238384Sjkim *
23238384Sjkim */
24238384Sjkim
25238384Sjkim// Dictionaries - An Abstract Data Type
26238384Sjkim
27238384Sjkim#include "adlc.hpp"
28238384Sjkim
29238384Sjkim// #include "dict.hpp"
30238384Sjkim
31238384Sjkim
32238384Sjkim//------------------------------data-----------------------------------------
33238384Sjkim// String hash tables
34238384Sjkim#define MAXID 20
35238384Sjkimstatic char initflag = 0;       // True after 1st initialization
36238384Sjkimstatic char shft[MAXID] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6};
37238384Sjkimstatic short xsum[MAXID + 1];
38238384Sjkim
39238384Sjkim//------------------------------bucket---------------------------------------
40238384Sjkimclass bucket {
41238384Sjkimpublic:
42238384Sjkim  int          _cnt, _max;      // Size of bucket
43238384Sjkim  const void **_keyvals;        // Array of keys and values
44238384Sjkim};
45238384Sjkim
46238384Sjkim//------------------------------Dict-----------------------------------------
47238384Sjkim// The dictionary is kept has a hash table.  The hash table is a even power
48238384Sjkim// of two, for nice modulo operations.  Each bucket in the hash table points
49238384Sjkim// to a linear list of key-value pairs; each key & value is just a (void *).
50238384Sjkim// The list starts with a count.  A hash lookup finds the list head, then a
51238384Sjkim// simple linear scan finds the key.  If the table gets too full, it's
52238384Sjkim// doubled in size; the total amount of EXTRA times all hash functions are
53238384Sjkim// computed for the doubling is no more than the current size - thus the
54238384Sjkim// doubling in size costs no more than a constant factor in speed.
55238384SjkimDict::Dict(CmpKey initcmp, Hash inithash) : _hash(inithash), _cmp(initcmp), _arena(NULL) {
56238384Sjkim  init();
57238384Sjkim}
58238384Sjkim
59238384SjkimDict::Dict(CmpKey initcmp, Hash inithash, Arena *arena) : _hash(inithash), _cmp(initcmp), _arena(arena) {
60238384Sjkim  init();
61238384Sjkim}
62238384Sjkim
63238384Sjkimvoid Dict::init() {
64238384Sjkim  int i;
65238384Sjkim
66238384Sjkim  // Precompute table of null character hashes
67238384Sjkim  if( !initflag ) {             // Not initializated yet?
68238384Sjkim    xsum[0] = (1<<shft[0])+1;   // Initialize
69238384Sjkim    for( i = 1; i < MAXID + 1; i++) {
70238384Sjkim      xsum[i] = (1<<shft[i])+1+xsum[i-1];
71238384Sjkim    }
72238384Sjkim    initflag = 1;               // Never again
73238384Sjkim  }
74238384Sjkim
75238384Sjkim  _size = 16;                   // Size is a power of 2
76238384Sjkim  _cnt = 0;                     // Dictionary is empty
77238384Sjkim  _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
78238384Sjkim  memset(_bin,0,sizeof(bucket)*_size);
79238384Sjkim}
80238384Sjkim
81238384Sjkim//------------------------------~Dict------------------------------------------
82238384Sjkim// Delete an existing dictionary.
83238384SjkimDict::~Dict() {
84238384Sjkim}
85238384Sjkim
86238384Sjkim//------------------------------Clear----------------------------------------
87238384Sjkim// Zap to empty; ready for re-use
88238384Sjkimvoid Dict::Clear() {
89238384Sjkim  _cnt = 0;                     // Empty contents
90238384Sjkim  for( int i=0; i<_size; i++ )
91238384Sjkim    _bin[i]._cnt = 0;           // Empty buckets, but leave allocated
92238384Sjkim  // Leave _size & _bin alone, under the assumption that dictionary will
93238384Sjkim  // grow to this size again.
94238384Sjkim}
95238384Sjkim
96238384Sjkim//------------------------------doubhash---------------------------------------
97238384Sjkim// Double hash table size.  If can't do so, just suffer.  If can, then run
98238384Sjkim// thru old hash table, moving things to new table.  Note that since hash
99238384Sjkim// table doubled, exactly 1 new bit is exposed in the mask - so everything
100238384Sjkim// in the old table ends up on 1 of two lists in the new table; a hi and a
101238384Sjkim// lo list depending on the value of the bit.
102238384Sjkimvoid Dict::doubhash(void) {
103238384Sjkim  int oldsize = _size;
104238384Sjkim  _size <<= 1;                  // Double in size
105238384Sjkim  _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*oldsize, sizeof(bucket)*_size );
106238384Sjkim  memset( &_bin[oldsize], 0, oldsize*sizeof(bucket) );
107238384Sjkim  // Rehash things to spread into new table
108238384Sjkim  for( int i=0; i < oldsize; i++) { // For complete OLD table do
109238384Sjkim    bucket *b = &_bin[i];       // Handy shortcut for _bin[i]
110238384Sjkim    if( !b->_keyvals ) continue;        // Skip empties fast
111238384Sjkim
112238384Sjkim    bucket *nb = &_bin[i+oldsize];  // New bucket shortcut
113238384Sjkim    int j = b->_max;                // Trim new bucket to nearest power of 2
114238384Sjkim    while( j > b->_cnt ) j >>= 1;   // above old bucket _cnt
115238384Sjkim    if( !j ) j = 1;             // Handle zero-sized buckets
116238384Sjkim    nb->_max = j<<1;
117238384Sjkim    // Allocate worst case space for key-value pairs
118238384Sjkim    nb->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*nb->_max*2 );
119238384Sjkim    int nbcnt = 0;
120238384Sjkim
121238384Sjkim    for( j=0; j<b->_cnt; j++ ) {  // Rehash all keys in this bucket
122238384Sjkim      const void *key = b->_keyvals[j+j];
123238384Sjkim      if( (_hash( key ) & (_size-1)) != i ) { // Moving to hi bucket?
124238384Sjkim        nb->_keyvals[nbcnt+nbcnt] = key;
125238384Sjkim        nb->_keyvals[nbcnt+nbcnt+1] = b->_keyvals[j+j+1];
126238384Sjkim        nb->_cnt = nbcnt = nbcnt+1;
127238384Sjkim        b->_cnt--;              // Remove key/value from lo bucket
128238384Sjkim        b->_keyvals[j+j  ] = b->_keyvals[b->_cnt+b->_cnt  ];
129238384Sjkim        b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
130238384Sjkim        j--;                    // Hash compacted element also
131238384Sjkim      }
132238384Sjkim    } // End of for all key-value pairs in bucket
133238384Sjkim  } // End of for all buckets
134238384Sjkim
135238384Sjkim
136238384Sjkim}
137238384Sjkim
138238384Sjkim//------------------------------Dict-----------------------------------------
139238384Sjkim// Deep copy a dictionary.
140238384SjkimDict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) {
141238384Sjkim  _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
142238384Sjkim  memcpy( _bin, d._bin, sizeof(bucket)*_size );
143238384Sjkim  for( int i=0; i<_size; i++ ) {
144238384Sjkim    if( !_bin[i]._keyvals ) continue;
145238384Sjkim    _bin[i]._keyvals=(const void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2);
146238384Sjkim    memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*));
147238384Sjkim  }
148238384Sjkim}
149238384Sjkim
150238384Sjkim//------------------------------Dict-----------------------------------------
151238384Sjkim// Deep copy a dictionary.
152238384SjkimDict &Dict::operator =( const Dict &d ) {
153238384Sjkim  if( _size < d._size ) {       // If must have more buckets
154238384Sjkim    _arena = d._arena;
155238384Sjkim    _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );
156238384Sjkim    memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) );
157238384Sjkim    _size = d._size;
158238384Sjkim  }
159238384Sjkim  for( int i=0; i<_size; i++ ) // All buckets are empty
160238384Sjkim    _bin[i]._cnt = 0;           // But leave bucket allocations alone
161238384Sjkim  _cnt = d._cnt;
162238384Sjkim  *(Hash*)(&_hash) = d._hash;
163238384Sjkim  *(CmpKey*)(&_cmp) = d._cmp;
164238384Sjkim  for(int k=0; k<_size; k++ ) {
165238384Sjkim    bucket *b = &d._bin[k];     // Shortcut to source bucket
166238384Sjkim    for( int j=0; j<b->_cnt; j++ )
167238384Sjkim      Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] );
168238384Sjkim  }
169238384Sjkim  return *this;
170238384Sjkim}
171238384Sjkim
172238384Sjkim//------------------------------Insert---------------------------------------
173238384Sjkim// Insert or replace a key/value pair in the given dictionary.  If the
174238384Sjkim// dictionary is too full, it's size is doubled.  The prior value being
175238384Sjkim// replaced is returned (NULL if this is a 1st insertion of that key).  If
176238384Sjkim// an old value is found, it's swapped with the prior key-value pair on the
177238384Sjkim// list.  This moves a commonly searched-for value towards the list head.
178238384Sjkimconst void *Dict::Insert(const void *key, const void *val) {
179238384Sjkim  int hash = _hash( key );      // Get hash key
180238384Sjkim  int i = hash & (_size-1);     // Get hash key, corrected for size
181238384Sjkim  bucket *b = &_bin[i];         // Handy shortcut
182238384Sjkim  for( int j=0; j<b->_cnt; j++ )
183238384Sjkim    if( !_cmp(key,b->_keyvals[j+j]) ) {
184238384Sjkim      const void *prior = b->_keyvals[j+j+1];
185238384Sjkim      b->_keyvals[j+j  ] = key; // Insert current key-value
186238384Sjkim      b->_keyvals[j+j+1] = val;
187238384Sjkim      return prior;             // Return prior
188238384Sjkim    }
189238384Sjkim
190238384Sjkim  if( ++_cnt > _size ) {        // Hash table is full
191238384Sjkim    doubhash();                 // Grow whole table if too full
192238384Sjkim    i = hash & (_size-1);       // Rehash
193238384Sjkim    b = &_bin[i];               // Handy shortcut
194238384Sjkim  }
195238384Sjkim  if( b->_cnt == b->_max ) {    // Must grow bucket?
196238384Sjkim    if( !b->_keyvals ) {
197238384Sjkim      b->_max = 2;              // Initial bucket size
198238384Sjkim      b->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*b->_max*2 );
199238384Sjkim    } else {
200238384Sjkim      b->_keyvals = (const void**)_arena->Arealloc( b->_keyvals, sizeof(void *)*b->_max*2, sizeof(void *)*b->_max*4 );
201238384Sjkim      b->_max <<= 1;            // Double bucket
202238384Sjkim    }
203238384Sjkim  }
204238384Sjkim  b->_keyvals[b->_cnt+b->_cnt  ] = key;
205238384Sjkim  b->_keyvals[b->_cnt+b->_cnt+1] = val;
206238384Sjkim  b->_cnt++;
207238384Sjkim  return NULL;                  // Nothing found prior
208238384Sjkim}
209238384Sjkim
210238384Sjkim//------------------------------Delete---------------------------------------
211238384Sjkim// Find & remove a value from dictionary. Return old value.
212238384Sjkimconst void *Dict::Delete(void *key) {
213238384Sjkim  int i = _hash( key ) & (_size-1);     // Get hash key, corrected for size
214238384Sjkim  bucket *b = &_bin[i];         // Handy shortcut
215238384Sjkim  for( int j=0; j<b->_cnt; j++ )
216238384Sjkim    if( !_cmp(key,b->_keyvals[j+j]) ) {
217238384Sjkim      const void *prior = b->_keyvals[j+j+1];
218238384Sjkim      b->_cnt--;                // Remove key/value from lo bucket
219238384Sjkim      b->_keyvals[j+j  ] = b->_keyvals[b->_cnt+b->_cnt  ];
220238384Sjkim      b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
221238384Sjkim      _cnt--;                   // One less thing in table
222238384Sjkim      return prior;
223238384Sjkim    }
224238384Sjkim  return NULL;
225238384Sjkim}
226238384Sjkim
227238384Sjkim//------------------------------FindDict-------------------------------------
228238384Sjkim// Find a key-value pair in the given dictionary.  If not found, return NULL.
229238384Sjkim// If found, move key-value pair towards head of list.
230238384Sjkimconst void *Dict::operator [](const void *key) const {
231238384Sjkim  int i = _hash( key ) & (_size-1);     // Get hash key, corrected for size
232238384Sjkim  bucket *b = &_bin[i];         // Handy shortcut
233238384Sjkim  for( int j=0; j<b->_cnt; j++ )
234238384Sjkim    if( !_cmp(key,b->_keyvals[j+j]) )
235238384Sjkim      return b->_keyvals[j+j+1];
236238384Sjkim  return NULL;
237238384Sjkim}
238238384Sjkim
239238384Sjkim//------------------------------CmpDict--------------------------------------
240238384Sjkim// CmpDict compares two dictionaries; they must have the same keys (their
241238384Sjkim// keys must match using CmpKey) and they must have the same values (pointer
242238384Sjkim// comparison).  If so 1 is returned, if not 0 is returned.
243238384Sjkimint Dict::operator ==(const Dict &d2) const {
244238384Sjkim  if( _cnt != d2._cnt ) return 0;
245238384Sjkim  if( _hash != d2._hash ) return 0;
246238384Sjkim  if( _cmp != d2._cmp ) return 0;
247238384Sjkim  for( int i=0; i < _size; i++) {       // For complete hash table do
248238384Sjkim    bucket *b = &_bin[i];       // Handy shortcut
249238384Sjkim    if( b->_cnt != d2._bin[i]._cnt ) return 0;
250238384Sjkim    if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) )
251238384Sjkim      return 0;                 // Key-value pairs must match
252238384Sjkim  }
253  return 1;                     // All match, is OK
254}
255
256
257//------------------------------print----------------------------------------
258static void printvoid(const void* x) { printf("%p", x);  }
259void Dict::print() {
260  print(printvoid, printvoid);
261}
262void Dict::print(PrintKeyOrValue print_key, PrintKeyOrValue print_value) {
263  for( int i=0; i < _size; i++) {       // For complete hash table do
264    bucket *b = &_bin[i];       // Handy shortcut
265    for( int j=0; j<b->_cnt; j++ ) {
266      print_key(  b->_keyvals[j+j  ]);
267      printf(" -> ");
268      print_value(b->_keyvals[j+j+1]);
269      printf("\n");
270    }
271  }
272}
273
274//------------------------------Hashing Functions----------------------------
275// Convert string to hash key.  This algorithm implements a universal hash
276// function with the multipliers frozen (ok, so it's not universal).  The
277// multipliers (and allowable characters) are all odd, so the resultant sum
278// is odd - guarenteed not divisible by any power of two, so the hash tables
279// can be any power of two with good results.  Also, I choose multipliers
280// that have only 2 bits set (the low is always set to be odd) so
281// multiplication requires only shifts and adds.  Characters are required to
282// be in the range 0-127 (I double & add 1 to force oddness).  Keys are
283// limited to MAXID characters in length.  Experimental evidence on 150K of
284// C text shows excellent spreading of values for any size hash table.
285int hashstr(const void *t) {
286  register char c, k = 0;
287  register int sum = 0;
288  register const char *s = (const char *)t;
289
290  while( ((c = s[k]) != '\0') && (k < MAXID-1) ) { // Get characters till nul
291    c = (c<<1)+1;               // Characters are always odd!
292    sum += c + (c<<shft[k++]);  // Universal hash function
293  }
294  assert( k < (MAXID + 1), "Exceeded maximum name length");
295  return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size
296}
297
298//------------------------------hashptr--------------------------------------
299// Slimey cheap hash function; no guarenteed performance.  Better than the
300// default for pointers, especially on MS-DOS machines.
301int hashptr(const void *key) {
302#ifdef __TURBOC__
303    return (int)((intptr_t)key >> 16);
304#else  // __TURBOC__
305    return (int)((intptr_t)key >> 2);
306#endif
307}
308
309// Slimey cheap hash function; no guarenteed performance.
310int hashkey(const void *key) {
311  return (int)((intptr_t)key);
312}
313
314//------------------------------Key Comparator Functions---------------------
315int cmpstr(const void *k1, const void *k2) {
316  return strcmp((const char *)k1,(const char *)k2);
317}
318
319// Slimey cheap key comparator.
320int cmpkey(const void *key1, const void *key2) {
321  return (int)((intptr_t)key1 - (intptr_t)key2);
322}
323
324//=============================================================================
325//------------------------------reset------------------------------------------
326// Create an iterator and initialize the first variables.
327void DictI::reset( const Dict *dict ) {
328  _d = dict;                    // The dictionary
329  _i = (int)-1;         // Before the first bin
330  _j = 0;                       // Nothing left in the current bin
331  ++(*this);                    // Step to first real value
332}
333
334//------------------------------next-------------------------------------------
335// Find the next key-value pair in the dictionary, or return a NULL key and
336// value.
337void DictI::operator ++(void) {
338  if( _j-- ) {                  // Still working in current bin?
339    _key   = _d->_bin[_i]._keyvals[_j+_j];
340    _value = _d->_bin[_i]._keyvals[_j+_j+1];
341    return;
342  }
343
344  while( ++_i < _d->_size ) {   // Else scan for non-zero bucket
345    _j = _d->_bin[_i]._cnt;
346    if( !_j ) continue;
347    _j--;
348    _key   = _d->_bin[_i]._keyvals[_j+_j];
349    _value = _d->_bin[_i]._keyvals[_j+_j+1];
350    return;
351  }
352  _key = _value = NULL;
353}
354