splaytree 3.1.1 → 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/Readme.md CHANGED
@@ -1,25 +1,24 @@
1
- # Fast splay tree [![npm version](https://badge.fury.io/js/splaytree.svg)](https://badge.fury.io/js/splaytree) [![build](https://travis-ci.org/w8r/splay-tree.svg?branch=master)](https://travis-ci.org/w8r/splay-tree) ![deps](https://david-dm.org/w8r/splay-tree/status.svg) [![codecov](https://codecov.io/gh/w8r/splay-tree/branch/master/graph/badge.svg)](https://codecov.io/gh/w8r/splay-tree)
1
+ # Fast splay tree [![npm version](https://badge.fury.io/js/splaytree.svg)](https://badge.fury.io/js/splaytree) [![codecov](https://codecov.io/gh/w8r/splay-tree/branch/master/graph/badge.svg)](https://codecov.io/gh/w8r/splay-tree)
2
2
 
3
3
  [Splay-tree](https://en.wikipedia.org/wiki/Splay_tree): **[fast](#benchmarks)**(non-recursive) and **simple**(< 1000 lines of code)
4
4
  Implementation is adapted directly from Wikipedia with the same API as [w8r/avl](https://github.com/w8r/avl), to run the benchmarks against other trees.
5
5
 
6
-
7
6
  This tree is based on **top-down** splaying algorithm by D.Sleator. It supports
8
- - splitting, merging
9
- - updating of the keys
10
- - bulk loading of the items into an empty or non-empty tree
11
- - insertion with duplicates or no duplicates
12
- - lookup without splaying
13
7
 
14
- ![Splay-tree](https://i.stack.imgur.com/CNSAZ.png)
8
+ - splitting, merging
9
+ - updating of the keys
10
+ - bulk loading of the items into an empty or non-empty tree
11
+ - insertion with duplicates or no duplicates
12
+ - lookup without splaying
15
13
 
16
- | Operation | Average | Worst case |
17
- | ------------- | ------------- | ---------------------- |
18
- | Space | **O(n)** | **O(n)** |
19
- | Search | **O(log n)** | **amortized O(log n)** |
20
- | Insert | **O(log n)** | **amortized O(log n)** |
21
- | Delete | **O(log n)** | **amortized O(log n)** |
14
+ ![Splay-tree](https://i.stack.imgur.com/CNSAZ.png)
22
15
 
16
+ | Operation | Average | Worst case |
17
+ | --------- | ------------ | ---------------------- |
18
+ | Space | **O(n)** | **O(n)** |
19
+ | Search | **O(log n)** | **amortized O(log n)** |
20
+ | Insert | **O(log n)** | **amortized O(log n)** |
21
+ | Delete | **O(log n)** | **amortized O(log n)** |
23
22
 
24
23
  ## Install
25
24
 
@@ -28,11 +27,12 @@ npm i -S splaytree
28
27
  ```
29
28
 
30
29
  ```js
31
- import SplayTree from 'splaytree';
30
+ import SplayTree from "splaytree";
32
31
  const tree = new SplayTree();
33
32
  ```
34
33
 
35
34
  Or get it from CDN
35
+
36
36
  ```html
37
37
  <script src="https://unpkg.com/splaytree"></script>
38
38
  <script>
@@ -40,56 +40,58 @@ Or get it from CDN
40
40
  ...
41
41
  </script>
42
42
  ```
43
+
43
44
  Or use the compiled version 'dist/splay.js'.
44
45
 
45
46
  [Try it in your browser](https://npm.runkit.com/splaytree)
46
47
 
47
48
  ## API
48
49
 
49
- * `new SplayTree([comparator])`, where `comparator` is optional comparison function
50
- * `tree.insert(key:any, [data:any]):Node` - Insert item, allow duplicate keys
51
- * `tree.add(key:any, [data:any]):Node` - Insert item if it is not present
52
- * `tree.remove(key:any)` - Remove item
53
- * `tree.find(key):Node|Null` - Return node by its key
54
- * `tree.findStatic(key):Node|Null` - Return node by its key (doesn't re-balance the tree)
55
- * `tree.at(index:Number):Node|Null` - Return node by its index in sorted order of keys
56
- * `tree.contains(key):Boolean` - Whether a node with the given key is in the tree
57
- * `tree.forEach(function(node) {...}):Tree` In-order traversal
58
- * `tree.keys():Array<key>` - Returns the array of keys in order
59
- * `tree.values():Array<*>` - Returns the array of data fields in order
60
- * `tree.range(lo, high, function(node) {} [, context]):Tree` - Walks the range of keys in order. Stops, if the visitor function returns a non-zero value.
61
- * `tree.pop():Node` - Removes smallest node
62
- * `tree.min():key` - Returns min key
63
- * `tree.max():key` - Returns max key
64
- * `tree.minNode():Node` - Returns the node with smallest key
65
- * `tree.maxNode():Node` - Returns the node with highest key
66
- * `tree.prev(node):Node` - Predecessor node
67
- * `tree.next(node):Node` - Successor node
68
- * `tree.load(keys:Array<*>, [values:Array<*>][,presort=false]):Tree` - Bulk-load items. It expects values and keys to be sorted, but if `presort` is `true`, it will sort keys and values using the comparator(in-place, your arrays are going to be altered).
50
+ - `new SplayTree([comparator])`, where `comparator` is optional comparison function
51
+ - `tree.insert(key:any, [data:any]):Node` - Insert item, allow duplicate keys
52
+ - `tree.add(key:any, [data:any]):Node` - Insert item if it is not present
53
+ - `tree.remove(key:any)` - Remove item
54
+ - `tree.find(key):Node|Null` - Return node by its key
55
+ - `tree.findStatic(key):Node|Null` - Return node by its key (doesn't re-balance the tree)
56
+ - `tree.at(index:Number):Node|Null` - Return node by its index in sorted order of keys
57
+ - `tree.contains(key):Boolean` - Whether a node with the given key is in the tree
58
+ - `tree.forEach(function(node) {...}):Tree` In-order traversal
59
+ - `tree.keys():Array<key>` - Returns the array of keys in order
60
+ - `tree.values():Array<*>` - Returns the array of data fields in order
61
+ - `tree.range(lo, high, function(node) {} [, context]):Tree` - Walks the range of keys in order. Stops, if the visitor function returns a non-zero value.
62
+ - `tree.pop():Node` - Removes smallest node
63
+ - `tree.min():key` - Returns min key
64
+ - `tree.max():key` - Returns max key
65
+ - `tree.minNode():Node` - Returns the node with smallest key
66
+ - `tree.maxNode():Node` - Returns the node with highest key
67
+ - `tree.prev(node):Node` - Predecessor node
68
+ - `tree.next(node):Node` - Successor node
69
+ - `tree.load(keys:Array<*>, [values:Array<*>][,presort=false]):Tree` - Bulk-load items. It expects values and keys to be sorted, but if `presort` is `true`, it will sort keys and values using the comparator(in-place, your arrays are going to be altered).
69
70
 
70
71
  **Comparator**
71
72
 
72
73
  `function(a:key,b:key):Number` - Comparator function between two keys, it returns
73
- * `0` if the keys are equal
74
- * `<0` if `a < b`
75
- * `>0` if `a > b`
76
74
 
77
- The comparator function is extremely important, in case of errors you might end
78
- up with a wrongly constructed tree or would not be able to retrieve your items.
79
- It is crucial to test the return values of your `comparator(a,b)` and `comparator(b,a)`
80
- to make sure it's working correctly, otherwise you may have bugs that are very
81
- unpredictable and hard to catch.
75
+ - `0` if the keys are equal
76
+ - `<0` if `a < b`
77
+ - `>0` if `a > b`
78
+
79
+ The comparator function is extremely important, in case of errors you might end
80
+ up with a wrongly constructed tree or would not be able to retrieve your items.
81
+ It is crucial to test the return values of your `comparator(a,b)` and `comparator(b,a)`
82
+ to make sure it's working correctly, otherwise you may have bugs that are very
83
+ unpredictable and hard to catch.
82
84
 
83
- **Duplicate keys**
85
+ **Duplicate keys**
84
86
 
85
- * `insert()` method allows duplicate keys. This can be useful in certain applications (example: overlapping
86
- points in 2D).
87
- * `add()` method will not allow duplicate keys - if key is already present in the tree, no new node is created
87
+ - `insert()` method allows duplicate keys. This can be useful in certain applications (example: overlapping
88
+ points in 2D).
89
+ - `add()` method will not allow duplicate keys - if key is already present in the tree, no new node is created
88
90
 
89
91
  ## Example
90
92
 
91
93
  ```js
92
- import Tree from 'splaytree';
94
+ import Tree from "splaytree";
93
95
 
94
96
  const t = new Tree();
95
97
  t.insert(5);
@@ -99,18 +101,18 @@ t.insert(33);
99
101
  t.insert(2);
100
102
 
101
103
  console.log(t.keys()); // [-10, 0, 2, 5, 33]
102
- console.log(t.size); // 5
103
- console.log(t.min()); // -10
104
- console.log(t.max()); // -33
104
+ console.log(t.size); // 5
105
+ console.log(t.min()); // -10
106
+ console.log(t.max()); // -33
105
107
 
106
108
  t.remove(0);
107
- console.log(t.size); // 4
109
+ console.log(t.size); // 4
108
110
  ```
109
111
 
110
112
  **Custom comparator (reverse sort)**
111
113
 
112
114
  ```js
113
- import Tree from 'splaytree';
115
+ import Tree from "splaytree";
114
116
 
115
117
  const t = new Tree((a, b) => b - a);
116
118
  t.insert(5);
@@ -125,11 +127,11 @@ console.log(t.keys()); // [33, 5, 2, 0, -10]
125
127
  **Bulk insert**
126
128
 
127
129
  ```js
128
- import Tree from 'splaytree';
130
+ import Tree from "splaytree";
129
131
 
130
132
  const t = new Tree();
131
- t.load([3,2,-10,20], ['C', 'B', 'A', 'D']);
132
- console.log(t.keys()); // [-10, 2, 3, 20]
133
+ t.load([3, 2, -10, 20], ["C", "B", "A", "D"]);
134
+ console.log(t.keys()); // [-10, 2, 3, 20]
133
135
  console.log(t.values()); // ['A', 'B', 'C', 'D']
134
136
  ```
135
137
 
@@ -196,7 +198,6 @@ npm run build
196
198
 
197
199
  - [ ] try and add parent fields for efficient `.prev()` and `.next()`, or iterators
198
200
 
199
-
200
201
  ## License
201
202
 
202
203
  The MIT License (MIT)
@@ -0,0 +1,344 @@
1
+ class f {
2
+ constructor(t, e) {
3
+ this.next = null, this.key = t, this.data = e, this.left = null, this.right = null;
4
+ }
5
+ }
6
+ function d(n, t) {
7
+ return n > t ? 1 : n < t ? -1 : 0;
8
+ }
9
+ function u(n, t, e) {
10
+ const r = new f(null, null);
11
+ let l = r, i = r;
12
+ for (; ; ) {
13
+ const o = e(n, t.key);
14
+ if (o < 0) {
15
+ if (t.left === null) break;
16
+ if (e(n, t.left.key) < 0) {
17
+ const s = t.left;
18
+ if (t.left = s.right, s.right = t, t = s, t.left === null) break;
19
+ }
20
+ i.left = t, i = t, t = t.left;
21
+ } else if (o > 0) {
22
+ if (t.right === null) break;
23
+ if (e(n, t.right.key) > 0) {
24
+ const s = t.right;
25
+ if (t.right = s.left, s.left = t, t = s, t.right === null) break;
26
+ }
27
+ l.right = t, l = t, t = t.right;
28
+ } else break;
29
+ }
30
+ return l.right = t.left, i.left = t.right, t.left = r.right, t.right = r.left, t;
31
+ }
32
+ function c(n, t, e, r) {
33
+ const l = new f(n, t);
34
+ if (e === null)
35
+ return l.left = l.right = null, l;
36
+ e = u(n, e, r);
37
+ const i = r(n, e.key);
38
+ return i < 0 ? (l.left = e.left, l.right = e, e.left = null) : i >= 0 && (l.right = e.right, l.left = e, e.right = null), l;
39
+ }
40
+ function m(n, t, e) {
41
+ let r = null, l = null;
42
+ if (t) {
43
+ t = u(n, t, e);
44
+ const i = e(t.key, n);
45
+ i === 0 ? (r = t.left, l = t.right) : i < 0 ? (l = t.right, t.right = null, r = t) : (r = t.left, t.left = null, l = t);
46
+ }
47
+ return { left: r, right: l };
48
+ }
49
+ function w(n, t, e) {
50
+ return t === null ? n : (n === null || (t = u(n.key, t, e), t.left = n), t);
51
+ }
52
+ function _(n, t, e, r, l) {
53
+ if (n) {
54
+ r(`${t}${e ? "└── " : "├── "}${l(n)}
55
+ `);
56
+ const i = t + (e ? " " : "│ ");
57
+ n.left && _(n.left, i, !1, r, l), n.right && _(n.right, i, !0, r, l);
58
+ }
59
+ }
60
+ class z {
61
+ constructor(t = d) {
62
+ this._root = null, this._size = 0, this._comparator = t;
63
+ }
64
+ /**
65
+ * Inserts a key, allows duplicates
66
+ */
67
+ insert(t, e) {
68
+ return this._size++, this._root = c(t, e, this._root, this._comparator);
69
+ }
70
+ /**
71
+ * Adds a key, if it is not present in the tree
72
+ */
73
+ add(t, e) {
74
+ const r = new f(t, e);
75
+ this._root === null && (r.left = r.right = null, this._size++, this._root = r);
76
+ const l = this._comparator, i = u(t, this._root, l), o = l(t, i.key);
77
+ return o === 0 ? this._root = i : (o < 0 ? (r.left = i.left, r.right = i, i.left = null) : o > 0 && (r.right = i.right, r.left = i, i.right = null), this._size++, this._root = r), this._root;
78
+ }
79
+ /**
80
+ * @param {Key} key
81
+ * @return {Node|null}
82
+ */
83
+ remove(t) {
84
+ this._root = this._remove(t, this._root, this._comparator);
85
+ }
86
+ /**
87
+ * Deletes i from the tree if it's there
88
+ */
89
+ _remove(t, e, r) {
90
+ let l;
91
+ return e === null ? null : (e = u(t, e, r), r(t, e.key) === 0 ? (e.left === null ? l = e.right : (l = u(t, e.left, r), l.right = e.right), this._size--, l) : e);
92
+ }
93
+ /**
94
+ * Removes and returns the node with smallest key
95
+ */
96
+ pop() {
97
+ let t = this._root;
98
+ if (t) {
99
+ for (; t.left; ) t = t.left;
100
+ return this._root = u(t.key, this._root, this._comparator), this._root = this._remove(t.key, this._root, this._comparator), { key: t.key, data: t.data };
101
+ }
102
+ return null;
103
+ }
104
+ /**
105
+ * Find without splaying
106
+ */
107
+ findStatic(t) {
108
+ let e = this._root;
109
+ const r = this._comparator;
110
+ for (; e; ) {
111
+ const l = r(t, e.key);
112
+ if (l === 0) return e;
113
+ l < 0 ? e = e.left : e = e.right;
114
+ }
115
+ return null;
116
+ }
117
+ find(t) {
118
+ return this._root && (this._root = u(t, this._root, this._comparator), this._comparator(t, this._root.key) !== 0) ? null : this._root;
119
+ }
120
+ contains(t) {
121
+ let e = this._root;
122
+ const r = this._comparator;
123
+ for (; e; ) {
124
+ const l = r(t, e.key);
125
+ if (l === 0) return !0;
126
+ l < 0 ? e = e.left : e = e.right;
127
+ }
128
+ return !1;
129
+ }
130
+ forEach(t, e) {
131
+ let r = this._root;
132
+ const l = [];
133
+ let i = !1;
134
+ for (; !i; )
135
+ r !== null ? (l.push(r), r = r.left) : l.length !== 0 ? (r = l.pop(), t.call(e, r), r = r.right) : i = !0;
136
+ return this;
137
+ }
138
+ /**
139
+ * Walk key range from `low` to `high`. Stops if `fn` returns a value.
140
+ */
141
+ range(t, e, r, l) {
142
+ const i = [], o = this._comparator;
143
+ let s = this._root, h;
144
+ for (; i.length !== 0 || s; )
145
+ if (s)
146
+ i.push(s), s = s.left;
147
+ else {
148
+ if (s = i.pop(), h = o(s.key, e), h > 0)
149
+ break;
150
+ if (o(s.key, t) >= 0 && r.call(l, s))
151
+ return this;
152
+ s = s.right;
153
+ }
154
+ return this;
155
+ }
156
+ /**
157
+ * Returns array of keys
158
+ */
159
+ keys() {
160
+ const t = [];
161
+ return this.forEach(({ key: e }) => {
162
+ t.push(e);
163
+ }), t;
164
+ }
165
+ /**
166
+ * Returns array of all the data in the nodes
167
+ */
168
+ values() {
169
+ const t = [];
170
+ return this.forEach(({ data: e }) => {
171
+ t.push(e);
172
+ }), t;
173
+ }
174
+ min() {
175
+ return this._root ? this.minNode(this._root).key : null;
176
+ }
177
+ max() {
178
+ return this._root ? this.maxNode(this._root).key : null;
179
+ }
180
+ minNode(t = this._root) {
181
+ if (t) for (; t.left; ) t = t.left;
182
+ return t;
183
+ }
184
+ maxNode(t = this._root) {
185
+ if (t) for (; t.right; ) t = t.right;
186
+ return t;
187
+ }
188
+ /**
189
+ * Returns node at given index
190
+ */
191
+ at(t) {
192
+ let e = this._root, r = !1, l = 0;
193
+ const i = [];
194
+ for (; !r; )
195
+ if (e)
196
+ i.push(e), e = e.left;
197
+ else if (i.length > 0) {
198
+ if (e = i.pop(), l === t) return e;
199
+ l++, e = e.right;
200
+ } else r = !0;
201
+ return null;
202
+ }
203
+ next(t) {
204
+ let e = this._root, r = null;
205
+ if (t.right) {
206
+ for (r = t.right; r.left; ) r = r.left;
207
+ return r;
208
+ }
209
+ const l = this._comparator;
210
+ for (; e; ) {
211
+ const i = l(t.key, e.key);
212
+ if (i === 0) break;
213
+ i < 0 ? (r = e, e = e.left) : e = e.right;
214
+ }
215
+ return r;
216
+ }
217
+ prev(t) {
218
+ let e = this._root, r = null;
219
+ if (t.left !== null) {
220
+ for (r = t.left; r.right; ) r = r.right;
221
+ return r;
222
+ }
223
+ const l = this._comparator;
224
+ for (; e; ) {
225
+ const i = l(t.key, e.key);
226
+ if (i === 0) break;
227
+ i < 0 ? e = e.left : (r = e, e = e.right);
228
+ }
229
+ return r;
230
+ }
231
+ clear() {
232
+ return this._root = null, this._size = 0, this;
233
+ }
234
+ toList() {
235
+ return k(this._root);
236
+ }
237
+ /**
238
+ * Bulk-load items. Both array have to be same size
239
+ */
240
+ load(t, e = [], r = !1) {
241
+ let l = t.length;
242
+ const i = this._comparator;
243
+ if (r && g(t, e, 0, l - 1, i), this._root === null)
244
+ this._root = a(t, e, 0, l), this._size = l;
245
+ else {
246
+ const o = y(
247
+ this.toList(),
248
+ x(t, e),
249
+ i
250
+ );
251
+ l = this._size + l, this._root = p({ head: o }, 0, l);
252
+ }
253
+ return this;
254
+ }
255
+ isEmpty() {
256
+ return this._root === null;
257
+ }
258
+ get size() {
259
+ return this._size;
260
+ }
261
+ get root() {
262
+ return this._root;
263
+ }
264
+ toString(t = (e) => String(e.key)) {
265
+ const e = [];
266
+ return _(this._root, "", !0, (r) => e.push(r), t), e.join("");
267
+ }
268
+ update(t, e, r) {
269
+ const l = this._comparator;
270
+ let { left: i, right: o } = m(t, this._root, l);
271
+ l(t, e) < 0 ? o = c(e, r, o, l) : i = c(e, r, i, l), this._root = w(i, o, l);
272
+ }
273
+ split(t) {
274
+ return m(t, this._root, this._comparator);
275
+ }
276
+ *[Symbol.iterator]() {
277
+ let t = this._root;
278
+ const e = [];
279
+ let r = !1;
280
+ for (; !r; )
281
+ t !== null ? (e.push(t), t = t.left) : e.length !== 0 ? (t = e.pop(), yield t, t = t.right) : r = !0;
282
+ }
283
+ }
284
+ function a(n, t, e, r) {
285
+ const l = r - e;
286
+ if (l > 0) {
287
+ const i = e + Math.floor(l / 2), o = n[i], s = t[i], h = new f(o, s);
288
+ return h.left = a(n, t, e, i), h.right = a(n, t, i + 1, r), h;
289
+ }
290
+ return null;
291
+ }
292
+ function x(n, t) {
293
+ const e = new f(null, null);
294
+ let r = e;
295
+ for (let l = 0; l < n.length; l++)
296
+ r = r.next = new f(n[l], t[l]);
297
+ return r.next = null, e.next;
298
+ }
299
+ function k(n) {
300
+ let t = n;
301
+ const e = [];
302
+ let r = !1;
303
+ const l = new f(null, null);
304
+ let i = l;
305
+ for (; !r; )
306
+ t ? (e.push(t), t = t.left) : e.length > 0 ? (t = i = i.next = e.pop(), t = t.right) : r = !0;
307
+ return i.next = null, l.next;
308
+ }
309
+ function p(n, t, e) {
310
+ const r = e - t;
311
+ if (r > 0) {
312
+ const l = t + Math.floor(r / 2), i = p(n, t, l), o = n.head;
313
+ return o.left = i, n.head = n.head.next, o.right = p(n, l + 1, e), o;
314
+ }
315
+ return null;
316
+ }
317
+ function y(n, t, e) {
318
+ const r = new f(null, null);
319
+ let l = r, i = n, o = t;
320
+ for (; i !== null && o !== null; )
321
+ e(i.key, o.key) < 0 ? (l.next = i, i = i.next) : (l.next = o, o = o.next), l = l.next;
322
+ return i !== null ? l.next = i : o !== null && (l.next = o), r.next;
323
+ }
324
+ function g(n, t, e, r, l) {
325
+ if (e >= r) return;
326
+ const i = n[e + r >> 1];
327
+ let o = e - 1, s = r + 1;
328
+ for (; ; ) {
329
+ do
330
+ o++;
331
+ while (l(n[o], i) < 0);
332
+ do
333
+ s--;
334
+ while (l(n[s], i) > 0);
335
+ if (o >= s) break;
336
+ let h = n[o];
337
+ n[o] = n[s], n[s] = h, h = t[o], t[o] = t[s], t[s] = h;
338
+ }
339
+ g(n, t, e, s, l), g(n, t, s + 1, r, l);
340
+ }
341
+ export {
342
+ z as default
343
+ };
344
+ //# sourceMappingURL=splaytree.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"splaytree.js","sources":["../src/node.ts","../src/index.ts"],"sourcesContent":["\nexport default class Node<Key, Value> {\n public key:Key;\n public data:any;\n public left:Node<Key, Value>|null;\n public right:Node<Key, Value>|null;\n public next:Node<Key, Value>|null = null;\n\n constructor (key:Key, data?:any) {\n this.key = key;\n this.data = data;\n this.left = null;\n this.right = null;\n }\n}","import Node from \"./node\";\nimport { Key, Value } from \"./types\";\n\nexport type Comparator<Key> = (a: Key, b: Key) => number;\nexport type Visitor<Key, Value> = (\n node: Node<Key, Value>\n) => undefined | boolean | void;\nexport type NodePrinter<Key, Value> = (node: Node<Key, Value>) => string;\n\n/* follows \"An implementation of top-down splaying\"\n * by D. Sleator <sleator@cs.cmu.edu> March 1992\n */\n\nfunction DEFAULT_COMPARE(a: Key, b: Key): number {\n return a > b ? 1 : a < b ? -1 : 0;\n}\n\ntype TreeNodeList<Key, Value> = { head: Node<Key, Value> | null };\n\n/**\n * Simple top down splay, not requiring i to be in the tree t.\n */\nfunction splay(\n i: Key,\n t: Node<Key, Value>,\n comparator: Comparator<Key>\n): Node<Key, Value> {\n const N = new Node(null, null);\n let l = N;\n let r = N;\n\n while (true) {\n const cmp = comparator(i, t!.key);\n //if (i < t.key) {\n if (cmp < 0) {\n if (t!.left === null) break;\n //if (i < t.left.key) {\n if (comparator(i, t.left.key) < 0) {\n const y = t.left; /* rotate right */\n t.left = y.right;\n y.right = t;\n t = y;\n if (t.left === null) break;\n }\n r.left = t; /* link right */\n r = t;\n t = t.left;\n //} else if (i > t.key) {\n } else if (cmp > 0) {\n if (t.right === null) break;\n //if (i > t.right.key) {\n if (comparator(i, t.right.key) > 0) {\n const y = t.right; /* rotate left */\n t.right = y.left;\n y.left = t;\n t = y;\n if (t.right === null) break;\n }\n l.right = t; /* link left */\n l = t;\n t = t.right;\n } else break;\n }\n /* assemble */\n l.right = t.left;\n r.left = t.right;\n t.left = N.right;\n t.right = N.left;\n return t;\n}\n\nfunction insert(\n i: Key,\n data: Value,\n t: Node<Key, Value>,\n comparator: Comparator<Key>\n): Node<Key, Value> {\n const node = new Node(i, data);\n\n if (t === null) {\n node.left = node.right = null;\n return node;\n }\n\n t = splay(i, t, comparator);\n const cmp = comparator(i, t.key);\n if (cmp < 0) {\n node.left = t.left;\n node.right = t;\n t.left = null;\n } else if (cmp >= 0) {\n node.right = t.right;\n node.left = t;\n t.right = null;\n }\n return node;\n}\n\nfunction split(\n key: Key,\n v: Node<Key, Value>,\n comparator: Comparator<Key>\n): {\n left: Node<Key, Value> | null;\n right: Node<Key, Value> | null;\n} {\n let left = null;\n let right = null;\n if (v) {\n v = splay(key, v, comparator);\n\n const cmp = comparator(v.key, key);\n if (cmp === 0) {\n left = v.left;\n right = v.right;\n } else if (cmp < 0) {\n right = v.right;\n v.right = null;\n left = v;\n } else {\n left = v.left;\n v.left = null;\n right = v;\n }\n }\n return { left, right };\n}\n\nfunction merge(\n left: Node<Key, Value> | null,\n right: Node<Key, Value> | null,\n comparator: Comparator<Key>\n) {\n if (right === null) return left;\n if (left === null) return right;\n\n right = splay(left.key, right, comparator);\n right.left = left;\n return right;\n}\n\ntype StringCollector = (s: string) => void;\n\n/**\n * Prints level of the tree\n */\nfunction printRow(\n root: Node<Key, Value>,\n prefix: string,\n isTail: boolean,\n out: StringCollector,\n printNode: NodePrinter<Key, Value>\n) {\n if (root) {\n out(`${prefix}${isTail ? \"└── \" : \"├── \"}${printNode(root)}\\n`);\n const indent = prefix + (isTail ? \" \" : \"│ \");\n if (root.left) printRow(root.left, indent, false, out, printNode);\n if (root.right) printRow(root.right, indent, true, out, printNode);\n }\n}\n\nexport default class Tree<Key = number, Value = any> {\n private _comparator: Comparator<Key>;\n private _root: Node<Key, Value> | null = null;\n private _size: number = 0;\n\n constructor(comparator = DEFAULT_COMPARE) {\n this._comparator = comparator;\n }\n\n /**\n * Inserts a key, allows duplicates\n */\n public insert(key: Key, data?: Value): Node<Key, Value> {\n this._size++;\n return (this._root = insert(key, data, this._root!, this._comparator));\n }\n\n /**\n * Adds a key, if it is not present in the tree\n */\n public add(key: Key, data?: Value): Node<Key, Value> {\n const node = new Node(key, data);\n\n if (this._root === null) {\n node.left = node.right = null;\n this._size++;\n this._root = node;\n }\n\n const comparator = this._comparator;\n const t = splay(key, this._root, comparator);\n const cmp = comparator(key, t.key);\n if (cmp === 0) this._root = t;\n else {\n if (cmp < 0) {\n node.left = t.left;\n node.right = t;\n t.left = null;\n } else if (cmp > 0) {\n node.right = t.right;\n node.left = t;\n t.right = null;\n }\n this._size++;\n this._root = node;\n }\n\n return this._root;\n }\n\n /**\n * @param {Key} key\n * @return {Node|null}\n */\n public remove(key: Key): void {\n this._root = this._remove(key, this._root!, this._comparator);\n }\n\n /**\n * Deletes i from the tree if it's there\n */\n private _remove(i: Key, t: Node<Key, Value>, comparator: Comparator<Key>) {\n let x;\n if (t === null) return null;\n t = splay(i, t, comparator);\n const cmp = comparator(i, t.key);\n if (cmp === 0) {\n /* found it */\n if (t.left === null) {\n x = t.right!;\n } else {\n x = splay(i, t.left, comparator);\n x.right = t.right;\n }\n this._size--;\n return x;\n }\n return t; /* It wasn't there */\n }\n\n /**\n * Removes and returns the node with smallest key\n */\n public pop(): { key: Key; data: Value } | null {\n let node = this._root;\n if (node) {\n while (node.left) node = node.left;\n this._root = splay(node.key, this._root!, this._comparator);\n this._root = this._remove(node.key, this._root, this._comparator);\n return { key: node.key, data: node.data };\n }\n return null;\n }\n\n /**\n * Find without splaying\n */\n public findStatic(key: Key): Node<Key, Value> | null {\n let current = this._root;\n const compare = this._comparator;\n while (current) {\n const cmp = compare(key, current.key);\n if (cmp === 0) return current;\n else if (cmp < 0) current = current.left;\n else current = current.right;\n }\n return null;\n }\n\n public find(key: Key): Node<Key, Value> | null {\n if (this._root) {\n this._root = splay(key, this._root, this._comparator);\n if (this._comparator(key, this._root.key) !== 0) return null;\n }\n return this._root;\n }\n\n public contains(key: Key): boolean {\n let current = this._root;\n const compare = this._comparator;\n while (current) {\n const cmp = compare(key, current.key);\n if (cmp === 0) return true;\n else if (cmp < 0) current = current.left;\n else current = current.right;\n }\n return false;\n }\n\n public forEach(visitor: Visitor<Key, Value>, ctx?: any): Tree<Key, Value> {\n let current = this._root;\n const Q = []; /* Initialize stack s */\n let done = false;\n\n while (!done) {\n if (current !== null) {\n Q.push(current);\n current = current.left;\n } else {\n if (Q.length !== 0) {\n current = Q.pop()!;\n visitor.call(ctx, current);\n\n current = current.right;\n } else done = true;\n }\n }\n return this;\n }\n\n /**\n * Walk key range from `low` to `high`. Stops if `fn` returns a value.\n */\n public range(\n low: Key,\n high: Key,\n fn: Visitor<Key, Value>,\n ctx?: any\n ): Tree<Key, Value> {\n const Q = [];\n const compare = this._comparator;\n let node = this._root;\n let cmp;\n\n while (Q.length !== 0 || node) {\n if (node) {\n Q.push(node);\n node = node.left;\n } else {\n node = Q.pop()!;\n cmp = compare(node.key, high);\n if (cmp > 0) {\n break;\n } else if (compare(node.key, low) >= 0) {\n if (fn.call(ctx, node)) return this; // stop if smth is returned\n }\n node = node.right;\n }\n }\n return this;\n }\n\n /**\n * Returns array of keys\n */\n public keys(): Key[] {\n const keys: Key[] = [];\n this.forEach(({ key }) => {\n keys.push(key);\n });\n return keys;\n }\n\n /**\n * Returns array of all the data in the nodes\n */\n public values(): Value[] {\n const values: Value[] = [];\n this.forEach(({ data }) => {\n values.push(data);\n });\n return values;\n }\n\n public min(): Key | null {\n if (this._root) return this.minNode(this._root)!.key;\n return null;\n }\n\n public max(): Key | null {\n if (this._root) return this.maxNode(this._root)!.key;\n return null;\n }\n\n public minNode(t = this._root): Node<Key, Value> | null {\n if (t) while (t.left) t = t.left;\n return t;\n }\n\n public maxNode(t = this._root): Node<Key, Value> | null {\n if (t) while (t.right) t = t.right;\n return t;\n }\n\n /**\n * Returns node at given index\n */\n public at(index: number): Node<Key, Value> | null {\n let current = this._root;\n let done = false;\n let i = 0;\n const Q = [];\n\n while (!done) {\n if (current) {\n Q.push(current);\n current = current.left;\n } else {\n if (Q.length > 0) {\n current = Q.pop()!;\n if (i === index) return current;\n i++;\n current = current.right!;\n } else done = true;\n }\n }\n return null;\n }\n\n public next(d: Node<Key, Value>): Node<Key, Value> | null {\n let root = this._root;\n let successor = null;\n\n if (d.right) {\n successor = d.right;\n while (successor.left) successor = successor.left;\n return successor;\n }\n\n const comparator = this._comparator;\n while (root) {\n const cmp = comparator(d.key, root.key);\n if (cmp === 0) break;\n else if (cmp < 0) {\n successor = root;\n root = root.left;\n } else root = root.right;\n }\n\n return successor;\n }\n\n public prev(d: Node<Key, Value>): Node<Key, Value> | null {\n let root = this._root;\n let predecessor = null;\n\n if (d.left !== null) {\n predecessor = d.left;\n while (predecessor.right) predecessor = predecessor.right;\n return predecessor;\n }\n\n const comparator = this._comparator;\n while (root) {\n const cmp = comparator(d.key, root.key);\n if (cmp === 0) break;\n else if (cmp < 0) root = root.left;\n else {\n predecessor = root;\n root = root.right;\n }\n }\n return predecessor;\n }\n\n public clear(): Tree<Key, Value> {\n this._root = null;\n this._size = 0;\n return this;\n }\n\n public toList() {\n return toList(this._root!);\n }\n\n /**\n * Bulk-load items. Both array have to be same size\n */\n public load(keys: Key[], values: Value[] = [], presort: boolean = false) {\n let size = keys.length;\n const comparator = this._comparator;\n\n // sort if needed\n if (presort) sort(keys, values, 0, size - 1, comparator);\n\n if (this._root === null) {\n // empty tree\n this._root = loadRecursive(keys, values, 0, size);\n this._size = size;\n } else {\n // that re-builds the whole tree from two in-order traversals\n const mergedList = mergeLists(\n this.toList(),\n createList(keys, values),\n comparator\n );\n size = this._size + size;\n this._root = sortedListToBST({ head: mergedList }, 0, size);\n }\n return this;\n }\n\n public isEmpty(): boolean {\n return this._root === null;\n }\n\n get size(): number {\n return this._size;\n }\n get root(): Node<Key, Value> | null {\n return this._root;\n }\n\n public toString(\n printNode: NodePrinter<Key, Value> = (n) => String(n.key)\n ): string {\n const out: string[] = [];\n printRow(this._root!, \"\", true, (v) => out.push(v), printNode);\n return out.join(\"\");\n }\n\n public update(key: Key, newKey: Key, newData?: Value): void {\n const comparator = this._comparator;\n let { left, right } = split(key, this._root!, comparator);\n if (comparator(key, newKey) < 0) {\n right = insert(newKey, newData, right!, comparator);\n } else {\n left = insert(newKey, newData, left!, comparator);\n }\n this._root = merge(left, right, comparator);\n }\n\n public split(key: Key) {\n return split(key, this._root!, this._comparator);\n }\n\n *[Symbol.iterator]() {\n let current = this._root;\n const Q: Node<Key, Value>[] = []; /* Initialize stack s */\n let done = false;\n\n while (!done) {\n if (current !== null) {\n Q.push(current);\n current = current.left;\n } else {\n if (Q.length !== 0) {\n current = Q.pop()!;\n yield current;\n\n current = current.right;\n } else done = true;\n }\n }\n }\n}\n\nfunction loadRecursive(\n keys: Key[],\n values: Value[],\n start: number,\n end: number\n): Node<Key, Value> | null {\n const size = end - start;\n if (size > 0) {\n const middle = start + Math.floor(size / 2);\n const key = keys[middle];\n const data = values[middle];\n const node = new Node(key, data);\n node.left = loadRecursive(keys, values, start, middle);\n node.right = loadRecursive(keys, values, middle + 1, end);\n return node;\n }\n return null;\n}\n\nfunction createList(keys: Key[], values: Value[]): Node<Key, Value> {\n const head = new Node<Key, Value>(null, null);\n let p: Node<Key, Value> = head;\n for (let i = 0; i < keys.length; i++) {\n p = p.next = new Node(keys[i], values[i]);\n }\n p.next = null;\n return head.next!;\n}\n\nfunction toList(root: Node<Key, Value>): Node<Key, Value> {\n let current = root;\n const Q = [];\n let done = false;\n\n const head = new Node<Key, Value>(null, null);\n let p = head;\n\n while (!done) {\n if (current) {\n Q.push(current);\n current = current.left!;\n } else {\n if (Q.length > 0) {\n current = p = p.next = Q.pop()!;\n current = current.right!;\n } else done = true;\n }\n }\n p.next = null; // that'll work even if the tree was empty\n return head.next!;\n}\n\nfunction sortedListToBST(\n list: TreeNodeList<Key, Value>,\n start: number,\n end: number\n) {\n const size = end - start;\n if (size > 0) {\n const middle = start + Math.floor(size / 2);\n const left = sortedListToBST(list, start, middle);\n\n const root = list.head;\n root!.left = left;\n\n list.head = list.head!.next;\n\n root!.right = sortedListToBST(list, middle + 1, end);\n return root!;\n }\n return null;\n}\n\nfunction mergeLists<Key, Value>(\n l1: Node<Key, Value>,\n l2: Node<Key, Value>,\n compare: Comparator<Key>\n): Node<Key, Value> {\n const head: Node<Key, Value> = new Node<Key, Value>(null as Key, null); // dummy\n let p = head;\n\n let p1: Node<Key, Value> = l1;\n let p2: Node<Key, Value> = l2;\n\n while (p1 !== null && p2 !== null) {\n if (compare(p1.key, p2.key) < 0) {\n p.next = p1;\n p1 = p1.next!;\n } else {\n p.next = p2;\n p2 = p2.next!;\n }\n p = p.next;\n }\n\n if (p1 !== null) {\n p.next = p1;\n } else if (p2 !== null) {\n p.next = p2;\n }\n\n return head.next!;\n}\n\nfunction sort(\n keys: Key[],\n values: Value[],\n left: number,\n right: number,\n compare: Comparator<Key>\n) {\n if (left >= right) return;\n\n const pivot = keys[(left + right) >> 1];\n let i = left - 1;\n let j = right + 1;\n\n while (true) {\n do i++;\n while (compare(keys[i], pivot) < 0);\n do j--;\n while (compare(keys[j], pivot) > 0);\n if (i >= j) break;\n\n let tmp = keys[i];\n keys[i] = keys[j];\n keys[j] = tmp;\n\n tmp = values[i];\n values[i] = values[j];\n values[j] = tmp;\n }\n\n sort(keys, values, left, j, compare);\n sort(keys, values, j + 1, right, compare);\n}\n"],"names":["Node","key","data","DEFAULT_COMPARE","a","b","splay","i","comparator","N","r","cmp","y","insert","t","node","split","v","left","right","merge","printRow","root","prefix","isTail","out","printNode","indent","Tree","x","current","compare","visitor","ctx","Q","done","low","high","fn","keys","values","index","d","successor","predecessor","toList","presort","size","sort","loadRecursive","mergedList","mergeLists","createList","sortedListToBST","n","newKey","newData","start","end","middle","head","p","list","l1","l2","p1","p2","pivot","j","tmp"],"mappings":"AACA,MAAqBA,EAAiB;AAAA,EAOpC,YAAaC,GAASC,GAAW;AAFjC,SAAO,OAA6B,MAGlC,KAAK,MAASD,GACd,KAAK,OAASC,GACd,KAAK,OAAS,MACd,KAAK,QAAS;AAAA,EAChB;AACF;ACDA,SAASC,EAAgBC,GAAQC,GAAgB;AAC/C,SAAOD,IAAIC,IAAI,IAAID,IAAIC,IAAI,KAAK;AAClC;AAOA,SAASC,EACPC,GACA,GACAC,GACkB;AAClB,QAAMC,IAAI,IAAIT,EAAK,MAAM,IAAI;AAC7B,MAAI,IAAIS,GACJC,IAAID;AAER,aAAa;AACX,UAAME,IAAMH,EAAWD,GAAG,EAAG,GAAG;AAEhC,QAAII,IAAM,GAAG;AACX,UAAI,EAAG,SAAS,KAAM;AAEtB,UAAIH,EAAWD,GAAG,EAAE,KAAK,GAAG,IAAI,GAAG;AACjC,cAAMK,IAAI,EAAE;AAIZ,YAHA,EAAE,OAAOA,EAAE,OACXA,EAAE,QAAQ,GACV,IAAIA,GACA,EAAE,SAAS,KAAM;AAAA,MACvB;AACA,MAAAF,EAAE,OAAO,GACTA,IAAI,GACJ,IAAI,EAAE;AAAA,IAER,WAAWC,IAAM,GAAG;AAClB,UAAI,EAAE,UAAU,KAAM;AAEtB,UAAIH,EAAWD,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG;AAClC,cAAMK,IAAI,EAAE;AAIZ,YAHA,EAAE,QAAQA,EAAE,MACZA,EAAE,OAAO,GACT,IAAIA,GACA,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GAAMkD,GAAGrC,CAAO,GACnCiB,EAAKT,GAAMC,GAAQ4B,IAAI,GAAGjD,GAAOY,CAAO;AAC1C;"}
@@ -0,0 +1,3 @@
1
+ (function(h,c){typeof exports=="object"&&typeof module<"u"?module.exports=c():typeof define=="function"&&define.amd?define(c):(h=typeof globalThis<"u"?globalThis:h||self,h.SplayTree=c())})(this,(function(){"use strict";class h{constructor(t,e){this.next=null,this.key=t,this.data=e,this.left=null,this.right=null}}function c(n,t){return n>t?1:n<t?-1:0}function u(n,t,e){const r=new h(null,null);let i=r,l=r;for(;;){const o=e(n,t.key);if(o<0){if(t.left===null)break;if(e(n,t.left.key)<0){const s=t.left;if(t.left=s.right,s.right=t,t=s,t.left===null)break}l.left=t,l=t,t=t.left}else if(o>0){if(t.right===null)break;if(e(n,t.right.key)>0){const s=t.right;if(t.right=s.left,s.left=t,t=s,t.right===null)break}i.right=t,i=t,t=t.right}else break}return i.right=t.left,l.left=t.right,t.left=r.right,t.right=r.left,t}function _(n,t,e,r){const i=new h(n,t);if(e===null)return i.left=i.right=null,i;e=u(n,e,r);const l=r(n,e.key);return l<0?(i.left=e.left,i.right=e,e.left=null):l>=0&&(i.right=e.right,i.left=e,e.right=null),i}function d(n,t,e){let r=null,i=null;if(t){t=u(n,t,e);const l=e(t.key,n);l===0?(r=t.left,i=t.right):l<0?(i=t.right,t.right=null,r=t):(r=t.left,t.left=null,i=t)}return{left:r,right:i}}function w(n,t,e){return t===null?n:(n===null||(t=u(n.key,t,e),t.left=n),t)}function a(n,t,e,r,i){if(n){r(`${t}${e?"└── ":"├── "}${i(n)}
2
+ `);const l=t+(e?" ":"│ ");n.left&&a(n.left,l,!1,r,i),n.right&&a(n.right,l,!0,r,i)}}class x{constructor(t=c){this._root=null,this._size=0,this._comparator=t}insert(t,e){return this._size++,this._root=_(t,e,this._root,this._comparator)}add(t,e){const r=new h(t,e);this._root===null&&(r.left=r.right=null,this._size++,this._root=r);const i=this._comparator,l=u(t,this._root,i),o=i(t,l.key);return o===0?this._root=l:(o<0?(r.left=l.left,r.right=l,l.left=null):o>0&&(r.right=l.right,r.left=l,l.right=null),this._size++,this._root=r),this._root}remove(t){this._root=this._remove(t,this._root,this._comparator)}_remove(t,e,r){let i;return e===null?null:(e=u(t,e,r),r(t,e.key)===0?(e.left===null?i=e.right:(i=u(t,e.left,r),i.right=e.right),this._size--,i):e)}pop(){let t=this._root;if(t){for(;t.left;)t=t.left;return this._root=u(t.key,this._root,this._comparator),this._root=this._remove(t.key,this._root,this._comparator),{key:t.key,data:t.data}}return null}findStatic(t){let e=this._root;const r=this._comparator;for(;e;){const i=r(t,e.key);if(i===0)return e;i<0?e=e.left:e=e.right}return null}find(t){return this._root&&(this._root=u(t,this._root,this._comparator),this._comparator(t,this._root.key)!==0)?null:this._root}contains(t){let e=this._root;const r=this._comparator;for(;e;){const i=r(t,e.key);if(i===0)return!0;i<0?e=e.left:e=e.right}return!1}forEach(t,e){let r=this._root;const i=[];let l=!1;for(;!l;)r!==null?(i.push(r),r=r.left):i.length!==0?(r=i.pop(),t.call(e,r),r=r.right):l=!0;return this}range(t,e,r,i){const l=[],o=this._comparator;let s=this._root,f;for(;l.length!==0||s;)if(s)l.push(s),s=s.left;else{if(s=l.pop(),f=o(s.key,e),f>0)break;if(o(s.key,t)>=0&&r.call(i,s))return this;s=s.right}return this}keys(){const t=[];return this.forEach(({key:e})=>{t.push(e)}),t}values(){const t=[];return this.forEach(({data:e})=>{t.push(e)}),t}min(){return this._root?this.minNode(this._root).key:null}max(){return this._root?this.maxNode(this._root).key:null}minNode(t=this._root){if(t)for(;t.left;)t=t.left;return t}maxNode(t=this._root){if(t)for(;t.right;)t=t.right;return t}at(t){let e=this._root,r=!1,i=0;const l=[];for(;!r;)if(e)l.push(e),e=e.left;else if(l.length>0){if(e=l.pop(),i===t)return e;i++,e=e.right}else r=!0;return null}next(t){let e=this._root,r=null;if(t.right){for(r=t.right;r.left;)r=r.left;return r}const i=this._comparator;for(;e;){const l=i(t.key,e.key);if(l===0)break;l<0?(r=e,e=e.left):e=e.right}return r}prev(t){let e=this._root,r=null;if(t.left!==null){for(r=t.left;r.right;)r=r.right;return r}const i=this._comparator;for(;e;){const l=i(t.key,e.key);if(l===0)break;l<0?e=e.left:(r=e,e=e.right)}return r}clear(){return this._root=null,this._size=0,this}toList(){return y(this._root)}load(t,e=[],r=!1){let i=t.length;const l=this._comparator;if(r&&m(t,e,0,i-1,l),this._root===null)this._root=p(t,e,0,i),this._size=i;else{const o=z(this.toList(),k(t,e),l);i=this._size+i,this._root=g({head:o},0,i)}return this}isEmpty(){return this._root===null}get size(){return this._size}get root(){return this._root}toString(t=e=>String(e.key)){const e=[];return a(this._root,"",!0,r=>e.push(r),t),e.join("")}update(t,e,r){const i=this._comparator;let{left:l,right:o}=d(t,this._root,i);i(t,e)<0?o=_(e,r,o,i):l=_(e,r,l,i),this._root=w(l,o,i)}split(t){return d(t,this._root,this._comparator)}*[Symbol.iterator](){let t=this._root;const e=[];let r=!1;for(;!r;)t!==null?(e.push(t),t=t.left):e.length!==0?(t=e.pop(),yield t,t=t.right):r=!0}}function p(n,t,e,r){const i=r-e;if(i>0){const l=e+Math.floor(i/2),o=n[l],s=t[l],f=new h(o,s);return f.left=p(n,t,e,l),f.right=p(n,t,l+1,r),f}return null}function k(n,t){const e=new h(null,null);let r=e;for(let i=0;i<n.length;i++)r=r.next=new h(n[i],t[i]);return r.next=null,e.next}function y(n){let t=n;const e=[];let r=!1;const i=new h(null,null);let l=i;for(;!r;)t?(e.push(t),t=t.left):e.length>0?(t=l=l.next=e.pop(),t=t.right):r=!0;return l.next=null,i.next}function g(n,t,e){const r=e-t;if(r>0){const i=t+Math.floor(r/2),l=g(n,t,i),o=n.head;return o.left=l,n.head=n.head.next,o.right=g(n,i+1,e),o}return null}function z(n,t,e){const r=new h(null,null);let i=r,l=n,o=t;for(;l!==null&&o!==null;)e(l.key,o.key)<0?(i.next=l,l=l.next):(i.next=o,o=o.next),i=i.next;return l!==null?i.next=l:o!==null&&(i.next=o),r.next}function m(n,t,e,r,i){if(e>=r)return;const l=n[e+r>>1];let o=e-1,s=r+1;for(;;){do o++;while(i(n[o],l)<0);do s--;while(i(n[s],l)>0);if(o>=s)break;let f=n[o];n[o]=n[s],n[s]=f,f=t[o],t[o]=t[s],t[s]=f}m(n,t,e,s,i),m(n,t,s+1,r,i)}return x}));
3
+ //# sourceMappingURL=splaytree.umd.cjs.map