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