jdk
1/*
2* Copyright (c) 1998, 2023, 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 "adlc.hpp"28
29// #include "dict.hpp"
30
31
32//------------------------------data-----------------------------------------
33// String hash tables
34#define MAXID 2035static char initflag = 0; // True after 1st initialization36static char shft[MAXID + 1] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7};37static short xsum[MAXID];38
39//------------------------------bucket---------------------------------------
40class bucket {41public:42int _cnt, _max; // Size of bucket43const void **_keyvals; // Array of keys and values44};45
46//------------------------------Dict-----------------------------------------
47// The dictionary is kept has a hash table. The hash table is a even power
48// of two, for nice modulo operations. Each bucket in the hash table points
49// to a linear list of key-value pairs; each key & value is just a (void *).
50// The list starts with a count. A hash lookup finds the list head, then a
51// simple linear scan finds the key. If the table gets too full, it's
52// doubled in size; the total amount of EXTRA times all hash functions are
53// computed for the doubling is no more than the current size - thus the
54// doubling in size costs no more than a constant factor in speed.
55Dict::Dict(CmpKey initcmp, Hash inithash) : _hash(inithash), _cmp(initcmp), _arena(nullptr) {56init();57}
58
59Dict::Dict(CmpKey initcmp, Hash inithash, AdlArena *arena) : _hash(inithash), _cmp(initcmp), _arena(arena) {60init();61}
62
63void Dict::init() {64int i;65
66// Precompute table of null character hashes67if (!initflag) { // Not initializated yet?68xsum[0] = (short) ((1 << shft[0]) + 1); // Initialize69for( i = 1; i < MAXID; i++) {70xsum[i] = (short) ((1 << shft[i]) + 1 + xsum[i-1]);71}72initflag = 1; // Never again73}74
75_size = 16; // Size is a power of 276_cnt = 0; // Dictionary is empty77_bin = (bucket*)_arena->AmallocWords(sizeof(bucket) * _size);78memset(_bin, 0, sizeof(bucket) * _size);79}
80
81//------------------------------~Dict------------------------------------------
82// Delete an existing dictionary.
83Dict::~Dict() {84}
85
86//------------------------------Clear----------------------------------------
87// Zap to empty; ready for re-use
88void Dict::Clear() {89_cnt = 0; // Empty contents90for( int i=0; i<_size; i++ )91_bin[i]._cnt = 0; // Empty buckets, but leave allocated92// Leave _size & _bin alone, under the assumption that dictionary will93// grow to this size again.94}
95
96//------------------------------doubhash---------------------------------------
97// Double hash table size. If can't do so, just suffer. If can, then run
98// thru old hash table, moving things to new table. Note that since hash
99// table doubled, exactly 1 new bit is exposed in the mask - so everything
100// in the old table ends up on 1 of two lists in the new table; a hi and a
101// lo list depending on the value of the bit.
102void Dict::doubhash(void) {103int oldsize = _size;104_size <<= 1; // Double in size105_bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*oldsize, sizeof(bucket)*_size );106memset( &_bin[oldsize], 0, oldsize*sizeof(bucket) );107// Rehash things to spread into new table108for( int i=0; i < oldsize; i++) { // For complete OLD table do109bucket *b = &_bin[i]; // Handy shortcut for _bin[i]110if( !b->_keyvals ) continue; // Skip empties fast111
112bucket *nb = &_bin[i+oldsize]; // New bucket shortcut113int j = b->_max; // Trim new bucket to nearest power of 2114while( j > b->_cnt ) j >>= 1; // above old bucket _cnt115if( !j ) j = 1; // Handle zero-sized buckets116nb->_max = j<<1;117// Allocate worst case space for key-value pairs118nb->_keyvals = (const void**)_arena->AmallocWords( sizeof(void *)*nb->_max*2 );119int nbcnt = 0;120
121for( j=0; j<b->_cnt; j++ ) { // Rehash all keys in this bucket122const void *key = b->_keyvals[j+j];123if( (_hash( key ) & (_size-1)) != i ) { // Moving to hi bucket?124nb->_keyvals[nbcnt+nbcnt] = key;125nb->_keyvals[nbcnt+nbcnt+1] = b->_keyvals[j+j+1];126nb->_cnt = nbcnt = nbcnt+1;127b->_cnt--; // Remove key/value from lo bucket128b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];129b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];130j--; // Hash compacted element also131}132} // End of for all key-value pairs in bucket133} // End of for all buckets134
135
136}
137
138//------------------------------Dict-----------------------------------------
139// Deep copy a dictionary.
140Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) {141_bin = (bucket*)_arena->AmallocWords(sizeof(bucket)*_size);142memcpy( _bin, d._bin, sizeof(bucket)*_size );143for( int i=0; i<_size; i++ ) {144if( !_bin[i]._keyvals ) continue;145_bin[i]._keyvals=(const void**)_arena->AmallocWords( sizeof(void *)*_bin[i]._max*2);146memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*));147}148}
149
150//------------------------------Dict-----------------------------------------
151// Deep copy a dictionary.
152Dict &Dict::operator =( const Dict &d ) {153if( _size < d._size ) { // If must have more buckets154_arena = d._arena;155_bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );156memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) );157_size = d._size;158}159for( int i=0; i<_size; i++ ) // All buckets are empty160_bin[i]._cnt = 0; // But leave bucket allocations alone161_cnt = d._cnt;162*(Hash*)(&_hash) = d._hash;163*(CmpKey*)(&_cmp) = d._cmp;164for(int k=0; k<_size; k++ ) {165bucket *b = &d._bin[k]; // Shortcut to source bucket166for( int j=0; j<b->_cnt; j++ )167Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] );168}169return *this;170}
171
172//------------------------------Insert---------------------------------------
173// Insert or replace a key/value pair in the given dictionary. If the
174// dictionary is too full, it's size is doubled. The prior value being
175// replaced is returned (null if this is a 1st insertion of that key). If
176// an old value is found, it's swapped with the prior key-value pair on the
177// list. This moves a commonly searched-for value towards the list head.
178const void *Dict::Insert(const void *key, const void *val) {179int hash = _hash( key ); // Get hash key180int i = hash & (_size-1); // Get hash key, corrected for size181bucket *b = &_bin[i]; // Handy shortcut182for( int j=0; j<b->_cnt; j++ )183if( !_cmp(key,b->_keyvals[j+j]) ) {184const void *prior = b->_keyvals[j+j+1];185b->_keyvals[j+j ] = key; // Insert current key-value186b->_keyvals[j+j+1] = val;187return prior; // Return prior188}189
190if( ++_cnt > _size ) { // Hash table is full191doubhash(); // Grow whole table if too full192i = hash & (_size-1); // Rehash193b = &_bin[i]; // Handy shortcut194}195if( b->_cnt == b->_max ) { // Must grow bucket?196if( !b->_keyvals ) {197b->_max = 2; // Initial bucket size198b->_keyvals = (const void**)_arena->AmallocWords( sizeof(void *)*b->_max*2 );199} else {200b->_keyvals = (const void**)_arena->Arealloc( b->_keyvals, sizeof(void *)*b->_max*2, sizeof(void *)*b->_max*4 );201b->_max <<= 1; // Double bucket202}203}204b->_keyvals[b->_cnt+b->_cnt ] = key;205b->_keyvals[b->_cnt+b->_cnt+1] = val;206b->_cnt++;207return nullptr; // Nothing found prior208}
209
210//------------------------------Delete---------------------------------------
211// Find & remove a value from dictionary. Return old value.
212const void *Dict::Delete(void *key) {213int i = _hash( key ) & (_size-1); // Get hash key, corrected for size214bucket *b = &_bin[i]; // Handy shortcut215for( int j=0; j<b->_cnt; j++ )216if( !_cmp(key,b->_keyvals[j+j]) ) {217const void *prior = b->_keyvals[j+j+1];218b->_cnt--; // Remove key/value from lo bucket219b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];220b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];221_cnt--; // One less thing in table222return prior;223}224return nullptr;225}
226
227//------------------------------FindDict-------------------------------------
228// Find a key-value pair in the given dictionary. If not found, return null.
229// If found, move key-value pair towards head of list.
230const void *Dict::operator [](const void *key) const {231int i = _hash( key ) & (_size-1); // Get hash key, corrected for size232bucket *b = &_bin[i]; // Handy shortcut233for( int j=0; j<b->_cnt; j++ )234if( !_cmp(key,b->_keyvals[j+j]) )235return b->_keyvals[j+j+1];236return nullptr;237}
238
239//------------------------------CmpDict--------------------------------------
240// CmpDict compares two dictionaries; they must have the same keys (their
241// keys must match using CmpKey) and they must have the same values (pointer
242// comparison). If so 1 is returned, if not 0 is returned.
243int Dict::operator ==(const Dict &d2) const {244if( _cnt != d2._cnt ) return 0;245if( _hash != d2._hash ) return 0;246if( _cmp != d2._cmp ) return 0;247for( int i=0; i < _size; i++) { // For complete hash table do248bucket *b = &_bin[i]; // Handy shortcut249if( b->_cnt != d2._bin[i]._cnt ) return 0;250if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) )251return 0; // Key-value pairs must match252}253return 1; // All match, is OK254}
255
256
257//------------------------------print----------------------------------------
258static void printvoid(const void* x) { printf("%p", x); }259void Dict::print() {260print(printvoid, printvoid);261}
262void Dict::print(PrintKeyOrValue print_key, PrintKeyOrValue print_value) {263for( int i=0; i < _size; i++) { // For complete hash table do264bucket *b = &_bin[i]; // Handy shortcut265for( int j=0; j<b->_cnt; j++ ) {266print_key( b->_keyvals[j+j ]);267printf(" -> ");268print_value(b->_keyvals[j+j+1]);269printf("\n");270}271}272}
273
274//------------------------------Hashing Functions----------------------------
275// Convert string to hash key. This algorithm implements a universal hash
276// function with the multipliers frozen (ok, so it's not universal). The
277// multipliers (and allowable characters) are all odd, so the resultant sum
278// is odd - guaranteed not divisible by any power of two, so the hash tables
279// can be any power of two with good results. Also, I choose multipliers
280// that have only 2 bits set (the low is always set to be odd) so
281// multiplication requires only shifts and adds. Characters are required to
282// be in the range 0-127 (I double & add 1 to force oddness). Keys are
283// limited to MAXID characters in length. Experimental evidence on 150K of
284// C text shows excellent spreading of values for any size hash table.
285int hashstr(const void *t) {286char c, k = 0;287int sum = 0;288const char *s = (const char *)t;289
290while (((c = s[k]) != '\0') && (k < MAXID-1)) { // Get characters till nul291c = (char) ((c << 1) + 1); // Characters are always odd!292sum += c + (c << shft[k++]); // Universal hash function293}294assert(k < (MAXID), "Exceeded maximum name length");295return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size296}
297
298//------------------------------hashptr--------------------------------------
299// Slimey cheap hash function; no guaranteed performance. Better than the
300// default for pointers, especially on MS-DOS machines.
301int hashptr(const void *key) {302#ifdef __TURBOC__303return (int)((intptr_t)key >> 16);304#else // __TURBOC__305return (int)((intptr_t)key >> 2);306#endif307}
308
309// Slimey cheap hash function; no guaranteed performance.
310int hashkey(const void *key) {311return (int)((intptr_t)key);312}
313
314//------------------------------Key Comparator Functions---------------------
315int cmpstr(const void *k1, const void *k2) {316return strcmp((const char *)k1,(const char *)k2);317}
318
319// Cheap key comparator.
320int cmpkey(const void *key1, const void *key2) {321if (key1 == key2) return 0;322intptr_t delta = (intptr_t)key1 - (intptr_t)key2;323if (delta > 0) return 1;324return -1;325}
326
327//=============================================================================
328//------------------------------reset------------------------------------------
329// Create an iterator and initialize the first variables.
330void DictI::reset( const Dict *dict ) {331_d = dict; // The dictionary332_i = (int)-1; // Before the first bin333_j = 0; // Nothing left in the current bin334++(*this); // Step to first real value335}
336
337//------------------------------next-------------------------------------------
338// Find the next key-value pair in the dictionary, or return a null key and
339// value.
340void DictI::operator ++(void) {341if( _j-- ) { // Still working in current bin?342_key = _d->_bin[_i]._keyvals[_j+_j];343_value = _d->_bin[_i]._keyvals[_j+_j+1];344return;345}346
347while( ++_i < _d->_size ) { // Else scan for non-zero bucket348_j = _d->_bin[_i]._cnt;349if( !_j ) continue;350_j--;351_key = _d->_bin[_i]._keyvals[_j+_j];352_value = _d->_bin[_i]._keyvals[_j+_j+1];353return;354}355_key = _value = nullptr;356}
357