-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
fixed-critbit-tree-map.js
427 lines (336 loc) · 8.68 KB
/
fixed-critbit-tree-map.js
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
/* eslint no-constant-condition: 0 */
/* eslint-disable */
/**
* Mnemonist FixedFixedCritBitTreeMap
* ===================================
*
* TODO...
*
* [References]:
* https://cr.yp.to/critbit.html
* https://www.imperialviolet.org/binary/critbit.pdf
*/
var bitwise = require('./utils/bitwise.js'),
typed = require('./utils/typed-arrays.js');
/**
* Helpers.
*/
/**
* Helper returning the direction we need to take given a key and an
* encoded critbit.
*
* @param {string} key - Target key.
* @param {number} critbit - Packed address of byte + mask.
* @return {number} - 0, left or 1, right.
*/
function getDirection(key, critbit) {
var byteIndex = critbit >> 8;
if (byteIndex > key.length - 1)
return 0;
var byte = key.charCodeAt(byteIndex),
mask = critbit & 0xff;
return byte & mask;
}
/**
* Helper returning the packed address of byte + mask or -1 if strings
* are identical.
*
* @param {string} a - First key.
* @param {string} b - Second key.
* @return {number} - Packed address of byte + mask.
*/
function findCriticalBit(a, b) {
var i = 0,
tmp;
// Swapping so a is the shortest
if (a.length > b.length) {
tmp = b;
b = a;
a = tmp;
}
var l = a.length,
mask;
while (i < l) {
if (a[i] !== b[i]) {
mask = bitwise.msb8(
a.charCodeAt(i) ^ b.charCodeAt(i)
);
return (i << 8) | mask;
}
i++;
}
// Strings are identical
if (a.length === b.length)
return -1;
// NOTE: x ^ 0 is the same as x
mask = bitwise.msb8(b.charCodeAt(i));
return (i << 8) | mask;
}
/**
* FixedCritBitTreeMap.
*
* @constructor
*/
function FixedCritBitTreeMap(capacity) {
if (typeof capacity !== 'number' || capacity <= 0)
throw new Error('mnemonist/fixed-critbit-tree-map: `capacity` should be a positive number.');
// Properties
this.capacity = capacity;
this.offset = 0;
this.root = 0;
this.size = 0;
var PointerArray = typed.getSignedPointerArray(capacity + 1);
this.keys = new Array(capacity);
this.values = new Array(capacity);
this.lefts = new PointerArray(capacity - 1);
this.rights = new PointerArray(capacity - 1);
this.critbits = new Uint32Array(capacity);
}
/**
* Method used to clear the FixedCritBitTreeMap.
*
* @return {undefined}
*/
FixedCritBitTreeMap.prototype.clear = function() {
// Properties
// TODO...
this.root = null;
this.size = 0;
};
/**
* Method used to set the value of the given key in the trie.
*
* @param {string} key - Key to set.
* @param {any} value - Arbitrary value.
* @return {FixedCritBitTreeMap}
*/
FixedCritBitTreeMap.prototype.set = function(key, value) {
var pointer;
// TODO: yell if capacity is already full!
// Tree is empty
if (this.size === 0) {
this.keys[0] = key;
this.values[0] = value;
this.size++;
this.root = -1;
return this;
}
// Walk state
var pointer = this.root,
newPointer,
leftOrRight,
opposite,
ancestors = [],
path = [],
ancestor,
parent,
child,
critbit,
internal,
best,
dir,
i,
l;
// Walking the tree
while (true) {
// Traversing an internal node
if (pointer > 0) {
pointer -= 1;
// Choosing the correct direction
dir = getDirection(key, this.critbits[pointer]);
leftOrRight = dir === 0 ? this.lefts : this.rights;
newPointer = leftOrRight[pointer];
if (newPointer === 0) {
// Creating a fitting external node
pointer = this.size++;
leftOrRight[newPointer] = -(pointer + 1);
this.keys[pointer] = key;
this.values[pointer] = value;
return this;
}
ancestors.push(pointer);
path.push(dir);
pointer = newPointer;
}
// Reaching an external node
else {
pointer = -pointer;
pointer -= 1;
// 1. Creating a new external node
critbit = findCriticalBit(key, this.keys[pointer]);
// Key is identical, we just replace the value
if (critbit === -1) {
this.values[pointer] = value;
return this;
}
internal = this.offset++;
newPointer = this.size++;
this.keys[newPointer] = key;
this.values[newPointer] = value;
this.critbits[internal] = critbit;
dir = getDirection(key, critbit);
leftOrRight = dir === 0 ? this.lefts : this.rights;
opposite = dir === 0 ? this.rights : this.lefts;
leftOrRight[internal] = -(newPointer + 1);
opposite[internal] = -(pointer + 1);
// 2. Bubbling up
best = -1;
l = ancestors.length;
for (i = l - 1; i >= 0; i--) {
ancestor = ancestors[i];
// TODO: this can be made faster
if ((this.critbits[ancestor] >> 8) > (critbit >> 8)) {
continue;
}
else if ((this.critbits[ancestor] >> 8) === (critbit >> 8)) {
if ((this.critbits[ancestor] & 0xff) < (critbit & 0xff))
continue;
}
best = i;
break;
}
// Do we need to attach to the root?
if (best < 0) {
this.root = internal + 1;
// Need to rewire parent as child?
if (l > 0) {
parent = ancestors[0];
opposite[internal] = parent + 1;
}
}
// Simple case without rotation
else if (best === l - 1) {
parent = ancestors[best];
dir = path[best];
leftOrRight = dir === 0 ? this.lefts : this.rights;
leftOrRight[parent] = internal + 1;
}
// Full rotation
else {
parent = ancestors[best];
dir = path[best];
child = ancestors[best + 1];
opposite[internal] = child + 1;
leftOrRight = dir === 0 ? this.lefts : this.rights;
leftOrRight[parent] = internal + 1;
}
return this;
}
}
};
/**
* Method used to get the value attached to the given key in the tree or
* undefined if not found.
*
* @param {string} key - Key to get.
* @return {any}
*/
FixedCritBitTreeMap.prototype.get = function(key) {
// Walk state
var pointer = this.root,
dir;
// Walking the tree
while (true) {
// Dead end
if (pointer === 0)
return;
// Traversing an internal node
if (pointer > 0) {
pointer -= 1;
dir = getDirection(key, this.critbits[pointer]);
pointer = dir === 0 ? this.lefts[pointer] : this.rights[pointer];
}
// Reaching an external node
else {
pointer = -pointer;
pointer -= 1;
if (this.keys[pointer] !== key)
return;
return this.values[pointer];
}
}
};
/**
* Method used to return whether the given key exists in the tree.
*
* @param {string} key - Key to test.
* @return {boolean}
*/
FixedCritBitTreeMap.prototype.has = function(key) {
// Walk state
var pointer = this.root,
dir;
// Walking the tree
while (true) {
// Dead end
if (pointer === 0)
return false;
// Traversing an internal node
if (pointer > 0) {
pointer -= 1;
dir = getDirection(key, this.critbits[pointer]);
pointer = dir === 0 ? this.lefts[pointer] : this.rights[pointer];
}
// Reaching an external node
else {
pointer = -pointer;
pointer -= 1;
return this.keys[pointer] === key;
}
}
};
/**
* Method used to iterate over the tree in key order.
*
* @param {function} callback - Function to call for each item.
* @param {object} scope - Optional scope.
* @return {undefined}
*/
FixedCritBitTreeMap.prototype.forEach = function(callback, scope) {
scope = arguments.length > 1 ? scope : this;
// Inorder traversal of the tree
var current = this.root,
stack = [],
p;
while (true) {
if (current !== 0) {
stack.push(current);
current = current > 0 ? this.lefts[current - 1] : 0;
}
else {
if (stack.length > 0) {
current = stack.pop();
if (current < 0) {
p = -current;
p -= 1;
callback.call(scope, this.values[p], this.keys[p]);
}
current = current > 0 ? this.rights[current - 1] : 0;
}
else {
break;
}
}
}
};
/**
* Convenience known methods.
*/
FixedCritBitTreeMap.prototype.inspect = function() {
return this;
};
if (typeof Symbol !== 'undefined')
FixedCritBitTreeMap.prototype[Symbol.for('nodejs.util.inspect.custom')] = FixedCritBitTreeMap.prototype.inspect;
/**
* Static @.from function taking an arbitrary iterable & converting it into
* a FixedCritBitTreeMap.
*
* @param {Iterable} iterable - Target iterable.
* @return {FixedCritBitTreeMap}
*/
// FixedCritBitTreeMap.from = function(iterable) {
// };
/**
* Exporting.
*/
module.exports = FixedCritBitTreeMap;