This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
forked from neurodata/neuroglancer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash_table.ts
488 lines (438 loc) · 13.4 KB
/
hash_table.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
* @license
* Copyright 2016 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {hashCombine} from 'neuroglancer/gpu_hash/hash_function';
import {getRandomValues} from 'neuroglancer/util/random';
import {Uint64} from 'neuroglancer/util/uint64';
export const NUM_ALTERNATIVES = 3;
const DEFAULT_LOAD_FACTOR = 0.9;
const DEBUG = false;
// Key that needs to be inserted. Temporary variables used during insert. These can safely be
// global because control never leaves functions defined in this module while these are in use.
let pendingLow = 0, pendingHigh = 0, backupPendingLow = 0, backupPendingHigh = 0;
export abstract class HashTableBase {
loadFactor = DEFAULT_LOAD_FACTOR;
size = 0;
table: Uint32Array;
tableSize: number;
emptyLow = 4294967295;
emptyHigh = 4294967295;
maxRehashAttempts = 5;
maxAttempts = 5;
capacity: number;
/**
* Number of uint32 elements per entry in hash table.
*/
entryStride: number;
generation = 0;
mungedEmptyKey = -1;
constructor(public hashSeeds = HashTableBase.generateHashSeeds(NUM_ALTERNATIVES)) {
// Minimum size must be greater than 2 * hashSeeds.length. Otherwise, tableWithMungedEmptyKey
// may loop infinitely.
let initialSize = 8;
while (initialSize < 2 * hashSeeds.length) {
initialSize *= 2;
}
this.allocate(initialSize);
}
private updateHashFunctions(numHashes: number) {
this.hashSeeds = HashTableBase.generateHashSeeds(numHashes);
this.mungedEmptyKey = -1;
}
/**
* Invokes callback with a modified version of the hash table data array.
*
* Replaces all slots that appear to be valid entries for (emptyLow, emptyHigh), i.e. slots that
* contain (emptyLow, emptyHigh) and to which (emptyLow, emptyHigh) hashes, with (mungedEmptyKey,
* mungedEmptyKey).
*
* mungedEmptyKey is chosen to be a 32-bit value with the property that the 64-bit value
* (mungedEmptyKey, mungedEmptyKey) does not hash to any of the same slots as (emptyLow,
* emptyHigh).
*
* This allows the modified data array to be used for lookups without special casing the empty
* key.
*/
tableWithMungedEmptyKey(callback: (table: Uint32Array) => void) {
const numHashes = this.hashSeeds.length;
const emptySlots = new Array<number>(numHashes);
for (let i = 0; i < numHashes; ++i) {
emptySlots[i] = this.getHash(i, this.emptyLow, this.emptyHigh);
}
let {mungedEmptyKey} = this;
if (mungedEmptyKey === -1) {
chooseMungedEmptyKey: while (true) {
mungedEmptyKey = (Math.random() * 0x1000000) >>> 0;
for (let i = 0; i < numHashes; ++i) {
let h = this.getHash(i, mungedEmptyKey, mungedEmptyKey);
for (let j = 0; j < numHashes; ++j) {
if (emptySlots[j] === h) {
continue chooseMungedEmptyKey;
}
}
}
this.mungedEmptyKey = mungedEmptyKey;
break;
}
}
let {table, emptyLow, emptyHigh} = this;
for (let i = 0; i < numHashes; ++i) {
let h = emptySlots[i];
if (table[h] === emptyLow && table[h + 1] === emptyHigh) {
table[h] = mungedEmptyKey;
table[h + 1] = mungedEmptyKey;
}
}
try {
callback(table);
} finally {
for (let i = 0; i < numHashes; ++i) {
let h = emptySlots[i];
if (table[h] === mungedEmptyKey && table[h + 1] === mungedEmptyKey) {
table[h] = emptyLow;
table[h + 1] = emptyHigh;
}
}
}
}
static generateHashSeeds(numAlternatives = NUM_ALTERNATIVES) {
return getRandomValues(new Uint32Array(numAlternatives));
}
getHash(hashIndex: number, low: number, high: number) {
let hash = this.hashSeeds[hashIndex];
hash = hashCombine(hash, low);
hash = hashCombine(hash, high);
return this.entryStride * (hash & (this.tableSize - 1));
}
/**
* Iterates over the Uint64 keys contained in the hash set.
*
* The same temp value will be modified and yielded at every iteration.
*/
* keys(temp = new Uint64()) {
let {emptyLow, emptyHigh, entryStride} = this;
let {table} = this;
for (let i = 0, length = table.length; i < length; i += entryStride) {
let low = table[i], high = table[i + 1];
if (low !== emptyLow || high !== emptyHigh) {
temp.low = low;
temp.high = high;
yield temp;
}
}
}
indexOfPair(low: number, high: number) {
let {table, emptyLow, emptyHigh} = this;
if (low === emptyLow && high === emptyHigh) {
return -1;
}
for (let i = 0, numHashes = this.hashSeeds.length; i < numHashes; ++i) {
let h = this.getHash(i, low, high);
if (table[h] === low && table[h + 1] === high) {
return h;
}
}
return -1;
}
/**
* Returns the offset into the hash table of the specified element, or -1 if the element is not
* present.
*/
indexOf(x: Uint64) {
return this.indexOfPair(x.low, x.high);
}
/**
* Changes the empty key to a value that is not equal to the current empty key and is not present
* in the table.
*
* This is called when an attempt is made to insert the empty key.
*/
private chooseAnotherEmptyKey() {
let {emptyLow, emptyHigh, table, entryStride} = this;
let newLow: number, newHigh: number;
while (true) {
newLow = (Math.random() * 0x100000000) >>> 0;
newHigh = (Math.random() * 0x100000000) >>> 0;
if (newLow === emptyLow && newHigh === emptyHigh) {
continue;
}
if (this.hasPair(newLow, newHigh)) {
continue;
}
break;
}
this.emptyLow = newLow;
this.emptyHigh = newHigh;
// Replace empty keys in the table.
for (let h = 0, length = table.length; h < length; h += entryStride) {
if (table[h] === emptyLow && table[h + 1] === emptyHigh) {
table[h] = newLow;
table[h + 1] = newHigh;
}
}
}
/**
* Returns true iff the specified element is present.
*/
has(x: Uint64) {
return this.indexOf(x) !== -1;
}
/**
* Returns true iff the specified element is present.
*/
hasPair(low: number, high: number) {
return this.indexOfPair(low, high) !== -1;
}
delete(x: Uint64) {
let index = this.indexOf(x);
if (index !== -1) {
let {table} = this;
table[index] = this.emptyLow;
table[index + 1] = this.emptyHigh;
++this.generation;
this.size--;
return true;
}
return false;
}
private clearTable() {
let {table, entryStride, emptyLow, emptyHigh} = this;
let length = table.length;
for (let h = 0; h < length; h += entryStride) {
table[h] = emptyLow;
table[h + 1] = emptyHigh;
}
}
clear() {
if (this.size === 0) {
return false;
}
this.size = 0;
++this.generation;
this.clearTable();
return true;
}
protected swapPending(table: Uint32Array, offset: number) {
let tempLow = pendingLow, tempHigh = pendingHigh;
this.storePending(table, offset);
table[offset] = tempLow;
table[offset + 1] = tempHigh;
}
protected storePending(table: Uint32Array, offset: number) {
pendingLow = table[offset];
pendingHigh = table[offset + 1];
}
protected backupPending() {
backupPendingLow = pendingLow;
backupPendingHigh = pendingHigh;
}
protected restorePending() {
pendingLow = backupPendingLow;
pendingHigh = backupPendingHigh;
}
private tryToInsert() {
if (DEBUG) {
console.log(`tryToInsert: ${pendingLow}, ${pendingHigh}`);
}
let attempt = 0;
let {emptyLow, emptyHigh, maxAttempts, table} = this;
let numHashes = this.hashSeeds.length;
let tableIndex = Math.floor(Math.random() * numHashes);
while (true) {
let h = this.getHash(tableIndex, pendingLow, pendingHigh);
this.swapPending(table, h);
if (pendingLow === emptyLow && pendingHigh === emptyHigh) {
return true;
}
if (++attempt === maxAttempts) {
break;
}
tableIndex = (tableIndex + Math.floor(Math.random() * (numHashes - 1)) + 1) % numHashes;
}
return false;
}
private allocate(tableSize: number) {
this.tableSize = tableSize;
let {entryStride} = this;
this.table = new Uint32Array(tableSize * entryStride);
this.maxAttempts = tableSize;
this.clearTable();
this.capacity = tableSize * this.loadFactor;
this.mungedEmptyKey = -1;
}
private rehash(oldTable: Uint32Array, tableSize: number) {
if (DEBUG) {
console.log('rehash begin');
}
this.allocate(tableSize);
this.updateHashFunctions(this.hashSeeds.length);
let {emptyLow, emptyHigh, entryStride} = this;
for (let h = 0, length = oldTable.length; h < length; h += entryStride) {
let low = oldTable[h], high = oldTable[h + 1];
if (low !== emptyLow || high !== emptyHigh) {
this.storePending(oldTable, h);
if (!this.tryToInsert()) {
if (DEBUG) {
console.log('rehash failed');
}
return false;
}
}
}
if (DEBUG) {
console.log('rehash end');
}
return true;
}
private grow(desiredTableSize: number) {
if (DEBUG) {
console.log(`grow: ${desiredTableSize}`);
}
let oldTable = this.table;
let {tableSize} = this;
while (tableSize < desiredTableSize) {
tableSize *= 2;
}
while (true) {
for (let rehashAttempt = 0; rehashAttempt < this.maxRehashAttempts; ++rehashAttempt) {
if (this.rehash(oldTable, tableSize)) {
if (DEBUG) {
console.log(`grow end`);
}
return;
}
}
tableSize *= 2;
}
}
protected insertInternal() {
++this.generation;
if (pendingLow === this.emptyLow && pendingHigh === this.emptyHigh) {
this.chooseAnotherEmptyKey();
}
if (++this.size > this.capacity) {
this.backupPending();
this.grow(this.tableSize * 2);
this.restorePending();
}
while (!this.tryToInsert()) {
this.backupPending();
this.grow(this.tableSize);
this.restorePending();
}
}
}
export class HashSetUint64 extends HashTableBase {
add(x: Uint64) {
let {low, high} = x;
if (this.hasPair(low, high)) {
return false;
}
if (DEBUG) {
console.log(`add: ${low},${high}`);
}
pendingLow = low;
pendingHigh = high;
this.insertInternal();
return true;
}
/**
* Iterates over the keys. The same temporary value will be modified and yielded at every
* iteration.
*/
[Symbol.iterator]() {
return this.keys();
}
}
HashSetUint64.prototype.entryStride = 2;
// Value that needs to be inserted. Temporary variables used during insert. These can safely be
// global because control never leaves functions defined in this module while these are in use.
let pendingValueLow = 0, pendingValueHigh = 0, backupPendingValueLow = 0,
backupPendingValueHigh = 0;
export class HashMapUint64 extends HashTableBase {
set(key: Uint64, value: Uint64) {
let {low, high} = key;
if (this.hasPair(low, high)) {
return false;
}
if (DEBUG) {
console.log(`add: ${low},${high} -> ${value.low},${value.high}`);
}
pendingLow = low;
pendingHigh = high;
pendingValueLow = value.low;
pendingValueHigh = value.high;
this.insertInternal();
return true;
}
get(key: Uint64, value: Uint64): boolean {
let h = this.indexOf(key);
if (h === -1) {
return false;
}
let {table} = this;
value.low = table[h + 2];
value.high = table[h + 3];
return true;
}
protected swapPending(table: Uint32Array, offset: number) {
let tempLow = pendingValueLow, tempHigh = pendingValueHigh;
super.swapPending(table, offset);
table[offset + 2] = tempLow;
table[offset + 3] = tempHigh;
}
protected storePending(table: Uint32Array, offset: number) {
super.storePending(table, offset);
pendingValueLow = table[offset + 2];
pendingValueHigh = table[offset + 3];
}
protected backupPending() {
super.backupPending();
backupPendingValueLow = pendingValueLow;
backupPendingValueHigh = pendingValueHigh;
}
protected restorePending() {
super.restorePending();
pendingValueLow = backupPendingValueLow;
pendingValueHigh = backupPendingValueHigh;
}
/**
* Iterates over entries. The same temporary value will be modified and yielded at every
* iteration.
*/
[Symbol.iterator]() {
return this.entries();
}
/**
* Iterates over entries. The same temporary value will be modified and yielded at every
* iteration.
*/
* entries(temp: [Uint64, Uint64] = [new Uint64(), new Uint64()]) {
let {emptyLow, emptyHigh, entryStride} = this;
let {table} = this;
let [key, value] = temp;
for (let i = 0, length = table.length; i < length; i += entryStride) {
let low = table[i], high = table[i + 1];
if (low !== emptyLow || high !== emptyHigh) {
key.low = low;
key.high = high;
value.low = table[i + 2];
value.high = table[i + 3];
yield temp;
}
}
}
}
HashMapUint64.prototype.entryStride = 4;