src/share/vm/libadt/dict.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 1997-2003 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 "incls/_precompiled.incl"
duke@435 28 #include "incls/_dict.cpp.incl"
duke@435 29
duke@435 30 // %%%%% includes not needed with AVM framework - Ungar
duke@435 31
duke@435 32 // #include "port.hpp"
duke@435 33 //IMPLEMENTATION
duke@435 34 // #include "dict.hpp"
duke@435 35
duke@435 36 #include <assert.h>
duke@435 37
duke@435 38 // The iostream is not needed and it gets confused for gcc by the
duke@435 39 // define of bool.
duke@435 40 //
duke@435 41 // #include <iostream.h>
duke@435 42
duke@435 43 //------------------------------data-----------------------------------------
duke@435 44 // String hash tables
duke@435 45 #define MAXID 20
duke@435 46 static byte initflag = 0; // True after 1st initialization
duke@435 47 static const 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 48 static short xsum[MAXID];
duke@435 49
duke@435 50 //------------------------------bucket---------------------------------------
duke@435 51 class bucket : public ResourceObj {
duke@435 52 public:
duke@435 53 uint _cnt, _max; // Size of bucket
duke@435 54 void **_keyvals; // Array of keys and values
duke@435 55 };
duke@435 56
duke@435 57 //------------------------------Dict-----------------------------------------
duke@435 58 // The dictionary is kept has a hash table. The hash table is a even power
duke@435 59 // of two, for nice modulo operations. Each bucket in the hash table points
duke@435 60 // to a linear list of key-value pairs; each key & value is just a (void *).
duke@435 61 // The list starts with a count. A hash lookup finds the list head, then a
duke@435 62 // simple linear scan finds the key. If the table gets too full, it's
duke@435 63 // doubled in size; the total amount of EXTRA times all hash functions are
duke@435 64 // computed for the doubling is no more than the current size - thus the
duke@435 65 // doubling in size costs no more than a constant factor in speed.
duke@435 66 Dict::Dict(CmpKey initcmp, Hash inithash) : _hash(inithash), _cmp(initcmp),
duke@435 67 _arena(Thread::current()->resource_area()) {
duke@435 68 int i;
duke@435 69
duke@435 70 // Precompute table of null character hashes
duke@435 71 if( !initflag ) { // Not initializated yet?
duke@435 72 xsum[0] = (1<<shft[0])+1; // Initialize
duke@435 73 for(i=1; i<MAXID; i++) {
duke@435 74 xsum[i] = (1<<shft[i])+1+xsum[i-1];
duke@435 75 }
duke@435 76 initflag = 1; // Never again
duke@435 77 }
duke@435 78
duke@435 79 _size = 16; // Size is a power of 2
duke@435 80 _cnt = 0; // Dictionary is empty
duke@435 81 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
duke@435 82 memset(_bin,0,sizeof(bucket)*_size);
duke@435 83 }
duke@435 84
duke@435 85 Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena, int size)
duke@435 86 : _hash(inithash), _cmp(initcmp), _arena(arena) {
duke@435 87 int i;
duke@435 88
duke@435 89 // Precompute table of null character hashes
duke@435 90 if( !initflag ) { // Not initializated yet?
duke@435 91 xsum[0] = (1<<shft[0])+1; // Initialize
duke@435 92 for(i=1; i<MAXID; i++) {
duke@435 93 xsum[i] = (1<<shft[i])+1+xsum[i-1];
duke@435 94 }
duke@435 95 initflag = 1; // Never again
duke@435 96 }
duke@435 97
duke@435 98 i=16;
duke@435 99 while( i < size ) i <<= 1;
duke@435 100 _size = i; // Size is a power of 2
duke@435 101 _cnt = 0; // Dictionary is empty
duke@435 102 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
duke@435 103 memset(_bin,0,sizeof(bucket)*_size);
duke@435 104 }
duke@435 105
duke@435 106 //------------------------------~Dict------------------------------------------
duke@435 107 // Delete an existing dictionary.
duke@435 108 Dict::~Dict() {
duke@435 109 /*
duke@435 110 tty->print("~Dict %d/%d: ",_cnt,_size);
duke@435 111 for( uint i=0; i < _size; i++) // For complete new table do
duke@435 112 tty->print("%d ",_bin[i]._cnt);
duke@435 113 tty->print("\n");*/
duke@435 114 /*for( uint i=0; i<_size; i++ ) {
duke@435 115 FREE_FAST( _bin[i]._keyvals );
duke@435 116 } */
duke@435 117 }
duke@435 118
duke@435 119 //------------------------------Clear----------------------------------------
duke@435 120 // Zap to empty; ready for re-use
duke@435 121 void Dict::Clear() {
duke@435 122 _cnt = 0; // Empty contents
duke@435 123 for( uint i=0; i<_size; i++ )
duke@435 124 _bin[i]._cnt = 0; // Empty buckets, but leave allocated
duke@435 125 // Leave _size & _bin alone, under the assumption that dictionary will
duke@435 126 // grow to this size again.
duke@435 127 }
duke@435 128
duke@435 129 //------------------------------doubhash---------------------------------------
duke@435 130 // Double hash table size. If can't do so, just suffer. If can, then run
duke@435 131 // thru old hash table, moving things to new table. Note that since hash
duke@435 132 // table doubled, exactly 1 new bit is exposed in the mask - so everything
duke@435 133 // in the old table ends up on 1 of two lists in the new table; a hi and a
duke@435 134 // lo list depending on the value of the bit.
duke@435 135 void Dict::doubhash(void) {
duke@435 136 uint oldsize = _size;
duke@435 137 _size <<= 1; // Double in size
duke@435 138 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*oldsize, sizeof(bucket)*_size );
duke@435 139 memset( &_bin[oldsize], 0, oldsize*sizeof(bucket) );
duke@435 140 // Rehash things to spread into new table
duke@435 141 for( uint i=0; i < oldsize; i++) { // For complete OLD table do
duke@435 142 bucket *b = &_bin[i]; // Handy shortcut for _bin[i]
duke@435 143 if( !b->_keyvals ) continue; // Skip empties fast
duke@435 144
duke@435 145 bucket *nb = &_bin[i+oldsize]; // New bucket shortcut
duke@435 146 uint j = b->_max; // Trim new bucket to nearest power of 2
duke@435 147 while( j > b->_cnt ) j >>= 1; // above old bucket _cnt
duke@435 148 if( !j ) j = 1; // Handle zero-sized buckets
duke@435 149 nb->_max = j<<1;
duke@435 150 // Allocate worst case space for key-value pairs
duke@435 151 nb->_keyvals = (void**)_arena->Amalloc_4( sizeof(void *)*nb->_max*2 );
duke@435 152 uint nbcnt = 0;
duke@435 153
duke@435 154 for( j=0; j<b->_cnt; j++ ) { // Rehash all keys in this bucket
duke@435 155 void *key = b->_keyvals[j+j];
duke@435 156 if( (_hash( key ) & (_size-1)) != i ) { // Moving to hi bucket?
duke@435 157 nb->_keyvals[nbcnt+nbcnt] = key;
duke@435 158 nb->_keyvals[nbcnt+nbcnt+1] = b->_keyvals[j+j+1];
duke@435 159 nb->_cnt = nbcnt = nbcnt+1;
duke@435 160 b->_cnt--; // Remove key/value from lo bucket
duke@435 161 b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];
duke@435 162 b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
duke@435 163 j--; // Hash compacted element also
duke@435 164 }
duke@435 165 } // End of for all key-value pairs in bucket
duke@435 166 } // End of for all buckets
duke@435 167
duke@435 168
duke@435 169 }
duke@435 170
duke@435 171 //------------------------------Dict-----------------------------------------
duke@435 172 // Deep copy a dictionary.
duke@435 173 Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) {
duke@435 174 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
duke@435 175 memcpy( _bin, d._bin, sizeof(bucket)*_size );
duke@435 176 for( uint i=0; i<_size; i++ ) {
duke@435 177 if( !_bin[i]._keyvals ) continue;
duke@435 178 _bin[i]._keyvals=(void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2);
duke@435 179 memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*));
duke@435 180 }
duke@435 181 }
duke@435 182
duke@435 183 //------------------------------Dict-----------------------------------------
duke@435 184 // Deep copy a dictionary.
duke@435 185 Dict &Dict::operator =( const Dict &d ) {
duke@435 186 if( _size < d._size ) { // If must have more buckets
duke@435 187 _arena = d._arena;
duke@435 188 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );
duke@435 189 memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) );
duke@435 190 _size = d._size;
duke@435 191 }
duke@435 192 uint i;
duke@435 193 for( i=0; i<_size; i++ ) // All buckets are empty
duke@435 194 _bin[i]._cnt = 0; // But leave bucket allocations alone
duke@435 195 _cnt = d._cnt;
duke@435 196 *(Hash*)(&_hash) = d._hash;
duke@435 197 *(CmpKey*)(&_cmp) = d._cmp;
duke@435 198 for( i=0; i<_size; i++ ) {
duke@435 199 bucket *b = &d._bin[i]; // Shortcut to source bucket
duke@435 200 for( uint j=0; j<b->_cnt; j++ )
duke@435 201 Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] );
duke@435 202 }
duke@435 203 return *this;
duke@435 204 }
duke@435 205
duke@435 206 //------------------------------Insert----------------------------------------
duke@435 207 // Insert or replace a key/value pair in the given dictionary. If the
duke@435 208 // dictionary is too full, it's size is doubled. The prior value being
duke@435 209 // replaced is returned (NULL if this is a 1st insertion of that key). If
duke@435 210 // an old value is found, it's swapped with the prior key-value pair on the
duke@435 211 // list. This moves a commonly searched-for value towards the list head.
duke@435 212 void *Dict::Insert(void *key, void *val, bool replace) {
duke@435 213 uint hash = _hash( key ); // Get hash key
duke@435 214 uint i = hash & (_size-1); // Get hash key, corrected for size
duke@435 215 bucket *b = &_bin[i]; // Handy shortcut
duke@435 216 for( uint j=0; j<b->_cnt; j++ ) {
duke@435 217 if( !_cmp(key,b->_keyvals[j+j]) ) {
duke@435 218 if (!replace) {
duke@435 219 return b->_keyvals[j+j+1];
duke@435 220 } else {
duke@435 221 void *prior = b->_keyvals[j+j+1];
duke@435 222 b->_keyvals[j+j ] = key; // Insert current key-value
duke@435 223 b->_keyvals[j+j+1] = val;
duke@435 224 return prior; // Return prior
duke@435 225 }
duke@435 226 }
duke@435 227 }
duke@435 228 if( ++_cnt > _size ) { // Hash table is full
duke@435 229 doubhash(); // Grow whole table if too full
duke@435 230 i = hash & (_size-1); // Rehash
duke@435 231 b = &_bin[i]; // Handy shortcut
duke@435 232 }
duke@435 233 if( b->_cnt == b->_max ) { // Must grow bucket?
duke@435 234 if( !b->_keyvals ) {
duke@435 235 b->_max = 2; // Initial bucket size
duke@435 236 b->_keyvals = (void**)_arena->Amalloc_4(sizeof(void*) * b->_max * 2);
duke@435 237 } else {
duke@435 238 b->_keyvals = (void**)_arena->Arealloc(b->_keyvals, sizeof(void*) * b->_max * 2, sizeof(void*) * b->_max * 4);
duke@435 239 b->_max <<= 1; // Double bucket
duke@435 240 }
duke@435 241 }
duke@435 242 b->_keyvals[b->_cnt+b->_cnt ] = key;
duke@435 243 b->_keyvals[b->_cnt+b->_cnt+1] = val;
duke@435 244 b->_cnt++;
duke@435 245 return NULL; // Nothing found prior
duke@435 246 }
duke@435 247
duke@435 248 //------------------------------Delete---------------------------------------
duke@435 249 // Find & remove a value from dictionary. Return old value.
duke@435 250 void *Dict::Delete(void *key) {
duke@435 251 uint i = _hash( key ) & (_size-1); // Get hash key, corrected for size
duke@435 252 bucket *b = &_bin[i]; // Handy shortcut
duke@435 253 for( uint j=0; j<b->_cnt; j++ )
duke@435 254 if( !_cmp(key,b->_keyvals[j+j]) ) {
duke@435 255 void *prior = b->_keyvals[j+j+1];
duke@435 256 b->_cnt--; // Remove key/value from lo bucket
duke@435 257 b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];
duke@435 258 b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
duke@435 259 _cnt--; // One less thing in table
duke@435 260 return prior;
duke@435 261 }
duke@435 262 return NULL;
duke@435 263 }
duke@435 264
duke@435 265 //------------------------------FindDict-------------------------------------
duke@435 266 // Find a key-value pair in the given dictionary. If not found, return NULL.
duke@435 267 // If found, move key-value pair towards head of list.
duke@435 268 void *Dict::operator [](const void *key) const {
duke@435 269 uint i = _hash( key ) & (_size-1); // Get hash key, corrected for size
duke@435 270 bucket *b = &_bin[i]; // Handy shortcut
duke@435 271 for( uint j=0; j<b->_cnt; j++ )
duke@435 272 if( !_cmp(key,b->_keyvals[j+j]) )
duke@435 273 return b->_keyvals[j+j+1];
duke@435 274 return NULL;
duke@435 275 }
duke@435 276
duke@435 277 //------------------------------CmpDict--------------------------------------
duke@435 278 // CmpDict compares two dictionaries; they must have the same keys (their
duke@435 279 // keys must match using CmpKey) and they must have the same values (pointer
duke@435 280 // comparison). If so 1 is returned, if not 0 is returned.
duke@435 281 int32 Dict::operator ==(const Dict &d2) const {
duke@435 282 if( _cnt != d2._cnt ) return 0;
duke@435 283 if( _hash != d2._hash ) return 0;
duke@435 284 if( _cmp != d2._cmp ) return 0;
duke@435 285 for( uint i=0; i < _size; i++) { // For complete hash table do
duke@435 286 bucket *b = &_bin[i]; // Handy shortcut
duke@435 287 if( b->_cnt != d2._bin[i]._cnt ) return 0;
duke@435 288 if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) )
duke@435 289 return 0; // Key-value pairs must match
duke@435 290 }
duke@435 291 return 1; // All match, is OK
duke@435 292 }
duke@435 293
duke@435 294 //------------------------------print------------------------------------------
duke@435 295 // Handier print routine
duke@435 296 void Dict::print() {
duke@435 297 DictI i(this); // Moved definition in iterator here because of g++.
duke@435 298 tty->print("Dict@0x%lx[%d] = {", this, _cnt);
duke@435 299 for( ; i.test(); ++i ) {
duke@435 300 tty->print("(0x%lx,0x%lx),", i._key, i._value);
duke@435 301 }
duke@435 302 tty->print_cr("}");
duke@435 303 }
duke@435 304
duke@435 305 //------------------------------Hashing Functions----------------------------
duke@435 306 // Convert string to hash key. This algorithm implements a universal hash
duke@435 307 // function with the multipliers frozen (ok, so it's not universal). The
duke@435 308 // multipliers (and allowable characters) are all odd, so the resultant sum
twisti@1040 309 // is odd - guaranteed not divisible by any power of two, so the hash tables
duke@435 310 // can be any power of two with good results. Also, I choose multipliers
duke@435 311 // that have only 2 bits set (the low is always set to be odd) so
duke@435 312 // multiplication requires only shifts and adds. Characters are required to
duke@435 313 // be in the range 0-127 (I double & add 1 to force oddness). Keys are
duke@435 314 // limited to MAXID characters in length. Experimental evidence on 150K of
duke@435 315 // C text shows excellent spreading of values for any size hash table.
duke@435 316 int hashstr(const void *t) {
duke@435 317 register char c, k = 0;
duke@435 318 register int32 sum = 0;
duke@435 319 register const char *s = (const char *)t;
duke@435 320
duke@435 321 while( ((c = *s++) != '\0') && (k < MAXID-1) ) { // Get characters till null or MAXID-1
duke@435 322 c = (c<<1)+1; // Characters are always odd!
duke@435 323 sum += c + (c<<shft[k++]); // Universal hash function
duke@435 324 }
duke@435 325 return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size
duke@435 326 }
duke@435 327
duke@435 328 //------------------------------hashptr--------------------------------------
twisti@1040 329 // Slimey cheap hash function; no guaranteed performance. Better than the
duke@435 330 // default for pointers, especially on MS-DOS machines.
duke@435 331 int hashptr(const void *key) {
duke@435 332 #ifdef __TURBOC__
duke@435 333 return ((intptr_t)key >> 16);
duke@435 334 #else // __TURBOC__
duke@435 335 return ((intptr_t)key >> 2);
duke@435 336 #endif
duke@435 337 }
duke@435 338
twisti@1040 339 // Slimey cheap hash function; no guaranteed performance.
duke@435 340 int hashkey(const void *key) {
duke@435 341 return (intptr_t)key;
duke@435 342 }
duke@435 343
duke@435 344 //------------------------------Key Comparator Functions---------------------
duke@435 345 int32 cmpstr(const void *k1, const void *k2) {
duke@435 346 return strcmp((const char *)k1,(const char *)k2);
duke@435 347 }
duke@435 348
never@997 349 // Cheap key comparator.
duke@435 350 int32 cmpkey(const void *key1, const void *key2) {
never@997 351 if (key1 == key2) return 0;
never@997 352 intptr_t delta = (intptr_t)key1 - (intptr_t)key2;
never@997 353 if (delta > 0) return 1;
never@997 354 return -1;
duke@435 355 }
duke@435 356
duke@435 357 //=============================================================================
duke@435 358 //------------------------------reset------------------------------------------
duke@435 359 // Create an iterator and initialize the first variables.
duke@435 360 void DictI::reset( const Dict *dict ) {
duke@435 361 _d = dict; // The dictionary
duke@435 362 _i = (uint)-1; // Before the first bin
duke@435 363 _j = 0; // Nothing left in the current bin
duke@435 364 ++(*this); // Step to first real value
duke@435 365 }
duke@435 366
duke@435 367 //------------------------------next-------------------------------------------
duke@435 368 // Find the next key-value pair in the dictionary, or return a NULL key and
duke@435 369 // value.
duke@435 370 void DictI::operator ++(void) {
duke@435 371 if( _j-- ) { // Still working in current bin?
duke@435 372 _key = _d->_bin[_i]._keyvals[_j+_j];
duke@435 373 _value = _d->_bin[_i]._keyvals[_j+_j+1];
duke@435 374 return;
duke@435 375 }
duke@435 376
duke@435 377 while( ++_i < _d->_size ) { // Else scan for non-zero bucket
duke@435 378 _j = _d->_bin[_i]._cnt;
duke@435 379 if( !_j ) continue;
duke@435 380 _j--;
duke@435 381 _key = _d->_bin[_i]._keyvals[_j+_j];
duke@435 382 _value = _d->_bin[_i]._keyvals[_j+_j+1];
duke@435 383 return;
duke@435 384 }
duke@435 385 _key = _value = NULL;
duke@435 386 }

mercurial