src/share/vm/libadt/dict.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial