src/share/vm/adlc/dict2.cpp

Fri, 27 Feb 2009 13:27:09 -0800

author
twisti
date
Fri, 27 Feb 2009 13:27:09 -0800
changeset 1040
98cb887364d3
parent 997
1580954e694c
child 1063
7bb995fbd3c0
permissions
-rw-r--r--

6810672: Comment typos
Summary: I have collected some typos I have found while looking at the code.
Reviewed-by: kvn, never

duke@435 1 /*
duke@435 2 * Copyright 1998-2002 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // Dictionaries - An Abstract Data Type
duke@435 26
duke@435 27 #include "adlc.hpp"
duke@435 28
duke@435 29 // #include "dict.hpp"
duke@435 30
duke@435 31
duke@435 32 //------------------------------data-----------------------------------------
duke@435 33 // String hash tables
duke@435 34 #define MAXID 20
duke@435 35 static char initflag = 0; // True after 1st initialization
duke@435 36 static char shft[MAXID] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6};
duke@435 37 static short xsum[MAXID + 1];
duke@435 38
duke@435 39 //------------------------------bucket---------------------------------------
duke@435 40 class bucket {
duke@435 41 public:
duke@435 42 int _cnt, _max; // Size of bucket
duke@435 43 const void **_keyvals; // Array of keys and values
duke@435 44 };
duke@435 45
duke@435 46 //------------------------------Dict-----------------------------------------
duke@435 47 // The dictionary is kept has a hash table. The hash table is a even power
duke@435 48 // of two, for nice modulo operations. Each bucket in the hash table points
duke@435 49 // to a linear list of key-value pairs; each key & value is just a (void *).
duke@435 50 // The list starts with a count. A hash lookup finds the list head, then a
duke@435 51 // simple linear scan finds the key. If the table gets too full, it's
duke@435 52 // doubled in size; the total amount of EXTRA times all hash functions are
duke@435 53 // computed for the doubling is no more than the current size - thus the
duke@435 54 // doubling in size costs no more than a constant factor in speed.
duke@435 55 Dict::Dict(CmpKey initcmp, Hash inithash) : _hash(inithash), _cmp(initcmp), _arena(NULL) {
duke@435 56 init();
duke@435 57 }
duke@435 58
duke@435 59 Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena) : _hash(inithash), _cmp(initcmp), _arena(arena) {
duke@435 60 init();
duke@435 61 }
duke@435 62
duke@435 63 void Dict::init() {
duke@435 64 int i;
duke@435 65
duke@435 66 // Precompute table of null character hashes
duke@435 67 if( !initflag ) { // Not initializated yet?
duke@435 68 xsum[0] = (1<<shft[0])+1; // Initialize
duke@435 69 for( i = 1; i < MAXID + 1; i++) {
duke@435 70 xsum[i] = (1<<shft[i])+1+xsum[i-1];
duke@435 71 }
duke@435 72 initflag = 1; // Never again
duke@435 73 }
duke@435 74
duke@435 75 _size = 16; // Size is a power of 2
duke@435 76 _cnt = 0; // Dictionary is empty
duke@435 77 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
duke@435 78 memset(_bin,0,sizeof(bucket)*_size);
duke@435 79 }
duke@435 80
duke@435 81 //------------------------------~Dict------------------------------------------
duke@435 82 // Delete an existing dictionary.
duke@435 83 Dict::~Dict() {
duke@435 84 }
duke@435 85
duke@435 86 //------------------------------Clear----------------------------------------
duke@435 87 // Zap to empty; ready for re-use
duke@435 88 void Dict::Clear() {
duke@435 89 _cnt = 0; // Empty contents
duke@435 90 for( int i=0; i<_size; i++ )
duke@435 91 _bin[i]._cnt = 0; // Empty buckets, but leave allocated
duke@435 92 // Leave _size & _bin alone, under the assumption that dictionary will
duke@435 93 // grow to this size again.
duke@435 94 }
duke@435 95
duke@435 96 //------------------------------doubhash---------------------------------------
duke@435 97 // Double hash table size. If can't do so, just suffer. If can, then run
duke@435 98 // thru old hash table, moving things to new table. Note that since hash
duke@435 99 // table doubled, exactly 1 new bit is exposed in the mask - so everything
duke@435 100 // in the old table ends up on 1 of two lists in the new table; a hi and a
duke@435 101 // lo list depending on the value of the bit.
duke@435 102 void Dict::doubhash(void) {
duke@435 103 int oldsize = _size;
duke@435 104 _size <<= 1; // Double in size
duke@435 105 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*oldsize, sizeof(bucket)*_size );
duke@435 106 memset( &_bin[oldsize], 0, oldsize*sizeof(bucket) );
duke@435 107 // Rehash things to spread into new table
duke@435 108 for( int i=0; i < oldsize; i++) { // For complete OLD table do
duke@435 109 bucket *b = &_bin[i]; // Handy shortcut for _bin[i]
duke@435 110 if( !b->_keyvals ) continue; // Skip empties fast
duke@435 111
duke@435 112 bucket *nb = &_bin[i+oldsize]; // New bucket shortcut
duke@435 113 int j = b->_max; // Trim new bucket to nearest power of 2
duke@435 114 while( j > b->_cnt ) j >>= 1; // above old bucket _cnt
duke@435 115 if( !j ) j = 1; // Handle zero-sized buckets
duke@435 116 nb->_max = j<<1;
duke@435 117 // Allocate worst case space for key-value pairs
duke@435 118 nb->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*nb->_max*2 );
duke@435 119 int nbcnt = 0;
duke@435 120
duke@435 121 for( j=0; j<b->_cnt; j++ ) { // Rehash all keys in this bucket
duke@435 122 const void *key = b->_keyvals[j+j];
duke@435 123 if( (_hash( key ) & (_size-1)) != i ) { // Moving to hi bucket?
duke@435 124 nb->_keyvals[nbcnt+nbcnt] = key;
duke@435 125 nb->_keyvals[nbcnt+nbcnt+1] = b->_keyvals[j+j+1];
duke@435 126 nb->_cnt = nbcnt = nbcnt+1;
duke@435 127 b->_cnt--; // Remove key/value from lo bucket
duke@435 128 b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];
duke@435 129 b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
duke@435 130 j--; // Hash compacted element also
duke@435 131 }
duke@435 132 } // End of for all key-value pairs in bucket
duke@435 133 } // End of for all buckets
duke@435 134
duke@435 135
duke@435 136 }
duke@435 137
duke@435 138 //------------------------------Dict-----------------------------------------
duke@435 139 // Deep copy a dictionary.
duke@435 140 Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) {
duke@435 141 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
duke@435 142 memcpy( _bin, d._bin, sizeof(bucket)*_size );
duke@435 143 for( int i=0; i<_size; i++ ) {
duke@435 144 if( !_bin[i]._keyvals ) continue;
duke@435 145 _bin[i]._keyvals=(const void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2);
duke@435 146 memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*));
duke@435 147 }
duke@435 148 }
duke@435 149
duke@435 150 //------------------------------Dict-----------------------------------------
duke@435 151 // Deep copy a dictionary.
duke@435 152 Dict &Dict::operator =( const Dict &d ) {
duke@435 153 if( _size < d._size ) { // If must have more buckets
duke@435 154 _arena = d._arena;
duke@435 155 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );
duke@435 156 memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) );
duke@435 157 _size = d._size;
duke@435 158 }
duke@435 159 for( int i=0; i<_size; i++ ) // All buckets are empty
duke@435 160 _bin[i]._cnt = 0; // But leave bucket allocations alone
duke@435 161 _cnt = d._cnt;
duke@435 162 *(Hash*)(&_hash) = d._hash;
duke@435 163 *(CmpKey*)(&_cmp) = d._cmp;
duke@435 164 for(int k=0; k<_size; k++ ) {
duke@435 165 bucket *b = &d._bin[k]; // Shortcut to source bucket
duke@435 166 for( int j=0; j<b->_cnt; j++ )
duke@435 167 Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] );
duke@435 168 }
duke@435 169 return *this;
duke@435 170 }
duke@435 171
duke@435 172 //------------------------------Insert---------------------------------------
duke@435 173 // Insert or replace a key/value pair in the given dictionary. If the
duke@435 174 // dictionary is too full, it's size is doubled. The prior value being
duke@435 175 // replaced is returned (NULL if this is a 1st insertion of that key). If
duke@435 176 // an old value is found, it's swapped with the prior key-value pair on the
duke@435 177 // list. This moves a commonly searched-for value towards the list head.
duke@435 178 const void *Dict::Insert(const void *key, const void *val) {
duke@435 179 int hash = _hash( key ); // Get hash key
duke@435 180 int i = hash & (_size-1); // Get hash key, corrected for size
duke@435 181 bucket *b = &_bin[i]; // Handy shortcut
duke@435 182 for( int j=0; j<b->_cnt; j++ )
duke@435 183 if( !_cmp(key,b->_keyvals[j+j]) ) {
duke@435 184 const void *prior = b->_keyvals[j+j+1];
duke@435 185 b->_keyvals[j+j ] = key; // Insert current key-value
duke@435 186 b->_keyvals[j+j+1] = val;
duke@435 187 return prior; // Return prior
duke@435 188 }
duke@435 189
duke@435 190 if( ++_cnt > _size ) { // Hash table is full
duke@435 191 doubhash(); // Grow whole table if too full
duke@435 192 i = hash & (_size-1); // Rehash
duke@435 193 b = &_bin[i]; // Handy shortcut
duke@435 194 }
duke@435 195 if( b->_cnt == b->_max ) { // Must grow bucket?
duke@435 196 if( !b->_keyvals ) {
duke@435 197 b->_max = 2; // Initial bucket size
duke@435 198 b->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*b->_max*2 );
duke@435 199 } else {
duke@435 200 b->_keyvals = (const void**)_arena->Arealloc( b->_keyvals, sizeof(void *)*b->_max*2, sizeof(void *)*b->_max*4 );
duke@435 201 b->_max <<= 1; // Double bucket
duke@435 202 }
duke@435 203 }
duke@435 204 b->_keyvals[b->_cnt+b->_cnt ] = key;
duke@435 205 b->_keyvals[b->_cnt+b->_cnt+1] = val;
duke@435 206 b->_cnt++;
duke@435 207 return NULL; // Nothing found prior
duke@435 208 }
duke@435 209
duke@435 210 //------------------------------Delete---------------------------------------
duke@435 211 // Find & remove a value from dictionary. Return old value.
duke@435 212 const void *Dict::Delete(void *key) {
duke@435 213 int i = _hash( key ) & (_size-1); // Get hash key, corrected for size
duke@435 214 bucket *b = &_bin[i]; // Handy shortcut
duke@435 215 for( int j=0; j<b->_cnt; j++ )
duke@435 216 if( !_cmp(key,b->_keyvals[j+j]) ) {
duke@435 217 const void *prior = b->_keyvals[j+j+1];
duke@435 218 b->_cnt--; // Remove key/value from lo bucket
duke@435 219 b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];
duke@435 220 b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
duke@435 221 _cnt--; // One less thing in table
duke@435 222 return prior;
duke@435 223 }
duke@435 224 return NULL;
duke@435 225 }
duke@435 226
duke@435 227 //------------------------------FindDict-------------------------------------
duke@435 228 // Find a key-value pair in the given dictionary. If not found, return NULL.
duke@435 229 // If found, move key-value pair towards head of list.
duke@435 230 const void *Dict::operator [](const void *key) const {
duke@435 231 int i = _hash( key ) & (_size-1); // Get hash key, corrected for size
duke@435 232 bucket *b = &_bin[i]; // Handy shortcut
duke@435 233 for( int j=0; j<b->_cnt; j++ )
duke@435 234 if( !_cmp(key,b->_keyvals[j+j]) )
duke@435 235 return b->_keyvals[j+j+1];
duke@435 236 return NULL;
duke@435 237 }
duke@435 238
duke@435 239 //------------------------------CmpDict--------------------------------------
duke@435 240 // CmpDict compares two dictionaries; they must have the same keys (their
duke@435 241 // keys must match using CmpKey) and they must have the same values (pointer
duke@435 242 // comparison). If so 1 is returned, if not 0 is returned.
duke@435 243 int Dict::operator ==(const Dict &d2) const {
duke@435 244 if( _cnt != d2._cnt ) return 0;
duke@435 245 if( _hash != d2._hash ) return 0;
duke@435 246 if( _cmp != d2._cmp ) return 0;
duke@435 247 for( int i=0; i < _size; i++) { // For complete hash table do
duke@435 248 bucket *b = &_bin[i]; // Handy shortcut
duke@435 249 if( b->_cnt != d2._bin[i]._cnt ) return 0;
duke@435 250 if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) )
duke@435 251 return 0; // Key-value pairs must match
duke@435 252 }
duke@435 253 return 1; // All match, is OK
duke@435 254 }
duke@435 255
duke@435 256
duke@435 257 //------------------------------print----------------------------------------
duke@435 258 static void printvoid(const void* x) { printf("%p", x); }
duke@435 259 void Dict::print() {
duke@435 260 print(printvoid, printvoid);
duke@435 261 }
duke@435 262 void Dict::print(PrintKeyOrValue print_key, PrintKeyOrValue print_value) {
duke@435 263 for( int i=0; i < _size; i++) { // For complete hash table do
duke@435 264 bucket *b = &_bin[i]; // Handy shortcut
duke@435 265 for( int j=0; j<b->_cnt; j++ ) {
duke@435 266 print_key( b->_keyvals[j+j ]);
duke@435 267 printf(" -> ");
duke@435 268 print_value(b->_keyvals[j+j+1]);
duke@435 269 printf("\n");
duke@435 270 }
duke@435 271 }
duke@435 272 }
duke@435 273
duke@435 274 //------------------------------Hashing Functions----------------------------
duke@435 275 // Convert string to hash key. This algorithm implements a universal hash
duke@435 276 // function with the multipliers frozen (ok, so it's not universal). The
duke@435 277 // multipliers (and allowable characters) are all odd, so the resultant sum
twisti@1040 278 // is odd - guaranteed not divisible by any power of two, so the hash tables
duke@435 279 // can be any power of two with good results. Also, I choose multipliers
duke@435 280 // that have only 2 bits set (the low is always set to be odd) so
duke@435 281 // multiplication requires only shifts and adds. Characters are required to
duke@435 282 // be in the range 0-127 (I double & add 1 to force oddness). Keys are
duke@435 283 // limited to MAXID characters in length. Experimental evidence on 150K of
duke@435 284 // C text shows excellent spreading of values for any size hash table.
duke@435 285 int hashstr(const void *t) {
duke@435 286 register char c, k = 0;
duke@435 287 register int sum = 0;
duke@435 288 register const char *s = (const char *)t;
duke@435 289
duke@435 290 while( ((c = s[k]) != '\0') && (k < MAXID-1) ) { // Get characters till nul
duke@435 291 c = (c<<1)+1; // Characters are always odd!
duke@435 292 sum += c + (c<<shft[k++]); // Universal hash function
duke@435 293 }
duke@435 294 assert( k < (MAXID + 1), "Exceeded maximum name length");
duke@435 295 return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size
duke@435 296 }
duke@435 297
duke@435 298 //------------------------------hashptr--------------------------------------
twisti@1040 299 // Slimey cheap hash function; no guaranteed performance. Better than the
duke@435 300 // default for pointers, especially on MS-DOS machines.
duke@435 301 int hashptr(const void *key) {
duke@435 302 #ifdef __TURBOC__
duke@435 303 return (int)((intptr_t)key >> 16);
duke@435 304 #else // __TURBOC__
duke@435 305 return (int)((intptr_t)key >> 2);
duke@435 306 #endif
duke@435 307 }
duke@435 308
twisti@1040 309 // Slimey cheap hash function; no guaranteed performance.
duke@435 310 int hashkey(const void *key) {
duke@435 311 return (int)((intptr_t)key);
duke@435 312 }
duke@435 313
duke@435 314 //------------------------------Key Comparator Functions---------------------
duke@435 315 int cmpstr(const void *k1, const void *k2) {
duke@435 316 return strcmp((const char *)k1,(const char *)k2);
duke@435 317 }
duke@435 318
never@997 319 // Cheap key comparator.
duke@435 320 int cmpkey(const void *key1, const void *key2) {
never@997 321 if (key1 == key2) return 0;
never@997 322 intptr_t delta = (intptr_t)key1 - (intptr_t)key2;
never@997 323 if (delta > 0) return 1;
never@997 324 return -1;
duke@435 325 }
duke@435 326
duke@435 327 //=============================================================================
duke@435 328 //------------------------------reset------------------------------------------
duke@435 329 // Create an iterator and initialize the first variables.
duke@435 330 void DictI::reset( const Dict *dict ) {
duke@435 331 _d = dict; // The dictionary
duke@435 332 _i = (int)-1; // Before the first bin
duke@435 333 _j = 0; // Nothing left in the current bin
duke@435 334 ++(*this); // Step to first real value
duke@435 335 }
duke@435 336
duke@435 337 //------------------------------next-------------------------------------------
duke@435 338 // Find the next key-value pair in the dictionary, or return a NULL key and
duke@435 339 // value.
duke@435 340 void DictI::operator ++(void) {
duke@435 341 if( _j-- ) { // Still working in current bin?
duke@435 342 _key = _d->_bin[_i]._keyvals[_j+_j];
duke@435 343 _value = _d->_bin[_i]._keyvals[_j+_j+1];
duke@435 344 return;
duke@435 345 }
duke@435 346
duke@435 347 while( ++_i < _d->_size ) { // Else scan for non-zero bucket
duke@435 348 _j = _d->_bin[_i]._cnt;
duke@435 349 if( !_j ) continue;
duke@435 350 _j--;
duke@435 351 _key = _d->_bin[_i]._keyvals[_j+_j];
duke@435 352 _value = _d->_bin[_i]._keyvals[_j+_j+1];
duke@435 353 return;
duke@435 354 }
duke@435 355 _key = _value = NULL;
duke@435 356 }

mercurial