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