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.
- package/README.md +40 -37
- package/llms-full.txt +743 -0
- package/llms.txt +100 -0
- package/package.json +34 -29
- 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 -2
- package/src/heap/min-heap.d.ts +270 -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 +148 -10
- 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 +151 -6
- package/src/slist/ext-value.d.ts +188 -0
- package/src/slist/ext-value.js +35 -6
- package/src/slist/ext.d.ts +182 -0
- package/src/slist/ext.js +114 -12
- 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
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {PtrBase, HeadNode} from './nodes.js';
|
|
2
|
+
|
|
3
|
+
/** Pointer for navigating and mutating a hosted DLL ({@link List}). */
|
|
4
|
+
export class Ptr<T extends object = object> extends PtrBase<T> {
|
|
5
|
+
/** The hosted list this pointer belongs to. */
|
|
6
|
+
list: HeadNode;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param list - Owning list or another Ptr to copy.
|
|
10
|
+
* @param node - Target node.
|
|
11
|
+
*/
|
|
12
|
+
constructor(list: HeadNode | Ptr<T>, node?: T);
|
|
13
|
+
|
|
14
|
+
/** Whether the pointer is on the sentinel head node. */
|
|
15
|
+
get isHead(): boolean;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Create a copy of this pointer.
|
|
19
|
+
* @returns A new Ptr referencing the same list and node.
|
|
20
|
+
*/
|
|
21
|
+
clone(): Ptr<T>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Remove the current node and advance to the next.
|
|
25
|
+
* @returns The removed node, or `null` if on the head.
|
|
26
|
+
*/
|
|
27
|
+
removeCurrent(): T | null;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Insert a value before the current node.
|
|
31
|
+
* @param value - Value to insert (adopted via the list).
|
|
32
|
+
* @returns A Ptr to the newly inserted node.
|
|
33
|
+
*/
|
|
34
|
+
addBefore(value: T | PtrBase<T>): Ptr<T>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Insert an already-prepared node before the current node.
|
|
38
|
+
* @param node - Node or pointer to insert.
|
|
39
|
+
* @returns A Ptr to the inserted node.
|
|
40
|
+
*/
|
|
41
|
+
addNodeBefore(node: T | PtrBase<T>): Ptr<T>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Insert a value after the current node.
|
|
45
|
+
* @param value - Value to insert (adopted via the list).
|
|
46
|
+
* @returns A Ptr to the newly inserted node.
|
|
47
|
+
*/
|
|
48
|
+
addAfter(value: T | PtrBase<T>): Ptr<T>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Insert an already-prepared node after the current node.
|
|
52
|
+
* @param node - Node or pointer to insert.
|
|
53
|
+
* @returns A Ptr to the inserted node.
|
|
54
|
+
*/
|
|
55
|
+
addNodeAfter(node: T | PtrBase<T>): Ptr<T>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Splice another list's nodes before the current node.
|
|
59
|
+
* @param list - Compatible hosted list to consume.
|
|
60
|
+
* @returns A Ptr to the first inserted node, or `null` if the list was empty.
|
|
61
|
+
*/
|
|
62
|
+
insertBefore(list: HeadNode): Ptr<T> | null;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Splice another list's nodes after the current node.
|
|
66
|
+
* @param list - Compatible hosted list to consume.
|
|
67
|
+
* @returns A Ptr to the first inserted node, or `null` if the list was empty.
|
|
68
|
+
*/
|
|
69
|
+
insertAfter(list: HeadNode): Ptr<T> | null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export default Ptr;
|
package/src/list/ptr.js
CHANGED
|
@@ -1,44 +1,81 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
import {HeadNode, PtrBase} from './nodes.js';
|
|
4
2
|
import {pop, splice} from './basics.js';
|
|
5
3
|
|
|
4
|
+
/** Pointer for navigating and mutating a hosted doubly linked list. */
|
|
6
5
|
export class Ptr extends PtrBase {
|
|
6
|
+
/**
|
|
7
|
+
* @param {HeadNode|Ptr} list - Owning list or another Ptr to copy.
|
|
8
|
+
* @param {object} [node] - Target node.
|
|
9
|
+
*/
|
|
7
10
|
constructor(list, node) {
|
|
8
11
|
super(list, node, HeadNode);
|
|
9
12
|
}
|
|
13
|
+
/** Whether the pointer is at the head sentinel. */
|
|
10
14
|
get isHead() {
|
|
11
15
|
return this.node === this.list;
|
|
12
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a copy of this pointer.
|
|
19
|
+
* @returns {Ptr} A new Ptr referencing the same list and node.
|
|
20
|
+
*/
|
|
13
21
|
clone() {
|
|
14
22
|
return new Ptr(this);
|
|
15
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Remove the current node and advance to the next.
|
|
26
|
+
* @returns {object|null} The removed node, or `null` if at head.
|
|
27
|
+
*/
|
|
16
28
|
removeCurrent() {
|
|
17
29
|
if (this.node === this.list) return null;
|
|
18
30
|
const node = this.node;
|
|
19
31
|
this.node = node[this.list.nextName];
|
|
20
32
|
return pop(this.list, node).extracted;
|
|
21
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Insert a value before the current node.
|
|
36
|
+
* @param {*} value - Value or node to insert.
|
|
37
|
+
* @returns {Ptr} A Ptr to the inserted node.
|
|
38
|
+
*/
|
|
22
39
|
addBefore(value) {
|
|
23
40
|
const node = this.list.adoptValue(value);
|
|
24
41
|
splice(this.list, this.node[this.list.prevName], node);
|
|
25
42
|
return this.list.makePtr(node);
|
|
26
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Insert an existing node before the current node.
|
|
46
|
+
* @param {object} node - Node to insert.
|
|
47
|
+
* @returns {Ptr} A Ptr to the inserted node.
|
|
48
|
+
*/
|
|
27
49
|
addNodeBefore(node) {
|
|
28
50
|
node = this.list.adoptNode(node);
|
|
29
51
|
splice(this.list, this.node[this.list.prevName], node);
|
|
30
52
|
return this.list.makePtr(node);
|
|
31
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Insert a value after the current node.
|
|
56
|
+
* @param {*} value - Value or node to insert.
|
|
57
|
+
* @returns {Ptr} A Ptr to the inserted node.
|
|
58
|
+
*/
|
|
32
59
|
addAfter(value) {
|
|
33
60
|
const node = this.list.adoptValue(value);
|
|
34
61
|
splice(this.list, this.node, node);
|
|
35
62
|
return this.list.makePtr(node);
|
|
36
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Insert an existing node after the current node.
|
|
66
|
+
* @param {object} node - Node to insert.
|
|
67
|
+
* @returns {Ptr} A Ptr to the inserted node.
|
|
68
|
+
*/
|
|
37
69
|
addNodeAfter(node) {
|
|
38
70
|
node = this.list.adoptNode(node);
|
|
39
71
|
splice(this.list, this.node, node);
|
|
40
72
|
return this.list.makePtr(node);
|
|
41
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Splice another list's nodes before the current node.
|
|
76
|
+
* @param {HeadNode} list - Compatible list to consume.
|
|
77
|
+
* @returns {Ptr|null} A Ptr to the first inserted node, or `null` if empty.
|
|
78
|
+
*/
|
|
42
79
|
insertBefore(list) {
|
|
43
80
|
if (!this.list.isCompatible(list)) throw new Error('Incompatible lists');
|
|
44
81
|
if (list.isEmpty) return null;
|
|
@@ -46,6 +83,11 @@ export class Ptr extends PtrBase {
|
|
|
46
83
|
splice(this.list, this.node[this.list.prevName], head);
|
|
47
84
|
return this.list.makePtr(head);
|
|
48
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Splice another list's nodes after the current node.
|
|
88
|
+
* @param {HeadNode} list - Compatible list to consume.
|
|
89
|
+
* @returns {Ptr|null} A Ptr to the first inserted node, or `null` if empty.
|
|
90
|
+
*/
|
|
49
91
|
insertAfter(list) {
|
|
50
92
|
if (!this.list.isCompatible(list)) throw new Error('Incompatible lists');
|
|
51
93
|
if (list.isEmpty) return null;
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import {HeadNode, ValueNode, PtrBase, DllOptions, DllRange} from './nodes.js';
|
|
2
|
+
import {Ptr} from './ptr.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hosted value-based doubly linked list. Wraps values in {@link ValueNode}.
|
|
6
|
+
*
|
|
7
|
+
* Extends `List` at runtime. Internally nodes are `ValueNode<V>`.
|
|
8
|
+
* The default iterator and `getIterator`/`getReverseIterator` yield
|
|
9
|
+
* unwrapped `V` values; use `getNodeIterator`/`getReverseNodeIterator`
|
|
10
|
+
* for `ValueNode<V>` nodes.
|
|
11
|
+
*/
|
|
12
|
+
export class ValueList<V = unknown> extends HeadNode {
|
|
13
|
+
/** The first ValueNode after the head. */
|
|
14
|
+
get front(): ValueNode<V>;
|
|
15
|
+
|
|
16
|
+
/** The last ValueNode before the head. */
|
|
17
|
+
get back(): ValueNode<V>;
|
|
18
|
+
|
|
19
|
+
/** A range spanning all nodes, or `null` if empty. */
|
|
20
|
+
get range(): DllRange<ValueNode<V>> | null;
|
|
21
|
+
|
|
22
|
+
/** Pointer to the first node. */
|
|
23
|
+
get frontPtr(): Ptr<ValueNode<V>>;
|
|
24
|
+
|
|
25
|
+
/** Pointer to the last node. */
|
|
26
|
+
get backPtr(): Ptr<ValueNode<V>>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Create a pointer to a node in this list.
|
|
30
|
+
* @param node - Target node, or `undefined` for the front.
|
|
31
|
+
* @returns A new Ptr.
|
|
32
|
+
*/
|
|
33
|
+
makePtr(node?: ValueNode<V>): Ptr<ValueNode<V>>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Create a pointer to the node after `prev`.
|
|
37
|
+
* @param prev - Preceding node, or `undefined` for the front.
|
|
38
|
+
* @returns A new Ptr.
|
|
39
|
+
*/
|
|
40
|
+
makePtrFromPrev(prev?: ValueNode<V>): Ptr<ValueNode<V>>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Remove and return the front value.
|
|
44
|
+
* @returns The unwrapped value, or `undefined` if empty.
|
|
45
|
+
*/
|
|
46
|
+
popFront(): V | undefined;
|
|
47
|
+
|
|
48
|
+
/** Alias for {@link popFront}. */
|
|
49
|
+
pop(): V | undefined;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Remove and return the back value.
|
|
53
|
+
* @returns The unwrapped value, or `undefined` if empty.
|
|
54
|
+
*/
|
|
55
|
+
popBack(): V | undefined;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Remove and return the front ValueNode.
|
|
59
|
+
* @returns The removed node, or `undefined` if empty.
|
|
60
|
+
*/
|
|
61
|
+
popFrontNode(): ValueNode<V> | undefined;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Remove and return the back ValueNode.
|
|
65
|
+
* @returns The removed node, or `undefined` if empty.
|
|
66
|
+
*/
|
|
67
|
+
popBackNode(): ValueNode<V> | undefined;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Insert a value at the front.
|
|
71
|
+
* @param value - Raw value, ValueNode, or Ptr.
|
|
72
|
+
* @returns A Ptr to the inserted node.
|
|
73
|
+
*/
|
|
74
|
+
pushFront(value: V | ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
|
|
75
|
+
|
|
76
|
+
/** Alias for {@link pushFront}. */
|
|
77
|
+
push(value: V | ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Insert a value at the back.
|
|
81
|
+
* @param value - Raw value, ValueNode, or Ptr.
|
|
82
|
+
* @returns A Ptr to the inserted node.
|
|
83
|
+
*/
|
|
84
|
+
pushBack(value: V | ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Insert an existing ValueNode at the front.
|
|
88
|
+
* @param nodeOrPtr - ValueNode or pointer to insert.
|
|
89
|
+
* @returns A Ptr to the inserted node.
|
|
90
|
+
*/
|
|
91
|
+
pushFrontNode(nodeOrPtr: ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Insert an existing ValueNode at the back.
|
|
95
|
+
* @param nodeOrPtr - ValueNode or pointer to insert.
|
|
96
|
+
* @returns A Ptr to the inserted node.
|
|
97
|
+
*/
|
|
98
|
+
pushBackNode(nodeOrPtr: ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Move all nodes from another list to the front.
|
|
102
|
+
* @param list - Compatible list to consume.
|
|
103
|
+
* @returns A Ptr to the new front.
|
|
104
|
+
*/
|
|
105
|
+
appendFront(list: HeadNode): Ptr<ValueNode<V>>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Move all nodes from another list to the back.
|
|
109
|
+
* @param list - Compatible list to consume.
|
|
110
|
+
* @returns A Ptr to the first appended node.
|
|
111
|
+
*/
|
|
112
|
+
appendBack(list: HeadNode): Ptr<ValueNode<V>>;
|
|
113
|
+
|
|
114
|
+
/** Alias for {@link appendBack}. */
|
|
115
|
+
append(list: HeadNode): Ptr<ValueNode<V>>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Move a node to the front of the list.
|
|
119
|
+
* @param nodeOrPtr - Node or pointer to move.
|
|
120
|
+
* @returns A Ptr to the front.
|
|
121
|
+
*/
|
|
122
|
+
moveToFront(nodeOrPtr: ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Move a node to the back of the list.
|
|
126
|
+
* @param nodeOrPtr - Node or pointer to move.
|
|
127
|
+
* @returns A Ptr to the back.
|
|
128
|
+
*/
|
|
129
|
+
moveToBack(nodeOrPtr: ValueNode<V> | PtrBase<ValueNode<V>>): Ptr<ValueNode<V>>;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Remove all nodes.
|
|
133
|
+
* @param drop - If `true`, pop nodes one by one (making each stand-alone).
|
|
134
|
+
* @returns `this` for chaining.
|
|
135
|
+
*/
|
|
136
|
+
clear(drop?: boolean): this;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Remove a single node from the list.
|
|
140
|
+
* @param nodeOrPtr - Node or pointer to remove.
|
|
141
|
+
* @returns The removed node.
|
|
142
|
+
*/
|
|
143
|
+
removeNode(nodeOrPtr: ValueNode<V> | PtrBase<ValueNode<V>>): ValueNode<V>;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Remove a range of nodes and optionally drop them.
|
|
147
|
+
* @param range - Range to remove.
|
|
148
|
+
* @param drop - If `true`, make each removed node stand-alone.
|
|
149
|
+
* @returns A new ValueList containing the removed values.
|
|
150
|
+
*/
|
|
151
|
+
removeRange(range?: DllRange<ValueNode<V>>, drop?: boolean): ValueList<V>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Extract a range of nodes into a new list.
|
|
155
|
+
* @param range - Range to extract (defaults to the whole list).
|
|
156
|
+
* @returns A new ValueList containing the extracted values.
|
|
157
|
+
*/
|
|
158
|
+
extractRange(range?: DllRange<ValueNode<V>>): ValueList<V>;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Extract nodes that satisfy a condition into a new list.
|
|
162
|
+
* @param condition - Predicate receiving each ValueNode.
|
|
163
|
+
* @returns A new ValueList containing the extracted values.
|
|
164
|
+
*/
|
|
165
|
+
extractBy(condition: (node: ValueNode<V>) => boolean): ValueList<V>;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Reverse the order of all nodes in place.
|
|
169
|
+
* @returns `this` for chaining.
|
|
170
|
+
*/
|
|
171
|
+
reverse(): this;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Sort nodes in place using merge sort.
|
|
175
|
+
* @param lessFn - Comparison receiving ValueNodes, returns `true` if `a` should precede `b`.
|
|
176
|
+
* @returns `this` for chaining.
|
|
177
|
+
*/
|
|
178
|
+
sort(lessFn: (a: ValueNode<V>, b: ValueNode<V>) => boolean): this;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Detach all nodes as a raw circular list (no sentinel).
|
|
182
|
+
* @returns Head of the raw circular list, or `null` if empty.
|
|
183
|
+
*/
|
|
184
|
+
releaseRawList(): ValueNode<V> | null;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Detach all nodes as a null-terminated list.
|
|
188
|
+
* @returns Object with `head` and `tail`, or `null` if empty.
|
|
189
|
+
*/
|
|
190
|
+
releaseNTList(): {head: ValueNode<V>; tail: ValueNode<V>} | null;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Validate that a range is reachable within this list.
|
|
194
|
+
* @param range - Range to validate.
|
|
195
|
+
* @returns `true` if the range is valid.
|
|
196
|
+
*/
|
|
197
|
+
validateRange(range?: DllRange<ValueNode<V>>): boolean;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Adopt a value, pointer, or ValueNode into this list.
|
|
201
|
+
* @param value - Raw value, Ptr, or ValueNode.
|
|
202
|
+
* @returns A ValueNode ready for insertion.
|
|
203
|
+
*/
|
|
204
|
+
adoptValue(value: V | ValueNode<V> | PtrBase<ValueNode<V>>): ValueNode<V>;
|
|
205
|
+
|
|
206
|
+
/** Iterate over unwrapped values from front to back. */
|
|
207
|
+
[Symbol.iterator](): IterableIterator<V>;
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Get an iterable over ValueNodes in a range.
|
|
211
|
+
* @param range - Sub-range to iterate.
|
|
212
|
+
* @returns An iterable iterator of ValueNodes.
|
|
213
|
+
*/
|
|
214
|
+
getNodeIterator(range?: DllRange<ValueNode<V>>): IterableIterator<ValueNode<V>>;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Get an iterable over unwrapped values in a range.
|
|
218
|
+
* @param range - Sub-range to iterate.
|
|
219
|
+
* @returns An iterable iterator of values.
|
|
220
|
+
*/
|
|
221
|
+
getValueIterator(range?: DllRange<ValueNode<V>>): IterableIterator<V>;
|
|
222
|
+
|
|
223
|
+
/** Alias for {@link getValueIterator}. */
|
|
224
|
+
getIterator(range?: DllRange<ValueNode<V>>): IterableIterator<V>;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Get an iterable over ValueNodes in reverse order.
|
|
228
|
+
* @param range - Sub-range to iterate.
|
|
229
|
+
* @returns An iterable iterator of ValueNodes.
|
|
230
|
+
*/
|
|
231
|
+
getReverseNodeIterator(range?: DllRange<ValueNode<V>>): IterableIterator<ValueNode<V>>;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Get an iterable over unwrapped values in reverse order.
|
|
235
|
+
* @param range - Sub-range to iterate.
|
|
236
|
+
* @returns An iterable iterator of values.
|
|
237
|
+
*/
|
|
238
|
+
getReverseValueIterator(range?: DllRange<ValueNode<V>>): IterableIterator<V>;
|
|
239
|
+
|
|
240
|
+
/** Alias for {@link getReverseValueIterator}. */
|
|
241
|
+
getReverseIterator(range?: DllRange<ValueNode<V>>): IterableIterator<V>;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Get an iterable of Ptr objects over a range.
|
|
245
|
+
* @param range - Sub-range to iterate.
|
|
246
|
+
* @returns An iterable iterator of Ptrs.
|
|
247
|
+
*/
|
|
248
|
+
getPtrIterator(range?: DllRange<ValueNode<V>>): IterableIterator<Ptr<ValueNode<V>>>;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Get an iterable of Ptr objects in reverse order.
|
|
252
|
+
* @param range - Sub-range to iterate.
|
|
253
|
+
* @returns An iterable iterator of Ptrs.
|
|
254
|
+
*/
|
|
255
|
+
getReversePtrIterator(range?: DllRange<ValueNode<V>>): IterableIterator<Ptr<ValueNode<V>>>;
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Create a shallow clone of this list.
|
|
259
|
+
* @returns A new ValueList with the same values.
|
|
260
|
+
*/
|
|
261
|
+
clone(): ValueList<V>;
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Create an empty list with the same options.
|
|
265
|
+
* @returns A new empty ValueList.
|
|
266
|
+
*/
|
|
267
|
+
make(): ValueList<V>;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Create a list from values with the same options.
|
|
271
|
+
* @param values - Iterable of values.
|
|
272
|
+
* @returns A new ValueList.
|
|
273
|
+
*/
|
|
274
|
+
makeFrom(values: Iterable<V>): ValueList<V>;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Build a ValueList from an iterable.
|
|
278
|
+
* @param values - Iterable of values.
|
|
279
|
+
* @param options - Link property names.
|
|
280
|
+
* @returns A new ValueList.
|
|
281
|
+
*/
|
|
282
|
+
static from<V = unknown>(values: Iterable<V>, options?: DllOptions): ValueList<V>;
|
|
283
|
+
|
|
284
|
+
/** The Ptr class associated with this list type. */
|
|
285
|
+
static Ptr: typeof Ptr;
|
|
286
|
+
|
|
287
|
+
/** The ValueNode class used by this list type. */
|
|
288
|
+
static ValueNode: typeof ValueNode;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export {ValueNode, Ptr};
|
|
292
|
+
export default ValueList;
|
package/src/list/value.js
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
import List, {Ptr} from './core.js';
|
|
4
2
|
import {ValueNode} from './nodes.js';
|
|
5
3
|
import {pop} from './basics.js';
|
|
6
4
|
import {addAliases, mapIterator, normalizeIterator} from '../meta-utils.js';
|
|
7
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Hosted value-based doubly linked list. Wraps values in {@link ValueNode}.
|
|
8
|
+
* Iterators yield unwrapped values; use `getNodeIterator` for ValueNode access.
|
|
9
|
+
*/
|
|
8
10
|
export class ValueList extends List {
|
|
11
|
+
/**
|
|
12
|
+
* Remove and return the front value.
|
|
13
|
+
* @returns {*} The unwrapped value, or `undefined` if empty.
|
|
14
|
+
*/
|
|
9
15
|
popFront() {
|
|
10
16
|
if (!this.isEmpty) return pop(this, this[this.nextName]).extracted.value;
|
|
11
17
|
}
|
|
12
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Remove and return the back value.
|
|
21
|
+
* @returns {*} The unwrapped value, or `undefined` if empty.
|
|
22
|
+
*/
|
|
13
23
|
popBack() {
|
|
14
24
|
if (!this.isEmpty) return pop(this, this[this.prevName]).extracted.value;
|
|
15
25
|
}
|
|
16
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Adopt a value, pointer, or ValueNode into this list.
|
|
29
|
+
* @param {*} value - Raw value, Ptr, or ValueNode.
|
|
30
|
+
* @returns {ValueNode} A ValueNode ready for insertion.
|
|
31
|
+
*/
|
|
17
32
|
adoptValue(value) {
|
|
18
33
|
if (value instanceof Ptr) {
|
|
19
34
|
if (!this.isCompatiblePtr(value)) throw new Error('Incompatible pointer');
|
|
@@ -30,8 +45,7 @@ export class ValueList extends List {
|
|
|
30
45
|
return new ValueNode(value, this);
|
|
31
46
|
}
|
|
32
47
|
|
|
33
|
-
|
|
34
|
-
|
|
48
|
+
/** Iterate over unwrapped values from front to back. */
|
|
35
49
|
[Symbol.iterator]() {
|
|
36
50
|
let current = this[this.nextName],
|
|
37
51
|
readyToStop = this.isEmpty;
|
|
@@ -46,28 +60,55 @@ export class ValueList extends List {
|
|
|
46
60
|
});
|
|
47
61
|
}
|
|
48
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Get an iterable over unwrapped values in a range.
|
|
65
|
+
* @param {object} [range] - Sub-range to iterate.
|
|
66
|
+
* @returns {Iterable} An iterable iterator of values.
|
|
67
|
+
*/
|
|
49
68
|
getValueIterator(range) {
|
|
50
69
|
return mapIterator(this.getNodeIterator(range), node => node.value);
|
|
51
70
|
}
|
|
52
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Get an iterable over unwrapped values in reverse order.
|
|
74
|
+
* @param {object} [range] - Sub-range to iterate.
|
|
75
|
+
* @returns {Iterable} An iterable iterator of values.
|
|
76
|
+
*/
|
|
53
77
|
getReverseValueIterator(range) {
|
|
54
78
|
return mapIterator(this.getReverseNodeIterator(range), node => node.value);
|
|
55
79
|
}
|
|
56
80
|
|
|
57
|
-
|
|
58
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Create a shallow clone of this list.
|
|
83
|
+
* @returns {ValueList} A new ValueList with the same values.
|
|
84
|
+
*/
|
|
59
85
|
clone() {
|
|
60
86
|
return ValueList.from(this, this);
|
|
61
87
|
}
|
|
62
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Create an empty list with the same options.
|
|
91
|
+
* @returns {ValueList} A new empty ValueList.
|
|
92
|
+
*/
|
|
63
93
|
make() {
|
|
64
94
|
return new ValueList(this);
|
|
65
95
|
}
|
|
66
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Create a list from values with the same options.
|
|
99
|
+
* @param {Iterable} values - Iterable of values.
|
|
100
|
+
* @returns {ValueList} A new ValueList.
|
|
101
|
+
*/
|
|
67
102
|
makeFrom(values) {
|
|
68
103
|
return ValueList.from(values, this);
|
|
69
104
|
}
|
|
70
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Build a ValueList from an iterable.
|
|
108
|
+
* @param {Iterable} values - Iterable of values.
|
|
109
|
+
* @param {object} [options] - Link property names.
|
|
110
|
+
* @returns {ValueList} A new ValueList.
|
|
111
|
+
*/
|
|
71
112
|
static from(values, options) {
|
|
72
113
|
const list = new ValueList(options);
|
|
73
114
|
for (const value of values) list.pushBack(value);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize a node or pointer to a plain node, validating compatibility.
|
|
3
|
+
* @param list - Owning list for validation.
|
|
4
|
+
* @param node - Node or pointer to normalize.
|
|
5
|
+
* @param PtrBase - Pointer base class for `instanceof` checks.
|
|
6
|
+
* @returns The underlying node, or `null`.
|
|
7
|
+
*/
|
|
8
|
+
export function normalizeNode(list: object, node: any, PtrBase: Function): object | null;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Check whether a range is compatible with a list host.
|
|
12
|
+
* @param listHost - List to validate against.
|
|
13
|
+
* @param range - Range to check.
|
|
14
|
+
* @param PtrBase - Pointer base class for `instanceof` checks.
|
|
15
|
+
* @returns `true` if the range is compatible.
|
|
16
|
+
*/
|
|
17
|
+
export function isRangeLike(listHost: object, range: any, PtrBase: Function): boolean;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Normalize a range, resolving any pointers to plain nodes.
|
|
21
|
+
* @param listHost - List to validate against.
|
|
22
|
+
* @param range - Range to normalize.
|
|
23
|
+
* @param PtrBase - Pointer base class for `instanceof` checks.
|
|
24
|
+
* @returns The normalized range with `from`/`to` as plain nodes, or `null`.
|
|
25
|
+
*/
|
|
26
|
+
export function normalizeRange(listHost: object, range: any, PtrBase: Function): object | null;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Check whether a pointer-based range is compatible with a list host.
|
|
30
|
+
* @param listHost - List to validate against.
|
|
31
|
+
* @param range - Pointer range to check (must have `from` as a PtrBase).
|
|
32
|
+
* @param PtrBase - Pointer base class for `instanceof` checks.
|
|
33
|
+
* @returns `true` if the pointer range is compatible.
|
|
34
|
+
*/
|
|
35
|
+
export function isPtrRangeLike(listHost: object, range: any, PtrBase: Function): boolean;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Normalize a pointer-based range, resolving `to` to a plain node.
|
|
39
|
+
* @param listHost - List to validate against.
|
|
40
|
+
* @param range - Pointer range to normalize.
|
|
41
|
+
* @param PtrBase - Pointer base class for `instanceof` checks.
|
|
42
|
+
* @returns The normalized pointer range, or `null`.
|
|
43
|
+
*/
|
|
44
|
+
export function normalizePtrRange(listHost: object, range: any, PtrBase: Function): object | null;
|
package/src/list-helpers.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Normalize a node or pointer to a plain node, validating compatibility.
|
|
3
|
+
* @param {object} list - Owning list for validation.
|
|
4
|
+
* @param {object|null} node - Node or pointer to normalize.
|
|
5
|
+
* @param {Function} PtrBase - Pointer base class for `instanceof` checks.
|
|
6
|
+
* @returns {object|null} The underlying node, or `null`.
|
|
7
|
+
*/
|
|
3
8
|
export const normalizeNode = (list, node, PtrBase) => {
|
|
4
9
|
if (!node) return null;
|
|
5
10
|
if (node instanceof PtrBase) {
|
|
@@ -11,6 +16,13 @@ export const normalizeNode = (list, node, PtrBase) => {
|
|
|
11
16
|
return node;
|
|
12
17
|
};
|
|
13
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Check whether a range is compatible with a list host.
|
|
21
|
+
* @param {object} listHost - List to validate against.
|
|
22
|
+
* @param {object} [range] - Range to check.
|
|
23
|
+
* @param {Function} PtrBase - Pointer base class for `instanceof` checks.
|
|
24
|
+
* @returns {boolean} `true` if the range is compatible.
|
|
25
|
+
*/
|
|
14
26
|
export const isRangeLike = (listHost, range, PtrBase) => {
|
|
15
27
|
if (!range) return true;
|
|
16
28
|
|
|
@@ -30,7 +42,7 @@ export const isRangeLike = (listHost, range, PtrBase) => {
|
|
|
30
42
|
}
|
|
31
43
|
|
|
32
44
|
if (range.to instanceof PtrBase) {
|
|
33
|
-
if (list){
|
|
45
|
+
if (list) {
|
|
34
46
|
if (range.to.list !== list) return false;
|
|
35
47
|
} else {
|
|
36
48
|
if (!listHost.isCompatible(range.to.list)) return false;
|
|
@@ -42,6 +54,13 @@ export const isRangeLike = (listHost, range, PtrBase) => {
|
|
|
42
54
|
return true;
|
|
43
55
|
};
|
|
44
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Normalize a range, resolving any pointers to plain nodes.
|
|
59
|
+
* @param {object} listHost - List to validate against.
|
|
60
|
+
* @param {object} [range] - Range to normalize.
|
|
61
|
+
* @param {Function} PtrBase - Pointer base class for `instanceof` checks.
|
|
62
|
+
* @returns {object|null} The normalized range with `from`/`to` as plain nodes, or `null`.
|
|
63
|
+
*/
|
|
45
64
|
export const normalizeRange = (listHost, range, PtrBase) => {
|
|
46
65
|
if (!range) return null;
|
|
47
66
|
if (!isRangeLike(listHost, range, PtrBase)) throw new Error('Not a compatible range');
|
|
@@ -51,6 +70,13 @@ export const normalizeRange = (listHost, range, PtrBase) => {
|
|
|
51
70
|
return {...range, from, to};
|
|
52
71
|
};
|
|
53
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Check whether a pointer-based range is compatible with a list host.
|
|
75
|
+
* @param {object} listHost - List to validate against.
|
|
76
|
+
* @param {object} [range] - Pointer range to check (must have `from` as a PtrBase).
|
|
77
|
+
* @param {Function} PtrBase - Pointer base class for `instanceof` checks.
|
|
78
|
+
* @returns {boolean} `true` if the pointer range is compatible.
|
|
79
|
+
*/
|
|
54
80
|
export const isPtrRangeLike = (listHost, range, PtrBase) => {
|
|
55
81
|
if (!range) return true;
|
|
56
82
|
if (!(range.from instanceof PtrBase)) return false;
|
|
@@ -71,6 +97,13 @@ export const isPtrRangeLike = (listHost, range, PtrBase) => {
|
|
|
71
97
|
return true;
|
|
72
98
|
};
|
|
73
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Normalize a pointer-based range, resolving `to` to a plain node.
|
|
102
|
+
* @param {object} listHost - List to validate against.
|
|
103
|
+
* @param {object} [range] - Pointer range to normalize.
|
|
104
|
+
* @param {Function} PtrBase - Pointer base class for `instanceof` checks.
|
|
105
|
+
* @returns {object|null} The normalized pointer range, or `null`.
|
|
106
|
+
*/
|
|
74
107
|
export const normalizePtrRange = (listHost, range, PtrBase) => {
|
|
75
108
|
if (!range) return null;
|
|
76
109
|
if (!isPtrRangeLike(listHost, range, PtrBase)) throw new Error('Not a compatible ptr range');
|