Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/libadt/dict.cpp
40951 views
1
/*
2
* Copyright (c) 1997, 2020, 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
#include "precompiled.hpp"
26
#include "libadt/dict.hpp"
27
#include "utilities/powerOfTwo.hpp"
28
29
// Dictionaries - An Abstract Data Type
30
31
// %%%%% includes not needed with AVM framework - Ungar
32
33
#include <assert.h>
34
35
//------------------------------data-----------------------------------------
36
// String hash tables
37
#define MAXID 20
38
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};
39
// Precomputed table of null character hashes
40
// xsum[0] = (1 << shft[0]) + 1;
41
// for(int i = 1; i < MAXID; i++) {
42
// xsum[i] = (1 << shft[i]) + 1 + xsum[i - 1];
43
// }
44
static const short xsum[MAXID] = {3,8,17,34,67,132,261,264,269,278,295,328,393,522,525,530,539,556,589,654};
45
46
//------------------------------bucket---------------------------------------
47
class bucket : public ResourceObj {
48
public:
49
uint _cnt, _max; // Size of bucket
50
void** _keyvals; // Array of keys and values
51
};
52
53
//------------------------------Dict-----------------------------------------
54
// The dictionary is kept has a hash table. The hash table is a even power
55
// of two, for nice modulo operations. Each bucket in the hash table points
56
// to a linear list of key-value pairs; each key & value is just a (void *).
57
// The list starts with a count. A hash lookup finds the list head, then a
58
// simple linear scan finds the key. If the table gets too full, it's
59
// doubled in size; the total amount of EXTRA times all hash functions are
60
// computed for the doubling is no more than the current size - thus the
61
// doubling in size costs no more than a constant factor in speed.
62
Dict::Dict(CmpKey initcmp, Hash inithash) : _arena(Thread::current()->resource_area()),
63
_hash(inithash), _cmp(initcmp) {
64
65
_size = 16; // Size is a power of 2
66
_cnt = 0; // Dictionary is empty
67
_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket) * _size);
68
memset((void*)_bin, 0, sizeof(bucket) * _size);
69
}
70
71
Dict::Dict(CmpKey initcmp, Hash inithash, Arena* arena, int size)
72
: _arena(arena), _hash(inithash), _cmp(initcmp) {
73
// Size is a power of 2
74
_size = MAX2(16, round_up_power_of_2(size));
75
76
_cnt = 0; // Dictionary is empty
77
_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket) * _size);
78
memset((void*)_bin, 0, sizeof(bucket) * _size);
79
}
80
81
// Deep copy into arena of choice
82
Dict::Dict(const Dict &d, Arena* arena)
83
: _arena(arena), _size(d._size), _cnt(d._cnt), _hash(d._hash), _cmp(d._cmp) {
84
_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket) * _size);
85
memcpy((void*)_bin, (void*)d._bin, sizeof(bucket) * _size);
86
for (uint i = 0; i < _size; i++) {
87
if (!_bin[i]._keyvals) {
88
continue;
89
}
90
_bin[i]._keyvals = (void**)_arena->Amalloc_4(sizeof(void*) * _bin[i]._max * 2);
91
memcpy(_bin[i]._keyvals, d._bin[i]._keyvals, _bin[i]._cnt * 2 * sizeof(void*));
92
}
93
}
94
95
//------------------------------~Dict------------------------------------------
96
// Delete an existing dictionary.
97
Dict::~Dict() { }
98
99
//------------------------------doubhash---------------------------------------
100
// Double hash table size. If can't do so, just suffer. If can, then run
101
// thru old hash table, moving things to new table. Note that since hash
102
// table doubled, exactly 1 new bit is exposed in the mask - so everything
103
// in the old table ends up on 1 of two lists in the new table; a hi and a
104
// lo list depending on the value of the bit.
105
void Dict::doubhash() {
106
uint oldsize = _size;
107
_size <<= 1; // Double in size
108
_bin = (bucket*)_arena->Arealloc(_bin, sizeof(bucket) * oldsize, sizeof(bucket) * _size);
109
memset((void*)(&_bin[oldsize]), 0, oldsize * sizeof(bucket));
110
// Rehash things to spread into new table
111
for (uint i = 0; i < oldsize; i++) { // For complete OLD table do
112
bucket* b = &_bin[i]; // Handy shortcut for _bin[i]
113
if (!b->_keyvals) continue; // Skip empties fast
114
115
bucket* nb = &_bin[i+oldsize]; // New bucket shortcut
116
uint j = b->_max; // Trim new bucket to nearest power of 2
117
while (j > b->_cnt) { j >>= 1; } // above old bucket _cnt
118
if (!j) { j = 1; } // Handle zero-sized buckets
119
nb->_max = j << 1;
120
// Allocate worst case space for key-value pairs
121
nb->_keyvals = (void**)_arena->Amalloc_4(sizeof(void* ) * nb->_max * 2);
122
uint nbcnt = 0;
123
124
for (j = 0; j < b->_cnt;) { // Rehash all keys in this bucket
125
void* key = b->_keyvals[j + j];
126
if ((_hash(key) & (_size-1)) != i) { // Moving to hi bucket?
127
nb->_keyvals[nbcnt + nbcnt] = key;
128
nb->_keyvals[nbcnt + nbcnt + 1] = b->_keyvals[j + j + 1];
129
nb->_cnt = nbcnt = nbcnt + 1;
130
b->_cnt--; // Remove key/value from lo bucket
131
b->_keyvals[j + j] = b->_keyvals[b->_cnt + b->_cnt];
132
b->_keyvals[j + j + 1] = b->_keyvals[b->_cnt + b->_cnt + 1];
133
// Don't increment j, hash compacted element also.
134
} else {
135
j++; // Iterate.
136
}
137
} // End of for all key-value pairs in bucket
138
} // End of for all buckets
139
}
140
141
//------------------------------Insert----------------------------------------
142
// Insert or replace a key/value pair in the given dictionary. If the
143
// dictionary is too full, it's size is doubled. The prior value being
144
// replaced is returned (NULL if this is a 1st insertion of that key). If
145
// an old value is found, it's swapped with the prior key-value pair on the
146
// list. This moves a commonly searched-for value towards the list head.
147
void*Dict::Insert(void* key, void* val, bool replace) {
148
uint hash = _hash(key); // Get hash key
149
uint i = hash & (_size - 1); // Get hash key, corrected for size
150
bucket* b = &_bin[i];
151
for (uint j = 0; j < b->_cnt; j++) {
152
if (!_cmp(key, b->_keyvals[j + j])) {
153
if (!replace) {
154
return b->_keyvals[j + j + 1];
155
} else {
156
void* prior = b->_keyvals[j + j + 1];
157
b->_keyvals[j + j ] = key;
158
b->_keyvals[j + j + 1] = val;
159
return prior;
160
}
161
}
162
}
163
if (++_cnt > _size) { // Hash table is full
164
doubhash(); // Grow whole table if too full
165
i = hash & (_size - 1); // Rehash
166
b = &_bin[i];
167
}
168
if (b->_cnt == b->_max) { // Must grow bucket?
169
if (!b->_keyvals) {
170
b->_max = 2; // Initial bucket size
171
b->_keyvals = (void**)_arena->Amalloc_4(sizeof(void*) * b->_max * 2);
172
} else {
173
b->_keyvals = (void**)_arena->Arealloc(b->_keyvals, sizeof(void*) * b->_max * 2, sizeof(void*) * b->_max * 4);
174
b->_max <<= 1; // Double bucket
175
}
176
}
177
b->_keyvals[b->_cnt + b->_cnt ] = key;
178
b->_keyvals[b->_cnt + b->_cnt + 1] = val;
179
b->_cnt++;
180
return NULL; // Nothing found prior
181
}
182
183
//------------------------------Delete---------------------------------------
184
// Find & remove a value from dictionary. Return old value.
185
void* Dict::Delete(void* key) {
186
uint i = _hash(key) & (_size - 1); // Get hash key, corrected for size
187
bucket* b = &_bin[i]; // Handy shortcut
188
for (uint j = 0; j < b->_cnt; j++) {
189
if (!_cmp(key, b->_keyvals[j + j])) {
190
void* prior = b->_keyvals[j + j + 1];
191
b->_cnt--; // Remove key/value from lo bucket
192
b->_keyvals[j+j ] = b->_keyvals[b->_cnt + b->_cnt ];
193
b->_keyvals[j+j+1] = b->_keyvals[b->_cnt + b->_cnt + 1];
194
_cnt--; // One less thing in table
195
return prior;
196
}
197
}
198
return NULL;
199
}
200
201
//------------------------------FindDict-------------------------------------
202
// Find a key-value pair in the given dictionary. If not found, return NULL.
203
// If found, move key-value pair towards head of list.
204
void* Dict::operator [](const void* key) const {
205
uint i = _hash(key) & (_size - 1); // Get hash key, corrected for size
206
bucket* b = &_bin[i]; // Handy shortcut
207
for (uint j = 0; j < b->_cnt; j++) {
208
if (!_cmp(key, b->_keyvals[j + j])) {
209
return b->_keyvals[j + j + 1];
210
}
211
}
212
return NULL;
213
}
214
215
//------------------------------print------------------------------------------
216
// Handier print routine
217
void Dict::print() {
218
DictI i(this); // Moved definition in iterator here because of g++.
219
tty->print("Dict@" INTPTR_FORMAT "[%d] = {", p2i(this), _cnt);
220
for (; i.test(); ++i) {
221
tty->print("(" INTPTR_FORMAT "," INTPTR_FORMAT "),", p2i(i._key), p2i(i._value));
222
}
223
tty->print_cr("}");
224
}
225
226
//------------------------------Hashing Functions----------------------------
227
// Convert string to hash key. This algorithm implements a universal hash
228
// function with the multipliers frozen (ok, so it's not universal). The
229
// multipliers (and allowable characters) are all odd, so the resultant sum
230
// is odd - guaranteed not divisible by any power of two, so the hash tables
231
// can be any power of two with good results. Also, I choose multipliers
232
// that have only 2 bits set (the low is always set to be odd) so
233
// multiplication requires only shifts and adds. Characters are required to
234
// be in the range 0-127 (I double & add 1 to force oddness). Keys are
235
// limited to MAXID characters in length. Experimental evidence on 150K of
236
// C text shows excellent spreading of values for any size hash table.
237
int hashstr(const void* t) {
238
char c, k = 0;
239
int32_t sum = 0;
240
const char* s = (const char*)t;
241
242
while (((c = *s++) != '\0') && (k < MAXID-1)) { // Get characters till null or MAXID-1
243
c = (c << 1) + 1; // Characters are always odd!
244
sum += c + (c << shft[k++]); // Universal hash function
245
}
246
return (int)((sum + xsum[k]) >> 1); // Hash key, un-modulo'd table size
247
}
248
249
//------------------------------hashptr--------------------------------------
250
// Slimey cheap hash function; no guaranteed performance. Better than the
251
// default for pointers, especially on MS-DOS machines.
252
int hashptr(const void* key) {
253
return ((intptr_t)key >> 2);
254
}
255
256
// Slimey cheap hash function; no guaranteed performance.
257
int hashkey(const void* key) {
258
return (intptr_t)key;
259
}
260
261
//------------------------------Key Comparator Functions---------------------
262
int32_t cmpstr(const void* k1, const void* k2) {
263
return strcmp((const char*)k1, (const char*)k2);
264
}
265
266
// Cheap key comparator.
267
int32_t cmpkey(const void* key1, const void* key2) {
268
if (key1 == key2) {
269
return 0;
270
}
271
intptr_t delta = (intptr_t)key1 - (intptr_t)key2;
272
if (delta > 0) {
273
return 1;
274
}
275
return -1;
276
}
277
278
//=============================================================================
279
//------------------------------reset------------------------------------------
280
// Create an iterator and initialize the first variables.
281
void DictI::reset(const Dict* dict) {
282
_d = dict;
283
_i = (uint)-1; // Before the first bin
284
_j = 0; // Nothing left in the current bin
285
++(*this); // Step to first real value
286
}
287
288
//------------------------------next-------------------------------------------
289
// Find the next key-value pair in the dictionary, or return a NULL key and
290
// value.
291
void DictI::operator ++(void) {
292
if (_j--) { // Still working in current bin?
293
_key = _d->_bin[_i]._keyvals[_j + _j];
294
_value = _d->_bin[_i]._keyvals[_j + _j + 1];
295
return;
296
}
297
298
while (++_i < _d->_size) { // Else scan for non-zero bucket
299
_j = _d->_bin[_i]._cnt;
300
if (!_j) {
301
continue;
302
}
303
_j--;
304
_key = _d->_bin[_i]._keyvals[_j+_j];
305
_value = _d->_bin[_i]._keyvals[_j+_j+1];
306
return;
307
}
308
_key = _value = NULL;
309
}
310
311