list-toolkit 2.2.6 → 2.3.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.
Files changed (125) hide show
  1. package/README.md +40 -37
  2. package/llms-full.txt +743 -0
  3. package/llms.txt +100 -0
  4. package/package.json +34 -29
  5. package/src/cache/cache-fifo.d.ts +6 -0
  6. package/src/cache/cache-fifo.js +7 -4
  7. package/src/cache/cache-lfu.d.ts +18 -0
  8. package/src/cache/cache-lfu.js +18 -6
  9. package/src/cache/cache-lru.d.ts +74 -0
  10. package/src/cache/cache-lru.js +60 -5
  11. package/src/cache/cache-random.d.ts +20 -0
  12. package/src/cache/cache-random.js +17 -6
  13. package/src/cache/decorator.d.ts +46 -0
  14. package/src/cache/decorator.js +26 -2
  15. package/src/cache.d.ts +13 -0
  16. package/src/cache.js +7 -2
  17. package/src/ext-list.d.ts +3 -0
  18. package/src/ext-list.js +0 -2
  19. package/src/ext-slist.d.ts +3 -0
  20. package/src/ext-slist.js +0 -2
  21. package/src/ext-value-list.d.ts +3 -0
  22. package/src/ext-value-list.js +0 -2
  23. package/src/ext-value-slist.d.ts +3 -0
  24. package/src/ext-value-slist.js +0 -2
  25. package/src/heap/basics.d.ts +89 -0
  26. package/src/heap/basics.js +42 -5
  27. package/src/heap/leftist-heap.d.ts +107 -0
  28. package/src/heap/leftist-heap.js +54 -2
  29. package/src/heap/min-heap.d.ts +270 -0
  30. package/src/heap/min-heap.js +186 -2
  31. package/src/heap/skew-heap.d.ts +105 -0
  32. package/src/heap/skew-heap.js +54 -2
  33. package/src/heap.d.ts +3 -0
  34. package/src/heap.js +0 -2
  35. package/src/list/basics.d.ts +43 -0
  36. package/src/list/basics.js +26 -8
  37. package/src/list/core.d.ts +271 -0
  38. package/src/list/core.js +162 -7
  39. package/src/list/ext-value.d.ts +253 -0
  40. package/src/list/ext-value.js +40 -6
  41. package/src/list/ext.d.ts +242 -0
  42. package/src/list/ext.js +148 -10
  43. package/src/list/nodes.d.ts +336 -0
  44. package/src/list/nodes.js +141 -3
  45. package/src/list/ptr.d.ts +72 -0
  46. package/src/list/ptr.js +44 -2
  47. package/src/list/value.d.ts +292 -0
  48. package/src/list/value.js +47 -6
  49. package/src/list-helpers.d.ts +44 -0
  50. package/src/list-helpers.js +36 -3
  51. package/src/list-utils.d.ts +141 -0
  52. package/src/list-utils.js +89 -3
  53. package/src/list.d.ts +3 -0
  54. package/src/list.js +0 -2
  55. package/src/meta-utils.d.ts +212 -0
  56. package/src/meta-utils.js +152 -1
  57. package/src/nt-utils.d.ts +91 -0
  58. package/src/nt-utils.js +65 -4
  59. package/src/queue.d.ts +74 -0
  60. package/src/queue.js +28 -2
  61. package/src/slist/basics.d.ts +47 -0
  62. package/src/slist/basics.js +23 -8
  63. package/src/slist/core.d.ts +251 -0
  64. package/src/slist/core.js +151 -6
  65. package/src/slist/ext-value.d.ts +188 -0
  66. package/src/slist/ext-value.js +35 -6
  67. package/src/slist/ext.d.ts +182 -0
  68. package/src/slist/ext.js +114 -12
  69. package/src/slist/nodes.d.ts +361 -0
  70. package/src/slist/nodes.js +156 -3
  71. package/src/slist/ptr.d.ts +73 -0
  72. package/src/slist/ptr.js +45 -2
  73. package/src/slist/value.d.ts +246 -0
  74. package/src/slist/value.js +38 -6
  75. package/src/slist.d.ts +3 -0
  76. package/src/slist.js +0 -2
  77. package/src/stack.d.ts +59 -0
  78. package/src/stack.js +29 -3
  79. package/src/tree/splay-tree.d.ts +151 -0
  80. package/src/tree/splay-tree.js +94 -3
  81. package/src/value-list.d.ts +3 -0
  82. package/src/value-list.js +0 -2
  83. package/src/value-slist.d.ts +3 -0
  84. package/src/value-slist.js +0 -2
  85. package/cjs/cache/cache-fifo.js +0 -37
  86. package/cjs/cache/cache-lfu.js +0 -76
  87. package/cjs/cache/cache-lru.js +0 -100
  88. package/cjs/cache/cache-random.js +0 -77
  89. package/cjs/cache/decorator.js +0 -47
  90. package/cjs/cache.js +0 -27
  91. package/cjs/ext-list.js +0 -21
  92. package/cjs/ext-slist.js +0 -21
  93. package/cjs/ext-value-list.js +0 -21
  94. package/cjs/ext-value-slist.js +0 -21
  95. package/cjs/heap/basics.js +0 -63
  96. package/cjs/heap/leftist-heap.js +0 -124
  97. package/cjs/heap/min-heap.js +0 -294
  98. package/cjs/heap/skew-heap.js +0 -114
  99. package/cjs/heap.js +0 -21
  100. package/cjs/list/basics.js +0 -88
  101. package/cjs/list/core.js +0 -305
  102. package/cjs/list/ext-value.js +0 -88
  103. package/cjs/list/ext.js +0 -356
  104. package/cjs/list/nodes.js +0 -240
  105. package/cjs/list/ptr.js +0 -61
  106. package/cjs/list/value.js +0 -99
  107. package/cjs/list-helpers.js +0 -91
  108. package/cjs/list-utils.js +0 -141
  109. package/cjs/list.js +0 -21
  110. package/cjs/meta-utils.js +0 -171
  111. package/cjs/nt-utils.js +0 -132
  112. package/cjs/package.json +0 -1
  113. package/cjs/queue.js +0 -58
  114. package/cjs/slist/basics.js +0 -71
  115. package/cjs/slist/core.js +0 -362
  116. package/cjs/slist/ext-value.js +0 -82
  117. package/cjs/slist/ext.js +0 -336
  118. package/cjs/slist/nodes.js +0 -276
  119. package/cjs/slist/ptr.js +0 -87
  120. package/cjs/slist/value.js +0 -90
  121. package/cjs/slist.js +0 -21
  122. package/cjs/stack.js +0 -55
  123. package/cjs/tree/splay-tree.js +0 -362
  124. package/cjs/value-list.js +0 -21
  125. package/cjs/value-slist.js +0 -21
