duke@435: /* mikael@6198: * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: // Dictionaries - An Abstract Data Type duke@435: duke@435: #include "adlc.hpp" duke@435: duke@435: // #include "dict.hpp" duke@435: duke@435: duke@435: //------------------------------data----------------------------------------- duke@435: // String hash tables duke@435: #define MAXID 20 duke@435: static char initflag = 0; // True after 1st initialization kvn@4161: static char shft[MAXID + 1] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7}; never@2402: static short xsum[MAXID]; duke@435: duke@435: //------------------------------bucket--------------------------------------- duke@435: class bucket { duke@435: public: duke@435: int _cnt, _max; // Size of bucket duke@435: const void **_keyvals; // Array of keys and values duke@435: }; duke@435: duke@435: //------------------------------Dict----------------------------------------- duke@435: // The dictionary is kept has a hash table. The hash table is a even power duke@435: // of two, for nice modulo operations. Each bucket in the hash table points duke@435: // to a linear list of key-value pairs; each key & value is just a (void *). duke@435: // The list starts with a count. A hash lookup finds the list head, then a duke@435: // simple linear scan finds the key. If the table gets too full, it's duke@435: // doubled in size; the total amount of EXTRA times all hash functions are duke@435: // computed for the doubling is no more than the current size - thus the duke@435: // doubling in size costs no more than a constant factor in speed. duke@435: Dict::Dict(CmpKey initcmp, Hash inithash) : _hash(inithash), _cmp(initcmp), _arena(NULL) { duke@435: init(); duke@435: } duke@435: duke@435: Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena) : _hash(inithash), _cmp(initcmp), _arena(arena) { duke@435: init(); duke@435: } duke@435: duke@435: void Dict::init() { duke@435: int i; duke@435: duke@435: // Precompute table of null character hashes twisti@5221: if (!initflag) { // Not initializated yet? twisti@5221: xsum[0] = (short) ((1 << shft[0]) + 1); // Initialize never@2402: for( i = 1; i < MAXID; i++) { twisti@5221: xsum[i] = (short) ((1 << shft[i]) + 1 + xsum[i-1]); duke@435: } duke@435: initflag = 1; // Never again duke@435: } duke@435: duke@435: _size = 16; // Size is a power of 2 duke@435: _cnt = 0; // Dictionary is empty twisti@5221: _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket) * _size); twisti@5221: memset(_bin, 0, sizeof(bucket) * _size); duke@435: } duke@435: duke@435: //------------------------------~Dict------------------------------------------ duke@435: // Delete an existing dictionary. duke@435: Dict::~Dict() { duke@435: } duke@435: duke@435: //------------------------------Clear---------------------------------------- duke@435: // Zap to empty; ready for re-use duke@435: void Dict::Clear() { duke@435: _cnt = 0; // Empty contents duke@435: for( int i=0; i<_size; i++ ) duke@435: _bin[i]._cnt = 0; // Empty buckets, but leave allocated duke@435: // Leave _size & _bin alone, under the assumption that dictionary will duke@435: // grow to this size again. duke@435: } duke@435: duke@435: //------------------------------doubhash--------------------------------------- duke@435: // Double hash table size. If can't do so, just suffer. If can, then run duke@435: // thru old hash table, moving things to new table. Note that since hash duke@435: // table doubled, exactly 1 new bit is exposed in the mask - so everything duke@435: // in the old table ends up on 1 of two lists in the new table; a hi and a duke@435: // lo list depending on the value of the bit. duke@435: void Dict::doubhash(void) { duke@435: int oldsize = _size; duke@435: _size <<= 1; // Double in size duke@435: _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*oldsize, sizeof(bucket)*_size ); duke@435: memset( &_bin[oldsize], 0, oldsize*sizeof(bucket) ); duke@435: // Rehash things to spread into new table duke@435: for( int i=0; i < oldsize; i++) { // For complete OLD table do duke@435: bucket *b = &_bin[i]; // Handy shortcut for _bin[i] duke@435: if( !b->_keyvals ) continue; // Skip empties fast duke@435: duke@435: bucket *nb = &_bin[i+oldsize]; // New bucket shortcut duke@435: int j = b->_max; // Trim new bucket to nearest power of 2 duke@435: while( j > b->_cnt ) j >>= 1; // above old bucket _cnt duke@435: if( !j ) j = 1; // Handle zero-sized buckets duke@435: nb->_max = j<<1; duke@435: // Allocate worst case space for key-value pairs duke@435: nb->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*nb->_max*2 ); duke@435: int nbcnt = 0; duke@435: duke@435: for( j=0; j_cnt; j++ ) { // Rehash all keys in this bucket duke@435: const void *key = b->_keyvals[j+j]; duke@435: if( (_hash( key ) & (_size-1)) != i ) { // Moving to hi bucket? duke@435: nb->_keyvals[nbcnt+nbcnt] = key; duke@435: nb->_keyvals[nbcnt+nbcnt+1] = b->_keyvals[j+j+1]; duke@435: nb->_cnt = nbcnt = nbcnt+1; duke@435: b->_cnt--; // Remove key/value from lo bucket duke@435: b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ]; duke@435: b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1]; duke@435: j--; // Hash compacted element also duke@435: } duke@435: } // End of for all key-value pairs in bucket duke@435: } // End of for all buckets duke@435: duke@435: duke@435: } duke@435: duke@435: //------------------------------Dict----------------------------------------- duke@435: // Deep copy a dictionary. duke@435: Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) { duke@435: _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size); duke@435: memcpy( _bin, d._bin, sizeof(bucket)*_size ); duke@435: for( int i=0; i<_size; i++ ) { duke@435: if( !_bin[i]._keyvals ) continue; duke@435: _bin[i]._keyvals=(const void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2); duke@435: memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*)); duke@435: } duke@435: } duke@435: duke@435: //------------------------------Dict----------------------------------------- duke@435: // Deep copy a dictionary. duke@435: Dict &Dict::operator =( const Dict &d ) { duke@435: if( _size < d._size ) { // If must have more buckets duke@435: _arena = d._arena; duke@435: _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size ); duke@435: memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) ); duke@435: _size = d._size; duke@435: } duke@435: for( int i=0; i<_size; i++ ) // All buckets are empty duke@435: _bin[i]._cnt = 0; // But leave bucket allocations alone duke@435: _cnt = d._cnt; duke@435: *(Hash*)(&_hash) = d._hash; duke@435: *(CmpKey*)(&_cmp) = d._cmp; duke@435: for(int k=0; k<_size; k++ ) { duke@435: bucket *b = &d._bin[k]; // Shortcut to source bucket duke@435: for( int j=0; j_cnt; j++ ) duke@435: Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] ); duke@435: } duke@435: return *this; duke@435: } duke@435: duke@435: //------------------------------Insert--------------------------------------- duke@435: // Insert or replace a key/value pair in the given dictionary. If the duke@435: // dictionary is too full, it's size is doubled. The prior value being duke@435: // replaced is returned (NULL if this is a 1st insertion of that key). If duke@435: // an old value is found, it's swapped with the prior key-value pair on the duke@435: // list. This moves a commonly searched-for value towards the list head. duke@435: const void *Dict::Insert(const void *key, const void *val) { duke@435: int hash = _hash( key ); // Get hash key duke@435: int i = hash & (_size-1); // Get hash key, corrected for size duke@435: bucket *b = &_bin[i]; // Handy shortcut duke@435: for( int j=0; j_cnt; j++ ) duke@435: if( !_cmp(key,b->_keyvals[j+j]) ) { duke@435: const void *prior = b->_keyvals[j+j+1]; duke@435: b->_keyvals[j+j ] = key; // Insert current key-value duke@435: b->_keyvals[j+j+1] = val; duke@435: return prior; // Return prior duke@435: } duke@435: duke@435: if( ++_cnt > _size ) { // Hash table is full duke@435: doubhash(); // Grow whole table if too full duke@435: i = hash & (_size-1); // Rehash duke@435: b = &_bin[i]; // Handy shortcut duke@435: } duke@435: if( b->_cnt == b->_max ) { // Must grow bucket? duke@435: if( !b->_keyvals ) { duke@435: b->_max = 2; // Initial bucket size duke@435: b->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*b->_max*2 ); duke@435: } else { duke@435: b->_keyvals = (const void**)_arena->Arealloc( b->_keyvals, sizeof(void *)*b->_max*2, sizeof(void *)*b->_max*4 ); duke@435: b->_max <<= 1; // Double bucket duke@435: } duke@435: } duke@435: b->_keyvals[b->_cnt+b->_cnt ] = key; duke@435: b->_keyvals[b->_cnt+b->_cnt+1] = val; duke@435: b->_cnt++; duke@435: return NULL; // Nothing found prior duke@435: } duke@435: duke@435: //------------------------------Delete--------------------------------------- duke@435: // Find & remove a value from dictionary. Return old value. duke@435: const void *Dict::Delete(void *key) { duke@435: int i = _hash( key ) & (_size-1); // Get hash key, corrected for size duke@435: bucket *b = &_bin[i]; // Handy shortcut duke@435: for( int j=0; j_cnt; j++ ) duke@435: if( !_cmp(key,b->_keyvals[j+j]) ) { duke@435: const void *prior = b->_keyvals[j+j+1]; duke@435: b->_cnt--; // Remove key/value from lo bucket duke@435: b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ]; duke@435: b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1]; duke@435: _cnt--; // One less thing in table duke@435: return prior; duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------FindDict------------------------------------- duke@435: // Find a key-value pair in the given dictionary. If not found, return NULL. duke@435: // If found, move key-value pair towards head of list. duke@435: const void *Dict::operator [](const void *key) const { duke@435: int i = _hash( key ) & (_size-1); // Get hash key, corrected for size duke@435: bucket *b = &_bin[i]; // Handy shortcut duke@435: for( int j=0; j_cnt; j++ ) duke@435: if( !_cmp(key,b->_keyvals[j+j]) ) duke@435: return b->_keyvals[j+j+1]; duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------CmpDict-------------------------------------- duke@435: // CmpDict compares two dictionaries; they must have the same keys (their duke@435: // keys must match using CmpKey) and they must have the same values (pointer duke@435: // comparison). If so 1 is returned, if not 0 is returned. duke@435: int Dict::operator ==(const Dict &d2) const { duke@435: if( _cnt != d2._cnt ) return 0; duke@435: if( _hash != d2._hash ) return 0; duke@435: if( _cmp != d2._cmp ) return 0; duke@435: for( int i=0; i < _size; i++) { // For complete hash table do duke@435: bucket *b = &_bin[i]; // Handy shortcut duke@435: if( b->_cnt != d2._bin[i]._cnt ) return 0; duke@435: if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) ) duke@435: return 0; // Key-value pairs must match duke@435: } duke@435: return 1; // All match, is OK duke@435: } duke@435: duke@435: duke@435: //------------------------------print---------------------------------------- duke@435: static void printvoid(const void* x) { printf("%p", x); } duke@435: void Dict::print() { duke@435: print(printvoid, printvoid); duke@435: } duke@435: void Dict::print(PrintKeyOrValue print_key, PrintKeyOrValue print_value) { duke@435: for( int i=0; i < _size; i++) { // For complete hash table do duke@435: bucket *b = &_bin[i]; // Handy shortcut duke@435: for( int j=0; j_cnt; j++ ) { duke@435: print_key( b->_keyvals[j+j ]); duke@435: printf(" -> "); duke@435: print_value(b->_keyvals[j+j+1]); duke@435: printf("\n"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: //------------------------------Hashing Functions---------------------------- duke@435: // Convert string to hash key. This algorithm implements a universal hash duke@435: // function with the multipliers frozen (ok, so it's not universal). The duke@435: // multipliers (and allowable characters) are all odd, so the resultant sum twisti@1040: // is odd - guaranteed not divisible by any power of two, so the hash tables duke@435: // can be any power of two with good results. Also, I choose multipliers duke@435: // that have only 2 bits set (the low is always set to be odd) so duke@435: // multiplication requires only shifts and adds. Characters are required to duke@435: // be in the range 0-127 (I double & add 1 to force oddness). Keys are duke@435: // limited to MAXID characters in length. Experimental evidence on 150K of duke@435: // C text shows excellent spreading of values for any size hash table. duke@435: int hashstr(const void *t) { duke@435: register char c, k = 0; duke@435: register int sum = 0; duke@435: register const char *s = (const char *)t; duke@435: twisti@5221: while (((c = s[k]) != '\0') && (k < MAXID-1)) { // Get characters till nul twisti@5221: c = (char) ((c << 1) + 1); // Characters are always odd! twisti@5221: sum += c + (c << shft[k++]); // Universal hash function duke@435: } twisti@5221: assert(k < (MAXID), "Exceeded maximum name length"); duke@435: return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size duke@435: } duke@435: duke@435: //------------------------------hashptr-------------------------------------- twisti@1040: // Slimey cheap hash function; no guaranteed performance. Better than the duke@435: // default for pointers, especially on MS-DOS machines. duke@435: int hashptr(const void *key) { duke@435: #ifdef __TURBOC__ duke@435: return (int)((intptr_t)key >> 16); duke@435: #else // __TURBOC__ duke@435: return (int)((intptr_t)key >> 2); duke@435: #endif duke@435: } duke@435: twisti@1040: // Slimey cheap hash function; no guaranteed performance. duke@435: int hashkey(const void *key) { duke@435: return (int)((intptr_t)key); duke@435: } duke@435: duke@435: //------------------------------Key Comparator Functions--------------------- duke@435: int cmpstr(const void *k1, const void *k2) { duke@435: return strcmp((const char *)k1,(const char *)k2); duke@435: } duke@435: never@997: // Cheap key comparator. duke@435: int cmpkey(const void *key1, const void *key2) { never@997: if (key1 == key2) return 0; never@997: intptr_t delta = (intptr_t)key1 - (intptr_t)key2; never@997: if (delta > 0) return 1; never@997: return -1; duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------reset------------------------------------------ duke@435: // Create an iterator and initialize the first variables. duke@435: void DictI::reset( const Dict *dict ) { duke@435: _d = dict; // The dictionary duke@435: _i = (int)-1; // Before the first bin duke@435: _j = 0; // Nothing left in the current bin duke@435: ++(*this); // Step to first real value duke@435: } duke@435: duke@435: //------------------------------next------------------------------------------- duke@435: // Find the next key-value pair in the dictionary, or return a NULL key and duke@435: // value. duke@435: void DictI::operator ++(void) { duke@435: if( _j-- ) { // Still working in current bin? duke@435: _key = _d->_bin[_i]._keyvals[_j+_j]; duke@435: _value = _d->_bin[_i]._keyvals[_j+_j+1]; duke@435: return; duke@435: } duke@435: duke@435: while( ++_i < _d->_size ) { // Else scan for non-zero bucket duke@435: _j = _d->_bin[_i]._cnt; duke@435: if( !_j ) continue; duke@435: _j--; duke@435: _key = _d->_bin[_i]._keyvals[_j+_j]; duke@435: _value = _d->_bin[_i]._keyvals[_j+_j+1]; duke@435: return; duke@435: } duke@435: _key = _value = NULL; duke@435: }