splaytree 3.0.0 → 3.1.1
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/dist/splay.esm.js +225 -142
- package/dist/splay.esm.js.map +1 -1
- package/dist/splay.js +665 -604
- package/dist/splay.js.map +1 -1
- package/package.json +13 -7
- package/src/index.ts +10 -2
- package/typings/index.d.ts +3 -2
package/dist/splay.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* splaytree v3.
|
|
2
|
+
* splaytree v3.1.1
|
|
3
3
|
* Fast Splay tree for Node and browser
|
|
4
4
|
*
|
|
5
5
|
* @author Alexander Milevski <info@w8r.name>
|
|
@@ -7,15 +7,59 @@
|
|
|
7
7
|
* @preserve
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
/*! *****************************************************************************
|
|
11
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
12
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
13
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
14
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
+
|
|
16
|
+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
|
18
|
+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
19
|
+
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
20
|
+
|
|
21
|
+
See the Apache Version 2.0 License for specific language governing permissions
|
|
22
|
+
and limitations under the License.
|
|
23
|
+
***************************************************************************** */
|
|
24
|
+
|
|
25
|
+
function __generator(thisArg, body) {
|
|
26
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
27
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
28
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
29
|
+
function step(op) {
|
|
30
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
31
|
+
while (_) try {
|
|
32
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
33
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
34
|
+
switch (op[0]) {
|
|
35
|
+
case 0: case 1: t = op; break;
|
|
36
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
37
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
38
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
39
|
+
default:
|
|
40
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
41
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
42
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
43
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
44
|
+
if (t[2]) _.ops.pop();
|
|
45
|
+
_.trys.pop(); continue;
|
|
46
|
+
}
|
|
47
|
+
op = body.call(thisArg, _);
|
|
48
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
49
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
var Node = /** @class */ (function () {
|
|
54
|
+
function Node(key, data) {
|
|
12
55
|
this.next = null;
|
|
13
56
|
this.key = key;
|
|
14
57
|
this.data = data;
|
|
15
58
|
this.left = null;
|
|
16
59
|
this.right = null;
|
|
17
60
|
}
|
|
18
|
-
|
|
61
|
+
return Node;
|
|
62
|
+
}());
|
|
19
63
|
|
|
20
64
|
/* follows "An implementation of top-down splaying"
|
|
21
65
|
* by D. Sleator <sleator@cs.cmu.edu> March 1992
|
|
@@ -27,18 +71,18 @@ function DEFAULT_COMPARE(a, b) {
|
|
|
27
71
|
* Simple top down splay, not requiring i to be in the tree t.
|
|
28
72
|
*/
|
|
29
73
|
function splay(i, t, comparator) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
74
|
+
var N = new Node(null, null);
|
|
75
|
+
var l = N;
|
|
76
|
+
var r = N;
|
|
33
77
|
while (true) {
|
|
34
|
-
|
|
78
|
+
var cmp = comparator(i, t.key);
|
|
35
79
|
//if (i < t.key) {
|
|
36
80
|
if (cmp < 0) {
|
|
37
81
|
if (t.left === null)
|
|
38
82
|
break;
|
|
39
83
|
//if (i < t.left.key) {
|
|
40
84
|
if (comparator(i, t.left.key) < 0) {
|
|
41
|
-
|
|
85
|
+
var y = t.left; /* rotate right */
|
|
42
86
|
t.left = y.right;
|
|
43
87
|
y.right = t;
|
|
44
88
|
t = y;
|
|
@@ -55,7 +99,7 @@ function splay(i, t, comparator) {
|
|
|
55
99
|
break;
|
|
56
100
|
//if (i > t.right.key) {
|
|
57
101
|
if (comparator(i, t.right.key) > 0) {
|
|
58
|
-
|
|
102
|
+
var y = t.right; /* rotate left */
|
|
59
103
|
t.right = y.left;
|
|
60
104
|
y.left = t;
|
|
61
105
|
t = y;
|
|
@@ -77,13 +121,13 @@ function splay(i, t, comparator) {
|
|
|
77
121
|
return t;
|
|
78
122
|
}
|
|
79
123
|
function insert(i, data, t, comparator) {
|
|
80
|
-
|
|
124
|
+
var node = new Node(i, data);
|
|
81
125
|
if (t === null) {
|
|
82
126
|
node.left = node.right = null;
|
|
83
127
|
return node;
|
|
84
128
|
}
|
|
85
129
|
t = splay(i, t, comparator);
|
|
86
|
-
|
|
130
|
+
var cmp = comparator(i, t.key);
|
|
87
131
|
if (cmp < 0) {
|
|
88
132
|
node.left = t.left;
|
|
89
133
|
node.right = t;
|
|
@@ -97,11 +141,11 @@ function insert(i, data, t, comparator) {
|
|
|
97
141
|
return node;
|
|
98
142
|
}
|
|
99
143
|
function split(key, v, comparator) {
|
|
100
|
-
|
|
101
|
-
|
|
144
|
+
var left = null;
|
|
145
|
+
var right = null;
|
|
102
146
|
if (v) {
|
|
103
147
|
v = splay(key, v, comparator);
|
|
104
|
-
|
|
148
|
+
var cmp = comparator(v.key, key);
|
|
105
149
|
if (cmp === 0) {
|
|
106
150
|
left = v.left;
|
|
107
151
|
right = v.right;
|
|
@@ -117,7 +161,7 @@ function split(key, v, comparator) {
|
|
|
117
161
|
right = v;
|
|
118
162
|
}
|
|
119
163
|
}
|
|
120
|
-
return { left, right };
|
|
164
|
+
return { left: left, right: right };
|
|
121
165
|
}
|
|
122
166
|
function merge(left, right, comparator) {
|
|
123
167
|
if (right === null)
|
|
@@ -133,16 +177,17 @@ function merge(left, right, comparator) {
|
|
|
133
177
|
*/
|
|
134
178
|
function printRow(root, prefix, isTail, out, printNode) {
|
|
135
179
|
if (root) {
|
|
136
|
-
out(
|
|
137
|
-
|
|
180
|
+
out("" + prefix + (isTail ? '└── ' : '├── ') + printNode(root) + "\n");
|
|
181
|
+
var indent = prefix + (isTail ? ' ' : '│ ');
|
|
138
182
|
if (root.left)
|
|
139
183
|
printRow(root.left, indent, false, out, printNode);
|
|
140
184
|
if (root.right)
|
|
141
185
|
printRow(root.right, indent, true, out, printNode);
|
|
142
186
|
}
|
|
143
187
|
}
|
|
144
|
-
|
|
145
|
-
|
|
188
|
+
var Tree = /** @class */ (function () {
|
|
189
|
+
function Tree(comparator) {
|
|
190
|
+
if (comparator === void 0) { comparator = DEFAULT_COMPARE; }
|
|
146
191
|
this._root = null;
|
|
147
192
|
this._size = 0;
|
|
148
193
|
this._comparator = comparator;
|
|
@@ -150,23 +195,23 @@ class Tree {
|
|
|
150
195
|
/**
|
|
151
196
|
* Inserts a key, allows duplicates
|
|
152
197
|
*/
|
|
153
|
-
insert(key, data) {
|
|
198
|
+
Tree.prototype.insert = function (key, data) {
|
|
154
199
|
this._size++;
|
|
155
200
|
return this._root = insert(key, data, this._root, this._comparator);
|
|
156
|
-
}
|
|
201
|
+
};
|
|
157
202
|
/**
|
|
158
203
|
* Adds a key, if it is not present in the tree
|
|
159
204
|
*/
|
|
160
|
-
add(key, data) {
|
|
161
|
-
|
|
205
|
+
Tree.prototype.add = function (key, data) {
|
|
206
|
+
var node = new Node(key, data);
|
|
162
207
|
if (this._root === null) {
|
|
163
208
|
node.left = node.right = null;
|
|
164
209
|
this._size++;
|
|
165
210
|
this._root = node;
|
|
166
211
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
212
|
+
var comparator = this._comparator;
|
|
213
|
+
var t = splay(key, this._root, comparator);
|
|
214
|
+
var cmp = comparator(key, t.key);
|
|
170
215
|
if (cmp === 0)
|
|
171
216
|
this._root = t;
|
|
172
217
|
else {
|
|
@@ -184,23 +229,23 @@ class Tree {
|
|
|
184
229
|
this._root = node;
|
|
185
230
|
}
|
|
186
231
|
return this._root;
|
|
187
|
-
}
|
|
232
|
+
};
|
|
188
233
|
/**
|
|
189
234
|
* @param {Key} key
|
|
190
235
|
* @return {Node|null}
|
|
191
236
|
*/
|
|
192
|
-
remove(key) {
|
|
237
|
+
Tree.prototype.remove = function (key) {
|
|
193
238
|
this._root = this._remove(key, this._root, this._comparator);
|
|
194
|
-
}
|
|
239
|
+
};
|
|
195
240
|
/**
|
|
196
241
|
* Deletes i from the tree if it's there
|
|
197
242
|
*/
|
|
198
|
-
_remove(i, t, comparator) {
|
|
199
|
-
|
|
243
|
+
Tree.prototype._remove = function (i, t, comparator) {
|
|
244
|
+
var x;
|
|
200
245
|
if (t === null)
|
|
201
246
|
return null;
|
|
202
247
|
t = splay(i, t, comparator);
|
|
203
|
-
|
|
248
|
+
var cmp = comparator(i, t.key);
|
|
204
249
|
if (cmp === 0) { /* found it */
|
|
205
250
|
if (t.left === null) {
|
|
206
251
|
x = t.right;
|
|
@@ -213,12 +258,12 @@ class Tree {
|
|
|
213
258
|
return x;
|
|
214
259
|
}
|
|
215
260
|
return t; /* It wasn't there */
|
|
216
|
-
}
|
|
261
|
+
};
|
|
217
262
|
/**
|
|
218
263
|
* Removes and returns the node with smallest key
|
|
219
264
|
*/
|
|
220
|
-
pop() {
|
|
221
|
-
|
|
265
|
+
Tree.prototype.pop = function () {
|
|
266
|
+
var node = this._root;
|
|
222
267
|
if (node) {
|
|
223
268
|
while (node.left)
|
|
224
269
|
node = node.left;
|
|
@@ -227,15 +272,15 @@ class Tree {
|
|
|
227
272
|
return { key: node.key, data: node.data };
|
|
228
273
|
}
|
|
229
274
|
return null;
|
|
230
|
-
}
|
|
275
|
+
};
|
|
231
276
|
/**
|
|
232
277
|
* Find without splaying
|
|
233
278
|
*/
|
|
234
|
-
findStatic(key) {
|
|
235
|
-
|
|
236
|
-
|
|
279
|
+
Tree.prototype.findStatic = function (key) {
|
|
280
|
+
var current = this._root;
|
|
281
|
+
var compare = this._comparator;
|
|
237
282
|
while (current) {
|
|
238
|
-
|
|
283
|
+
var cmp = compare(key, current.key);
|
|
239
284
|
if (cmp === 0)
|
|
240
285
|
return current;
|
|
241
286
|
else if (cmp < 0)
|
|
@@ -244,20 +289,20 @@ class Tree {
|
|
|
244
289
|
current = current.right;
|
|
245
290
|
}
|
|
246
291
|
return null;
|
|
247
|
-
}
|
|
248
|
-
find(key) {
|
|
292
|
+
};
|
|
293
|
+
Tree.prototype.find = function (key) {
|
|
249
294
|
if (this._root) {
|
|
250
295
|
this._root = splay(key, this._root, this._comparator);
|
|
251
296
|
if (this._comparator(key, this._root.key) !== 0)
|
|
252
297
|
return null;
|
|
253
298
|
}
|
|
254
299
|
return this._root;
|
|
255
|
-
}
|
|
256
|
-
contains(key) {
|
|
257
|
-
|
|
258
|
-
|
|
300
|
+
};
|
|
301
|
+
Tree.prototype.contains = function (key) {
|
|
302
|
+
var current = this._root;
|
|
303
|
+
var compare = this._comparator;
|
|
259
304
|
while (current) {
|
|
260
|
-
|
|
305
|
+
var cmp = compare(key, current.key);
|
|
261
306
|
if (cmp === 0)
|
|
262
307
|
return true;
|
|
263
308
|
else if (cmp < 0)
|
|
@@ -266,11 +311,11 @@ class Tree {
|
|
|
266
311
|
current = current.right;
|
|
267
312
|
}
|
|
268
313
|
return false;
|
|
269
|
-
}
|
|
270
|
-
forEach(visitor, ctx) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
314
|
+
};
|
|
315
|
+
Tree.prototype.forEach = function (visitor, ctx) {
|
|
316
|
+
var current = this._root;
|
|
317
|
+
var Q = []; /* Initialize stack s */
|
|
318
|
+
var done = false;
|
|
274
319
|
while (!done) {
|
|
275
320
|
if (current !== null) {
|
|
276
321
|
Q.push(current);
|
|
@@ -287,15 +332,15 @@ class Tree {
|
|
|
287
332
|
}
|
|
288
333
|
}
|
|
289
334
|
return this;
|
|
290
|
-
}
|
|
335
|
+
};
|
|
291
336
|
/**
|
|
292
337
|
* Walk key range from `low` to `high`. Stops if `fn` returns a value.
|
|
293
338
|
*/
|
|
294
|
-
range(low, high, fn, ctx) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
339
|
+
Tree.prototype.range = function (low, high, fn, ctx) {
|
|
340
|
+
var Q = [];
|
|
341
|
+
var compare = this._comparator;
|
|
342
|
+
var node = this._root;
|
|
343
|
+
var cmp;
|
|
299
344
|
while (Q.length !== 0 || node) {
|
|
300
345
|
if (node) {
|
|
301
346
|
Q.push(node);
|
|
@@ -315,53 +360,61 @@ class Tree {
|
|
|
315
360
|
}
|
|
316
361
|
}
|
|
317
362
|
return this;
|
|
318
|
-
}
|
|
363
|
+
};
|
|
319
364
|
/**
|
|
320
365
|
* Returns array of keys
|
|
321
366
|
*/
|
|
322
|
-
keys() {
|
|
323
|
-
|
|
324
|
-
this.forEach((
|
|
367
|
+
Tree.prototype.keys = function () {
|
|
368
|
+
var keys = [];
|
|
369
|
+
this.forEach(function (_a) {
|
|
370
|
+
var key = _a.key;
|
|
371
|
+
return keys.push(key);
|
|
372
|
+
});
|
|
325
373
|
return keys;
|
|
326
|
-
}
|
|
374
|
+
};
|
|
327
375
|
/**
|
|
328
376
|
* Returns array of all the data in the nodes
|
|
329
377
|
*/
|
|
330
|
-
values() {
|
|
331
|
-
|
|
332
|
-
this.forEach((
|
|
378
|
+
Tree.prototype.values = function () {
|
|
379
|
+
var values = [];
|
|
380
|
+
this.forEach(function (_a) {
|
|
381
|
+
var data = _a.data;
|
|
382
|
+
return values.push(data);
|
|
383
|
+
});
|
|
333
384
|
return values;
|
|
334
|
-
}
|
|
335
|
-
min() {
|
|
385
|
+
};
|
|
386
|
+
Tree.prototype.min = function () {
|
|
336
387
|
if (this._root)
|
|
337
388
|
return this.minNode(this._root).key;
|
|
338
389
|
return null;
|
|
339
|
-
}
|
|
340
|
-
max() {
|
|
390
|
+
};
|
|
391
|
+
Tree.prototype.max = function () {
|
|
341
392
|
if (this._root)
|
|
342
393
|
return this.maxNode(this._root).key;
|
|
343
394
|
return null;
|
|
344
|
-
}
|
|
345
|
-
minNode
|
|
395
|
+
};
|
|
396
|
+
Tree.prototype.minNode = function (t) {
|
|
397
|
+
if (t === void 0) { t = this._root; }
|
|
346
398
|
if (t)
|
|
347
399
|
while (t.left)
|
|
348
400
|
t = t.left;
|
|
349
401
|
return t;
|
|
350
|
-
}
|
|
351
|
-
maxNode
|
|
402
|
+
};
|
|
403
|
+
Tree.prototype.maxNode = function (t) {
|
|
404
|
+
if (t === void 0) { t = this._root; }
|
|
352
405
|
if (t)
|
|
353
406
|
while (t.right)
|
|
354
407
|
t = t.right;
|
|
355
408
|
return t;
|
|
356
|
-
}
|
|
409
|
+
};
|
|
357
410
|
/**
|
|
358
411
|
* Returns node at given index
|
|
359
412
|
*/
|
|
360
|
-
at(index) {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
413
|
+
Tree.prototype.at = function (index) {
|
|
414
|
+
var current = this._root;
|
|
415
|
+
var done = false;
|
|
416
|
+
var i = 0;
|
|
417
|
+
var Q = [];
|
|
365
418
|
while (!done) {
|
|
366
419
|
if (current) {
|
|
367
420
|
Q.push(current);
|
|
@@ -380,19 +433,19 @@ class Tree {
|
|
|
380
433
|
}
|
|
381
434
|
}
|
|
382
435
|
return null;
|
|
383
|
-
}
|
|
384
|
-
next(d) {
|
|
385
|
-
|
|
386
|
-
|
|
436
|
+
};
|
|
437
|
+
Tree.prototype.next = function (d) {
|
|
438
|
+
var root = this._root;
|
|
439
|
+
var successor = null;
|
|
387
440
|
if (d.right) {
|
|
388
441
|
successor = d.right;
|
|
389
442
|
while (successor.left)
|
|
390
443
|
successor = successor.left;
|
|
391
444
|
return successor;
|
|
392
445
|
}
|
|
393
|
-
|
|
446
|
+
var comparator = this._comparator;
|
|
394
447
|
while (root) {
|
|
395
|
-
|
|
448
|
+
var cmp = comparator(d.key, root.key);
|
|
396
449
|
if (cmp === 0)
|
|
397
450
|
break;
|
|
398
451
|
else if (cmp < 0) {
|
|
@@ -403,19 +456,19 @@ class Tree {
|
|
|
403
456
|
root = root.right;
|
|
404
457
|
}
|
|
405
458
|
return successor;
|
|
406
|
-
}
|
|
407
|
-
prev(d) {
|
|
408
|
-
|
|
409
|
-
|
|
459
|
+
};
|
|
460
|
+
Tree.prototype.prev = function (d) {
|
|
461
|
+
var root = this._root;
|
|
462
|
+
var predecessor = null;
|
|
410
463
|
if (d.left !== null) {
|
|
411
464
|
predecessor = d.left;
|
|
412
465
|
while (predecessor.right)
|
|
413
466
|
predecessor = predecessor.right;
|
|
414
467
|
return predecessor;
|
|
415
468
|
}
|
|
416
|
-
|
|
469
|
+
var comparator = this._comparator;
|
|
417
470
|
while (root) {
|
|
418
|
-
|
|
471
|
+
var cmp = comparator(d.key, root.key);
|
|
419
472
|
if (cmp === 0)
|
|
420
473
|
break;
|
|
421
474
|
else if (cmp < 0)
|
|
@@ -426,21 +479,23 @@ class Tree {
|
|
|
426
479
|
}
|
|
427
480
|
}
|
|
428
481
|
return predecessor;
|
|
429
|
-
}
|
|
430
|
-
clear() {
|
|
482
|
+
};
|
|
483
|
+
Tree.prototype.clear = function () {
|
|
431
484
|
this._root = null;
|
|
432
485
|
this._size = 0;
|
|
433
486
|
return this;
|
|
434
|
-
}
|
|
435
|
-
toList() {
|
|
487
|
+
};
|
|
488
|
+
Tree.prototype.toList = function () {
|
|
436
489
|
return toList(this._root);
|
|
437
|
-
}
|
|
490
|
+
};
|
|
438
491
|
/**
|
|
439
492
|
* Bulk-load items. Both array have to be same size
|
|
440
493
|
*/
|
|
441
|
-
load(keys, values
|
|
442
|
-
|
|
443
|
-
|
|
494
|
+
Tree.prototype.load = function (keys, values, presort) {
|
|
495
|
+
if (values === void 0) { values = []; }
|
|
496
|
+
if (presort === void 0) { presort = false; }
|
|
497
|
+
var size = keys.length;
|
|
498
|
+
var comparator = this._comparator;
|
|
444
499
|
// sort if needed
|
|
445
500
|
if (presort)
|
|
446
501
|
sort(keys, values, 0, size - 1, comparator);
|
|
@@ -449,23 +504,32 @@ class Tree {
|
|
|
449
504
|
this._size = size;
|
|
450
505
|
}
|
|
451
506
|
else { // that re-builds the whole tree from two in-order traversals
|
|
452
|
-
|
|
507
|
+
var mergedList = mergeLists(this.toList(), createList(keys, values), comparator);
|
|
453
508
|
size = this._size + size;
|
|
454
509
|
this._root = sortedListToBST({ head: mergedList }, 0, size);
|
|
455
510
|
}
|
|
456
511
|
return this;
|
|
457
|
-
}
|
|
458
|
-
isEmpty() { return this._root === null; }
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
512
|
+
};
|
|
513
|
+
Tree.prototype.isEmpty = function () { return this._root === null; };
|
|
514
|
+
Object.defineProperty(Tree.prototype, "size", {
|
|
515
|
+
get: function () { return this._size; },
|
|
516
|
+
enumerable: true,
|
|
517
|
+
configurable: true
|
|
518
|
+
});
|
|
519
|
+
Object.defineProperty(Tree.prototype, "root", {
|
|
520
|
+
get: function () { return this._root; },
|
|
521
|
+
enumerable: true,
|
|
522
|
+
configurable: true
|
|
523
|
+
});
|
|
524
|
+
Tree.prototype.toString = function (printNode) {
|
|
525
|
+
if (printNode === void 0) { printNode = function (n) { return String(n.key); }; }
|
|
526
|
+
var out = [];
|
|
527
|
+
printRow(this._root, '', true, function (v) { return out.push(v); }, printNode);
|
|
464
528
|
return out.join('');
|
|
465
|
-
}
|
|
466
|
-
update(key, newKey, newData) {
|
|
467
|
-
|
|
468
|
-
|
|
529
|
+
};
|
|
530
|
+
Tree.prototype.update = function (key, newKey, newData) {
|
|
531
|
+
var comparator = this._comparator;
|
|
532
|
+
var _a = split(key, this._root, comparator), left = _a.left, right = _a.right;
|
|
469
533
|
if (comparator(key, newKey) < 0) {
|
|
470
534
|
right = insert(newKey, newData, right, comparator);
|
|
471
535
|
}
|
|
@@ -473,18 +537,37 @@ class Tree {
|
|
|
473
537
|
left = insert(newKey, newData, left, comparator);
|
|
474
538
|
}
|
|
475
539
|
this._root = merge(left, right, comparator);
|
|
476
|
-
}
|
|
477
|
-
split(key) {
|
|
540
|
+
};
|
|
541
|
+
Tree.prototype.split = function (key) {
|
|
478
542
|
return split(key, this._root, this._comparator);
|
|
479
|
-
}
|
|
480
|
-
|
|
543
|
+
};
|
|
544
|
+
Tree.prototype[Symbol.iterator] = function () {
|
|
545
|
+
var n;
|
|
546
|
+
return __generator(this, function (_a) {
|
|
547
|
+
switch (_a.label) {
|
|
548
|
+
case 0:
|
|
549
|
+
n = this.minNode();
|
|
550
|
+
_a.label = 1;
|
|
551
|
+
case 1:
|
|
552
|
+
if (!n) return [3 /*break*/, 3];
|
|
553
|
+
return [4 /*yield*/, n];
|
|
554
|
+
case 2:
|
|
555
|
+
_a.sent();
|
|
556
|
+
n = this.next(n);
|
|
557
|
+
return [3 /*break*/, 1];
|
|
558
|
+
case 3: return [2 /*return*/];
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
};
|
|
562
|
+
return Tree;
|
|
563
|
+
}());
|
|
481
564
|
function loadRecursive(keys, values, start, end) {
|
|
482
|
-
|
|
565
|
+
var size = end - start;
|
|
483
566
|
if (size > 0) {
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
567
|
+
var middle = start + Math.floor(size / 2);
|
|
568
|
+
var key = keys[middle];
|
|
569
|
+
var data = values[middle];
|
|
570
|
+
var node = new Node(key, data);
|
|
488
571
|
node.left = loadRecursive(keys, values, start, middle);
|
|
489
572
|
node.right = loadRecursive(keys, values, middle + 1, end);
|
|
490
573
|
return node;
|
|
@@ -492,20 +575,20 @@ function loadRecursive(keys, values, start, end) {
|
|
|
492
575
|
return null;
|
|
493
576
|
}
|
|
494
577
|
function createList(keys, values) {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
for (
|
|
578
|
+
var head = new Node(null, null);
|
|
579
|
+
var p = head;
|
|
580
|
+
for (var i = 0; i < keys.length; i++) {
|
|
498
581
|
p = p.next = new Node(keys[i], values[i]);
|
|
499
582
|
}
|
|
500
583
|
p.next = null;
|
|
501
584
|
return head.next;
|
|
502
585
|
}
|
|
503
586
|
function toList(root) {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
587
|
+
var current = root;
|
|
588
|
+
var Q = [];
|
|
589
|
+
var done = false;
|
|
590
|
+
var head = new Node(null, null);
|
|
591
|
+
var p = head;
|
|
509
592
|
while (!done) {
|
|
510
593
|
if (current) {
|
|
511
594
|
Q.push(current);
|
|
@@ -524,11 +607,11 @@ function toList(root) {
|
|
|
524
607
|
return head.next;
|
|
525
608
|
}
|
|
526
609
|
function sortedListToBST(list, start, end) {
|
|
527
|
-
|
|
610
|
+
var size = end - start;
|
|
528
611
|
if (size > 0) {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
612
|
+
var middle = start + Math.floor(size / 2);
|
|
613
|
+
var left = sortedListToBST(list, start, middle);
|
|
614
|
+
var root = list.head;
|
|
532
615
|
root.left = left;
|
|
533
616
|
list.head = list.head.next;
|
|
534
617
|
root.right = sortedListToBST(list, middle + 1, end);
|
|
@@ -537,10 +620,10 @@ function sortedListToBST(list, start, end) {
|
|
|
537
620
|
return null;
|
|
538
621
|
}
|
|
539
622
|
function mergeLists(l1, l2, compare) {
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
623
|
+
var head = new Node(null, null); // dummy
|
|
624
|
+
var p = head;
|
|
625
|
+
var p1 = l1;
|
|
626
|
+
var p2 = l2;
|
|
544
627
|
while (p1 !== null && p2 !== null) {
|
|
545
628
|
if (compare(p1.key, p2.key) < 0) {
|
|
546
629
|
p.next = p1;
|
|
@@ -563,9 +646,9 @@ function mergeLists(l1, l2, compare) {
|
|
|
563
646
|
function sort(keys, values, left, right, compare) {
|
|
564
647
|
if (left >= right)
|
|
565
648
|
return;
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
649
|
+
var pivot = keys[(left + right) >> 1];
|
|
650
|
+
var i = left - 1;
|
|
651
|
+
var j = right + 1;
|
|
569
652
|
while (true) {
|
|
570
653
|
do
|
|
571
654
|
i++;
|
|
@@ -575,7 +658,7 @@ function sort(keys, values, left, right, compare) {
|
|
|
575
658
|
while (compare(keys[j], pivot) > 0);
|
|
576
659
|
if (i >= j)
|
|
577
660
|
break;
|
|
578
|
-
|
|
661
|
+
var tmp = keys[i];
|
|
579
662
|
keys[i] = keys[j];
|
|
580
663
|
keys[j] = tmp;
|
|
581
664
|
tmp = values[i];
|
package/dist/splay.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"splay.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"splay.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|