linked-list-typed 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +2152 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +2143 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +11 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/linked-list-typed.js +2146 -0
- package/dist/umd/linked-list-typed.js.map +1 -0
- package/dist/umd/linked-list-typed.min.js +9 -0
- package/dist/umd/linked-list-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -28
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -0,0 +1,2146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var linkedListTyped = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
+
|
|
23
|
+
// src/index.ts
|
|
24
|
+
var src_exports = {};
|
|
25
|
+
__export(src_exports, {
|
|
26
|
+
DFSOperation: () => DFSOperation,
|
|
27
|
+
DoublyLinkedList: () => DoublyLinkedList,
|
|
28
|
+
DoublyLinkedListNode: () => DoublyLinkedListNode,
|
|
29
|
+
Range: () => Range,
|
|
30
|
+
SinglyLinkedList: () => SinglyLinkedList,
|
|
31
|
+
SinglyLinkedListNode: () => SinglyLinkedListNode,
|
|
32
|
+
SkipList: () => SkipList,
|
|
33
|
+
SkipListNode: () => SkipListNode
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// src/data-structures/base/iterable-element-base.ts
|
|
37
|
+
var IterableElementBase = class {
|
|
38
|
+
/**
|
|
39
|
+
* Create a new iterable base.
|
|
40
|
+
*
|
|
41
|
+
* @param options Optional behavior overrides. When provided, a `toElementFn`
|
|
42
|
+
* is used to convert a raw element (`R`) into a public element (`E`).
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* Time O(1), Space O(1).
|
|
46
|
+
*/
|
|
47
|
+
constructor(options) {
|
|
48
|
+
/**
|
|
49
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* Time O(1), Space O(1).
|
|
53
|
+
*/
|
|
54
|
+
__publicField(this, "_toElementFn");
|
|
55
|
+
if (options) {
|
|
56
|
+
const { toElementFn } = options;
|
|
57
|
+
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
58
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Exposes the current `toElementFn`, if configured.
|
|
63
|
+
*
|
|
64
|
+
* @returns The converter function or `undefined` when not set.
|
|
65
|
+
* @remarks
|
|
66
|
+
* Time O(1), Space O(1).
|
|
67
|
+
*/
|
|
68
|
+
get toElementFn() {
|
|
69
|
+
return this._toElementFn;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns an iterator over the structure's elements.
|
|
73
|
+
*
|
|
74
|
+
* @param args Optional iterator arguments forwarded to the internal iterator.
|
|
75
|
+
* @returns An `IterableIterator<E>` that yields the elements in traversal order.
|
|
76
|
+
*
|
|
77
|
+
* @remarks
|
|
78
|
+
* Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
79
|
+
*/
|
|
80
|
+
*[Symbol.iterator](...args) {
|
|
81
|
+
yield* this._getIterator(...args);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns an iterator over the values (alias of the default iterator).
|
|
85
|
+
*
|
|
86
|
+
* @returns An `IterableIterator<E>` over all elements.
|
|
87
|
+
* @remarks
|
|
88
|
+
* Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
89
|
+
*/
|
|
90
|
+
*values() {
|
|
91
|
+
for (const item of this) yield item;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Tests whether all elements satisfy the predicate.
|
|
95
|
+
*
|
|
96
|
+
* @template TReturn
|
|
97
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
98
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
99
|
+
* @returns `true` if every element passes; otherwise `false`.
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
102
|
+
* Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
103
|
+
*/
|
|
104
|
+
every(predicate, thisArg) {
|
|
105
|
+
let index = 0;
|
|
106
|
+
for (const item of this) {
|
|
107
|
+
if (thisArg === void 0) {
|
|
108
|
+
if (!predicate(item, index++, this)) return false;
|
|
109
|
+
} else {
|
|
110
|
+
const fn = predicate;
|
|
111
|
+
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Tests whether at least one element satisfies the predicate.
|
|
118
|
+
*
|
|
119
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
120
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
121
|
+
* @returns `true` if any element passes; otherwise `false`.
|
|
122
|
+
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
125
|
+
*/
|
|
126
|
+
some(predicate, thisArg) {
|
|
127
|
+
let index = 0;
|
|
128
|
+
for (const item of this) {
|
|
129
|
+
if (thisArg === void 0) {
|
|
130
|
+
if (predicate(item, index++, this)) return true;
|
|
131
|
+
} else {
|
|
132
|
+
const fn = predicate;
|
|
133
|
+
if (fn.call(thisArg, item, index++, this)) return true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Invokes a callback for each element in iteration order.
|
|
140
|
+
*
|
|
141
|
+
* @param callbackfn Function invoked per element with signature `(value, index, self)`.
|
|
142
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
143
|
+
* @returns `void`.
|
|
144
|
+
*
|
|
145
|
+
* @remarks
|
|
146
|
+
* Time O(n), Space O(1).
|
|
147
|
+
*/
|
|
148
|
+
forEach(callbackfn, thisArg) {
|
|
149
|
+
let index = 0;
|
|
150
|
+
for (const item of this) {
|
|
151
|
+
if (thisArg === void 0) {
|
|
152
|
+
callbackfn(item, index++, this);
|
|
153
|
+
} else {
|
|
154
|
+
const fn = callbackfn;
|
|
155
|
+
fn.call(thisArg, item, index++, this);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Implementation signature
|
|
160
|
+
find(predicate, thisArg) {
|
|
161
|
+
let index = 0;
|
|
162
|
+
for (const item of this) {
|
|
163
|
+
if (thisArg === void 0) {
|
|
164
|
+
if (predicate(item, index++, this)) return item;
|
|
165
|
+
} else {
|
|
166
|
+
const fn = predicate;
|
|
167
|
+
if (fn.call(thisArg, item, index++, this)) return item;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Checks whether a strictly-equal element exists in the structure.
|
|
174
|
+
*
|
|
175
|
+
* @param element The element to test with `===` equality.
|
|
176
|
+
* @returns `true` if an equal element is found; otherwise `false`.
|
|
177
|
+
*
|
|
178
|
+
* @remarks
|
|
179
|
+
* Time O(n) in the worst case. Space O(1).
|
|
180
|
+
*/
|
|
181
|
+
has(element) {
|
|
182
|
+
for (const ele of this) if (ele === element) return true;
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Reduces all elements to a single accumulated value.
|
|
187
|
+
*
|
|
188
|
+
* @overload
|
|
189
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
|
|
190
|
+
* @returns The final accumulated value typed as `E`.
|
|
191
|
+
*
|
|
192
|
+
* @overload
|
|
193
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
|
|
194
|
+
* @param initialValue The initial accumulator value of type `E`.
|
|
195
|
+
* @returns The final accumulated value typed as `E`.
|
|
196
|
+
*
|
|
197
|
+
* @overload
|
|
198
|
+
* @template U The accumulator type when it differs from `E`.
|
|
199
|
+
* @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
|
|
200
|
+
* @param initialValue The initial accumulator value of type `U`.
|
|
201
|
+
* @returns The final accumulated value typed as `U`.
|
|
202
|
+
*
|
|
203
|
+
* @remarks
|
|
204
|
+
* Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
|
|
205
|
+
*/
|
|
206
|
+
reduce(callbackfn, initialValue) {
|
|
207
|
+
let index = 0;
|
|
208
|
+
const iter = this[Symbol.iterator]();
|
|
209
|
+
let acc;
|
|
210
|
+
if (arguments.length >= 2) {
|
|
211
|
+
acc = initialValue;
|
|
212
|
+
} else {
|
|
213
|
+
const first = iter.next();
|
|
214
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
215
|
+
acc = first.value;
|
|
216
|
+
index = 1;
|
|
217
|
+
}
|
|
218
|
+
for (const value of iter) {
|
|
219
|
+
acc = callbackfn(acc, value, index++, this);
|
|
220
|
+
}
|
|
221
|
+
return acc;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Materializes the elements into a new array.
|
|
225
|
+
*
|
|
226
|
+
* @returns A shallow array copy of the iteration order.
|
|
227
|
+
* @remarks
|
|
228
|
+
* Time O(n), Space O(n).
|
|
229
|
+
*/
|
|
230
|
+
toArray() {
|
|
231
|
+
return [...this];
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Returns a representation of the structure suitable for quick visualization.
|
|
235
|
+
* Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
236
|
+
*
|
|
237
|
+
* @returns A visual representation (array by default).
|
|
238
|
+
* @remarks
|
|
239
|
+
* Time O(n), Space O(n).
|
|
240
|
+
*/
|
|
241
|
+
toVisual() {
|
|
242
|
+
return [...this];
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Prints `toVisual()` to the console. Intended for quick debugging.
|
|
246
|
+
*
|
|
247
|
+
* @returns `void`.
|
|
248
|
+
* @remarks
|
|
249
|
+
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
250
|
+
*/
|
|
251
|
+
print() {
|
|
252
|
+
console.log(this.toVisual());
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// src/data-structures/base/linear-base.ts
|
|
257
|
+
var LinkedListNode = class {
|
|
258
|
+
/**
|
|
259
|
+
* Initialize a node.
|
|
260
|
+
* @param value - Element value.
|
|
261
|
+
* @remarks Time O(1), Space O(1)
|
|
262
|
+
*/
|
|
263
|
+
constructor(value) {
|
|
264
|
+
__publicField(this, "_value");
|
|
265
|
+
__publicField(this, "_next");
|
|
266
|
+
this._value = value;
|
|
267
|
+
this._next = void 0;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Element payload getter.
|
|
271
|
+
* @returns Element value.
|
|
272
|
+
* @remarks Time O(1), Space O(1)
|
|
273
|
+
*/
|
|
274
|
+
get value() {
|
|
275
|
+
return this._value;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Element payload setter.
|
|
279
|
+
* @param value - New value.
|
|
280
|
+
* @remarks Time O(1), Space O(1)
|
|
281
|
+
*/
|
|
282
|
+
set value(value) {
|
|
283
|
+
this._value = value;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Next node getter.
|
|
287
|
+
* @returns Next node or `undefined`.
|
|
288
|
+
* @remarks Time O(1), Space O(1)
|
|
289
|
+
*/
|
|
290
|
+
get next() {
|
|
291
|
+
return this._next;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Next node setter.
|
|
295
|
+
* @param value - Next node or `undefined`.
|
|
296
|
+
* @remarks Time O(1), Space O(1)
|
|
297
|
+
*/
|
|
298
|
+
set next(value) {
|
|
299
|
+
this._next = value;
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
303
|
+
/**
|
|
304
|
+
* Construct a linear container with runtime options.
|
|
305
|
+
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
306
|
+
* @remarks Time O(1), Space O(1)
|
|
307
|
+
*/
|
|
308
|
+
constructor(options) {
|
|
309
|
+
super(options);
|
|
310
|
+
__publicField(this, "_maxLen", -1);
|
|
311
|
+
if (options) {
|
|
312
|
+
const { maxLen } = options;
|
|
313
|
+
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
318
|
+
* @returns Maximum allowed length.
|
|
319
|
+
* @remarks Time O(1), Space O(1)
|
|
320
|
+
*/
|
|
321
|
+
get maxLen() {
|
|
322
|
+
return this._maxLen;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* First index of a value from the left.
|
|
326
|
+
* @param searchElement - Value to match.
|
|
327
|
+
* @param fromIndex - Start position (supports negative index).
|
|
328
|
+
* @returns Index or `-1` if not found.
|
|
329
|
+
* @remarks Time O(n), Space O(1)
|
|
330
|
+
*/
|
|
331
|
+
indexOf(searchElement, fromIndex = 0) {
|
|
332
|
+
if (this.length === 0) return -1;
|
|
333
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
334
|
+
if (fromIndex < 0) fromIndex = 0;
|
|
335
|
+
for (let i = fromIndex; i < this.length; i++) {
|
|
336
|
+
const element = this.at(i);
|
|
337
|
+
if (element === searchElement) return i;
|
|
338
|
+
}
|
|
339
|
+
return -1;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Last index of a value from the right.
|
|
343
|
+
* @param searchElement - Value to match.
|
|
344
|
+
* @param fromIndex - Start position (supports negative index).
|
|
345
|
+
* @returns Index or `-1` if not found.
|
|
346
|
+
* @remarks Time O(n), Space O(1)
|
|
347
|
+
*/
|
|
348
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
349
|
+
if (this.length === 0) return -1;
|
|
350
|
+
if (fromIndex >= this.length) fromIndex = this.length - 1;
|
|
351
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
352
|
+
for (let i = fromIndex; i >= 0; i--) {
|
|
353
|
+
const element = this.at(i);
|
|
354
|
+
if (element === searchElement) return i;
|
|
355
|
+
}
|
|
356
|
+
return -1;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Find the first index matching a predicate.
|
|
360
|
+
* @param predicate - `(element, index, self) => boolean`.
|
|
361
|
+
* @param thisArg - Optional `this` for callback.
|
|
362
|
+
* @returns Index or `-1`.
|
|
363
|
+
* @remarks Time O(n), Space O(1)
|
|
364
|
+
*/
|
|
365
|
+
findIndex(predicate, thisArg) {
|
|
366
|
+
for (let i = 0; i < this.length; i++) {
|
|
367
|
+
const item = this.at(i);
|
|
368
|
+
if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
|
|
369
|
+
}
|
|
370
|
+
return -1;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Concatenate elements and/or containers.
|
|
374
|
+
* @param items - Elements or other containers.
|
|
375
|
+
* @returns New container with combined elements (`this` type).
|
|
376
|
+
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
377
|
+
*/
|
|
378
|
+
concat(...items) {
|
|
379
|
+
const newList = this.clone();
|
|
380
|
+
for (const item of items) {
|
|
381
|
+
if (item instanceof _LinearBase) {
|
|
382
|
+
newList.pushMany(item);
|
|
383
|
+
} else {
|
|
384
|
+
newList.push(item);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return newList;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* In-place stable order via array sort semantics.
|
|
391
|
+
* @param compareFn - Comparator `(a, b) => number`.
|
|
392
|
+
* @returns This container.
|
|
393
|
+
* @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
|
|
394
|
+
*/
|
|
395
|
+
sort(compareFn) {
|
|
396
|
+
const arr = this.toArray();
|
|
397
|
+
arr.sort(compareFn);
|
|
398
|
+
this.clear();
|
|
399
|
+
for (const item of arr) this.push(item);
|
|
400
|
+
return this;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Remove and/or insert elements at a position (array-compatible).
|
|
404
|
+
* @param start - Start index (supports negative index).
|
|
405
|
+
* @param deleteCount - How many to remove.
|
|
406
|
+
* @param items - Elements to insert.
|
|
407
|
+
* @returns Removed elements as a new list (`this` type).
|
|
408
|
+
* @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
|
|
409
|
+
*/
|
|
410
|
+
splice(start, deleteCount = 0, ...items) {
|
|
411
|
+
const removedList = this._createInstance();
|
|
412
|
+
start = start < 0 ? this.length + start : start;
|
|
413
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
414
|
+
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
415
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
416
|
+
const removed = this.deleteAt(start);
|
|
417
|
+
if (removed !== void 0) {
|
|
418
|
+
removedList.push(removed);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
for (let i = 0; i < items.length; i++) {
|
|
422
|
+
this.addAt(start + i, items[i]);
|
|
423
|
+
}
|
|
424
|
+
return removedList;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Join all elements into a string.
|
|
428
|
+
* @param separator - Separator string.
|
|
429
|
+
* @returns Concatenated string.
|
|
430
|
+
* @remarks Time O(n), Space O(n)
|
|
431
|
+
*/
|
|
432
|
+
join(separator = ",") {
|
|
433
|
+
return this.toArray().join(separator);
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Snapshot elements into a reversed array.
|
|
437
|
+
* @returns New reversed array.
|
|
438
|
+
* @remarks Time O(n), Space O(n)
|
|
439
|
+
*/
|
|
440
|
+
toReversedArray() {
|
|
441
|
+
const array = [];
|
|
442
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
443
|
+
array.push(this.at(i));
|
|
444
|
+
}
|
|
445
|
+
return array;
|
|
446
|
+
}
|
|
447
|
+
reduceRight(callbackfn, initialValue) {
|
|
448
|
+
let accumulator = initialValue != null ? initialValue : 0;
|
|
449
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
450
|
+
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
451
|
+
}
|
|
452
|
+
return accumulator;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Create a shallow copy of a subrange.
|
|
456
|
+
* @param start - Inclusive start (supports negative index).
|
|
457
|
+
* @param end - Exclusive end (supports negative index).
|
|
458
|
+
* @returns New list with the range (`this` type).
|
|
459
|
+
* @remarks Time O(n), Space O(n)
|
|
460
|
+
*/
|
|
461
|
+
slice(start = 0, end = this.length) {
|
|
462
|
+
start = start < 0 ? this.length + start : start;
|
|
463
|
+
end = end < 0 ? this.length + end : end;
|
|
464
|
+
const newList = this._createInstance();
|
|
465
|
+
for (let i = start; i < end; i++) {
|
|
466
|
+
newList.push(this.at(i));
|
|
467
|
+
}
|
|
468
|
+
return newList;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Fill a range with a value.
|
|
472
|
+
* @param value - Value to set.
|
|
473
|
+
* @param start - Inclusive start.
|
|
474
|
+
* @param end - Exclusive end.
|
|
475
|
+
* @returns This list.
|
|
476
|
+
* @remarks Time O(n), Space O(1)
|
|
477
|
+
*/
|
|
478
|
+
fill(value, start = 0, end = this.length) {
|
|
479
|
+
start = start < 0 ? this.length + start : start;
|
|
480
|
+
end = end < 0 ? this.length + end : end;
|
|
481
|
+
if (start < 0) start = 0;
|
|
482
|
+
if (end > this.length) end = this.length;
|
|
483
|
+
if (start >= end) return this;
|
|
484
|
+
for (let i = start; i < end; i++) {
|
|
485
|
+
this.setAt(i, value);
|
|
486
|
+
}
|
|
487
|
+
return this;
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
var LinearLinkedBase = class extends LinearBase {
|
|
491
|
+
constructor(options) {
|
|
492
|
+
super(options);
|
|
493
|
+
if (options) {
|
|
494
|
+
const { maxLen } = options;
|
|
495
|
+
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Linked-list optimized `indexOf` (forwards scan).
|
|
500
|
+
* @param searchElement - Value to match.
|
|
501
|
+
* @param fromIndex - Start position.
|
|
502
|
+
* @returns Index or `-1`.
|
|
503
|
+
* @remarks Time O(n), Space O(1)
|
|
504
|
+
*/
|
|
505
|
+
indexOf(searchElement, fromIndex = 0) {
|
|
506
|
+
const iterator = this._getIterator();
|
|
507
|
+
let current = iterator.next();
|
|
508
|
+
let index = 0;
|
|
509
|
+
while (index < fromIndex) {
|
|
510
|
+
current = iterator.next();
|
|
511
|
+
index++;
|
|
512
|
+
}
|
|
513
|
+
while (!current.done) {
|
|
514
|
+
if (current.value === searchElement) return index;
|
|
515
|
+
current = iterator.next();
|
|
516
|
+
index++;
|
|
517
|
+
}
|
|
518
|
+
return -1;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Linked-list optimized `lastIndexOf` (reverse scan).
|
|
522
|
+
* @param searchElement - Value to match.
|
|
523
|
+
* @param fromIndex - Start position.
|
|
524
|
+
* @returns Index or `-1`.
|
|
525
|
+
* @remarks Time O(n), Space O(1)
|
|
526
|
+
*/
|
|
527
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
528
|
+
const iterator = this._getReverseIterator();
|
|
529
|
+
let current = iterator.next();
|
|
530
|
+
let index = this.length - 1;
|
|
531
|
+
while (index > fromIndex) {
|
|
532
|
+
current = iterator.next();
|
|
533
|
+
index--;
|
|
534
|
+
}
|
|
535
|
+
while (!current.done) {
|
|
536
|
+
if (current.value === searchElement) return index;
|
|
537
|
+
current = iterator.next();
|
|
538
|
+
index--;
|
|
539
|
+
}
|
|
540
|
+
return -1;
|
|
541
|
+
}
|
|
542
|
+
concat(...items) {
|
|
543
|
+
const newList = this.clone();
|
|
544
|
+
for (const item of items) {
|
|
545
|
+
if (item instanceof LinearBase) {
|
|
546
|
+
newList.pushMany(item);
|
|
547
|
+
} else {
|
|
548
|
+
newList.push(item);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return newList;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Slice via forward iteration (no random access required).
|
|
555
|
+
* @param start - Inclusive start (supports negative index).
|
|
556
|
+
* @param end - Exclusive end (supports negative index).
|
|
557
|
+
* @returns New list (`this` type).
|
|
558
|
+
* @remarks Time O(n), Space O(n)
|
|
559
|
+
*/
|
|
560
|
+
slice(start = 0, end = this.length) {
|
|
561
|
+
start = start < 0 ? this.length + start : start;
|
|
562
|
+
end = end < 0 ? this.length + end : end;
|
|
563
|
+
const newList = this._createInstance();
|
|
564
|
+
const iterator = this._getIterator();
|
|
565
|
+
let current = iterator.next();
|
|
566
|
+
let c = 0;
|
|
567
|
+
while (c < start) {
|
|
568
|
+
current = iterator.next();
|
|
569
|
+
c++;
|
|
570
|
+
}
|
|
571
|
+
for (let i = start; i < end; i++) {
|
|
572
|
+
newList.push(current.value);
|
|
573
|
+
current = iterator.next();
|
|
574
|
+
}
|
|
575
|
+
return newList;
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Splice by walking node iterators from the start index.
|
|
579
|
+
* @param start - Start index.
|
|
580
|
+
* @param deleteCount - How many elements to remove.
|
|
581
|
+
* @param items - Elements to insert after the splice point.
|
|
582
|
+
* @returns Removed elements as a new list (`this` type).
|
|
583
|
+
* @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
|
|
584
|
+
*/
|
|
585
|
+
splice(start, deleteCount = 0, ...items) {
|
|
586
|
+
const removedList = this._createInstance();
|
|
587
|
+
start = start < 0 ? this.length + start : start;
|
|
588
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
589
|
+
deleteCount = Math.max(0, deleteCount);
|
|
590
|
+
let currentIndex = 0;
|
|
591
|
+
let currentNode = void 0;
|
|
592
|
+
let previousNode = void 0;
|
|
593
|
+
const iterator = this._getNodeIterator();
|
|
594
|
+
for (const node of iterator) {
|
|
595
|
+
if (currentIndex === start) {
|
|
596
|
+
currentNode = node;
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
previousNode = node;
|
|
600
|
+
currentIndex++;
|
|
601
|
+
}
|
|
602
|
+
for (let i = 0; i < deleteCount && currentNode; i++) {
|
|
603
|
+
removedList.push(currentNode.value);
|
|
604
|
+
const nextNode = currentNode.next;
|
|
605
|
+
this.delete(currentNode);
|
|
606
|
+
currentNode = nextNode;
|
|
607
|
+
}
|
|
608
|
+
for (let i = 0; i < items.length; i++) {
|
|
609
|
+
if (previousNode) {
|
|
610
|
+
this.addAfter(previousNode, items[i]);
|
|
611
|
+
previousNode = previousNode.next;
|
|
612
|
+
} else {
|
|
613
|
+
this.addAt(0, items[i]);
|
|
614
|
+
previousNode = this._getNodeIterator().next().value;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return removedList;
|
|
618
|
+
}
|
|
619
|
+
reduceRight(callbackfn, initialValue) {
|
|
620
|
+
let accumulator = initialValue != null ? initialValue : 0;
|
|
621
|
+
let index = this.length - 1;
|
|
622
|
+
for (const item of this._getReverseIterator()) {
|
|
623
|
+
accumulator = callbackfn(accumulator, item, index--, this);
|
|
624
|
+
}
|
|
625
|
+
return accumulator;
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
// src/data-structures/linked-list/singly-linked-list.ts
|
|
630
|
+
var SinglyLinkedListNode = class extends LinkedListNode {
|
|
631
|
+
/**
|
|
632
|
+
* Create a list node.
|
|
633
|
+
* @remarks Time O(1), Space O(1)
|
|
634
|
+
* @param value - Element value to store.
|
|
635
|
+
* @returns New node instance.
|
|
636
|
+
*/
|
|
637
|
+
constructor(value) {
|
|
638
|
+
super(value);
|
|
639
|
+
__publicField(this, "_next");
|
|
640
|
+
this._value = value;
|
|
641
|
+
this._next = void 0;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Get the next node.
|
|
645
|
+
* @remarks Time O(1), Space O(1)
|
|
646
|
+
* @returns Next node or undefined.
|
|
647
|
+
*/
|
|
648
|
+
get next() {
|
|
649
|
+
return this._next;
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Set the next node.
|
|
653
|
+
* @remarks Time O(1), Space O(1)
|
|
654
|
+
* @param value - Next node or undefined.
|
|
655
|
+
* @returns void
|
|
656
|
+
*/
|
|
657
|
+
set next(value) {
|
|
658
|
+
this._next = value;
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
var SinglyLinkedList = class extends LinearLinkedBase {
|
|
662
|
+
/**
|
|
663
|
+
* Create a SinglyLinkedList and optionally bulk-insert elements.
|
|
664
|
+
* @remarks Time O(N), Space O(N)
|
|
665
|
+
* @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
|
|
666
|
+
* @param [options] - Options such as maxLen and toElementFn.
|
|
667
|
+
* @returns New SinglyLinkedList instance.
|
|
668
|
+
*/
|
|
669
|
+
constructor(elements = [], options) {
|
|
670
|
+
super(options);
|
|
671
|
+
__publicField(this, "_equals", Object.is);
|
|
672
|
+
__publicField(this, "_head");
|
|
673
|
+
__publicField(this, "_tail");
|
|
674
|
+
__publicField(this, "_length", 0);
|
|
675
|
+
this.pushMany(elements);
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Get the head node.
|
|
679
|
+
* @remarks Time O(1), Space O(1)
|
|
680
|
+
* @returns Head node or undefined.
|
|
681
|
+
*/
|
|
682
|
+
get head() {
|
|
683
|
+
return this._head;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Get the tail node.
|
|
687
|
+
* @remarks Time O(1), Space O(1)
|
|
688
|
+
* @returns Tail node or undefined.
|
|
689
|
+
*/
|
|
690
|
+
get tail() {
|
|
691
|
+
return this._tail;
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Get the number of elements.
|
|
695
|
+
* @remarks Time O(1), Space O(1)
|
|
696
|
+
* @returns Current length.
|
|
697
|
+
*/
|
|
698
|
+
get length() {
|
|
699
|
+
return this._length;
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Get the first element value.
|
|
703
|
+
* @remarks Time O(1), Space O(1)
|
|
704
|
+
* @returns First element or undefined.
|
|
705
|
+
*/
|
|
706
|
+
get first() {
|
|
707
|
+
var _a;
|
|
708
|
+
return (_a = this.head) == null ? void 0 : _a.value;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Get the last element value.
|
|
712
|
+
* @remarks Time O(1), Space O(1)
|
|
713
|
+
* @returns Last element or undefined.
|
|
714
|
+
*/
|
|
715
|
+
get last() {
|
|
716
|
+
var _a;
|
|
717
|
+
return (_a = this.tail) == null ? void 0 : _a.value;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Create a new list from an iterable of elements.
|
|
721
|
+
* @remarks Time O(N), Space O(N)
|
|
722
|
+
* @template E
|
|
723
|
+
* @template R
|
|
724
|
+
* @template S
|
|
725
|
+
* @param this - The constructor (subclass) to instantiate.
|
|
726
|
+
* @param data - Iterable of elements to insert.
|
|
727
|
+
* @param [options] - Options forwarded to the constructor.
|
|
728
|
+
* @returns A new list populated with the iterable's elements.
|
|
729
|
+
*/
|
|
730
|
+
static from(data, options) {
|
|
731
|
+
const list = new this([], options);
|
|
732
|
+
for (const x of data) list.push(x);
|
|
733
|
+
return list;
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* Append an element/node to the tail.
|
|
737
|
+
* @remarks Time O(1), Space O(1)
|
|
738
|
+
* @param elementOrNode - Element or node to append.
|
|
739
|
+
* @returns True when appended.
|
|
740
|
+
*/
|
|
741
|
+
push(elementOrNode) {
|
|
742
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
743
|
+
if (!this.head) {
|
|
744
|
+
this._head = this._tail = newNode;
|
|
745
|
+
} else {
|
|
746
|
+
this.tail.next = newNode;
|
|
747
|
+
this._tail = newNode;
|
|
748
|
+
}
|
|
749
|
+
this._length++;
|
|
750
|
+
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
751
|
+
return true;
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Remove and return the tail element.
|
|
755
|
+
* @remarks Time O(N), Space O(1)
|
|
756
|
+
* @returns Removed element or undefined.
|
|
757
|
+
*/
|
|
758
|
+
pop() {
|
|
759
|
+
if (!this.head) return void 0;
|
|
760
|
+
if (this.head === this.tail) {
|
|
761
|
+
const value2 = this.head.value;
|
|
762
|
+
this._head = void 0;
|
|
763
|
+
this._tail = void 0;
|
|
764
|
+
this._length--;
|
|
765
|
+
return value2;
|
|
766
|
+
}
|
|
767
|
+
let current = this.head;
|
|
768
|
+
while (current.next !== this.tail) current = current.next;
|
|
769
|
+
const value = this.tail.value;
|
|
770
|
+
current.next = void 0;
|
|
771
|
+
this._tail = current;
|
|
772
|
+
this._length--;
|
|
773
|
+
return value;
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Remove and return the head element.
|
|
777
|
+
* @remarks Time O(1), Space O(1)
|
|
778
|
+
* @returns Removed element or undefined.
|
|
779
|
+
*/
|
|
780
|
+
shift() {
|
|
781
|
+
if (!this.head) return void 0;
|
|
782
|
+
const removed = this.head;
|
|
783
|
+
this._head = this.head.next;
|
|
784
|
+
if (!this._head) this._tail = void 0;
|
|
785
|
+
this._length--;
|
|
786
|
+
return removed.value;
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Prepend an element/node to the head.
|
|
790
|
+
* @remarks Time O(1), Space O(1)
|
|
791
|
+
* @param elementOrNode - Element or node to prepend.
|
|
792
|
+
* @returns True when prepended.
|
|
793
|
+
*/
|
|
794
|
+
unshift(elementOrNode) {
|
|
795
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
796
|
+
if (!this.head) {
|
|
797
|
+
this._head = this._tail = newNode;
|
|
798
|
+
} else {
|
|
799
|
+
newNode.next = this.head;
|
|
800
|
+
this._head = newNode;
|
|
801
|
+
}
|
|
802
|
+
this._length++;
|
|
803
|
+
return true;
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Append a sequence of elements/nodes.
|
|
807
|
+
* @remarks Time O(N), Space O(1)
|
|
808
|
+
* @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
|
|
809
|
+
* @returns Array of per-element success flags.
|
|
810
|
+
*/
|
|
811
|
+
pushMany(elements) {
|
|
812
|
+
const ans = [];
|
|
813
|
+
for (const el of elements) {
|
|
814
|
+
if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
|
|
815
|
+
else ans.push(this.push(el));
|
|
816
|
+
}
|
|
817
|
+
return ans;
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Prepend a sequence of elements/nodes.
|
|
821
|
+
* @remarks Time O(N), Space O(1)
|
|
822
|
+
* @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
|
|
823
|
+
* @returns Array of per-element success flags.
|
|
824
|
+
*/
|
|
825
|
+
unshiftMany(elements) {
|
|
826
|
+
const ans = [];
|
|
827
|
+
for (const el of elements) {
|
|
828
|
+
if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
|
|
829
|
+
else ans.push(this.unshift(el));
|
|
830
|
+
}
|
|
831
|
+
return ans;
|
|
832
|
+
}
|
|
833
|
+
/**
|
|
834
|
+
* Find the first value matching a predicate (by node).
|
|
835
|
+
* @remarks Time O(N), Space O(1)
|
|
836
|
+
* @param elementNodeOrPredicate - Element, node, or node predicate to match.
|
|
837
|
+
* @returns Matched value or undefined.
|
|
838
|
+
*/
|
|
839
|
+
search(elementNodeOrPredicate) {
|
|
840
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
841
|
+
let current = this.head;
|
|
842
|
+
while (current) {
|
|
843
|
+
if (predicate(current)) return current.value;
|
|
844
|
+
current = current.next;
|
|
845
|
+
}
|
|
846
|
+
return void 0;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* Get the element at a given index.
|
|
850
|
+
* @remarks Time O(N), Space O(1)
|
|
851
|
+
* @param index - Zero-based index.
|
|
852
|
+
* @returns Element or undefined.
|
|
853
|
+
*/
|
|
854
|
+
at(index) {
|
|
855
|
+
if (index < 0 || index >= this._length) return void 0;
|
|
856
|
+
let current = this.head;
|
|
857
|
+
for (let i = 0; i < index; i++) current = current.next;
|
|
858
|
+
return current.value;
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Type guard: check whether the input is a SinglyLinkedListNode.
|
|
862
|
+
* @remarks Time O(1), Space O(1)
|
|
863
|
+
* @param elementNodeOrPredicate - Element, node, or predicate.
|
|
864
|
+
* @returns True if the value is a SinglyLinkedListNode.
|
|
865
|
+
*/
|
|
866
|
+
isNode(elementNodeOrPredicate) {
|
|
867
|
+
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Get the node reference at a given index.
|
|
871
|
+
* @remarks Time O(N), Space O(1)
|
|
872
|
+
* @param index - Zero-based index.
|
|
873
|
+
* @returns Node or undefined.
|
|
874
|
+
*/
|
|
875
|
+
getNodeAt(index) {
|
|
876
|
+
if (index < 0 || index >= this._length) return void 0;
|
|
877
|
+
let current = this.head;
|
|
878
|
+
for (let i = 0; i < index; i++) current = current.next;
|
|
879
|
+
return current;
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* Delete the element at an index.
|
|
883
|
+
* @remarks Time O(N), Space O(1)
|
|
884
|
+
* @param index - Zero-based index.
|
|
885
|
+
* @returns Removed element or undefined.
|
|
886
|
+
*/
|
|
887
|
+
deleteAt(index) {
|
|
888
|
+
if (index < 0 || index >= this._length) return void 0;
|
|
889
|
+
if (index === 0) return this.shift();
|
|
890
|
+
const targetNode = this.getNodeAt(index);
|
|
891
|
+
const prevNode = this._getPrevNode(targetNode);
|
|
892
|
+
const value = targetNode.value;
|
|
893
|
+
prevNode.next = targetNode.next;
|
|
894
|
+
if (targetNode === this.tail) this._tail = prevNode;
|
|
895
|
+
this._length--;
|
|
896
|
+
return value;
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Delete the first match by value/node.
|
|
900
|
+
* @remarks Time O(N), Space O(1)
|
|
901
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
902
|
+
* @returns True if removed.
|
|
903
|
+
*/
|
|
904
|
+
delete(elementOrNode) {
|
|
905
|
+
if (elementOrNode === void 0 || !this.head) return false;
|
|
906
|
+
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
907
|
+
if (!node) return false;
|
|
908
|
+
const prevNode = this._getPrevNode(node);
|
|
909
|
+
if (!prevNode) {
|
|
910
|
+
this._head = node.next;
|
|
911
|
+
if (node === this.tail) this._tail = void 0;
|
|
912
|
+
} else {
|
|
913
|
+
prevNode.next = node.next;
|
|
914
|
+
if (node === this.tail) this._tail = prevNode;
|
|
915
|
+
}
|
|
916
|
+
this._length--;
|
|
917
|
+
return true;
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
921
|
+
* @remarks Time O(N), Space O(1)
|
|
922
|
+
* @param index - Zero-based index.
|
|
923
|
+
* @param newElementOrNode - Element or node to insert.
|
|
924
|
+
* @returns True if inserted.
|
|
925
|
+
*/
|
|
926
|
+
addAt(index, newElementOrNode) {
|
|
927
|
+
if (index < 0 || index > this._length) return false;
|
|
928
|
+
if (index === 0) return this.unshift(newElementOrNode);
|
|
929
|
+
if (index === this._length) return this.push(newElementOrNode);
|
|
930
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
931
|
+
const prevNode = this.getNodeAt(index - 1);
|
|
932
|
+
newNode.next = prevNode.next;
|
|
933
|
+
prevNode.next = newNode;
|
|
934
|
+
this._length++;
|
|
935
|
+
return true;
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* Set the element value at an index.
|
|
939
|
+
* @remarks Time O(N), Space O(1)
|
|
940
|
+
* @param index - Zero-based index.
|
|
941
|
+
* @param value - New value.
|
|
942
|
+
* @returns True if updated.
|
|
943
|
+
*/
|
|
944
|
+
setAt(index, value) {
|
|
945
|
+
const node = this.getNodeAt(index);
|
|
946
|
+
if (!node) return false;
|
|
947
|
+
node.value = value;
|
|
948
|
+
return true;
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* Check whether the list is empty.
|
|
952
|
+
* @remarks Time O(1), Space O(1)
|
|
953
|
+
* @returns True if length is 0.
|
|
954
|
+
*/
|
|
955
|
+
isEmpty() {
|
|
956
|
+
return this._length === 0;
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Remove all nodes and reset length.
|
|
960
|
+
* @remarks Time O(N), Space O(1)
|
|
961
|
+
* @returns void
|
|
962
|
+
*/
|
|
963
|
+
clear() {
|
|
964
|
+
this._head = void 0;
|
|
965
|
+
this._tail = void 0;
|
|
966
|
+
this._length = 0;
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Reverse the list in place.
|
|
970
|
+
* @remarks Time O(N), Space O(1)
|
|
971
|
+
* @returns This list.
|
|
972
|
+
*/
|
|
973
|
+
reverse() {
|
|
974
|
+
if (!this.head || this.head === this.tail) return this;
|
|
975
|
+
let prev;
|
|
976
|
+
let current = this.head;
|
|
977
|
+
let next;
|
|
978
|
+
while (current) {
|
|
979
|
+
next = current.next;
|
|
980
|
+
current.next = prev;
|
|
981
|
+
prev = current;
|
|
982
|
+
current = next;
|
|
983
|
+
}
|
|
984
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
985
|
+
return this;
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Find a node by value, reference, or predicate.
|
|
989
|
+
* @remarks Time O(N), Space O(1)
|
|
990
|
+
* @param [elementNodeOrPredicate] - Element, node, or node predicate to match.
|
|
991
|
+
* @returns Matching node or undefined.
|
|
992
|
+
*/
|
|
993
|
+
getNode(elementNodeOrPredicate) {
|
|
994
|
+
if (elementNodeOrPredicate === void 0) return;
|
|
995
|
+
if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
996
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
997
|
+
let current = this.head;
|
|
998
|
+
while (current) {
|
|
999
|
+
if (predicate(current)) return current;
|
|
1000
|
+
current = current.next;
|
|
1001
|
+
}
|
|
1002
|
+
return void 0;
|
|
1003
|
+
}
|
|
1004
|
+
/**
|
|
1005
|
+
* Insert a new element/node before an existing one.
|
|
1006
|
+
* @remarks Time O(N), Space O(1)
|
|
1007
|
+
* @param existingElementOrNode - Existing element or node.
|
|
1008
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1009
|
+
* @returns True if inserted.
|
|
1010
|
+
*/
|
|
1011
|
+
addBefore(existingElementOrNode, newElementOrNode) {
|
|
1012
|
+
const existingNode = this.getNode(existingElementOrNode);
|
|
1013
|
+
if (!existingNode) return false;
|
|
1014
|
+
const prevNode = this._getPrevNode(existingNode);
|
|
1015
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
1016
|
+
if (!prevNode) {
|
|
1017
|
+
newNode.next = this._head;
|
|
1018
|
+
this._head = newNode;
|
|
1019
|
+
if (!this._tail) this._tail = newNode;
|
|
1020
|
+
this._length++;
|
|
1021
|
+
} else {
|
|
1022
|
+
prevNode.next = newNode;
|
|
1023
|
+
newNode.next = existingNode;
|
|
1024
|
+
this._length++;
|
|
1025
|
+
}
|
|
1026
|
+
return true;
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Insert a new element/node after an existing one.
|
|
1030
|
+
* @remarks Time O(N), Space O(1)
|
|
1031
|
+
* @param existingElementOrNode - Existing element or node.
|
|
1032
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1033
|
+
* @returns True if inserted.
|
|
1034
|
+
*/
|
|
1035
|
+
addAfter(existingElementOrNode, newElementOrNode) {
|
|
1036
|
+
const existingNode = this.getNode(existingElementOrNode);
|
|
1037
|
+
if (!existingNode) return false;
|
|
1038
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
1039
|
+
newNode.next = existingNode.next;
|
|
1040
|
+
existingNode.next = newNode;
|
|
1041
|
+
if (existingNode === this.tail) this._tail = newNode;
|
|
1042
|
+
this._length++;
|
|
1043
|
+
return true;
|
|
1044
|
+
}
|
|
1045
|
+
/**
|
|
1046
|
+
* Remove and/or insert elements at a position (array-like behavior).
|
|
1047
|
+
* @remarks Time O(N + M), Space O(M)
|
|
1048
|
+
* @param start - Start index (clamped to [0, length]).
|
|
1049
|
+
* @param [deleteCount] - Number of elements to remove (default 0).
|
|
1050
|
+
* @param [items] - Elements to insert after `start`.
|
|
1051
|
+
* @returns A new list containing the removed elements (typed as `this`).
|
|
1052
|
+
*/
|
|
1053
|
+
splice(start, deleteCount = 0, ...items) {
|
|
1054
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
1055
|
+
deleteCount = Math.max(0, deleteCount);
|
|
1056
|
+
const removedList = this._createInstance();
|
|
1057
|
+
const prevNode = start === 0 ? void 0 : this.getNodeAt(start - 1);
|
|
1058
|
+
let cur = prevNode ? prevNode.next : this.head;
|
|
1059
|
+
let removedCount = 0;
|
|
1060
|
+
while (removedCount < deleteCount && cur) {
|
|
1061
|
+
removedList.push(cur.value);
|
|
1062
|
+
cur = cur.next;
|
|
1063
|
+
removedCount++;
|
|
1064
|
+
}
|
|
1065
|
+
const afterNode = cur;
|
|
1066
|
+
if (prevNode) {
|
|
1067
|
+
prevNode.next = afterNode;
|
|
1068
|
+
} else {
|
|
1069
|
+
this._head = afterNode;
|
|
1070
|
+
}
|
|
1071
|
+
if (!afterNode) this._tail = prevNode;
|
|
1072
|
+
if (items.length > 0) {
|
|
1073
|
+
let firstInserted;
|
|
1074
|
+
let lastInserted;
|
|
1075
|
+
for (const it of items) {
|
|
1076
|
+
const node = this._ensureNode(it);
|
|
1077
|
+
if (!firstInserted) firstInserted = node;
|
|
1078
|
+
if (lastInserted) lastInserted.next = node;
|
|
1079
|
+
lastInserted = node;
|
|
1080
|
+
}
|
|
1081
|
+
if (prevNode) prevNode.next = firstInserted;
|
|
1082
|
+
else this._head = firstInserted;
|
|
1083
|
+
lastInserted.next = afterNode;
|
|
1084
|
+
if (!afterNode) this._tail = lastInserted;
|
|
1085
|
+
}
|
|
1086
|
+
this._length += items.length - removedCount;
|
|
1087
|
+
if (this._length === 0) {
|
|
1088
|
+
this._head = void 0;
|
|
1089
|
+
this._tail = void 0;
|
|
1090
|
+
}
|
|
1091
|
+
return removedList;
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Count how many nodes match a value/node/predicate.
|
|
1095
|
+
* @remarks Time O(N), Space O(1)
|
|
1096
|
+
* @param elementOrNode - Element, node, or node predicate to match.
|
|
1097
|
+
* @returns Number of matches in the list.
|
|
1098
|
+
*/
|
|
1099
|
+
countOccurrences(elementOrNode) {
|
|
1100
|
+
const predicate = elementOrPredicate(elementOrNode, this._equals);
|
|
1101
|
+
let count = 0;
|
|
1102
|
+
let current = this.head;
|
|
1103
|
+
while (current) {
|
|
1104
|
+
if (predicate(current)) count++;
|
|
1105
|
+
current = current.next;
|
|
1106
|
+
}
|
|
1107
|
+
return count;
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
* Set the equality comparator used to compare values.
|
|
1111
|
+
* @remarks Time O(1), Space O(1)
|
|
1112
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
1113
|
+
* @returns This list.
|
|
1114
|
+
*/
|
|
1115
|
+
setEquality(equals) {
|
|
1116
|
+
this._equals = equals;
|
|
1117
|
+
return this;
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Delete the first node whose value matches a predicate.
|
|
1121
|
+
* @remarks Time O(N), Space O(1)
|
|
1122
|
+
* @param predicate - Predicate (value, index, list) → boolean to decide deletion.
|
|
1123
|
+
* @returns True if a node was removed.
|
|
1124
|
+
*/
|
|
1125
|
+
deleteWhere(predicate) {
|
|
1126
|
+
let prev;
|
|
1127
|
+
let current = this.head;
|
|
1128
|
+
let i = 0;
|
|
1129
|
+
while (current) {
|
|
1130
|
+
if (predicate(current.value, i++, this)) {
|
|
1131
|
+
if (!prev) {
|
|
1132
|
+
this._head = current.next;
|
|
1133
|
+
if (current === this._tail) this._tail = void 0;
|
|
1134
|
+
} else {
|
|
1135
|
+
prev.next = current.next;
|
|
1136
|
+
if (current === this._tail) this._tail = prev;
|
|
1137
|
+
}
|
|
1138
|
+
this._length--;
|
|
1139
|
+
return true;
|
|
1140
|
+
}
|
|
1141
|
+
prev = current;
|
|
1142
|
+
current = current.next;
|
|
1143
|
+
}
|
|
1144
|
+
return false;
|
|
1145
|
+
}
|
|
1146
|
+
/**
|
|
1147
|
+
* Deep clone this list (values are copied by reference).
|
|
1148
|
+
* @remarks Time O(N), Space O(N)
|
|
1149
|
+
* @returns A new list with the same element sequence.
|
|
1150
|
+
*/
|
|
1151
|
+
clone() {
|
|
1152
|
+
const out = this._createInstance();
|
|
1153
|
+
for (const v of this) out.push(v);
|
|
1154
|
+
return out;
|
|
1155
|
+
}
|
|
1156
|
+
/**
|
|
1157
|
+
* Filter values into a new list of the same class.
|
|
1158
|
+
* @remarks Time O(N), Space O(N)
|
|
1159
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1160
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1161
|
+
* @returns A new list with kept values.
|
|
1162
|
+
*/
|
|
1163
|
+
filter(callback, thisArg) {
|
|
1164
|
+
const out = this._createInstance();
|
|
1165
|
+
let index = 0;
|
|
1166
|
+
for (const value of this) if (callback.call(thisArg, value, index++, this)) out.push(value);
|
|
1167
|
+
return out;
|
|
1168
|
+
}
|
|
1169
|
+
/**
|
|
1170
|
+
* Map values into a new list of the same class.
|
|
1171
|
+
* @remarks Time O(N), Space O(N)
|
|
1172
|
+
* @param callback - Mapping function (value, index, list) → newValue.
|
|
1173
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1174
|
+
* @returns A new list with mapped values.
|
|
1175
|
+
*/
|
|
1176
|
+
mapSame(callback, thisArg) {
|
|
1177
|
+
const out = this._createInstance();
|
|
1178
|
+
let index = 0;
|
|
1179
|
+
for (const value of this) {
|
|
1180
|
+
const mv = thisArg === void 0 ? callback(value, index++, this) : callback.call(thisArg, value, index++, this);
|
|
1181
|
+
out.push(mv);
|
|
1182
|
+
}
|
|
1183
|
+
return out;
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* Map values into a new list (possibly different element type).
|
|
1187
|
+
* @remarks Time O(N), Space O(N)
|
|
1188
|
+
* @template EM
|
|
1189
|
+
* @template RM
|
|
1190
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1191
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1192
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1193
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1194
|
+
*/
|
|
1195
|
+
map(callback, options, thisArg) {
|
|
1196
|
+
const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
|
|
1197
|
+
let index = 0;
|
|
1198
|
+
for (const value of this) out.push(callback.call(thisArg, value, index++, this));
|
|
1199
|
+
return out;
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* (Protected) Create a node from a value.
|
|
1203
|
+
* @remarks Time O(1), Space O(1)
|
|
1204
|
+
* @param value - Value to wrap in a node.
|
|
1205
|
+
* @returns A new SinglyLinkedListNode instance.
|
|
1206
|
+
*/
|
|
1207
|
+
createNode(value) {
|
|
1208
|
+
return new SinglyLinkedListNode(value);
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* (Protected) Check if input is a node predicate function.
|
|
1212
|
+
* @remarks Time O(1), Space O(1)
|
|
1213
|
+
* @param elementNodeOrPredicate - Element, node, or node predicate.
|
|
1214
|
+
* @returns True if input is a predicate function.
|
|
1215
|
+
*/
|
|
1216
|
+
_isPredicate(elementNodeOrPredicate) {
|
|
1217
|
+
return typeof elementNodeOrPredicate === "function";
|
|
1218
|
+
}
|
|
1219
|
+
/**
|
|
1220
|
+
* (Protected) Normalize input into a node instance.
|
|
1221
|
+
* @remarks Time O(1), Space O(1)
|
|
1222
|
+
* @param elementOrNode - Element or node.
|
|
1223
|
+
* @returns A SinglyLinkedListNode for the provided input.
|
|
1224
|
+
*/
|
|
1225
|
+
_ensureNode(elementOrNode) {
|
|
1226
|
+
if (this.isNode(elementOrNode)) return elementOrNode;
|
|
1227
|
+
return this.createNode(elementOrNode);
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* (Protected) Normalize input into a node predicate.
|
|
1231
|
+
* @remarks Time O(1), Space O(1)
|
|
1232
|
+
* @param elementNodeOrPredicate - Element, node, or predicate.
|
|
1233
|
+
* @returns A predicate taking a node and returning true/false.
|
|
1234
|
+
*/
|
|
1235
|
+
_ensurePredicate(elementNodeOrPredicate) {
|
|
1236
|
+
if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
|
|
1237
|
+
if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
1238
|
+
const value = elementNodeOrPredicate;
|
|
1239
|
+
return (node) => this._equals(node.value, value);
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* (Protected) Get the previous node of a given node.
|
|
1243
|
+
* @remarks Time O(N), Space O(1)
|
|
1244
|
+
* @param node - A node in the list.
|
|
1245
|
+
* @returns Previous node or undefined.
|
|
1246
|
+
*/
|
|
1247
|
+
_getPrevNode(node) {
|
|
1248
|
+
if (!this.head || this.head === node) return void 0;
|
|
1249
|
+
let current = this.head;
|
|
1250
|
+
while (current.next && current.next !== node) current = current.next;
|
|
1251
|
+
return current.next === node ? current : void 0;
|
|
1252
|
+
}
|
|
1253
|
+
/**
|
|
1254
|
+
* (Protected) Iterate values from head to tail.
|
|
1255
|
+
* @remarks Time O(N), Space O(1)
|
|
1256
|
+
* @returns Iterator of values (E).
|
|
1257
|
+
*/
|
|
1258
|
+
*_getIterator() {
|
|
1259
|
+
let current = this.head;
|
|
1260
|
+
while (current) {
|
|
1261
|
+
yield current.value;
|
|
1262
|
+
current = current.next;
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* (Protected) Iterate values from tail to head.
|
|
1267
|
+
* @remarks Time O(N), Space O(N)
|
|
1268
|
+
* @returns Iterator of values (E).
|
|
1269
|
+
*/
|
|
1270
|
+
*_getReverseIterator() {
|
|
1271
|
+
const reversedArr = [...this].reverse();
|
|
1272
|
+
for (const item of reversedArr) yield item;
|
|
1273
|
+
}
|
|
1274
|
+
/**
|
|
1275
|
+
* (Protected) Iterate nodes from head to tail.
|
|
1276
|
+
* @remarks Time O(N), Space O(1)
|
|
1277
|
+
* @returns Iterator of nodes.
|
|
1278
|
+
*/
|
|
1279
|
+
*_getNodeIterator() {
|
|
1280
|
+
let current = this.head;
|
|
1281
|
+
while (current) {
|
|
1282
|
+
yield current;
|
|
1283
|
+
current = current.next;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
1288
|
+
* @remarks Time O(1), Space O(1)
|
|
1289
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1290
|
+
* @returns An empty like-kind list instance.
|
|
1291
|
+
*/
|
|
1292
|
+
_createInstance(options) {
|
|
1293
|
+
const Ctor = this.constructor;
|
|
1294
|
+
return new Ctor([], options);
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
1298
|
+
* @remarks Time O(N), Space O(N)
|
|
1299
|
+
* @template EM
|
|
1300
|
+
* @template RM
|
|
1301
|
+
* @param [elements] - Iterable used to seed the new list.
|
|
1302
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1303
|
+
* @returns A like-kind SinglyLinkedList instance.
|
|
1304
|
+
*/
|
|
1305
|
+
_createLike(elements = [], options) {
|
|
1306
|
+
const Ctor = this.constructor;
|
|
1307
|
+
return new Ctor(elements, options);
|
|
1308
|
+
}
|
|
1309
|
+
/**
|
|
1310
|
+
* (Protected) Spawn an empty like-kind list instance.
|
|
1311
|
+
* @remarks Time O(1), Space O(1)
|
|
1312
|
+
* @template EM
|
|
1313
|
+
* @template RM
|
|
1314
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1315
|
+
* @returns An empty like-kind SinglyLinkedList instance.
|
|
1316
|
+
*/
|
|
1317
|
+
_spawnLike(options) {
|
|
1318
|
+
return this._createLike([], options);
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1321
|
+
function elementOrPredicate(input, equals) {
|
|
1322
|
+
if (input instanceof SinglyLinkedListNode) return (node) => node === input;
|
|
1323
|
+
if (typeof input === "function") return input;
|
|
1324
|
+
const value = input;
|
|
1325
|
+
return (node) => equals(node.value, value);
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// src/data-structures/linked-list/doubly-linked-list.ts
|
|
1329
|
+
var DoublyLinkedListNode = class extends LinkedListNode {
|
|
1330
|
+
/**
|
|
1331
|
+
* Create a node.
|
|
1332
|
+
* @remarks Time O(1), Space O(1)
|
|
1333
|
+
* @param value - Element value to store.
|
|
1334
|
+
* @returns New node instance.
|
|
1335
|
+
*/
|
|
1336
|
+
constructor(value) {
|
|
1337
|
+
super(value);
|
|
1338
|
+
__publicField(this, "_next");
|
|
1339
|
+
__publicField(this, "_prev");
|
|
1340
|
+
this._value = value;
|
|
1341
|
+
this._next = void 0;
|
|
1342
|
+
this._prev = void 0;
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Get the next node link.
|
|
1346
|
+
* @remarks Time O(1), Space O(1)
|
|
1347
|
+
* @returns Next node or undefined.
|
|
1348
|
+
*/
|
|
1349
|
+
get next() {
|
|
1350
|
+
return this._next;
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Set the next node link.
|
|
1354
|
+
* @remarks Time O(1), Space O(1)
|
|
1355
|
+
* @param value - Next node or undefined.
|
|
1356
|
+
* @returns void
|
|
1357
|
+
*/
|
|
1358
|
+
set next(value) {
|
|
1359
|
+
this._next = value;
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* Get the previous node link.
|
|
1363
|
+
* @remarks Time O(1), Space O(1)
|
|
1364
|
+
* @returns Previous node or undefined.
|
|
1365
|
+
*/
|
|
1366
|
+
get prev() {
|
|
1367
|
+
return this._prev;
|
|
1368
|
+
}
|
|
1369
|
+
/**
|
|
1370
|
+
* Set the previous node link.
|
|
1371
|
+
* @remarks Time O(1), Space O(1)
|
|
1372
|
+
* @param value - Previous node or undefined.
|
|
1373
|
+
* @returns void
|
|
1374
|
+
*/
|
|
1375
|
+
set prev(value) {
|
|
1376
|
+
this._prev = value;
|
|
1377
|
+
}
|
|
1378
|
+
};
|
|
1379
|
+
var DoublyLinkedList = class extends LinearLinkedBase {
|
|
1380
|
+
/**
|
|
1381
|
+
* Create a DoublyLinkedList and optionally bulk-insert elements.
|
|
1382
|
+
* @remarks Time O(N), Space O(N)
|
|
1383
|
+
* @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
|
|
1384
|
+
* @param [options] - Options such as maxLen and toElementFn.
|
|
1385
|
+
* @returns New DoublyLinkedList instance.
|
|
1386
|
+
*/
|
|
1387
|
+
constructor(elements = [], options) {
|
|
1388
|
+
super(options);
|
|
1389
|
+
__publicField(this, "_equals", Object.is);
|
|
1390
|
+
__publicField(this, "_head");
|
|
1391
|
+
__publicField(this, "_tail");
|
|
1392
|
+
__publicField(this, "_length", 0);
|
|
1393
|
+
this._head = void 0;
|
|
1394
|
+
this._tail = void 0;
|
|
1395
|
+
this._length = 0;
|
|
1396
|
+
if ((options == null ? void 0 : options.maxLen) && Number.isInteger(options.maxLen) && options.maxLen > 0) {
|
|
1397
|
+
this._maxLen = options.maxLen;
|
|
1398
|
+
}
|
|
1399
|
+
this.pushMany(elements);
|
|
1400
|
+
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Get the head node.
|
|
1403
|
+
* @remarks Time O(1), Space O(1)
|
|
1404
|
+
* @returns Head node or undefined.
|
|
1405
|
+
*/
|
|
1406
|
+
get head() {
|
|
1407
|
+
return this._head;
|
|
1408
|
+
}
|
|
1409
|
+
/**
|
|
1410
|
+
* Get the tail node.
|
|
1411
|
+
* @remarks Time O(1), Space O(1)
|
|
1412
|
+
* @returns Tail node or undefined.
|
|
1413
|
+
*/
|
|
1414
|
+
get tail() {
|
|
1415
|
+
return this._tail;
|
|
1416
|
+
}
|
|
1417
|
+
/**
|
|
1418
|
+
* Get the number of elements.
|
|
1419
|
+
* @remarks Time O(1), Space O(1)
|
|
1420
|
+
* @returns Current length.
|
|
1421
|
+
*/
|
|
1422
|
+
get length() {
|
|
1423
|
+
return this._length;
|
|
1424
|
+
}
|
|
1425
|
+
/**
|
|
1426
|
+
* Get the first element value.
|
|
1427
|
+
* @remarks Time O(1), Space O(1)
|
|
1428
|
+
* @returns First element or undefined.
|
|
1429
|
+
*/
|
|
1430
|
+
get first() {
|
|
1431
|
+
var _a;
|
|
1432
|
+
return (_a = this.head) == null ? void 0 : _a.value;
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* Get the last element value.
|
|
1436
|
+
* @remarks Time O(1), Space O(1)
|
|
1437
|
+
* @returns Last element or undefined.
|
|
1438
|
+
*/
|
|
1439
|
+
get last() {
|
|
1440
|
+
var _a;
|
|
1441
|
+
return (_a = this.tail) == null ? void 0 : _a.value;
|
|
1442
|
+
}
|
|
1443
|
+
/**
|
|
1444
|
+
* Create a new list from an array of elements.
|
|
1445
|
+
* @remarks Time O(N), Space O(N)
|
|
1446
|
+
* @template E
|
|
1447
|
+
* @template R
|
|
1448
|
+
* @param this - The constructor (subclass) to instantiate.
|
|
1449
|
+
* @param data - Array of elements to insert.
|
|
1450
|
+
* @returns A new list populated with the array's elements.
|
|
1451
|
+
*/
|
|
1452
|
+
static fromArray(data) {
|
|
1453
|
+
return new this(data);
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* Type guard: check whether the input is a DoublyLinkedListNode.
|
|
1457
|
+
* @remarks Time O(1), Space O(1)
|
|
1458
|
+
* @param elementNodeOrPredicate - Element, node, or predicate.
|
|
1459
|
+
* @returns True if the value is a DoublyLinkedListNode.
|
|
1460
|
+
*/
|
|
1461
|
+
isNode(elementNodeOrPredicate) {
|
|
1462
|
+
return elementNodeOrPredicate instanceof DoublyLinkedListNode;
|
|
1463
|
+
}
|
|
1464
|
+
/**
|
|
1465
|
+
* Append an element/node to the tail.
|
|
1466
|
+
* @remarks Time O(1), Space O(1)
|
|
1467
|
+
* @param elementOrNode - Element or node to append.
|
|
1468
|
+
* @returns True when appended.
|
|
1469
|
+
*/
|
|
1470
|
+
push(elementOrNode) {
|
|
1471
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
1472
|
+
if (!this.head) {
|
|
1473
|
+
this._head = newNode;
|
|
1474
|
+
this._tail = newNode;
|
|
1475
|
+
} else {
|
|
1476
|
+
newNode.prev = this.tail;
|
|
1477
|
+
this.tail.next = newNode;
|
|
1478
|
+
this._tail = newNode;
|
|
1479
|
+
}
|
|
1480
|
+
this._length++;
|
|
1481
|
+
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
1482
|
+
return true;
|
|
1483
|
+
}
|
|
1484
|
+
/**
|
|
1485
|
+
* Remove and return the tail element.
|
|
1486
|
+
* @remarks Time O(1), Space O(1)
|
|
1487
|
+
* @returns Removed element or undefined.
|
|
1488
|
+
*/
|
|
1489
|
+
pop() {
|
|
1490
|
+
if (!this.tail) return void 0;
|
|
1491
|
+
const removed = this.tail;
|
|
1492
|
+
if (this.head === this.tail) {
|
|
1493
|
+
this._head = void 0;
|
|
1494
|
+
this._tail = void 0;
|
|
1495
|
+
} else {
|
|
1496
|
+
this._tail = removed.prev;
|
|
1497
|
+
this.tail.next = void 0;
|
|
1498
|
+
}
|
|
1499
|
+
this._length--;
|
|
1500
|
+
return removed.value;
|
|
1501
|
+
}
|
|
1502
|
+
/**
|
|
1503
|
+
* Remove and return the head element.
|
|
1504
|
+
* @remarks Time O(1), Space O(1)
|
|
1505
|
+
* @returns Removed element or undefined.
|
|
1506
|
+
*/
|
|
1507
|
+
shift() {
|
|
1508
|
+
if (!this.head) return void 0;
|
|
1509
|
+
const removed = this.head;
|
|
1510
|
+
if (this.head === this.tail) {
|
|
1511
|
+
this._head = void 0;
|
|
1512
|
+
this._tail = void 0;
|
|
1513
|
+
} else {
|
|
1514
|
+
this._head = removed.next;
|
|
1515
|
+
this.head.prev = void 0;
|
|
1516
|
+
}
|
|
1517
|
+
this._length--;
|
|
1518
|
+
return removed.value;
|
|
1519
|
+
}
|
|
1520
|
+
/**
|
|
1521
|
+
* Prepend an element/node to the head.
|
|
1522
|
+
* @remarks Time O(1), Space O(1)
|
|
1523
|
+
* @param elementOrNode - Element or node to prepend.
|
|
1524
|
+
* @returns True when prepended.
|
|
1525
|
+
*/
|
|
1526
|
+
unshift(elementOrNode) {
|
|
1527
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
1528
|
+
if (!this.head) {
|
|
1529
|
+
this._head = newNode;
|
|
1530
|
+
this._tail = newNode;
|
|
1531
|
+
} else {
|
|
1532
|
+
newNode.next = this.head;
|
|
1533
|
+
this.head.prev = newNode;
|
|
1534
|
+
this._head = newNode;
|
|
1535
|
+
}
|
|
1536
|
+
this._length++;
|
|
1537
|
+
if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
|
|
1538
|
+
return true;
|
|
1539
|
+
}
|
|
1540
|
+
/**
|
|
1541
|
+
* Append a sequence of elements/nodes.
|
|
1542
|
+
* @remarks Time O(N), Space O(1)
|
|
1543
|
+
* @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
|
|
1544
|
+
* @returns Array of per-element success flags.
|
|
1545
|
+
*/
|
|
1546
|
+
pushMany(elements) {
|
|
1547
|
+
const ans = [];
|
|
1548
|
+
for (const el of elements) {
|
|
1549
|
+
if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
|
|
1550
|
+
else ans.push(this.push(el));
|
|
1551
|
+
}
|
|
1552
|
+
return ans;
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* Prepend a sequence of elements/nodes.
|
|
1556
|
+
* @remarks Time O(N), Space O(1)
|
|
1557
|
+
* @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
|
|
1558
|
+
* @returns Array of per-element success flags.
|
|
1559
|
+
*/
|
|
1560
|
+
unshiftMany(elements) {
|
|
1561
|
+
const ans = [];
|
|
1562
|
+
for (const el of elements) {
|
|
1563
|
+
if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
|
|
1564
|
+
else ans.push(this.unshift(el));
|
|
1565
|
+
}
|
|
1566
|
+
return ans;
|
|
1567
|
+
}
|
|
1568
|
+
/**
|
|
1569
|
+
* Get the element at a given index.
|
|
1570
|
+
* @remarks Time O(N), Space O(1)
|
|
1571
|
+
* @param index - Zero-based index.
|
|
1572
|
+
* @returns Element or undefined.
|
|
1573
|
+
*/
|
|
1574
|
+
at(index) {
|
|
1575
|
+
if (index < 0 || index >= this._length) return void 0;
|
|
1576
|
+
let current = this.head;
|
|
1577
|
+
for (let i = 0; i < index; i++) current = current.next;
|
|
1578
|
+
return current.value;
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Get the node reference at a given index.
|
|
1582
|
+
* @remarks Time O(N), Space O(1)
|
|
1583
|
+
* @param index - Zero-based index.
|
|
1584
|
+
* @returns Node or undefined.
|
|
1585
|
+
*/
|
|
1586
|
+
getNodeAt(index) {
|
|
1587
|
+
if (index < 0 || index >= this._length) return void 0;
|
|
1588
|
+
let current = this.head;
|
|
1589
|
+
for (let i = 0; i < index; i++) current = current.next;
|
|
1590
|
+
return current;
|
|
1591
|
+
}
|
|
1592
|
+
/**
|
|
1593
|
+
* Find a node by value, reference, or predicate.
|
|
1594
|
+
* @remarks Time O(N), Space O(1)
|
|
1595
|
+
* @param [elementNodeOrPredicate] - Element, node, or predicate to match.
|
|
1596
|
+
* @returns Matching node or undefined.
|
|
1597
|
+
*/
|
|
1598
|
+
getNode(elementNodeOrPredicate) {
|
|
1599
|
+
if (elementNodeOrPredicate === void 0) return;
|
|
1600
|
+
if (this.isNode(elementNodeOrPredicate)) {
|
|
1601
|
+
const target = elementNodeOrPredicate;
|
|
1602
|
+
let cur = this.head;
|
|
1603
|
+
while (cur) {
|
|
1604
|
+
if (cur === target) return target;
|
|
1605
|
+
cur = cur.next;
|
|
1606
|
+
}
|
|
1607
|
+
const isMatch = (node) => this._equals(node.value, target.value);
|
|
1608
|
+
cur = this.head;
|
|
1609
|
+
while (cur) {
|
|
1610
|
+
if (isMatch(cur)) return cur;
|
|
1611
|
+
cur = cur.next;
|
|
1612
|
+
}
|
|
1613
|
+
return void 0;
|
|
1614
|
+
}
|
|
1615
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1616
|
+
let current = this.head;
|
|
1617
|
+
while (current) {
|
|
1618
|
+
if (predicate(current)) return current;
|
|
1619
|
+
current = current.next;
|
|
1620
|
+
}
|
|
1621
|
+
return void 0;
|
|
1622
|
+
}
|
|
1623
|
+
/**
|
|
1624
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1625
|
+
* @remarks Time O(N), Space O(1)
|
|
1626
|
+
* @param index - Zero-based index.
|
|
1627
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1628
|
+
* @returns True if inserted.
|
|
1629
|
+
*/
|
|
1630
|
+
addAt(index, newElementOrNode) {
|
|
1631
|
+
if (index < 0 || index > this._length) return false;
|
|
1632
|
+
if (index === 0) return this.unshift(newElementOrNode);
|
|
1633
|
+
if (index === this._length) return this.push(newElementOrNode);
|
|
1634
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
1635
|
+
const prevNode = this.getNodeAt(index - 1);
|
|
1636
|
+
const nextNode = prevNode.next;
|
|
1637
|
+
newNode.prev = prevNode;
|
|
1638
|
+
newNode.next = nextNode;
|
|
1639
|
+
prevNode.next = newNode;
|
|
1640
|
+
nextNode.prev = newNode;
|
|
1641
|
+
this._length++;
|
|
1642
|
+
return true;
|
|
1643
|
+
}
|
|
1644
|
+
/**
|
|
1645
|
+
* Insert a new element/node before an existing one.
|
|
1646
|
+
* @remarks Time O(N), Space O(1)
|
|
1647
|
+
* @param existingElementOrNode - Existing element or node.
|
|
1648
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1649
|
+
* @returns True if inserted.
|
|
1650
|
+
*/
|
|
1651
|
+
addBefore(existingElementOrNode, newElementOrNode) {
|
|
1652
|
+
const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
|
|
1653
|
+
if (!existingNode) return false;
|
|
1654
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
1655
|
+
newNode.prev = existingNode.prev;
|
|
1656
|
+
if (existingNode.prev) existingNode.prev.next = newNode;
|
|
1657
|
+
newNode.next = existingNode;
|
|
1658
|
+
existingNode.prev = newNode;
|
|
1659
|
+
if (existingNode === this.head) this._head = newNode;
|
|
1660
|
+
this._length++;
|
|
1661
|
+
return true;
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* Insert a new element/node after an existing one.
|
|
1665
|
+
* @remarks Time O(N), Space O(1)
|
|
1666
|
+
* @param existingElementOrNode - Existing element or node.
|
|
1667
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1668
|
+
* @returns True if inserted.
|
|
1669
|
+
*/
|
|
1670
|
+
addAfter(existingElementOrNode, newElementOrNode) {
|
|
1671
|
+
const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
|
|
1672
|
+
if (!existingNode) return false;
|
|
1673
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
1674
|
+
newNode.next = existingNode.next;
|
|
1675
|
+
if (existingNode.next) existingNode.next.prev = newNode;
|
|
1676
|
+
newNode.prev = existingNode;
|
|
1677
|
+
existingNode.next = newNode;
|
|
1678
|
+
if (existingNode === this.tail) this._tail = newNode;
|
|
1679
|
+
this._length++;
|
|
1680
|
+
return true;
|
|
1681
|
+
}
|
|
1682
|
+
/**
|
|
1683
|
+
* Set the element value at an index.
|
|
1684
|
+
* @remarks Time O(N), Space O(1)
|
|
1685
|
+
* @param index - Zero-based index.
|
|
1686
|
+
* @param value - New value.
|
|
1687
|
+
* @returns True if updated.
|
|
1688
|
+
*/
|
|
1689
|
+
setAt(index, value) {
|
|
1690
|
+
const node = this.getNodeAt(index);
|
|
1691
|
+
if (!node) return false;
|
|
1692
|
+
node.value = value;
|
|
1693
|
+
return true;
|
|
1694
|
+
}
|
|
1695
|
+
/**
|
|
1696
|
+
* Delete the element at an index.
|
|
1697
|
+
* @remarks Time O(N), Space O(1)
|
|
1698
|
+
* @param index - Zero-based index.
|
|
1699
|
+
* @returns Removed element or undefined.
|
|
1700
|
+
*/
|
|
1701
|
+
deleteAt(index) {
|
|
1702
|
+
if (index < 0 || index >= this._length) return;
|
|
1703
|
+
if (index === 0) return this.shift();
|
|
1704
|
+
if (index === this._length - 1) return this.pop();
|
|
1705
|
+
const removedNode = this.getNodeAt(index);
|
|
1706
|
+
const prevNode = removedNode.prev;
|
|
1707
|
+
const nextNode = removedNode.next;
|
|
1708
|
+
prevNode.next = nextNode;
|
|
1709
|
+
nextNode.prev = prevNode;
|
|
1710
|
+
this._length--;
|
|
1711
|
+
return removedNode.value;
|
|
1712
|
+
}
|
|
1713
|
+
/**
|
|
1714
|
+
* Delete the first match by value/node.
|
|
1715
|
+
* @remarks Time O(N), Space O(1)
|
|
1716
|
+
* @param [elementOrNode] - Element or node to remove.
|
|
1717
|
+
* @returns True if removed.
|
|
1718
|
+
*/
|
|
1719
|
+
delete(elementOrNode) {
|
|
1720
|
+
const node = this.getNode(elementOrNode);
|
|
1721
|
+
if (!node) return false;
|
|
1722
|
+
if (node === this.head) this.shift();
|
|
1723
|
+
else if (node === this.tail) this.pop();
|
|
1724
|
+
else {
|
|
1725
|
+
const prevNode = node.prev;
|
|
1726
|
+
const nextNode = node.next;
|
|
1727
|
+
prevNode.next = nextNode;
|
|
1728
|
+
nextNode.prev = prevNode;
|
|
1729
|
+
this._length--;
|
|
1730
|
+
}
|
|
1731
|
+
return true;
|
|
1732
|
+
}
|
|
1733
|
+
/**
|
|
1734
|
+
* Check whether the list is empty.
|
|
1735
|
+
* @remarks Time O(1), Space O(1)
|
|
1736
|
+
* @returns True if length is 0.
|
|
1737
|
+
*/
|
|
1738
|
+
isEmpty() {
|
|
1739
|
+
return this._length === 0;
|
|
1740
|
+
}
|
|
1741
|
+
/**
|
|
1742
|
+
* Remove all nodes and reset length.
|
|
1743
|
+
* @remarks Time O(N), Space O(1)
|
|
1744
|
+
* @returns void
|
|
1745
|
+
*/
|
|
1746
|
+
clear() {
|
|
1747
|
+
this._head = void 0;
|
|
1748
|
+
this._tail = void 0;
|
|
1749
|
+
this._length = 0;
|
|
1750
|
+
}
|
|
1751
|
+
/**
|
|
1752
|
+
* Find the first value matching a predicate scanning forward.
|
|
1753
|
+
* @remarks Time O(N), Space O(1)
|
|
1754
|
+
* @param elementNodeOrPredicate - Element, node, or predicate to match.
|
|
1755
|
+
* @returns Matched value or undefined.
|
|
1756
|
+
*/
|
|
1757
|
+
search(elementNodeOrPredicate) {
|
|
1758
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1759
|
+
let current = this.head;
|
|
1760
|
+
while (current) {
|
|
1761
|
+
if (predicate(current)) return current.value;
|
|
1762
|
+
current = current.next;
|
|
1763
|
+
}
|
|
1764
|
+
return void 0;
|
|
1765
|
+
}
|
|
1766
|
+
/**
|
|
1767
|
+
* Find the first value matching a predicate scanning backward.
|
|
1768
|
+
* @remarks Time O(N), Space O(1)
|
|
1769
|
+
* @param elementNodeOrPredicate - Element, node, or predicate to match.
|
|
1770
|
+
* @returns Matched value or undefined.
|
|
1771
|
+
*/
|
|
1772
|
+
getBackward(elementNodeOrPredicate) {
|
|
1773
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1774
|
+
let current = this.tail;
|
|
1775
|
+
while (current) {
|
|
1776
|
+
if (predicate(current)) return current.value;
|
|
1777
|
+
current = current.prev;
|
|
1778
|
+
}
|
|
1779
|
+
return void 0;
|
|
1780
|
+
}
|
|
1781
|
+
/**
|
|
1782
|
+
* Reverse the list in place.
|
|
1783
|
+
* @remarks Time O(N), Space O(1)
|
|
1784
|
+
* @returns This list.
|
|
1785
|
+
*/
|
|
1786
|
+
reverse() {
|
|
1787
|
+
let current = this.head;
|
|
1788
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
1789
|
+
while (current) {
|
|
1790
|
+
const next = current.next;
|
|
1791
|
+
[current.prev, current.next] = [current.next, current.prev];
|
|
1792
|
+
current = next;
|
|
1793
|
+
}
|
|
1794
|
+
return this;
|
|
1795
|
+
}
|
|
1796
|
+
/**
|
|
1797
|
+
* Set the equality comparator used to compare values.
|
|
1798
|
+
* @remarks Time O(1), Space O(1)
|
|
1799
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
1800
|
+
* @returns This list.
|
|
1801
|
+
*/
|
|
1802
|
+
setEquality(equals) {
|
|
1803
|
+
this._equals = equals;
|
|
1804
|
+
return this;
|
|
1805
|
+
}
|
|
1806
|
+
/**
|
|
1807
|
+
* Deep clone this list (values are copied by reference).
|
|
1808
|
+
* @remarks Time O(N), Space O(N)
|
|
1809
|
+
* @returns A new list with the same element sequence.
|
|
1810
|
+
*/
|
|
1811
|
+
clone() {
|
|
1812
|
+
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
1813
|
+
for (const v of this) out.push(v);
|
|
1814
|
+
return out;
|
|
1815
|
+
}
|
|
1816
|
+
/**
|
|
1817
|
+
* Filter values into a new list of the same class.
|
|
1818
|
+
* @remarks Time O(N), Space O(N)
|
|
1819
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1820
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1821
|
+
* @returns A new list with kept values.
|
|
1822
|
+
*/
|
|
1823
|
+
filter(callback, thisArg) {
|
|
1824
|
+
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
1825
|
+
let index = 0;
|
|
1826
|
+
for (const v of this) if (callback.call(thisArg, v, index++, this)) out.push(v);
|
|
1827
|
+
return out;
|
|
1828
|
+
}
|
|
1829
|
+
/**
|
|
1830
|
+
* Map values into a new list of the same class.
|
|
1831
|
+
* @remarks Time O(N), Space O(N)
|
|
1832
|
+
* @param callback - Mapping function (value, index, list) → newValue.
|
|
1833
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1834
|
+
* @returns A new list with mapped values.
|
|
1835
|
+
*/
|
|
1836
|
+
mapSame(callback, thisArg) {
|
|
1837
|
+
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
1838
|
+
let index = 0;
|
|
1839
|
+
for (const v of this) {
|
|
1840
|
+
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
1841
|
+
out.push(mv);
|
|
1842
|
+
}
|
|
1843
|
+
return out;
|
|
1844
|
+
}
|
|
1845
|
+
/**
|
|
1846
|
+
* Map values into a new list (possibly different element type).
|
|
1847
|
+
* @remarks Time O(N), Space O(N)
|
|
1848
|
+
* @template EM
|
|
1849
|
+
* @template RM
|
|
1850
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1851
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1852
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1853
|
+
* @returns A new DoublyLinkedList with mapped values.
|
|
1854
|
+
*/
|
|
1855
|
+
map(callback, options, thisArg) {
|
|
1856
|
+
const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
|
|
1857
|
+
let index = 0;
|
|
1858
|
+
for (const v of this) out.push(callback.call(thisArg, v, index++, this));
|
|
1859
|
+
return out;
|
|
1860
|
+
}
|
|
1861
|
+
/**
|
|
1862
|
+
* (Protected) Create or return a node for the given input (node or raw element).
|
|
1863
|
+
* @remarks Time O(1), Space O(1)
|
|
1864
|
+
* @param elementOrNode - Element value or node to normalize.
|
|
1865
|
+
* @returns A DoublyLinkedListNode for the provided input.
|
|
1866
|
+
*/
|
|
1867
|
+
_ensureNode(elementOrNode) {
|
|
1868
|
+
if (this.isNode(elementOrNode)) return elementOrNode;
|
|
1869
|
+
return new DoublyLinkedListNode(elementOrNode);
|
|
1870
|
+
}
|
|
1871
|
+
/**
|
|
1872
|
+
* (Protected) Normalize input into a predicate over nodes.
|
|
1873
|
+
* @remarks Time O(1), Space O(1)
|
|
1874
|
+
* @param elementNodeOrPredicate - Element, node, or node predicate.
|
|
1875
|
+
* @returns A predicate function taking a node and returning true/false.
|
|
1876
|
+
*/
|
|
1877
|
+
_ensurePredicate(elementNodeOrPredicate) {
|
|
1878
|
+
if (this.isNode(elementNodeOrPredicate)) {
|
|
1879
|
+
const target = elementNodeOrPredicate;
|
|
1880
|
+
return (node) => node === target;
|
|
1881
|
+
}
|
|
1882
|
+
if (typeof elementNodeOrPredicate === "function") {
|
|
1883
|
+
return elementNodeOrPredicate;
|
|
1884
|
+
}
|
|
1885
|
+
const value = elementNodeOrPredicate;
|
|
1886
|
+
return (node) => this._equals(node.value, value);
|
|
1887
|
+
}
|
|
1888
|
+
/**
|
|
1889
|
+
* (Protected) Get the previous node of a given node.
|
|
1890
|
+
* @remarks Time O(1), Space O(1)
|
|
1891
|
+
* @param node - A node in the list.
|
|
1892
|
+
* @returns Previous node or undefined.
|
|
1893
|
+
*/
|
|
1894
|
+
_getPrevNode(node) {
|
|
1895
|
+
return node.prev;
|
|
1896
|
+
}
|
|
1897
|
+
/**
|
|
1898
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
1899
|
+
* @remarks Time O(1), Space O(1)
|
|
1900
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1901
|
+
* @returns An empty like-kind list instance.
|
|
1902
|
+
*/
|
|
1903
|
+
_createInstance(options) {
|
|
1904
|
+
const Ctor = this.constructor;
|
|
1905
|
+
return new Ctor([], options);
|
|
1906
|
+
}
|
|
1907
|
+
/**
|
|
1908
|
+
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
1909
|
+
* @remarks Time O(N), Space O(N)
|
|
1910
|
+
* @template EM
|
|
1911
|
+
* @template RM
|
|
1912
|
+
* @param [elements] - Iterable used to seed the new list.
|
|
1913
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1914
|
+
* @returns A like-kind DoublyLinkedList instance.
|
|
1915
|
+
*/
|
|
1916
|
+
_createLike(elements = [], options) {
|
|
1917
|
+
const Ctor = this.constructor;
|
|
1918
|
+
return new Ctor(elements, options);
|
|
1919
|
+
}
|
|
1920
|
+
*_getIterator() {
|
|
1921
|
+
let current = this.head;
|
|
1922
|
+
while (current) {
|
|
1923
|
+
yield current.value;
|
|
1924
|
+
current = current.next;
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
*_getReverseIterator() {
|
|
1928
|
+
let current = this.tail;
|
|
1929
|
+
while (current) {
|
|
1930
|
+
yield current.value;
|
|
1931
|
+
current = current.prev;
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
*_getNodeIterator() {
|
|
1935
|
+
let current = this.head;
|
|
1936
|
+
while (current) {
|
|
1937
|
+
yield current;
|
|
1938
|
+
current = current.next;
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
};
|
|
1942
|
+
|
|
1943
|
+
// src/data-structures/linked-list/skip-linked-list.ts
|
|
1944
|
+
var SkipListNode = class {
|
|
1945
|
+
constructor(key, value, level) {
|
|
1946
|
+
__publicField(this, "key");
|
|
1947
|
+
__publicField(this, "value");
|
|
1948
|
+
__publicField(this, "forward");
|
|
1949
|
+
this.key = key;
|
|
1950
|
+
this.value = value;
|
|
1951
|
+
this.forward = new Array(level);
|
|
1952
|
+
}
|
|
1953
|
+
};
|
|
1954
|
+
var SkipList = class {
|
|
1955
|
+
constructor(elements = [], options) {
|
|
1956
|
+
__publicField(this, "_head", new SkipListNode(void 0, void 0, this.maxLevel));
|
|
1957
|
+
__publicField(this, "_level", 0);
|
|
1958
|
+
__publicField(this, "_maxLevel", 16);
|
|
1959
|
+
__publicField(this, "_probability", 0.5);
|
|
1960
|
+
if (options) {
|
|
1961
|
+
const { maxLevel, probability } = options;
|
|
1962
|
+
if (typeof maxLevel === "number") this._maxLevel = maxLevel;
|
|
1963
|
+
if (typeof probability === "number") this._probability = probability;
|
|
1964
|
+
}
|
|
1965
|
+
if (elements) {
|
|
1966
|
+
for (const [key, value] of elements) this.add(key, value);
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
get head() {
|
|
1970
|
+
return this._head;
|
|
1971
|
+
}
|
|
1972
|
+
get level() {
|
|
1973
|
+
return this._level;
|
|
1974
|
+
}
|
|
1975
|
+
get maxLevel() {
|
|
1976
|
+
return this._maxLevel;
|
|
1977
|
+
}
|
|
1978
|
+
get probability() {
|
|
1979
|
+
return this._probability;
|
|
1980
|
+
}
|
|
1981
|
+
get first() {
|
|
1982
|
+
const firstNode = this.head.forward[0];
|
|
1983
|
+
return firstNode ? firstNode.value : void 0;
|
|
1984
|
+
}
|
|
1985
|
+
get last() {
|
|
1986
|
+
let current = this.head;
|
|
1987
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
1988
|
+
while (current.forward[i]) {
|
|
1989
|
+
current = current.forward[i];
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
return current.value;
|
|
1993
|
+
}
|
|
1994
|
+
add(key, value) {
|
|
1995
|
+
const newNode = new SkipListNode(key, value, this._randomLevel());
|
|
1996
|
+
const update = new Array(this.maxLevel).fill(this.head);
|
|
1997
|
+
let current = this.head;
|
|
1998
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
1999
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
2000
|
+
current = current.forward[i];
|
|
2001
|
+
}
|
|
2002
|
+
update[i] = current;
|
|
2003
|
+
}
|
|
2004
|
+
for (let i = 0; i < newNode.forward.length; i++) {
|
|
2005
|
+
newNode.forward[i] = update[i].forward[i];
|
|
2006
|
+
update[i].forward[i] = newNode;
|
|
2007
|
+
}
|
|
2008
|
+
if (!newNode.forward[0]) {
|
|
2009
|
+
this._level = Math.max(this.level, newNode.forward.length);
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
get(key) {
|
|
2013
|
+
let current = this.head;
|
|
2014
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
2015
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
2016
|
+
current = current.forward[i];
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
current = current.forward[0];
|
|
2020
|
+
if (current && current.key === key) {
|
|
2021
|
+
return current.value;
|
|
2022
|
+
}
|
|
2023
|
+
return void 0;
|
|
2024
|
+
}
|
|
2025
|
+
has(key) {
|
|
2026
|
+
return this.get(key) !== void 0;
|
|
2027
|
+
}
|
|
2028
|
+
delete(key) {
|
|
2029
|
+
const update = new Array(this.maxLevel).fill(this.head);
|
|
2030
|
+
let current = this.head;
|
|
2031
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
2032
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
2033
|
+
current = current.forward[i];
|
|
2034
|
+
}
|
|
2035
|
+
update[i] = current;
|
|
2036
|
+
}
|
|
2037
|
+
current = current.forward[0];
|
|
2038
|
+
if (current && current.key === key) {
|
|
2039
|
+
for (let i = 0; i < this.level; i++) {
|
|
2040
|
+
if (update[i].forward[i] !== current) {
|
|
2041
|
+
break;
|
|
2042
|
+
}
|
|
2043
|
+
update[i].forward[i] = current.forward[i];
|
|
2044
|
+
}
|
|
2045
|
+
while (this.level > 0 && !this.head.forward[this.level - 1]) {
|
|
2046
|
+
this._level--;
|
|
2047
|
+
}
|
|
2048
|
+
return true;
|
|
2049
|
+
}
|
|
2050
|
+
return false;
|
|
2051
|
+
}
|
|
2052
|
+
higher(key) {
|
|
2053
|
+
let current = this.head;
|
|
2054
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
2055
|
+
while (current.forward[i] && current.forward[i].key <= key) {
|
|
2056
|
+
current = current.forward[i];
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
const nextNode = current.forward[0];
|
|
2060
|
+
return nextNode ? nextNode.value : void 0;
|
|
2061
|
+
}
|
|
2062
|
+
lower(key) {
|
|
2063
|
+
let current = this.head;
|
|
2064
|
+
let lastLess = void 0;
|
|
2065
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
2066
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
2067
|
+
current = current.forward[i];
|
|
2068
|
+
}
|
|
2069
|
+
if (current.key < key) {
|
|
2070
|
+
lastLess = current;
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
return lastLess ? lastLess.value : void 0;
|
|
2074
|
+
}
|
|
2075
|
+
_randomLevel() {
|
|
2076
|
+
let level = 1;
|
|
2077
|
+
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
2078
|
+
level++;
|
|
2079
|
+
}
|
|
2080
|
+
return level;
|
|
2081
|
+
}
|
|
2082
|
+
};
|
|
2083
|
+
|
|
2084
|
+
// src/utils/utils.ts
|
|
2085
|
+
function isPrimitiveComparable(value) {
|
|
2086
|
+
const valueType = typeof value;
|
|
2087
|
+
if (valueType === "number") return true;
|
|
2088
|
+
return valueType === "bigint" || valueType === "string" || valueType === "boolean";
|
|
2089
|
+
}
|
|
2090
|
+
function tryObjectToPrimitive(obj) {
|
|
2091
|
+
if (typeof obj.valueOf === "function") {
|
|
2092
|
+
const valueOfResult = obj.valueOf();
|
|
2093
|
+
if (valueOfResult !== obj) {
|
|
2094
|
+
if (isPrimitiveComparable(valueOfResult)) return valueOfResult;
|
|
2095
|
+
if (typeof valueOfResult === "object" && valueOfResult !== null) return tryObjectToPrimitive(valueOfResult);
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
if (typeof obj.toString === "function") {
|
|
2099
|
+
const stringResult = obj.toString();
|
|
2100
|
+
if (stringResult !== "[object Object]") return stringResult;
|
|
2101
|
+
}
|
|
2102
|
+
return null;
|
|
2103
|
+
}
|
|
2104
|
+
function isComparable(value, isForceObjectComparable = false) {
|
|
2105
|
+
if (value === null || value === void 0) return false;
|
|
2106
|
+
if (isPrimitiveComparable(value)) return true;
|
|
2107
|
+
if (typeof value !== "object") return false;
|
|
2108
|
+
if (value instanceof Date) return true;
|
|
2109
|
+
if (isForceObjectComparable) return true;
|
|
2110
|
+
const comparableValue = tryObjectToPrimitive(value);
|
|
2111
|
+
if (comparableValue === null || comparableValue === void 0) return false;
|
|
2112
|
+
return isPrimitiveComparable(comparableValue);
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
// src/common/index.ts
|
|
2116
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
2117
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
2118
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
2119
|
+
return DFSOperation2;
|
|
2120
|
+
})(DFSOperation || {});
|
|
2121
|
+
var Range = class {
|
|
2122
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
2123
|
+
this.low = low;
|
|
2124
|
+
this.high = high;
|
|
2125
|
+
this.includeLow = includeLow;
|
|
2126
|
+
this.includeHigh = includeHigh;
|
|
2127
|
+
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
2128
|
+
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
2129
|
+
}
|
|
2130
|
+
// Determine whether a key is within the range
|
|
2131
|
+
isInRange(key, comparator) {
|
|
2132
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
2133
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
2134
|
+
return lowCheck && highCheck;
|
|
2135
|
+
}
|
|
2136
|
+
};
|
|
2137
|
+
return __toCommonJS(src_exports);
|
|
2138
|
+
})();
|
|
2139
|
+
/**
|
|
2140
|
+
* data-structure-typed
|
|
2141
|
+
*
|
|
2142
|
+
* @author Pablo Zeng
|
|
2143
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
2144
|
+
* @license MIT License
|
|
2145
|
+
*/
|
|
2146
|
+
//# sourceMappingURL=linked-list-typed.js.map
|