src/share/vm/libadt/dict.cpp

Wed, 01 Dec 2010 15:04:06 +0100

author
stefank
date
Wed, 01 Dec 2010 15:04:06 +0100
changeset 2325
c760f78e0a53
parent 2314
f95d63e2154a
child 6680
78bbf4d43a14
permissions
-rw-r--r--

7003125: precompiled.hpp is included when precompiled headers are not used
Summary: Added an ifndef DONT_USE_PRECOMPILED_HEADER to precompiled.hpp. Set up DONT_USE_PRECOMPILED_HEADER when compiling with Sun Studio or when the user specifies USE_PRECOMPILED_HEADER=0. Fixed broken include dependencies.
Reviewed-by: coleenp, kvn

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

mercurial