/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/resourceArea.hpp"
#include "runtime/thread.hpp"
// Dictionaries - An Abstract Data Type
// %%%%% includes not needed with AVM framework - Ungar
// #include "port.hpp"
//IMPLEMENTATION
// #include "dict.hpp"
#include <assert.h>
// The iostream is not needed and it gets confused for gcc by the
// define of bool.
//
// #include <iostream.h>
//------------------------------data-----------------------------------------
// String hash tables
//------------------------------bucket---------------------------------------
public:
};
//------------------------------Dict-----------------------------------------
// The dictionary is kept has a hash table. The hash table is a even power
// of two, for nice modulo operations. Each bucket in the hash table points
// to a linear list of key-value pairs; each key & value is just a (void *).
// The list starts with a count. A hash lookup finds the list head, then a
// simple linear scan finds the key. If the table gets too full, it's
// doubled in size; the total amount of EXTRA times all hash functions are
// computed for the doubling is no more than the current size - thus the
// doubling in size costs no more than a constant factor in speed.
int i;
// Precompute table of null character hashes
if( !initflag ) { // Not initializated yet?
for(i=1; i<MAXID; i++) {
}
}
_cnt = 0; // Dictionary is empty
}
int i;
// Precompute table of null character hashes
if( !initflag ) { // Not initializated yet?
for(i=1; i<MAXID; i++) {
}
}
i=16;
while( i < size ) i <<= 1;
_size = i; // Size is a power of 2
_cnt = 0; // Dictionary is empty
}
//------------------------------~Dict------------------------------------------
// Delete an existing dictionary.
/*
tty->print("~Dict %d/%d: ",_cnt,_size);
for( uint i=0; i < _size; i++) // For complete new table do
tty->print("%d ",_bin[i]._cnt);
tty->print("\n");*/
/*for( uint i=0; i<_size; i++ ) {
FREE_FAST( _bin[i]._keyvals );
} */
}
//------------------------------Clear----------------------------------------
// Zap to empty; ready for re-use
_cnt = 0; // Empty contents
// Leave _size & _bin alone, under the assumption that dictionary will
// grow to this size again.
}
//------------------------------doubhash---------------------------------------
// Double hash table size. If can't do so, just suffer. If can, then run
// thru old hash table, moving things to new table. Note that since hash
// table doubled, exactly 1 new bit is exposed in the mask - so everything
// in the old table ends up on 1 of two lists in the new table; a hi and a
// lo list depending on the value of the bit.
// Rehash things to spread into new table
if( !b->_keyvals ) continue; // Skip empties fast
if( !j ) j = 1; // Handle zero-sized buckets
// Allocate worst case space for key-value pairs
for( j=0; j<b->_cnt; j++ ) { // Rehash all keys in this bucket
j--; // Hash compacted element also
}
} // End of for all key-value pairs in bucket
} // End of for all buckets
}
//------------------------------Dict-----------------------------------------
// Deep copy a dictionary.
Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) {
}
}
//------------------------------Dict-----------------------------------------
// Deep copy a dictionary.
}
uint i;
for( i=0; i<_size; i++ ) // All buckets are empty
for( i=0; i<_size; i++ ) {
}
return *this;
}
//------------------------------Insert----------------------------------------
// dictionary is too full, it's size is doubled. The prior value being
// replaced is returned (NULL if this is a 1st insertion of that key). If
// an old value is found, it's swapped with the prior key-value pair on the
// list. This moves a commonly searched-for value towards the list head.
if (!replace) {
return b->_keyvals[j+j+1];
} else {
return prior; // Return prior
}
}
}
doubhash(); // Grow whole table if too full
b = &_bin[i]; // Handy shortcut
}
if( !b->_keyvals ) {
} else {
b->_keyvals = (void**)_arena->Arealloc(b->_keyvals, sizeof(void*) * b->_max * 2, sizeof(void*) * b->_max * 4);
}
}
b->_cnt++;
return NULL; // Nothing found prior
}
//------------------------------Delete---------------------------------------
// Find & remove a value from dictionary. Return old value.
_cnt--; // One less thing in table
return prior;
}
return NULL;
}
//------------------------------FindDict-------------------------------------
// Find a key-value pair in the given dictionary. If not found, return NULL.
// If found, move key-value pair towards head of list.
return b->_keyvals[j+j+1];
return NULL;
}
//------------------------------CmpDict--------------------------------------
// CmpDict compares two dictionaries; they must have the same keys (their
// keys must match using CmpKey) and they must have the same values (pointer
// comparison). If so 1 is returned, if not 0 is returned.
return 0; // Key-value pairs must match
}
return 1; // All match, is OK
}
//------------------------------print------------------------------------------
// Handier print routine
DictI i(this); // Moved definition in iterator here because of g++.
for( ; i.test(); ++i ) {
}
}
//------------------------------Hashing Functions----------------------------
// Convert string to hash key. This algorithm implements a universal hash
// function with the multipliers frozen (ok, so it's not universal). The
// multipliers (and allowable characters) are all odd, so the resultant sum
// is odd - guaranteed not divisible by any power of two, so the hash tables
// can be any power of two with good results. Also, I choose multipliers
// that have only 2 bits set (the low is always set to be odd) so
// multiplication requires only shifts and adds. Characters are required to
// be in the range 0-127 (I double & add 1 to force oddness). Keys are
// limited to MAXID characters in length. Experimental evidence on 150K of
// C text shows excellent spreading of values for any size hash table.
int hashstr(const void *t) {
register char c, k = 0;
register const char *s = (const char *)t;
c = (c<<1)+1; // Characters are always odd!
}
}
//------------------------------hashptr--------------------------------------
// Slimey cheap hash function; no guaranteed performance. Better than the
// default for pointers, especially on MS-DOS machines.
#ifdef __TURBOC__
#else // __TURBOC__
#endif
}
// Slimey cheap hash function; no guaranteed performance.
}
//------------------------------Key Comparator Functions---------------------
}
// Cheap key comparator.
if (delta > 0) return 1;
return -1;
}
//=============================================================================
//------------------------------reset------------------------------------------
// Create an iterator and initialize the first variables.
_j = 0; // Nothing left in the current bin
++(*this); // Step to first real value
}
//------------------------------next-------------------------------------------
// Find the next key-value pair in the dictionary, or return a NULL key and
// value.
void DictI::operator ++(void) {
if( _j-- ) { // Still working in current bin?
return;
}
if( !_j ) continue;
_j--;
return;
}
}