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