splaytree 3.1.2 → 3.2.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/dist/splay.js DELETED
@@ -1,694 +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
- (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());
14
- }(this, (function () { 'use strict';
15
-
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
- }
58
-
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
- }());
69
-
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 current, Q, done;
552
- return __generator(this, function (_a) {
553
- switch (_a.label) {
554
- case 0:
555
- current = this._root;
556
- Q = [];
557
- done = false;
558
- _a.label = 1;
559
- case 1:
560
- if (!!done) return [3 /*break*/, 6];
561
- if (!(current !== null)) return [3 /*break*/, 2];
562
- Q.push(current);
563
- current = current.left;
564
- return [3 /*break*/, 5];
565
- case 2:
566
- if (!(Q.length !== 0)) return [3 /*break*/, 4];
567
- current = Q.pop();
568
- return [4 /*yield*/, current];
569
- case 3:
570
- _a.sent();
571
- current = current.right;
572
- return [3 /*break*/, 5];
573
- case 4:
574
- done = true;
575
- _a.label = 5;
576
- case 5: return [3 /*break*/, 1];
577
- case 6: return [2 /*return*/];
578
- }
579
- });
580
- };
581
- return Tree;
582
- }());
583
- function loadRecursive(keys, values, start, end) {
584
- var size = end - start;
585
- if (size > 0) {
586
- var middle = start + Math.floor(size / 2);
587
- var key = keys[middle];
588
- var data = values[middle];
589
- var node = new Node(key, data);
590
- node.left = loadRecursive(keys, values, start, middle);
591
- node.right = loadRecursive(keys, values, middle + 1, end);
592
- return node;
593
- }
594
- return null;
595
- }
596
- function createList(keys, values) {
597
- var head = new Node(null, null);
598
- var p = head;
599
- for (var i = 0; i < keys.length; i++) {
600
- p = p.next = new Node(keys[i], values[i]);
601
- }
602
- p.next = null;
603
- return head.next;
604
- }
605
- function toList(root) {
606
- var current = root;
607
- var Q = [];
608
- var done = false;
609
- var head = new Node(null, null);
610
- var p = head;
611
- while (!done) {
612
- if (current) {
613
- Q.push(current);
614
- current = current.left;
615
- }
616
- else {
617
- if (Q.length > 0) {
618
- current = p = p.next = Q.pop();
619
- current = current.right;
620
- }
621
- else
622
- done = true;
623
- }
624
- }
625
- p.next = null; // that'll work even if the tree was empty
626
- return head.next;
627
- }
628
- function sortedListToBST(list, start, end) {
629
- var size = end - start;
630
- if (size > 0) {
631
- var middle = start + Math.floor(size / 2);
632
- var left = sortedListToBST(list, start, middle);
633
- var root = list.head;
634
- root.left = left;
635
- list.head = list.head.next;
636
- root.right = sortedListToBST(list, middle + 1, end);
637
- return root;
638
- }
639
- return null;
640
- }
641
- function mergeLists(l1, l2, compare) {
642
- var head = new Node(null, null); // dummy
643
- var p = head;
644
- var p1 = l1;
645
- var p2 = l2;
646
- while (p1 !== null && p2 !== null) {
647
- if (compare(p1.key, p2.key) < 0) {
648
- p.next = p1;
649
- p1 = p1.next;
650
- }
651
- else {
652
- p.next = p2;
653
- p2 = p2.next;
654
- }
655
- p = p.next;
656
- }
657
- if (p1 !== null) {
658
- p.next = p1;
659
- }
660
- else if (p2 !== null) {
661
- p.next = p2;
662
- }
663
- return head.next;
664
- }
665
- function sort(keys, values, left, right, compare) {
666
- if (left >= right)
667
- return;
668
- var pivot = keys[(left + right) >> 1];
669
- var i = left - 1;
670
- var j = right + 1;
671
- while (true) {
672
- do
673
- i++;
674
- while (compare(keys[i], pivot) < 0);
675
- do
676
- j--;
677
- while (compare(keys[j], pivot) > 0);
678
- if (i >= j)
679
- break;
680
- var tmp = keys[i];
681
- keys[i] = keys[j];
682
- keys[j] = tmp;
683
- tmp = values[i];
684
- values[i] = values[j];
685
- values[j] = tmp;
686
- }
687
- sort(keys, values, left, j, compare);
688
- sort(keys, values, j + 1, right, compare);
689
- }
690
-
691
- return Tree;
692
-
693
- })));
694
- //# sourceMappingURL=splay.js.map
package/dist/splay.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"splay.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}