@@ -0,0 +1,246 @@
1
+ import {HeadNode, ValueNode, PtrBase, SllOptions, SllRange, SllPtrRange} from './nodes.js';
2
+ import {Ptr} from './ptr.js';
3
+
4
+ /**
5
+ * Hosted value-based singly linked list. Wraps values in {@link ValueNode}.
6
+ *
7
+ * Extends `SList` at runtime. Internally nodes are `ValueNode<V>`.
8
+ * The default iterator and `getIterator` yield unwrapped `V` values;
9
+ * use `getNodeIterator` for `ValueNode<V>` nodes.
10
+ */
11
+ export class ValueSList<V = unknown> extends HeadNode {
12
+ /** Pointer to the first node. */
13
+ get frontPtr(): Ptr<ValueNode<V>>;
14
+
15
+ /** A pointer-based range spanning all nodes, or `null` if empty. */
16
+ get ptrRange(): SllPtrRange<ValueNode<V>> | null;
17
+
18
+ /**
19
+ * Create a pointer to a node in this list.
20
+ * @param node - Target node, or `undefined` for the front.
21
+ * @returns A new Ptr.
22
+ */
23
+ makePtr(node?: ValueNode<V>): Ptr<ValueNode<V>>;
24
+
25
+ /**
26
+ * Create a pointer to the node after `prev`.
27
+ * @param prev - Preceding node, or `undefined` for the front.
28
+ * @returns A new Ptr.
29
+ */
30
+ makePtrFromPrev(prev?: ValueNode<V>): Ptr<ValueNode<V>>;
31
+
32
+ /**
33
+ * Remove and return the front value.
34
+ * @returns The unwrapped value, or `undefined` if empty.
35
+ */
36
+ popFront(): V | undefined;
37
+
38
+ /** Alias for {@link popFront}. */
39
+ pop(): V | undefined;
40
+
41
+ /**
42
+ * Remove and return the front ValueNode.
43
+ * @returns The removed node, or `undefined` if empty.
44
+ */
45
+ popFrontNode(): ValueNode<V> | undefined;
46
+
47
+ /**
48
+ * Insert a value at the front.
49
+ * @param value - Raw value, ValueNode, or Ptr.
50
+ * @returns A Ptr to the front.
51
+ */
52
+ pushFront(value: V | ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
53
+
54
+ /** Alias for {@link pushFront}. */
55
+ push(value: V | ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
56
+
57
+ /**
58
+ * Insert a value at the back.
59
+ * @param value - Raw value, ValueNode, or Ptr.
60
+ * @returns A Ptr to the inserted node.
61
+ */
62
+ pushBack(value: V | ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
63
+
64
+ /**
65
+ * Insert an existing ValueNode at the front.
66
+ * @param nodeOrPtr - ValueNode or pointer to insert.
67
+ * @returns A Ptr to the front.
68
+ */
69
+ pushFrontNode(nodeOrPtr: ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
70
+
71
+ /**
72
+ * Insert an existing ValueNode at the back.
73
+ * @param nodeOrPtr - ValueNode or pointer to insert.
74
+ * @returns A Ptr to the inserted node.
75
+ */
76
+ pushBackNode(nodeOrPtr: ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
77
+
78
+ /**
79
+ * Move all nodes from another list to the front.
80
+ * @param list - Compatible list to consume.
81
+ * @returns A Ptr to the new front.
82
+ */
83
+ appendFront(list: HeadNode): Ptr<ValueNode<V>>;
84
+
85
+ /**
86
+ * Move all nodes from another list to the back.
87
+ * @param list - Compatible list to consume.
88
+ * @returns A Ptr to the first appended node.
89
+ */
90
+ appendBack(list: HeadNode): Ptr<ValueNode<V>>;
91
+
92
+ /** Alias for {@link appendBack}. */
93
+ append(list: HeadNode): Ptr<ValueNode<V>>;
94
+
95
+ /**
96
+ * Move a pointed-to node to the front.
97
+ * @param ptr - Pointer to the node to move.
98
+ * @returns A Ptr to the front, or `this` if at head.
99
+ */
100
+ moveToFront(ptr: PtrBase<ValueNode<V>>): Ptr<ValueNode<V>> | this;
101
+
102
+ /**
103
+ * Move a pointed-to node to the back.
104
+ * @param ptr - Pointer to the node to move.
105
+ * @returns A Ptr to the back, or `this` if at head.
106
+ */
107
+ moveToBack(ptr: PtrBase<ValueNode<V>>): Ptr<ValueNode<V>> | this;
108
+
109
+ /**
110
+ * Remove all nodes.
111
+ * @param drop - If `true`, make each removed node stand-alone.
112
+ * @returns `this` for chaining.
113
+ */
114
+ clear(drop?: boolean): this;
115
+
116
+ /**
117
+ * Remove a node via its pointer.
118
+ * @param ptr - Pointer to the node to remove.
119
+ * @returns The removed node, or `null` if at head.
120
+ */
121
+ removeNode(ptr: PtrBase<ValueNode<V>>): ValueNode<V> | null;
122
+
123
+ /**
124
+ * Remove a pointer-based range and optionally drop nodes.
125
+ * @param ptrRange - Range to remove.
126
+ * @param drop - If `true`, make each removed node stand-alone.
127
+ * @returns A new ValueSList containing the removed values.
128
+ */
129
+ removeRange(ptrRange?: SllPtrRange<ValueNode<V>>, drop?: boolean): ValueSList<V>;
130
+
131
+ /**
132
+ * Extract a pointer-based range into a new list.
133
+ * @param ptrRange - Range to extract (defaults to the whole list).
134
+ * @returns A new ValueSList containing the extracted values.
135
+ */
136
+ extractRange(ptrRange?: SllPtrRange<ValueNode<V>>): ValueSList<V>;
137
+
138
+ /**
139
+ * Extract nodes that satisfy a condition into a new list.
140
+ * @param condition - Predicate receiving each ValueNode.
141
+ * @returns A new ValueSList containing the extracted values.
142
+ */
143
+ extractBy(condition: (node: ValueNode<V>) => boolean): ValueSList<V>;
144
+
145
+ /**
146
+ * Reverse the order of all nodes in place.
147
+ * @returns `this` for chaining.
148
+ */
149
+ reverse(): this;
150
+
151
+ /**
152
+ * Sort nodes in place using merge sort.
153
+ * @param lessFn - Comparison receiving ValueNodes, returns `true` if `a` should precede `b`.
154
+ * @returns `this` for chaining.
155
+ */
156
+ sort(lessFn: (a: ValueNode<V>, b: ValueNode<V>) => boolean): this;
157
+
158
+ /**
159
+ * Detach all nodes as a raw circular list.
160
+ * @returns Head of the raw circular list, or `null` if empty.
161
+ */
162
+ releaseRawList(): ValueNode<V> | null;
163
+
164
+ /**
165
+ * Detach all nodes as a null-terminated list.
166
+ * @returns Object with `head` and `tail`, or `null` if empty.
167
+ */
168
+ releaseNTList(): { head: ValueNode<V>; tail: ValueNode<V> } | null;
169
+
170
+ /**
171
+ * Validate that a range is reachable within this list.
172
+ * @param range - Range to validate.
173
+ * @returns `true` if the range is valid.
174
+ */
175
+ validateRange(range?: SllRange<ValueNode<V>>): boolean;
176
+
177
+ /**
178
+ * Adopt a value, pointer, or ValueNode into this list.
179
+ * @param value - Raw value, Ptr, or ValueNode.
180
+ * @returns A ValueNode ready for insertion.
181
+ */
182
+ adoptValue(value: V | ValueNode<V> | PtrBase<ValueNode<V>>): ValueNode<V>;
183
+
184
+ /** Iterate over unwrapped values from front to back. */
185
+ [Symbol.iterator](): IterableIterator<V>;
186
+
187
+ /**
188
+ * Get an iterable over ValueNodes in a range.
189
+ * @param range - Sub-range to iterate.
190
+ * @returns An iterable iterator of ValueNodes.
191
+ */
192
+ getNodeIterator(range?: SllRange<ValueNode<V>>): IterableIterator<ValueNode<V>>;
193
+
194
+ /**
195
+ * Get an iterable over unwrapped values in a range.
196
+ * @param range - Sub-range to iterate.
197
+ * @returns An iterable iterator of values.
198
+ */
199
+ getValueIterator(range?: SllRange<ValueNode<V>>): IterableIterator<V>;
200
+
201
+ /** Alias for {@link getValueIterator}. */
202
+ getIterator(range?: SllRange<ValueNode<V>>): IterableIterator<V>;
203
+
204
+ /**
205
+ * Get an iterable of Ptr objects over a pointer range.
206
+ * @param ptrRange - Sub-range to iterate.
207
+ * @returns An iterable iterator of Ptrs.
208
+ */
209
+ getPtrIterator(ptrRange?: SllPtrRange<ValueNode<V>>): IterableIterator<Ptr<ValueNode<V>>>;
210
+
211
+ /**
212
+ * Create a shallow clone of this list.
213
+ * @returns A new ValueSList with the same values.
214
+ */
215
+ clone(): ValueSList<V>;
216
+
217
+ /**
218
+ * Create an empty list with the same options.
219
+ * @returns A new empty ValueSList.
220
+ */
221
+ make(): ValueSList<V>;
222
+
223
+ /**
224
+ * Create a list from values with the same options.
225
+ * @param values - Iterable of values.
226
+ * @returns A new ValueSList.
227
+ */
228
+ makeFrom(values: Iterable<V>): ValueSList<V>;
229
+
230
+ /**
231
+ * Build a ValueSList from an iterable.
232
+ * @param values - Iterable of values.
233
+ * @param options - Link property names.
234
+ * @returns A new ValueSList.
235
+ */
236
+ static from<V = unknown>(values: Iterable<V>, options?: SllOptions): ValueSList<V>;
237
+
238
+ /** The Ptr class associated with this list type. */
239
+ static Ptr: typeof Ptr;
240
+
241
+ /** The ValueNode class used by this list type. */
242
+ static ValueNode: typeof ValueNode;
243
+ }
244
+
245
+ export { ValueNode, Ptr };
246
+ export default ValueSList;
@@ -1,14 +1,25 @@
1
- 'use strict';
2
-
3
1
  import List, {Ptr} from './core.js';
4
2
  import {ValueNode} from './nodes.js';
5
3
  import {addAliases, mapIterator, normalizeIterator} from '../meta-utils.js';
6
4
 
5
+ /**
6
+ * Hosted value-based singly linked list. Wraps values in {@link ValueNode}.
7
+ * Iterators yield unwrapped values; use `getNodeIterator` for ValueNode access.
8
+ */
7
9
  export class ValueSList extends List {
10
+ /**
11
+ * Remove and return the front value.
12
+ * @returns {*} The unwrapped value, or `undefined` if empty.
13
+ */
8
14
  popFront() {
9
15
  return this.popFrontNode()?.value;
10
16
  }
11
17
 
18
+ /**
19
+ * Adopt a value, pointer, or ValueNode into this list.
20
+ * @param {*} value - Raw value, Ptr, or ValueNode.
21
+ * @returns {ValueNode} A ValueNode ready for insertion.
22
+ */
12
23
  adoptValue(value) {
13
24
  if (value instanceof Ptr) {
14
25
  if (!this.isCompatiblePtr(value)) throw new Error('Incompatible pointer');
@@ -25,8 +36,7 @@ export class ValueSList extends List {
25
36
  return new ValueNode(value, this);
26
37
  }
27
38
 
28
- // iterators
29
-
39
+ /** Iterate over unwrapped values from front to back. */
30
40
  [Symbol.iterator]() {
31
41
  let current = this[this.nextName],
32
42
  readyToStop = this.isEmpty;
@@ -41,24 +51,46 @@ export class ValueSList extends List {
41
51
  });
42
52
  }
43
53
 
54
+ /**
55
+ * Get an iterable over unwrapped values in a range.
56
+ * @param {object} [range] - Sub-range to iterate.
57
+ * @returns {Iterable} An iterable iterator of values.
58
+ */
44
59
  getValueIterator(range) {
45
60
  return mapIterator(this.getNodeIterator(range), node => node.value);
46
61
  }
47
62
 
48
- // meta helpers
49
-
63
+ /**
64
+ * Create a shallow clone of this list.
65
+ * @returns {ValueSList} A new ValueSList with the same values.
66
+ */
50
67
  clone() {
51
68
  return ValueSList.from(this, this);
52
69
  }
53
70
 
71
+ /**
72
+ * Create an empty list with the same options.
73
+ * @returns {ValueSList} A new empty ValueSList.
74
+ */
54
75
  make() {
55
76
  return new ValueSList(this);
56
77
  }
57
78
 
79
+ /**
80
+ * Create a list from values with the same options.
81
+ * @param {Iterable} values - Iterable of values.
82
+ * @returns {ValueSList} A new ValueSList.
83
+ */
58
84
  makeFrom(values) {
59
85
  return ValueSList.from(values, this);
60
86
  }
61
87
 
88
+ /**
89
+ * Build a ValueSList from an iterable.
90
+ * @param {Iterable} values - Iterable of values.
91
+ * @param {object} [options] - Link property names.
92
+ * @returns {ValueSList} A new ValueSList.
93
+ */
62
94
  static from(values, options) {
63
95
  const list = new ValueSList(options);
64
96
  for (const value of values) list.pushBack(value);
package/src/slist.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './slist/core.js';
2
+ import SList from './slist/core.js';
3
+ export default SList;
package/src/slist.js CHANGED
@@ -1,5 +1,3 @@
1
- 'use strict';
2
-
3
1
  export * from './slist/core.js';
4
2
  import SList from './slist/core.js';
5
3
 
package/src/stack.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ /** LIFO stack backed by a value list. */
2
+ export class Stack<V = unknown> {
3
+ /** Number of elements in the stack. */
4
+ size: number;
5
+
6
+ /**
7
+ * @param UnderlyingList - Optional value list constructor to use (default: ValueList).
8
+ */
9
+ constructor(UnderlyingList?: new () => object);
10
+
11
+ /** Whether the stack has no elements. */
12
+ get isEmpty(): boolean;
13
+
14
+ /** The top element without removing it, or `undefined` if empty. */
15
+ get top(): V | undefined;
16
+
17
+ /** Alias for {@link top}. */
18
+ peek(): V | undefined;
19
+
20
+ /**
21
+ * Push a value onto the top of the stack.
22
+ * @param value - Value to push.
23
+ * @returns `this` for chaining.
24
+ */
25
+ push(value: V): this;
26
+
27
+ /** Alias for {@link push}. */
28
+ pushFront(value: V): this;
29
+
30
+ /**
31
+ * Remove and return the top value.
32
+ * @returns The top value, or `undefined` if empty.
33
+ */
34
+ pop(): V | undefined;
35
+
36
+ /**
37
+ * Push multiple values onto the stack.
38
+ * @param values - Iterable of values.
39
+ * @returns `this` for chaining.
40
+ */
41
+ pushValues(values: Iterable<V>): this;
42
+
43
+ /**
44
+ * Remove all elements.
45
+ * @returns `this` for chaining.
46
+ */
47
+ clear(): this;
48
+
49
+ /** Iterate over values from top to bottom. */
50
+ [Symbol.iterator](): IterableIterator<V>;
51
+
52
+ /**
53
+ * Iterate over values from bottom to top.
54
+ * @returns An iterable iterator.
55
+ */
56
+ getReverseIterator(): IterableIterator<V>;
57
+ }
58
+
59
+ export default Stack;
package/src/stack.js CHANGED
@@ -1,47 +1,73 @@
1
- 'use strict';
2
-
3
1
  import ValueList from './value-list.js';
4
2
  import {addAlias} from './meta-utils.js';
5
3
  import {pushValuesFront} from './list-utils.js';
6
4
 
5
+ /** LIFO stack backed by a value list. */
7
6
  export class Stack {
7
+ /** @param {Function} [UnderlyingList=ValueList] - Constructor for the backing list. */
8
8
  constructor(UnderlyingList = ValueList) {
9
9
  this.size = 0;
10
10
  this.list = new UnderlyingList();
11
11
  }
12
+ /** Whether the stack has no elements. */
12
13
  get isEmpty() {
13
14
  return this.list.isEmpty;
14
15
  }
16
+ /** The top element without removing it, or `undefined` if empty. */
15
17
  get top() {
16
18
  return this.list.isEmpty ? undefined : this.list.front.value;
17
19
  }
20
+ /** Alias for {@link Stack#top|top}. */
18
21
  peek() {
19
22
  return this.list.isEmpty ? undefined : this.list.front.value;
20
23
  }
24
+ /**
25
+ * Push a value onto the stack.
26
+ * @param {*} value - Value to push.
27
+ * @returns {Stack} `this` for chaining.
28
+ */
21
29
  push(value) {
22
30
  this.list.pushFront(value);
23
31
  ++this.size;
24
32
  return this;
25
33
  }
34
+ /**
35
+ * Remove and return the top value.
36
+ * @returns {*} The popped value, or `undefined` if empty.
37
+ */
26
38
  pop() {
27
39
  if (!this.list.isEmpty) {
28
40
  --this.size;
29
- return this.list.popFront().value;
41
+ return this.list.popFront();
30
42
  }
31
43
  // return undefined;
32
44
  }
45
+ /**
46
+ * Push multiple values onto the stack.
47
+ * @param {Iterable} values - Values to push.
48
+ * @returns {Stack} `this` for chaining.
49
+ */
33
50
  pushValues(values) {
34
51
  pushValuesFront(this, values);
35
52
  return this;
36
53
  }
54
+ /**
55
+ * Remove all elements.
56
+ * @returns {Stack} `this` for chaining.
57
+ */
37
58
  clear() {
38
59
  this.list.clear();
39
60
  this.size = 0;
40
61
  return this;
41
62
  }
63
+ /** Iterate over values from top to bottom. */
42
64
  [Symbol.iterator]() {
43
65
  return this.list[Symbol.iterator]();
44
66
  }
67
+ /**
68
+ * Get an iterable over values in reverse (bottom-to-top) order.
69
+ * @returns {Iterable} An iterable iterator of values.
70
+ */
45
71
  getReverseIterator() {
46
72
  return this.list.getReverseIterator?.();
47
73
  }
@@ -0,0 +1,151 @@
1
+ /** Options for configuring splay tree ordering. */
2
+ export interface SplayTreeOptions<T = unknown> {
3
+ /** Returns `true` if `a` should be ordered before `b`. */
4
+ less?: (a: T, b: T) => boolean;
5
+ /** Comparison function returning negative, zero, or positive. Overrides `less` when set. */
6
+ compare?: ((a: T, b: T) => number) | null;
7
+ }
8
+
9
+ /** Node used internally by {@link SplayTree}. */
10
+ export class SplayTreeNode<T = unknown> {
11
+ /** The stored value. */
12
+ value: T;
13
+ /** Left child, or `null`. */
14
+ left: SplayTreeNode<T> | null;
15
+ /** Right child, or `null`. */
16
+ right: SplayTreeNode<T> | null;
17
+ /** Parent node, or `null` for the root. */
18
+ parent: SplayTreeNode<T> | null;
19
+
20
+ /** @param value - Value to store. */
21
+ constructor(value: T);
22
+
23
+ /**
24
+ * Find the minimum node in this subtree.
25
+ * @returns The leftmost descendant.
26
+ */
27
+ getMin(): SplayTreeNode<T>;
28
+
29
+ /**
30
+ * Find the maximum node in this subtree.
31
+ * @returns The rightmost descendant.
32
+ */
33
+ getMax(): SplayTreeNode<T>;
34
+ }
35
+
36
+ /** Self-adjusting binary search tree. */
37
+ export class SplayTree<T = unknown> {
38
+ /** Returns `true` if `a` should be ordered before `b`. */
39
+ less: (a: T, b: T) => boolean;
40
+ /** Comparison function, or `null` if using `less`. */
41
+ compare: ((a: T, b: T) => number) | null;
42
+ /** Root node, or `null` if empty. */
43
+ root: SplayTreeNode<T> | null;
44
+ /** Number of elements. */
45
+ size: number;
46
+
47
+ /** @param options - Ordering functions. */
48
+ constructor(options?: SplayTreeOptions<T>);
49
+
50
+ /** Whether the tree has no elements. */
51
+ get isEmpty(): boolean;
52
+
53
+ /** Number of elements. */
54
+ get length(): number;
55
+
56
+ /**
57
+ * Find the node with the minimum value.
58
+ * @returns The minimum node.
59
+ */
60
+ getMin(): SplayTreeNode<T>;
61
+
62
+ /**
63
+ * Find the node with the maximum value.
64
+ * @returns The maximum node.
65
+ */
66
+ getMax(): SplayTreeNode<T>;
67
+
68
+ /**
69
+ * Find a node by value.
70
+ * @param value - Value to search for.
71
+ * @returns The matching node, or `null` if not found.
72
+ */
73
+ find(value: T): SplayTreeNode<T> | null;
74
+
75
+ /**
76
+ * Find and splay a node to the root.
77
+ * @param value - Value to search for and promote.
78
+ * @returns The matching node, or `null` if not found.
79
+ */
80
+ promote(value: T): SplayTreeNode<T> | null;
81
+
82
+ /**
83
+ * Splay a given node to the root.
84
+ * @param node - Node to splay.
85
+ * @returns `this` for chaining.
86
+ */
87
+ splay(node: SplayTreeNode<T>): this;
88
+
89
+ /**
90
+ * Insert a value. If the value already exists, it is splayed to the root.
91
+ * @param value - Value to insert.
92
+ * @returns `this` for chaining.
93
+ */
94
+ insert(value: T): this;
95
+
96
+ /**
97
+ * Remove a value from the tree.
98
+ * @param value - Value to remove.
99
+ * @returns `this` for chaining.
100
+ */
101
+ remove(value: T): this;
102
+
103
+ /**
104
+ * Remove all elements.
105
+ * @returns `this` for chaining.
106
+ */
107
+ clear(): this;
108
+
109
+ /**
110
+ * Split the tree: values ≤ `value` stay, values > `value` go to the returned tree.
111
+ * @param value - Split point.
112
+ * @returns A new SplayTree containing values greater than `value`.
113
+ */
114
+ splitMaxTree(value: T): SplayTree<T>;
115
+
116
+ /**
117
+ * Join another tree into this one (unsafe: assumes all values in `tree` are greater).
118
+ * @param tree - Tree to join (cleared after joining).
119
+ * @returns `this` for chaining.
120
+ */
121
+ joinMaxTreeUnsafe(tree: SplayTree<T>): this;
122
+
123
+ /**
124
+ * Join another tree into this one. Falls back to individual inserts if ranges overlap.
125
+ * @param tree - Tree to join (cleared after joining).
126
+ * @returns `this` for chaining.
127
+ */
128
+ join(tree: SplayTree<T>): this;
129
+
130
+ /** Iterate over values in ascending order. */
131
+ [Symbol.iterator](): IterableIterator<T>;
132
+
133
+ /**
134
+ * Iterate over values in descending order.
135
+ * @returns An iterable iterator.
136
+ */
137
+ getReverseIterator(): IterableIterator<T>;
138
+
139
+ /**
140
+ * Build a SplayTree from an iterable.
141
+ * @param values - Iterable of values to insert.
142
+ * @param options - Ordering functions.
143
+ * @returns A new SplayTree.
144
+ */
145
+ static from<T = unknown>(values: Iterable<T>, options?: SplayTreeOptions<T>): SplayTree<T>;
146
+
147
+ /** Default ordering functions. */
148
+ static defaults: SplayTreeOptions;
149
+ }
150
+
151
+ export default SplayTree;