@structs-arena/interpolated-skip-list 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +62 -3
- package/package.json +43 -6
- package/src/index.js +440 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Augusto Vasques
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @structs-arena/interpolated-skip-list
|
|
2
|
+
|
|
3
|
+
A skip list for finite JavaScript `Number` keys, backed by Balloc and organized as Structure of Arrays (SoA). Values are stored in a separate dictionary.
|
|
4
|
+
|
|
5
|
+
This package is independent of `@structs-arena/skip-list`; the integer-keyed `SkipList` remains unchanged.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @structs-arena/interpolated-skip-list
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Basic usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { InterpolatedSkipList } from "@structs-arena/interpolated-skip-list";
|
|
17
|
+
|
|
18
|
+
const list = new InterpolatedSkipList({
|
|
19
|
+
callback(indexA, valueA, indexB, valueB, index, consolidate) {
|
|
20
|
+
const ratio = (index - indexA) / (indexB - indexA);
|
|
21
|
+
return consolidate(valueA + (valueB - valueA) * ratio);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
list.insertMany([[0.5, 10], [2.5, 30]]);
|
|
26
|
+
console.log(list.get(1.5)); // 20; interpolated and consolidated
|
|
27
|
+
console.log(list.get(1.5)); // 20; direct lookup
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Lookup contract
|
|
31
|
+
|
|
32
|
+
`get(key, callback?)` returns an existing value without invoking the callback. For a missing key, the callback receives the adjacent predecessor and successor:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
callback(indexA, valueA, indexB, valueB, index, consolidate)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The callback defines the value semantics; the data structure does not assume linear interpolation. Calling `consolidate(value)` inserts the computed value at the requested key and returns it. If either neighbor is absent, the lookup throws `RangeError`. A missing-key lookup without a callback throws `TypeError`.
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
- `new InterpolatedSkipList(options)`: options are `callback`, `probability`, `seed`, `capacity`, and `maxLevel`.
|
|
43
|
+
- `insert(key, value)`, `insertMany(entries)`: insert or replace entries.
|
|
44
|
+
- `get(key, callback?)`: retrieve a value or invoke the interpolation callback.
|
|
45
|
+
- `has(key)`, `delete(key)`, `deleteMany(keys)`: membership and removal.
|
|
46
|
+
- `clear()`: remove all entries.
|
|
47
|
+
- `size`, `height`, `probability`: read-only state properties.
|
|
48
|
+
- `entries()`, `keys()`, `values()`, `[Symbol.iterator]()`: ordered iterators.
|
|
49
|
+
|
|
50
|
+
Keys must be finite JavaScript numbers. Negative values and fractions are supported; `NaN` and infinities are rejected. JavaScript treats `-0` and `0` as the same key.
|
|
51
|
+
|
|
52
|
+
## Storage model
|
|
53
|
+
|
|
54
|
+
- Keys: Balloc-managed `Float64Array` blocks.
|
|
55
|
+
- Forward links: Balloc-managed `Uint32Array` blocks with a fixed per-node level stride.
|
|
56
|
+
- Node heights: Balloc-managed `Uint8Array` blocks.
|
|
57
|
+
- Values: an `Object.create(null)` dictionary.
|
|
58
|
+
- Released node slots are tracked in a free stack and reused.
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@structs-arena/interpolated-skip-list",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@structs-arena/interpolated-skip-list",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"src",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"description": "Skip list for finite floating-point keys with callback-driven interpolation.",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"interpolated-skip-list",
|
|
17
|
+
"skip-list",
|
|
18
|
+
"interpolation",
|
|
19
|
+
"float64",
|
|
20
|
+
"soa",
|
|
21
|
+
"data-structures",
|
|
22
|
+
"javascript",
|
|
23
|
+
"balloc"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/AugustoVasques75/structs-arena.git",
|
|
28
|
+
"directory": "packages/interpolated-skip-list"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/AugustoVasques75/structs-arena/issues"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"test": "vitest run",
|
|
35
|
+
"test:watch": "vitest"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"balloc": "^2.0.1"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=20"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import Balloc from "balloc";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_SEED = 0x9E3779B9;
|
|
4
|
+
const MAX_CAPACITY = 0x80000000;
|
|
5
|
+
|
|
6
|
+
/** Deterministic 32-bit Xorshift generator used to select node heights. */
|
|
7
|
+
class Xorshift32 {
|
|
8
|
+
#state;
|
|
9
|
+
|
|
10
|
+
/** @param {number} seed - Initial pseudo-random state. */
|
|
11
|
+
constructor(seed) {
|
|
12
|
+
this.#state = seed >>> 0 || 1;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** @returns {number} The next unsigned 32-bit pseudo-random value. */
|
|
16
|
+
next() {
|
|
17
|
+
let x = this.#state;
|
|
18
|
+
x ^= x << 13;
|
|
19
|
+
x ^= x >>> 17;
|
|
20
|
+
x ^= x << 5;
|
|
21
|
+
this.#state = x >>> 0;
|
|
22
|
+
return this.#state;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Ordered skip list for finite JavaScript numbers with optional callback-driven
|
|
28
|
+
* interpolation for missing keys. Keys and links use Balloc-managed typed arrays;
|
|
29
|
+
* values are stored separately in a null-prototype dictionary.
|
|
30
|
+
*/
|
|
31
|
+
export class InterpolatedSkipList {
|
|
32
|
+
#memory;
|
|
33
|
+
#keys;
|
|
34
|
+
#heights;
|
|
35
|
+
#links;
|
|
36
|
+
#free;
|
|
37
|
+
#values = Object.create(null);
|
|
38
|
+
#callback;
|
|
39
|
+
#random;
|
|
40
|
+
#probability;
|
|
41
|
+
#threshold;
|
|
42
|
+
#maxLevel;
|
|
43
|
+
#insertPath;
|
|
44
|
+
#capacity;
|
|
45
|
+
#nextSlot = 1;
|
|
46
|
+
#size = 0;
|
|
47
|
+
#level = 1;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Create an empty interpolated skip list.
|
|
51
|
+
* @param {object} [options] - Construction options.
|
|
52
|
+
* @param {Function} [options.callback] - Default callback used to resolve missing keys.
|
|
53
|
+
* @param {number} [options.probability=0.25] - Probability used to promote a node to the next level.
|
|
54
|
+
* @param {number} [options.seed=0x9E3779B9] - Seed for deterministic node-height generation.
|
|
55
|
+
* @param {number} [options.capacity=16] - Initial node capacity.
|
|
56
|
+
* @param {number} [options.maxLevel=16] - Maximum number of levels.
|
|
57
|
+
* @throws {TypeError} If callback is provided and is not a function.
|
|
58
|
+
* @throws {RangeError} If a numeric option is outside its supported range.
|
|
59
|
+
*/
|
|
60
|
+
constructor({ callback, probability = 0.25, seed = DEFAULT_SEED, capacity = 16, maxLevel = 16 } = {}) {
|
|
61
|
+
if (callback !== undefined && typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
62
|
+
if (!Number.isFinite(probability) || probability < 0 || probability > 1) throw new RangeError("probability must be between 0 and 1");
|
|
63
|
+
if (!Number.isSafeInteger(seed)) throw new RangeError("seed must be a safe integer");
|
|
64
|
+
if (!Number.isSafeInteger(capacity) || capacity < 2) throw new RangeError("capacity must be an integer greater than or equal to 2");
|
|
65
|
+
if (!Number.isInteger(maxLevel) || maxLevel < 1 || maxLevel > 32) throw new RangeError("maxLevel must be an integer between 1 and 32");
|
|
66
|
+
if (capacity * maxLevel >= MAX_CAPACITY) throw new RangeError("initial capacity exceeds Balloc limits");
|
|
67
|
+
|
|
68
|
+
this.#callback = callback;
|
|
69
|
+
this.#probability = probability;
|
|
70
|
+
this.#threshold = probability * 0x100000000;
|
|
71
|
+
this.#random = new Xorshift32(seed);
|
|
72
|
+
this.#maxLevel = maxLevel;
|
|
73
|
+
this.#insertPath = new Uint32Array(maxLevel);
|
|
74
|
+
this.#capacity = capacity;
|
|
75
|
+
this.#memory = new Balloc(MAX_CAPACITY);
|
|
76
|
+
this.#keys = this.#memory.alloc(capacity, "64f");
|
|
77
|
+
this.#heights = this.#memory.alloc(capacity, "8u");
|
|
78
|
+
this.#links = this.#memory.alloc(capacity * maxLevel, "32u");
|
|
79
|
+
this.#free = this.#memory.alloc(capacity, "32u");
|
|
80
|
+
this.#memory.fillAll(this.#keys, 0);
|
|
81
|
+
this.#memory.fillAll(this.#heights, 0);
|
|
82
|
+
this.#memory.fillAll(this.#links, 0);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** @returns {number} Number of keys currently stored. */
|
|
86
|
+
get size() {
|
|
87
|
+
return this.#size;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** @returns {number} Number of active skip-list levels. */
|
|
91
|
+
get height() {
|
|
92
|
+
return this.#level;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** @returns {number} Configured node-promotion probability. */
|
|
96
|
+
get probability() {
|
|
97
|
+
return this.#probability;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#validateKey(key) {
|
|
101
|
+
if (typeof key !== "number" || !Number.isFinite(key)) throw new TypeError("key must be a finite Number");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
#ensureCapacity(required) {
|
|
105
|
+
while (required > this.#capacity) {
|
|
106
|
+
const capacity = this.#capacity * 2;
|
|
107
|
+
if (capacity * this.#maxLevel >= MAX_CAPACITY) throw new RangeError("Skip list capacity limit exceeded");
|
|
108
|
+
const keys = this.#memory.pick(this.#keys);
|
|
109
|
+
const heights = this.#memory.pick(this.#heights);
|
|
110
|
+
const links = this.#memory.pick(this.#links);
|
|
111
|
+
this.#memory.realloc(this.#keys, capacity);
|
|
112
|
+
this.#memory.realloc(this.#heights, capacity);
|
|
113
|
+
this.#memory.realloc(this.#links, capacity * this.#maxLevel);
|
|
114
|
+
this.#memory.fillAll(this.#keys, 0);
|
|
115
|
+
this.#memory.fillAll(this.#heights, 0);
|
|
116
|
+
this.#memory.fillAll(this.#links, 0);
|
|
117
|
+
this.#memory.put(this.#keys, keys);
|
|
118
|
+
this.#memory.put(this.#heights, heights);
|
|
119
|
+
this.#memory.put(this.#links, links);
|
|
120
|
+
this.#capacity = capacity;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
#randomHeight() {
|
|
125
|
+
let height = 1;
|
|
126
|
+
while (height < this.#maxLevel && this.#random.next() < this.#threshold) height++;
|
|
127
|
+
return height;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
#findPath(key) {
|
|
131
|
+
const path = new Uint32Array(this.#maxLevel);
|
|
132
|
+
let candidate = 0;
|
|
133
|
+
|
|
134
|
+
this.#memory.withView(this.#keys, this.#links, (keys, links) => {
|
|
135
|
+
for (let level = this.#level - 1; level >= 0; level--) {
|
|
136
|
+
let next = links[candidate * this.#maxLevel + level];
|
|
137
|
+
while (next !== 0 && keys[next] < key) {
|
|
138
|
+
candidate = next;
|
|
139
|
+
next = links[candidate * this.#maxLevel + level];
|
|
140
|
+
}
|
|
141
|
+
path[level] = candidate;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
return path;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#findNode(key, path) {
|
|
149
|
+
return this.#memory.get(this.#links, path[0] * this.#maxLevel);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Insert a key/value pair or replace the value for an existing key.
|
|
154
|
+
* @param {number} key - Finite numeric key.
|
|
155
|
+
* @param {*} value - Value associated with the key.
|
|
156
|
+
* @returns {this} This list, for chaining.
|
|
157
|
+
* @throws {TypeError} If key is not a finite number.
|
|
158
|
+
*/
|
|
159
|
+
insert(key, value) {
|
|
160
|
+
this.#validateKey(key);
|
|
161
|
+
const freeCount = this.#memory.occupied(this.#free);
|
|
162
|
+
if (freeCount === 0) this.#ensureCapacity(this.#nextSlot + 1);
|
|
163
|
+
|
|
164
|
+
this.#memory.withView(this.#keys, this.#heights, this.#links, (keys, heights, links) => {
|
|
165
|
+
const path = this.#insertPath;
|
|
166
|
+
let candidate = 0;
|
|
167
|
+
|
|
168
|
+
for (let level = this.#level - 1; level >= 0; level--) {
|
|
169
|
+
let next = links[candidate * this.#maxLevel + level];
|
|
170
|
+
while (next !== 0 && keys[next] < key) {
|
|
171
|
+
candidate = next;
|
|
172
|
+
next = links[candidate * this.#maxLevel + level];
|
|
173
|
+
}
|
|
174
|
+
path[level] = candidate;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const existing = links[path[0] * this.#maxLevel];
|
|
178
|
+
if (existing !== 0 && keys[existing] === key) {
|
|
179
|
+
this.#values[String(key)] = value;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const slot = freeCount > 0 ? this.#memory.pop(this.#free) : this.#nextSlot++;
|
|
184
|
+
const height = this.#randomHeight();
|
|
185
|
+
if (height > this.#level) {
|
|
186
|
+
for (let level = this.#level; level < height; level++) path[level] = 0;
|
|
187
|
+
this.#level = height;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
keys[slot] = key;
|
|
191
|
+
heights[slot] = height;
|
|
192
|
+
const base = slot * this.#maxLevel;
|
|
193
|
+
|
|
194
|
+
for (let level = 0; level < height; level++) {
|
|
195
|
+
const predecessorLink = path[level] * this.#maxLevel + level;
|
|
196
|
+
links[base + level] = links[predecessorLink];
|
|
197
|
+
links[predecessorLink] = slot;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
this.#values[String(key)] = value;
|
|
201
|
+
this.#size++;
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Insert or replace entries as a batch. Duplicate keys use the last value supplied.
|
|
209
|
+
* @param {Iterable<[number, *]>} entries - Iterable of key/value pairs.
|
|
210
|
+
* @returns {this} This list, for chaining.
|
|
211
|
+
* @throws {TypeError} If entries is not iterable, an entry is malformed, or a key is invalid.
|
|
212
|
+
*/
|
|
213
|
+
insertMany(entries) {
|
|
214
|
+
if (entries == null || typeof entries[Symbol.iterator] !== "function") throw new TypeError("entries must be iterable");
|
|
215
|
+
const batch = new Map();
|
|
216
|
+
|
|
217
|
+
for (const entry of entries) {
|
|
218
|
+
if (!Array.isArray(entry) || entry.length < 2) throw new TypeError("each entry must be a [key, value] pair");
|
|
219
|
+
this.#validateKey(entry[0]);
|
|
220
|
+
batch.set(entry[0], entry[1]);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let additions = 0;
|
|
224
|
+
for (const key of batch.keys()) if (!this.has(key)) additions++;
|
|
225
|
+
this.#ensureCapacity(this.#nextSlot + additions);
|
|
226
|
+
const ordered = [...batch].sort((a, b) => a[0] - b[0]);
|
|
227
|
+
|
|
228
|
+
this.#memory.withView(this.#keys, this.#heights, this.#links, (keys, heights, links) => {
|
|
229
|
+
for (const [key, value] of ordered) {
|
|
230
|
+
const path = new Uint32Array(this.#maxLevel);
|
|
231
|
+
let candidate = 0;
|
|
232
|
+
|
|
233
|
+
for (let level = this.#level - 1; level >= 0; level--) {
|
|
234
|
+
let next = links[candidate * this.#maxLevel + level];
|
|
235
|
+
while (next !== 0 && keys[next] < key) {
|
|
236
|
+
candidate = next;
|
|
237
|
+
next = links[candidate * this.#maxLevel + level];
|
|
238
|
+
}
|
|
239
|
+
path[level] = candidate;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const existing = links[path[0] * this.#maxLevel];
|
|
243
|
+
if (existing !== 0 && keys[existing] === key) {
|
|
244
|
+
this.#values[String(key)] = value;
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const slot = this.#memory.occupied(this.#free) > 0 ? this.#memory.pop(this.#free) : this.#nextSlot++;
|
|
249
|
+
const height = this.#randomHeight();
|
|
250
|
+
if (height > this.#level) {
|
|
251
|
+
for (let level = this.#level; level < height; level++) path[level] = 0;
|
|
252
|
+
this.#level = height;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
keys[slot] = key;
|
|
256
|
+
heights[slot] = height;
|
|
257
|
+
const base = slot * this.#maxLevel;
|
|
258
|
+
|
|
259
|
+
for (let level = 0; level < height; level++) {
|
|
260
|
+
const predecessorLink = path[level] * this.#maxLevel + level;
|
|
261
|
+
links[base + level] = links[predecessorLink];
|
|
262
|
+
links[predecessorLink] = slot;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
this.#values[String(key)] = value;
|
|
266
|
+
this.#size++;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
return this;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Return a stored value or resolve a missing key through an interpolation callback.
|
|
275
|
+
* @param {number} key - Finite numeric key.
|
|
276
|
+
* @param {Function} [callback=this.#callback] - Callback receiving adjacent keys and values plus a consolidation function.
|
|
277
|
+
* @returns {*} Stored or callback-produced value.
|
|
278
|
+
* @throws {TypeError} If the key is invalid or no callback is available for a missing key.
|
|
279
|
+
* @throws {RangeError} If a missing key has no predecessor or successor.
|
|
280
|
+
*/
|
|
281
|
+
get(key, callback = this.#callback) {
|
|
282
|
+
this.#validateKey(key);
|
|
283
|
+
const path = this.#findPath(key);
|
|
284
|
+
const node = this.#findNode(key, path);
|
|
285
|
+
|
|
286
|
+
if (node !== 0 && this.#memory.get(this.#keys, node) === key) return this.#values[String(key)];
|
|
287
|
+
if (this.#size < 2) throw new RangeError("Interpolation requires both predecessor and successor keys");
|
|
288
|
+
if (typeof callback !== "function") throw new TypeError("A callback is required for missing keys");
|
|
289
|
+
|
|
290
|
+
const predecessor = path[0];
|
|
291
|
+
const successor = this.#memory.get(this.#links, predecessor * this.#maxLevel);
|
|
292
|
+
if (predecessor === 0 || successor === 0) throw new RangeError("Interpolation requires both predecessor and successor keys");
|
|
293
|
+
|
|
294
|
+
const indexA = this.#memory.get(this.#keys, predecessor);
|
|
295
|
+
const indexB = this.#memory.get(this.#keys, successor);
|
|
296
|
+
const valueA = this.#values[String(indexA)];
|
|
297
|
+
const valueB = this.#values[String(indexB)];
|
|
298
|
+
const consolidate = value => {
|
|
299
|
+
this.insert(key, value);
|
|
300
|
+
return value;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
return callback(indexA, valueA, indexB, valueB, key, consolidate);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** @param {number} key - Finite numeric key. @returns {boolean} Whether the key is stored. */
|
|
307
|
+
has(key) {
|
|
308
|
+
this.#validateKey(key);
|
|
309
|
+
const path = this.#findPath(key);
|
|
310
|
+
const node = this.#findNode(key, path);
|
|
311
|
+
return node !== 0 && this.#memory.get(this.#keys, node) === key;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** @param {number} key - Finite numeric key. @returns {boolean} Whether an entry was removed. */
|
|
315
|
+
delete(key) {
|
|
316
|
+
this.#validateKey(key);
|
|
317
|
+
const path = this.#findPath(key);
|
|
318
|
+
const node = this.#findNode(key, path);
|
|
319
|
+
if (node === 0 || this.#memory.get(this.#keys, node) !== key) return false;
|
|
320
|
+
|
|
321
|
+
const height = this.#memory.get(this.#heights, node);
|
|
322
|
+
this.#memory.withView(this.#links, links => {
|
|
323
|
+
const base = node * this.#maxLevel;
|
|
324
|
+
for (let level = 0; level < height; level++) {
|
|
325
|
+
const predecessorLink = path[level] * this.#maxLevel + level;
|
|
326
|
+
links[predecessorLink] = links[base + level];
|
|
327
|
+
links[base + level] = 0;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
this.#memory.set(this.#heights, node, 0);
|
|
332
|
+
this.#memory.set(this.#keys, node, 0);
|
|
333
|
+
this.#memory.push(this.#free, node) || this.#growFreeStackAndPush(node);
|
|
334
|
+
delete this.#values[String(key)];
|
|
335
|
+
this.#size--;
|
|
336
|
+
|
|
337
|
+
while (this.#level > 1 && this.#memory.get(this.#links, this.#level - 1) === 0) this.#level--;
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
#growFreeStackAndPush(node) {
|
|
342
|
+
this.#memory.realloc(this.#free);
|
|
343
|
+
this.#memory.push(this.#free, node);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Remove the unique keys supplied by an iterable.
|
|
348
|
+
* @param {Iterable<number>} keys - Keys to remove.
|
|
349
|
+
* @returns {number} Number of entries removed.
|
|
350
|
+
* @throws {TypeError} If keys is not iterable or contains an invalid key.
|
|
351
|
+
*/
|
|
352
|
+
deleteMany(keys) {
|
|
353
|
+
if (keys == null || typeof keys[Symbol.iterator] !== "function") throw new TypeError("keys must be iterable");
|
|
354
|
+
const batch = [...new Set(keys)];
|
|
355
|
+
for (const key of batch) this.#validateKey(key);
|
|
356
|
+
const released = [];
|
|
357
|
+
let removed = 0;
|
|
358
|
+
|
|
359
|
+
this.#memory.withView(this.#keys, this.#heights, this.#links, (keyView, heights, links) => {
|
|
360
|
+
for (const key of batch) {
|
|
361
|
+
const path = new Uint32Array(this.#maxLevel);
|
|
362
|
+
let candidate = 0;
|
|
363
|
+
|
|
364
|
+
for (let level = this.#level - 1; level >= 0; level--) {
|
|
365
|
+
let next = links[candidate * this.#maxLevel + level];
|
|
366
|
+
while (next !== 0 && keyView[next] < key) {
|
|
367
|
+
candidate = next;
|
|
368
|
+
next = links[candidate * this.#maxLevel + level];
|
|
369
|
+
}
|
|
370
|
+
path[level] = candidate;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const node = links[path[0] * this.#maxLevel];
|
|
374
|
+
if (node === 0 || keyView[node] !== key) continue;
|
|
375
|
+
|
|
376
|
+
const height = heights[node];
|
|
377
|
+
const base = node * this.#maxLevel;
|
|
378
|
+
for (let level = 0; level < height; level++) {
|
|
379
|
+
const predecessorLink = path[level] * this.#maxLevel + level;
|
|
380
|
+
links[predecessorLink] = links[base + level];
|
|
381
|
+
}
|
|
382
|
+
links.fill(0, base, base + this.#maxLevel);
|
|
383
|
+
heights[node] = 0;
|
|
384
|
+
keyView[node] = 0;
|
|
385
|
+
released.push(node);
|
|
386
|
+
delete this.#values[String(key)];
|
|
387
|
+
this.#size--;
|
|
388
|
+
removed++;
|
|
389
|
+
|
|
390
|
+
while (this.#level > 1 && links[this.#level - 1] === 0) this.#level--;
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
for (const node of released) this.#memory.push(this.#free, node) || this.#growFreeStackAndPush(node);
|
|
395
|
+
return removed;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/** Remove all entries and reset the list to its initial logical state. @returns {this} This list. */
|
|
399
|
+
clear() {
|
|
400
|
+
this.#memory.fillAll(this.#keys, 0);
|
|
401
|
+
this.#memory.fillAll(this.#heights, 0);
|
|
402
|
+
this.#memory.fillAll(this.#links, 0);
|
|
403
|
+
this.#memory.clear(this.#free, 0);
|
|
404
|
+
this.#values = Object.create(null);
|
|
405
|
+
this.#nextSlot = 1;
|
|
406
|
+
this.#size = 0;
|
|
407
|
+
this.#level = 1;
|
|
408
|
+
return this;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** @yields {[number, *]} Stored key/value pairs in ascending key order. */
|
|
412
|
+
*entries() {
|
|
413
|
+
const entries = [];
|
|
414
|
+
this.#memory.withView(this.#keys, this.#links, (keys, links) => {
|
|
415
|
+
let node = links[0];
|
|
416
|
+
while (node !== 0) {
|
|
417
|
+
entries.push([keys[node], this.#values[String(keys[node])]]);
|
|
418
|
+
node = links[node * this.#maxLevel];
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
yield* entries;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** @yields {number} Stored keys in ascending order. */
|
|
425
|
+
*keys() {
|
|
426
|
+
for (const [key] of this.entries()) yield key;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** @yields {*} Values in ascending key order. */
|
|
430
|
+
*values() {
|
|
431
|
+
for (const [, value] of this.entries()) yield value;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** @returns {IterableIterator<[number, *]>} An iterator over ordered key/value pairs. */
|
|
435
|
+
[Symbol.iterator]() {
|
|
436
|
+
return this.entries();
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export default InterpolatedSkipList;
|