list-toolkit 2.2.6 → 2.3.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/README.md +37 -54
- package/llms-full.txt +743 -0
- package/llms.txt +100 -0
- package/package.json +39 -30
- package/src/cache/cache-fifo.d.ts +6 -0
- package/src/cache/cache-fifo.js +7 -4
- package/src/cache/cache-lfu.d.ts +18 -0
- package/src/cache/cache-lfu.js +18 -6
- package/src/cache/cache-lru.d.ts +74 -0
- package/src/cache/cache-lru.js +60 -5
- package/src/cache/cache-random.d.ts +20 -0
- package/src/cache/cache-random.js +17 -6
- package/src/cache/decorator.d.ts +46 -0
- package/src/cache/decorator.js +26 -2
- package/src/cache.d.ts +13 -0
- package/src/cache.js +7 -2
- package/src/ext-list.d.ts +3 -0
- package/src/ext-list.js +0 -2
- package/src/ext-slist.d.ts +3 -0
- package/src/ext-slist.js +0 -2
- package/src/ext-value-list.d.ts +3 -0
- package/src/ext-value-list.js +0 -2
- package/src/ext-value-slist.d.ts +3 -0
- package/src/ext-value-slist.js +0 -2
- package/src/heap/basics.d.ts +89 -0
- package/src/heap/basics.js +42 -5
- package/src/heap/leftist-heap.d.ts +107 -0
- package/src/heap/leftist-heap.js +54 -4
- package/src/heap/min-heap.d.ts +264 -0
- package/src/heap/min-heap.js +186 -2
- package/src/heap/skew-heap.d.ts +105 -0
- package/src/heap/skew-heap.js +54 -2
- package/src/heap.d.ts +3 -0
- package/src/heap.js +0 -2
- package/src/list/basics.d.ts +43 -0
- package/src/list/basics.js +26 -8
- package/src/list/core.d.ts +271 -0
- package/src/list/core.js +162 -7
- package/src/list/ext-value.d.ts +253 -0
- package/src/list/ext-value.js +40 -6
- package/src/list/ext.d.ts +242 -0
- package/src/list/ext.js +149 -11
- package/src/list/nodes.d.ts +336 -0
- package/src/list/nodes.js +141 -3
- package/src/list/ptr.d.ts +72 -0
- package/src/list/ptr.js +44 -2
- package/src/list/value.d.ts +292 -0
- package/src/list/value.js +47 -6
- package/src/list-helpers.d.ts +44 -0
- package/src/list-helpers.js +36 -3
- package/src/list-utils.d.ts +141 -0
- package/src/list-utils.js +89 -3
- package/src/list.d.ts +3 -0
- package/src/list.js +0 -2
- package/src/meta-utils.d.ts +212 -0
- package/src/meta-utils.js +152 -1
- package/src/nt-utils.d.ts +91 -0
- package/src/nt-utils.js +65 -4
- package/src/queue.d.ts +74 -0
- package/src/queue.js +28 -2
- package/src/slist/basics.d.ts +47 -0
- package/src/slist/basics.js +23 -8
- package/src/slist/core.d.ts +251 -0
- package/src/slist/core.js +158 -10
- package/src/slist/ext-value.d.ts +188 -0
- package/src/slist/ext-value.js +36 -7
- package/src/slist/ext.d.ts +182 -0
- package/src/slist/ext.js +115 -13
- package/src/slist/nodes.d.ts +361 -0
- package/src/slist/nodes.js +156 -3
- package/src/slist/ptr.d.ts +73 -0
- package/src/slist/ptr.js +45 -2
- package/src/slist/value.d.ts +246 -0
- package/src/slist/value.js +38 -6
- package/src/slist.d.ts +3 -0
- package/src/slist.js +0 -2
- package/src/stack.d.ts +59 -0
- package/src/stack.js +29 -3
- package/src/tree/splay-tree.d.ts +151 -0
- package/src/tree/splay-tree.js +94 -3
- package/src/value-list.d.ts +3 -0
- package/src/value-list.js +0 -2
- package/src/value-slist.d.ts +3 -0
- package/src/value-slist.js +0 -2
- package/cjs/cache/cache-fifo.js +0 -37
- package/cjs/cache/cache-lfu.js +0 -76
- package/cjs/cache/cache-lru.js +0 -100
- package/cjs/cache/cache-random.js +0 -77
- package/cjs/cache/decorator.js +0 -47
- package/cjs/cache.js +0 -27
- package/cjs/ext-list.js +0 -21
- package/cjs/ext-slist.js +0 -21
- package/cjs/ext-value-list.js +0 -21
- package/cjs/ext-value-slist.js +0 -21
- package/cjs/heap/basics.js +0 -63
- package/cjs/heap/leftist-heap.js +0 -124
- package/cjs/heap/min-heap.js +0 -294
- package/cjs/heap/skew-heap.js +0 -114
- package/cjs/heap.js +0 -21
- package/cjs/list/basics.js +0 -88
- package/cjs/list/core.js +0 -305
- package/cjs/list/ext-value.js +0 -88
- package/cjs/list/ext.js +0 -356
- package/cjs/list/nodes.js +0 -240
- package/cjs/list/ptr.js +0 -61
- package/cjs/list/value.js +0 -99
- package/cjs/list-helpers.js +0 -91
- package/cjs/list-utils.js +0 -141
- package/cjs/list.js +0 -21
- package/cjs/meta-utils.js +0 -171
- package/cjs/nt-utils.js +0 -132
- package/cjs/package.json +0 -1
- package/cjs/queue.js +0 -58
- package/cjs/slist/basics.js +0 -71
- package/cjs/slist/core.js +0 -362
- package/cjs/slist/ext-value.js +0 -82
- package/cjs/slist/ext.js +0 -336
- package/cjs/slist/nodes.js +0 -276
- package/cjs/slist/ptr.js +0 -87
- package/cjs/slist/value.js +0 -90
- package/cjs/slist.js +0 -21
- package/cjs/stack.js +0 -55
- package/cjs/tree/splay-tree.js +0 -362
- package/cjs/value-list.js +0 -21
- package/cjs/value-slist.js +0 -21
package/src/slist/ext.js
CHANGED
|
@@ -1,39 +1,66 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
import {ExtListBase, PtrBase} from './nodes.js';
|
|
4
2
|
import {pop, extract, splice} from './basics.js';
|
|
5
3
|
import {addAliases, normalizeIterator} from '../meta-utils.js';
|
|
6
4
|
|
|
5
|
+
/** Pointer for navigating and mutating an external singly linked list. */
|
|
7
6
|
export class Ptr extends PtrBase {
|
|
7
|
+
/**
|
|
8
|
+
* @param {ExtSList|Ptr} list - Owning list or another Ptr to copy.
|
|
9
|
+
* @param {object} [node] - Target node.
|
|
10
|
+
* @param {object} [prev] - Node preceding the target.
|
|
11
|
+
*/
|
|
8
12
|
constructor(list, node, prev) {
|
|
9
13
|
super(list, node, prev, ExtSList);
|
|
10
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Create a copy of this pointer.
|
|
17
|
+
* @returns {Ptr} A new Ptr referencing the same list and node.
|
|
18
|
+
*/
|
|
11
19
|
clone() {
|
|
12
20
|
return new Ptr(this);
|
|
13
21
|
}
|
|
14
22
|
}
|
|
15
23
|
|
|
24
|
+
/** External (headless) node-based singly linked list. */
|
|
16
25
|
export class ExtSList extends ExtListBase {
|
|
26
|
+
/** A pointer-based range spanning all nodes, or `null` if empty. */
|
|
17
27
|
get ptrRange() {
|
|
18
28
|
return this.head ? {from: this.makePtr(), to: this.head, list: this.head} : null;
|
|
19
29
|
}
|
|
20
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Create a pointer to a node in this list.
|
|
33
|
+
* @param {object} [node] - Target node, or `undefined` for the head.
|
|
34
|
+
* @returns {Ptr} A new Ptr.
|
|
35
|
+
*/
|
|
21
36
|
makePtr(node) {
|
|
22
37
|
if (node && !this.isNodeLike(node)) throw new Error('"node" is not a compatible node');
|
|
23
38
|
return new Ptr(this, node);
|
|
24
39
|
}
|
|
25
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Create a pointer to the node after `prev`.
|
|
43
|
+
* @param {object} [prev] - Preceding node, or `undefined` for the head.
|
|
44
|
+
* @returns {Ptr} A new Ptr.
|
|
45
|
+
*/
|
|
26
46
|
makePtrFromPrev(prev) {
|
|
27
47
|
if (prev && !this.isNodeLike(prev)) throw new Error('"prev" is not a compatible node');
|
|
28
48
|
return new Ptr(this, null, prev || this.head);
|
|
29
49
|
}
|
|
30
50
|
|
|
31
|
-
|
|
32
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Remove the node after the head.
|
|
53
|
+
* @returns {object|null} The removed node, or `null` if empty.
|
|
54
|
+
*/
|
|
33
55
|
removeNodeAfter() {
|
|
34
56
|
return this.head ? this.removeNode(this.makePtr()) : null;
|
|
35
57
|
}
|
|
36
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Insert a value after the head.
|
|
61
|
+
* @param {*} value - Value or node to insert.
|
|
62
|
+
* @returns {Ptr} A Ptr to the inserted node.
|
|
63
|
+
*/
|
|
37
64
|
addAfter(value) {
|
|
38
65
|
const node = this.adoptValue(value);
|
|
39
66
|
if (this.head) {
|
|
@@ -44,6 +71,11 @@ export class ExtSList extends ExtListBase {
|
|
|
44
71
|
return this.makePtr();
|
|
45
72
|
}
|
|
46
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Insert an existing node after the head.
|
|
76
|
+
* @param {object} node - Node to insert.
|
|
77
|
+
* @returns {Ptr} A Ptr to the inserted node.
|
|
78
|
+
*/
|
|
47
79
|
addNodeAfter(node) {
|
|
48
80
|
node = this.adoptNode(node);
|
|
49
81
|
if (this.head) {
|
|
@@ -54,6 +86,11 @@ export class ExtSList extends ExtListBase {
|
|
|
54
86
|
return this.makePtr();
|
|
55
87
|
}
|
|
56
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Splice another external list's nodes after the head.
|
|
91
|
+
* @param {ExtListBase} extList - Compatible external list to consume.
|
|
92
|
+
* @returns {Ptr} A Ptr to the first inserted node.
|
|
93
|
+
*/
|
|
57
94
|
insertAfter(extList) {
|
|
58
95
|
if (!this.isCompatible(extList)) throw new Error('Incompatible lists');
|
|
59
96
|
|
|
@@ -66,6 +103,11 @@ export class ExtSList extends ExtListBase {
|
|
|
66
103
|
return this.makePtr();
|
|
67
104
|
}
|
|
68
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Move a node to just after the head.
|
|
108
|
+
* @param {Ptr} ptr - Pointer to the node to move.
|
|
109
|
+
* @returns {Ptr|ExtSList} A Ptr to the moved node, or `this`.
|
|
110
|
+
*/
|
|
69
111
|
moveAfter(ptr) {
|
|
70
112
|
if (!this.isCompatiblePtr(ptr)) throw new Error('Incompatible pointer');
|
|
71
113
|
ptr.list = this;
|
|
@@ -87,8 +129,11 @@ export class ExtSList extends ExtListBase {
|
|
|
87
129
|
return ptr.clone();
|
|
88
130
|
}
|
|
89
131
|
|
|
90
|
-
|
|
91
|
-
|
|
132
|
+
/**
|
|
133
|
+
* Remove all nodes.
|
|
134
|
+
* @param {boolean} [drop] - If `true`, make each removed node stand-alone.
|
|
135
|
+
* @returns {ExtSList} `this` for chaining.
|
|
136
|
+
*/
|
|
92
137
|
clear(drop) {
|
|
93
138
|
if (drop) {
|
|
94
139
|
for (const current of this.getNodeIterator()) {
|
|
@@ -99,6 +144,11 @@ export class ExtSList extends ExtListBase {
|
|
|
99
144
|
return this;
|
|
100
145
|
}
|
|
101
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Remove the node at a pointer position.
|
|
149
|
+
* @param {Ptr} ptr - Pointer whose current node to remove.
|
|
150
|
+
* @returns {object|null} The removed node, or `null` if empty.
|
|
151
|
+
*/
|
|
102
152
|
removeNode(ptr) {
|
|
103
153
|
if (!this.head) return null;
|
|
104
154
|
if (!this.isCompatiblePtr(ptr)) throw new Error('Incompatible pointer');
|
|
@@ -112,10 +162,21 @@ export class ExtSList extends ExtListBase {
|
|
|
112
162
|
return pop(this, ptr.prevNode).extracted.to;
|
|
113
163
|
}
|
|
114
164
|
|
|
165
|
+
/**
|
|
166
|
+
* Remove a pointer-based range and optionally drop nodes.
|
|
167
|
+
* @param {object} [ptrRange] - Range to remove.
|
|
168
|
+
* @param {boolean} [drop] - If `true`, make each removed node stand-alone.
|
|
169
|
+
* @returns {ExtSList} A new ExtSList containing the removed nodes.
|
|
170
|
+
*/
|
|
115
171
|
removeRange(ptrRange, drop) {
|
|
116
172
|
return this.extractRange(ptrRange).clear(drop);
|
|
117
173
|
}
|
|
118
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Extract a pointer-based range into a new list.
|
|
177
|
+
* @param {object} [ptrRange={}] - Range to extract (defaults to the whole list).
|
|
178
|
+
* @returns {ExtSList} A new ExtSList containing the extracted nodes.
|
|
179
|
+
*/
|
|
119
180
|
extractRange(ptrRange = {}) {
|
|
120
181
|
ptrRange = this.normalizePtrRange(ptrRange.from ? ptrRange : {...ptrRange, from: this.makePtr()});
|
|
121
182
|
ptrRange.to ||= this.head;
|
|
@@ -131,6 +192,11 @@ export class ExtSList extends ExtListBase {
|
|
|
131
192
|
return extracted;
|
|
132
193
|
}
|
|
133
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Extract nodes that satisfy a condition into a new list.
|
|
197
|
+
* @param {Function} condition - Predicate receiving each node.
|
|
198
|
+
* @returns {ExtSList} A new ExtSList containing the extracted nodes.
|
|
199
|
+
*/
|
|
134
200
|
extractBy(condition) {
|
|
135
201
|
const extracted = this.make();
|
|
136
202
|
if (this.isEmpty) return extracted;
|
|
@@ -154,6 +220,10 @@ export class ExtSList extends ExtListBase {
|
|
|
154
220
|
return extracted;
|
|
155
221
|
}
|
|
156
222
|
|
|
223
|
+
/**
|
|
224
|
+
* Reverse the order of all nodes in place.
|
|
225
|
+
* @returns {ExtSList} `this` for chaining.
|
|
226
|
+
*/
|
|
157
227
|
reverse() {
|
|
158
228
|
if (this.isOneOrEmpty) return this;
|
|
159
229
|
|
|
@@ -172,6 +242,11 @@ export class ExtSList extends ExtListBase {
|
|
|
172
242
|
return this;
|
|
173
243
|
}
|
|
174
244
|
|
|
245
|
+
/**
|
|
246
|
+
* Sort nodes in place using merge sort.
|
|
247
|
+
* @param {Function} lessFn - Returns `true` if `a` should precede `b`.
|
|
248
|
+
* @returns {ExtSList} `this` for chaining.
|
|
249
|
+
*/
|
|
175
250
|
sort(lessFn) {
|
|
176
251
|
if (this.isOneOrEmpty) return this;
|
|
177
252
|
|
|
@@ -237,8 +312,7 @@ export class ExtSList extends ExtListBase {
|
|
|
237
312
|
return this.next();
|
|
238
313
|
}
|
|
239
314
|
|
|
240
|
-
|
|
241
|
-
|
|
315
|
+
/** Iterate over nodes starting from the head. */
|
|
242
316
|
[Symbol.iterator]() {
|
|
243
317
|
let current = this.head,
|
|
244
318
|
readyToStop = this.isEmpty;
|
|
@@ -253,6 +327,11 @@ export class ExtSList extends ExtListBase {
|
|
|
253
327
|
});
|
|
254
328
|
}
|
|
255
329
|
|
|
330
|
+
/**
|
|
331
|
+
* Get an iterable over nodes in a range.
|
|
332
|
+
* @param {object} [range={}] - Sub-range to iterate.
|
|
333
|
+
* @returns {Iterable} An iterable iterator of nodes.
|
|
334
|
+
*/
|
|
256
335
|
getNodeIterator(range = {}) {
|
|
257
336
|
range = this.normalizeRange(range);
|
|
258
337
|
const {from, to} = range;
|
|
@@ -274,15 +353,20 @@ export class ExtSList extends ExtListBase {
|
|
|
274
353
|
};
|
|
275
354
|
}
|
|
276
355
|
|
|
277
|
-
|
|
278
|
-
|
|
356
|
+
/**
|
|
357
|
+
* Get an iterable of Ptr objects over a pointer range.
|
|
358
|
+
* @param {object} [ptrRange={}] - Sub-range to iterate.
|
|
359
|
+
* @returns {Iterable} An iterable iterator of Ptrs.
|
|
360
|
+
*/
|
|
361
|
+
getPtrIterator(ptrRange = {}) {
|
|
362
|
+
if (!ptrRange.from) ptrRange = Object.assign({from: this.makePtr(this.head)}, ptrRange);
|
|
279
363
|
ptrRange = this.normalizePtrRange(ptrRange);
|
|
280
364
|
const {from: fromPtr, to} = ptrRange;
|
|
281
365
|
return {
|
|
282
366
|
[Symbol.iterator]: () => {
|
|
283
367
|
let current = fromPtr.clone(),
|
|
284
368
|
readyToStop = this.isEmpty;
|
|
285
|
-
const stop = to ? to[this.nextName] : this;
|
|
369
|
+
const stop = to ? to[this.nextName] : this.head;
|
|
286
370
|
return normalizeIterator({
|
|
287
371
|
next: () => {
|
|
288
372
|
if (readyToStop && current.node === stop) return {done: true};
|
|
@@ -296,20 +380,38 @@ export class ExtSList extends ExtListBase {
|
|
|
296
380
|
};
|
|
297
381
|
}
|
|
298
382
|
|
|
299
|
-
|
|
300
|
-
|
|
383
|
+
/**
|
|
384
|
+
* Create a shallow clone of this list.
|
|
385
|
+
* @returns {ExtSList} A new ExtSList pointing to the same head.
|
|
386
|
+
*/
|
|
301
387
|
clone() {
|
|
302
388
|
return new ExtSList(this);
|
|
303
389
|
}
|
|
304
390
|
|
|
391
|
+
/**
|
|
392
|
+
* Create an empty list with the same options.
|
|
393
|
+
* @param {object|null} [head=null] - Optional initial head node.
|
|
394
|
+
* @returns {ExtSList} A new ExtSList.
|
|
395
|
+
*/
|
|
305
396
|
make(head = null) {
|
|
306
397
|
return new ExtSList(head, this);
|
|
307
398
|
}
|
|
308
399
|
|
|
400
|
+
/**
|
|
401
|
+
* Create a list from values with the same options.
|
|
402
|
+
* @param {Iterable} values - Iterable of node objects.
|
|
403
|
+
* @returns {ExtSList} A new ExtSList.
|
|
404
|
+
*/
|
|
309
405
|
makeFrom(values) {
|
|
310
406
|
return ExtSList.from(values, this);
|
|
311
407
|
}
|
|
312
408
|
|
|
409
|
+
/**
|
|
410
|
+
* Build an ExtSList from an iterable of node objects.
|
|
411
|
+
* @param {Iterable} values - Iterable of nodes.
|
|
412
|
+
* @param {object} [options] - Link property names.
|
|
413
|
+
* @returns {ExtSList} A new ExtSList.
|
|
414
|
+
*/
|
|
313
415
|
static from(values, options) {
|
|
314
416
|
const list = new ExtSList(null, options);
|
|
315
417
|
for (const value of values) {
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
/** Options for configuring SLL link property names. */
|
|
2
|
+
export interface SllOptions {
|
|
3
|
+
/** Property name used for the next link. */
|
|
4
|
+
nextName?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** A range of nodes in a singly linked list. */
|
|
8
|
+
export interface SllRange<T extends object = object> {
|
|
9
|
+
/** First node of the range (or a Ptr). */
|
|
10
|
+
from?: T | PtrBase<T>;
|
|
11
|
+
/** Last node of the range (or a Ptr). */
|
|
12
|
+
to?: T | PtrBase<T>;
|
|
13
|
+
/** Owning list for validation. */
|
|
14
|
+
list?: HeadNode | ExtListBase<T>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A pointer-based range where `from` must be a Ptr. */
|
|
18
|
+
export interface SllPtrRange<T extends object = object> {
|
|
19
|
+
/** Pointer to the first node. */
|
|
20
|
+
from: PtrBase<T>;
|
|
21
|
+
/** Last node of the range (or a Ptr). */
|
|
22
|
+
to?: T | PtrBase<T>;
|
|
23
|
+
/** Owning list for validation. */
|
|
24
|
+
list?: HeadNode | ExtListBase<T>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Check whether a node has a valid next link.
|
|
29
|
+
* @param options - Link property names.
|
|
30
|
+
* @param node - Node to check.
|
|
31
|
+
* @returns `true` if the node has a truthy next link.
|
|
32
|
+
*/
|
|
33
|
+
export function isNodeLike<T extends object>(options: SllOptions, node: T): boolean;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Check whether a node points to itself (stand-alone).
|
|
37
|
+
* @param options - Link property names.
|
|
38
|
+
* @param node - Node to check.
|
|
39
|
+
* @returns `true` if the node's next link points to itself.
|
|
40
|
+
*/
|
|
41
|
+
export function isStandAlone<T extends object>(options: SllOptions, node: T): boolean;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Check whether two option sets share the same link property name.
|
|
45
|
+
* @param options1 - First options.
|
|
46
|
+
* @param options2 - Second options.
|
|
47
|
+
* @returns `true` if `nextName` matches.
|
|
48
|
+
*/
|
|
49
|
+
export function isCompatible(options1: SllOptions, options2: SllOptions): boolean;
|
|
50
|
+
|
|
51
|
+
/** Singly linked list node with a circular self-link. */
|
|
52
|
+
export class Node {
|
|
53
|
+
/** Property name for the next link. */
|
|
54
|
+
nextName: string;
|
|
55
|
+
|
|
56
|
+
/** @param options - Link property names. */
|
|
57
|
+
constructor(options?: SllOptions);
|
|
58
|
+
|
|
59
|
+
/** Whether this node's next link points to itself. */
|
|
60
|
+
get isStandAlone(): boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Sentinel head node for hosted singly linked lists. */
|
|
64
|
+
export class HeadNode extends Node {
|
|
65
|
+
/** Pointer to the last node in the list. */
|
|
66
|
+
last: Node | object;
|
|
67
|
+
|
|
68
|
+
/** @param options - Link property names. */
|
|
69
|
+
constructor(options?: SllOptions);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check whether a value looks like a compatible node.
|
|
73
|
+
* @param node - Value to check.
|
|
74
|
+
* @returns `true` if the value has a valid next link.
|
|
75
|
+
*/
|
|
76
|
+
isNodeLike(node: unknown): boolean;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Check whether another options object shares the same link names.
|
|
80
|
+
* @param options - Options to compare.
|
|
81
|
+
* @returns `true` if `nextName` matches.
|
|
82
|
+
*/
|
|
83
|
+
isCompatibleNames(options: SllOptions): boolean;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Check whether another list is compatible with this one.
|
|
87
|
+
* @param list - List to compare.
|
|
88
|
+
* @returns `true` if compatible.
|
|
89
|
+
*/
|
|
90
|
+
isCompatible(list: HeadNode | ExtListBase): boolean;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Check whether a pointer belongs to a compatible list.
|
|
94
|
+
* @param ptr - Pointer to check.
|
|
95
|
+
* @returns `true` if compatible.
|
|
96
|
+
*/
|
|
97
|
+
isCompatiblePtr(ptr: PtrBase): boolean;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Check whether a range is compatible with this list.
|
|
101
|
+
* @param range - Range to validate.
|
|
102
|
+
* @returns `true` if compatible.
|
|
103
|
+
*/
|
|
104
|
+
isCompatibleRange<T extends object>(range: SllRange<T>): boolean;
|
|
105
|
+
|
|
106
|
+
/** Whether the list has no nodes. */
|
|
107
|
+
get isEmpty(): boolean;
|
|
108
|
+
|
|
109
|
+
/** Whether the list has exactly one node. */
|
|
110
|
+
get isOne(): boolean;
|
|
111
|
+
|
|
112
|
+
/** Whether the list has zero or one node. */
|
|
113
|
+
get isOneOrEmpty(): boolean;
|
|
114
|
+
|
|
115
|
+
/** The head sentinel itself. */
|
|
116
|
+
get head(): this;
|
|
117
|
+
|
|
118
|
+
/** The first node after the head. */
|
|
119
|
+
get front(): object;
|
|
120
|
+
|
|
121
|
+
/** The last node in the list. */
|
|
122
|
+
get back(): object;
|
|
123
|
+
|
|
124
|
+
/** A range spanning all nodes, or `null` if empty. */
|
|
125
|
+
get range(): SllRange | null;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Count the number of nodes.
|
|
129
|
+
* @returns The node count.
|
|
130
|
+
*/
|
|
131
|
+
getLength(): number;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Adopt a node or pointer, making it stand-alone if needed.
|
|
135
|
+
* @param nodeOrPtr - Node or pointer to adopt.
|
|
136
|
+
* @returns The adopted node.
|
|
137
|
+
*/
|
|
138
|
+
adoptNode<T extends object>(nodeOrPtr: T | PtrBase<T>): T;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Adopt a value into this list. Overridden by value-list subclasses.
|
|
142
|
+
* @param nodeOrPtr - Node, pointer, or value to adopt.
|
|
143
|
+
* @returns The adopted node.
|
|
144
|
+
*/
|
|
145
|
+
adoptValue(nodeOrPtr: any): any;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Normalize a node or pointer to a plain node.
|
|
149
|
+
* @param nodeOrPtr - Node or pointer.
|
|
150
|
+
* @returns The underlying node, or `null`.
|
|
151
|
+
*/
|
|
152
|
+
normalizeNode<T extends object>(nodeOrPtr: T | PtrBase<T> | null): T | null;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Normalize a range, resolving any pointers to nodes.
|
|
156
|
+
* @param range - Range to normalize.
|
|
157
|
+
* @returns The normalized range, or `null`.
|
|
158
|
+
*/
|
|
159
|
+
normalizeRange<T extends object>(range?: SllRange<T> | null): SllRange<T> | null;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Normalize a pointer-based range.
|
|
163
|
+
* @param range - Pointer range to normalize.
|
|
164
|
+
* @returns The normalized pointer range, or `null`.
|
|
165
|
+
*/
|
|
166
|
+
normalizePtrRange<T extends object>(range?: SllPtrRange<T> | null): SllPtrRange<T> | null;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Recalculate the `last` pointer by traversing the list.
|
|
170
|
+
* @returns `this` for chaining.
|
|
171
|
+
*/
|
|
172
|
+
syncLast(): this;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Value wrapper node for singly linked lists. */
|
|
176
|
+
export class ValueNode<V = unknown> extends Node {
|
|
177
|
+
/** The wrapped value. */
|
|
178
|
+
value: V;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @param value - Value to wrap.
|
|
182
|
+
* @param options - Link property names.
|
|
183
|
+
*/
|
|
184
|
+
constructor(value: V, options?: SllOptions);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Base class for SLL pointers providing navigation. */
|
|
188
|
+
export class PtrBase<T extends object = object> {
|
|
189
|
+
/** The list this pointer belongs to. */
|
|
190
|
+
list: HeadNode | ExtListBase<T>;
|
|
191
|
+
/** The current node. */
|
|
192
|
+
node: T;
|
|
193
|
+
/** The node preceding the current node. */
|
|
194
|
+
prevNode: T;
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param list - Owning list or another PtrBase to copy.
|
|
198
|
+
* @param node - Target node.
|
|
199
|
+
* @param prev - Preceding node.
|
|
200
|
+
* @param ListClass - Expected list constructor for validation.
|
|
201
|
+
*/
|
|
202
|
+
constructor(list: PtrBase<T> | HeadNode | ExtListBase<T>, node?: T | PtrBase<T>, prev?: T, ListClass?: Function);
|
|
203
|
+
|
|
204
|
+
/** The node after the current node. */
|
|
205
|
+
get nextNode(): T;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Check and repair the prevNode link.
|
|
209
|
+
* @returns `true` if prevNode is valid.
|
|
210
|
+
*/
|
|
211
|
+
isPrevNodeValid(): boolean;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Advance to the next node.
|
|
215
|
+
* @returns `this` for chaining.
|
|
216
|
+
*/
|
|
217
|
+
next(): this;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Move to the previous node.
|
|
221
|
+
* @returns `this` for chaining.
|
|
222
|
+
* @throws If prevNode is invalid.
|
|
223
|
+
*/
|
|
224
|
+
prev(): this;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Synchronize prevNode by scanning forward.
|
|
228
|
+
* @returns `this` for chaining.
|
|
229
|
+
*/
|
|
230
|
+
syncPrev(): this;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Base class for external (headless) singly linked lists. */
|
|
234
|
+
export class ExtListBase<T extends object = object> {
|
|
235
|
+
/** Current head node, or `null` if empty. */
|
|
236
|
+
head: T | null;
|
|
237
|
+
/** Property name for the next link. */
|
|
238
|
+
nextName: string;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @param head - Initial head node, ExtListBase, or PtrBase.
|
|
242
|
+
* @param options - Link property names.
|
|
243
|
+
*/
|
|
244
|
+
constructor(head?: T | ExtListBase<T> | PtrBase<T> | null, options?: SllOptions);
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Check whether another list is compatible.
|
|
248
|
+
* @param list - List to compare.
|
|
249
|
+
* @returns `true` if compatible.
|
|
250
|
+
*/
|
|
251
|
+
isCompatible(list: ExtListBase | HeadNode): boolean;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Check whether a pointer belongs to a compatible list.
|
|
255
|
+
* @param ptr - Pointer to check.
|
|
256
|
+
* @returns `true` if compatible.
|
|
257
|
+
*/
|
|
258
|
+
isCompatiblePtr(ptr: PtrBase): boolean;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Check whether a value looks like a compatible node.
|
|
262
|
+
* @param node - Value to check.
|
|
263
|
+
* @returns `true` if the value has a valid next link.
|
|
264
|
+
*/
|
|
265
|
+
isNodeLike(node: unknown): boolean;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Check whether another options object shares the same link names.
|
|
269
|
+
* @param options - Options to compare.
|
|
270
|
+
* @returns `true` if `nextName` matches.
|
|
271
|
+
*/
|
|
272
|
+
isCompatibleNames(options: SllOptions): boolean;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Check whether a range is compatible with this list.
|
|
276
|
+
* @param range - Range to validate.
|
|
277
|
+
* @returns `true` if compatible.
|
|
278
|
+
*/
|
|
279
|
+
isCompatibleRange<T extends object>(range: SllRange<T>): boolean;
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Normalize a node or pointer to a plain node.
|
|
283
|
+
* @param nodeOrPtr - Node or pointer.
|
|
284
|
+
* @returns The underlying node, or `null`.
|
|
285
|
+
*/
|
|
286
|
+
normalizeNode<T extends object>(nodeOrPtr: T | PtrBase<T> | null): T | null;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Normalize a range, resolving any pointers to nodes.
|
|
290
|
+
* @param range - Range to normalize.
|
|
291
|
+
* @returns The normalized range, or `null`.
|
|
292
|
+
*/
|
|
293
|
+
normalizeRange<T extends object>(range?: SllRange<T> | null): SllRange<T> | null;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Normalize a pointer-based range.
|
|
297
|
+
* @param range - Pointer range to normalize.
|
|
298
|
+
* @returns The normalized pointer range, or `null`.
|
|
299
|
+
*/
|
|
300
|
+
normalizePtrRange<T extends object>(range?: SllPtrRange<T> | null): SllPtrRange<T> | null;
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Adopt a node or pointer, making it stand-alone if needed.
|
|
304
|
+
* @param nodeOrPtr - Node or pointer to adopt.
|
|
305
|
+
* @returns The adopted node.
|
|
306
|
+
*/
|
|
307
|
+
adoptNode<T extends object>(nodeOrPtr: T | PtrBase<T>): T;
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Adopt a value into this list. Overridden by value-list subclasses.
|
|
311
|
+
* @param nodeOrPtr - Node, pointer, or value to adopt.
|
|
312
|
+
* @returns The adopted node.
|
|
313
|
+
*/
|
|
314
|
+
adoptValue(nodeOrPtr: any): any;
|
|
315
|
+
|
|
316
|
+
/** Whether the list has no nodes. */
|
|
317
|
+
get isEmpty(): boolean;
|
|
318
|
+
|
|
319
|
+
/** Whether the list has exactly one node. */
|
|
320
|
+
get isOne(): boolean;
|
|
321
|
+
|
|
322
|
+
/** Whether the list has zero or one node. */
|
|
323
|
+
get isOneOrEmpty(): boolean;
|
|
324
|
+
|
|
325
|
+
/** The head node, or `null` if empty. */
|
|
326
|
+
get front(): T | null;
|
|
327
|
+
|
|
328
|
+
/** A range spanning all nodes, or `null` if empty. */
|
|
329
|
+
get range(): SllRange<T> | null;
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Count the number of nodes.
|
|
333
|
+
* @returns The node count.
|
|
334
|
+
*/
|
|
335
|
+
getLength(): number;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Get the last node by traversal.
|
|
339
|
+
* @returns The last node, or `null` if empty.
|
|
340
|
+
*/
|
|
341
|
+
getBack(): T | null;
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Set a new head node.
|
|
345
|
+
* @param head - New head node, pointer, or `null`.
|
|
346
|
+
* @returns The previous head node.
|
|
347
|
+
*/
|
|
348
|
+
attach(head?: T | PtrBase<T> | null): T | null;
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Remove the head reference without modifying nodes.
|
|
352
|
+
* @returns The previous head node.
|
|
353
|
+
*/
|
|
354
|
+
detach(): T | null;
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Advance the head to the next node.
|
|
358
|
+
* @returns `this` for chaining.
|
|
359
|
+
*/
|
|
360
|
+
next(): this;
|
|
361
|
+
}
|