data-structure-typed 2.4.5 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
  2. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
  3. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
  4. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
  5. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
  6. package/CHANGELOG.md +3 -1
  7. package/README.md +78 -31
  8. package/dist/cjs/binary-tree.cjs +23698 -0
  9. package/dist/cjs/graph.cjs +5236 -0
  10. package/dist/cjs/hash.cjs +1262 -0
  11. package/dist/cjs/heap.cjs +1540 -0
  12. package/dist/cjs/index.cjs +24509 -2899
  13. package/dist/cjs/linked-list.cjs +4370 -0
  14. package/dist/cjs/matrix.cjs +1042 -0
  15. package/dist/cjs/priority-queue.cjs +1314 -0
  16. package/dist/cjs/queue.cjs +4090 -0
  17. package/dist/cjs/stack.cjs +861 -0
  18. package/dist/cjs/trie.cjs +1173 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +23730 -0
  20. package/dist/cjs-legacy/graph.cjs +5234 -0
  21. package/dist/cjs-legacy/hash.cjs +1262 -0
  22. package/dist/cjs-legacy/heap.cjs +1537 -0
  23. package/dist/cjs-legacy/index.cjs +32555 -10936
  24. package/dist/cjs-legacy/linked-list.cjs +4376 -0
  25. package/dist/cjs-legacy/matrix.cjs +1045 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1312 -0
  27. package/dist/cjs-legacy/queue.cjs +4088 -0
  28. package/dist/cjs-legacy/stack.cjs +861 -0
  29. package/dist/cjs-legacy/trie.cjs +1172 -0
  30. package/dist/esm/binary-tree.mjs +23683 -0
  31. package/dist/esm/graph.mjs +5223 -0
  32. package/dist/esm/hash.mjs +1259 -0
  33. package/dist/esm/heap.mjs +1534 -0
  34. package/dist/esm/index.mjs +24507 -2898
  35. package/dist/esm/linked-list.mjs +4363 -0
  36. package/dist/esm/matrix.mjs +1038 -0
  37. package/dist/esm/priority-queue.mjs +1310 -0
  38. package/dist/esm/queue.mjs +4086 -0
  39. package/dist/esm/stack.mjs +859 -0
  40. package/dist/esm/trie.mjs +1170 -0
  41. package/dist/esm-legacy/binary-tree.mjs +23715 -0
  42. package/dist/esm-legacy/graph.mjs +5221 -0
  43. package/dist/esm-legacy/hash.mjs +1259 -0
  44. package/dist/esm-legacy/heap.mjs +1531 -0
  45. package/dist/esm-legacy/index.mjs +32553 -10935
  46. package/dist/esm-legacy/linked-list.mjs +4369 -0
  47. package/dist/esm-legacy/matrix.mjs +1041 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1308 -0
  49. package/dist/esm-legacy/queue.mjs +4084 -0
  50. package/dist/esm-legacy/stack.mjs +859 -0
  51. package/dist/esm-legacy/trie.mjs +1169 -0
  52. package/dist/types/data-structures/base/index.d.ts +1 -0
  53. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  54. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  55. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +368 -51
  57. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +473 -147
  58. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +931 -80
  59. package/dist/types/data-structures/binary-tree/bst.d.ts +792 -29
  60. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +592 -32
  61. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +320 -135
  62. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3662 -6
  63. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3487 -201
  64. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2778 -65
  65. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3414 -6
  66. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  67. package/dist/types/data-structures/graph/directed-graph.d.ts +419 -47
  68. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  69. package/dist/types/data-structures/graph/undirected-graph.d.ts +384 -59
  70. package/dist/types/data-structures/hash/hash-map.d.ts +462 -89
  71. package/dist/types/data-structures/heap/heap.d.ts +567 -99
  72. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  73. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  74. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +631 -49
  75. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +581 -68
  76. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +775 -12
  77. package/dist/types/data-structures/matrix/matrix.d.ts +491 -0
  78. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  79. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  80. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  81. package/dist/types/data-structures/queue/deque.d.ts +578 -71
  82. package/dist/types/data-structures/queue/queue.d.ts +451 -42
  83. package/dist/types/data-structures/stack/stack.d.ts +374 -32
  84. package/dist/types/data-structures/trie/trie.d.ts +458 -48
  85. package/dist/types/interfaces/graph.d.ts +1 -1
  86. package/dist/types/types/common.d.ts +2 -2
  87. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  88. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  89. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  90. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  91. package/dist/types/types/utils/validate-type.d.ts +4 -4
  92. package/dist/umd/data-structure-typed.js +32432 -10808
  93. package/dist/umd/data-structure-typed.min.js +10 -4
  94. package/docs-site-docusaurus/README.md +41 -0
  95. package/docs-site-docusaurus/docs/api/README.md +52 -0
  96. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6130 -0
  97. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  98. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  99. package/docs-site-docusaurus/docs/api/classes/BST.md +5831 -0
  100. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  101. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  102. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  103. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  104. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  105. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  106. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  107. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  108. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  109. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  110. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  111. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  112. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  113. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  114. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  115. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  116. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  117. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  118. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  119. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  120. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  121. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  122. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  123. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  124. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  125. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  126. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  127. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  128. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6374 -0
  129. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  130. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  131. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  132. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  133. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  134. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1257 -0
  135. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
  136. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -0
  137. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  138. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  139. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  140. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  141. package/docs-site-docusaurus/docs/guide/architecture.md +613 -0
  142. package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
  143. package/docs-site-docusaurus/docs/guide/guides.md +611 -0
  144. package/docs-site-docusaurus/docs/guide/installation.md +60 -0
  145. package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
  146. package/docs-site-docusaurus/docs/guide/overview.md +638 -0
  147. package/docs-site-docusaurus/docs/guide/performance.md +833 -0
  148. package/docs-site-docusaurus/docs/guide/quick-start.md +73 -0
  149. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  150. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  151. package/docs-site-docusaurus/package-lock.json +18667 -0
  152. package/docs-site-docusaurus/package.json +50 -0
  153. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  154. package/docs-site-docusaurus/sidebars.ts +23 -0
  155. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  156. package/docs-site-docusaurus/src/css/custom.css +96 -0
  157. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  158. package/docs-site-docusaurus/src/pages/index.tsx +71 -0
  159. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  160. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  161. package/docs-site-docusaurus/static/.nojekyll +0 -0
  162. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  163. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  164. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  165. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  166. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  167. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  168. package/docs-site-docusaurus/static/img/logo.png +0 -0
  169. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  170. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  171. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  172. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  173. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  174. package/docs-site-docusaurus/static/robots.txt +4 -0
  175. package/docs-site-docusaurus/typedoc.json +23 -0
  176. package/package.json +109 -12
  177. package/src/data-structures/base/index.ts +1 -0
  178. package/src/data-structures/base/iterable-element-base.ts +4 -5
  179. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  180. package/src/data-structures/base/linear-base.ts +3 -3
  181. package/src/data-structures/binary-tree/avl-tree.ts +386 -51
  182. package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
  183. package/src/data-structures/binary-tree/binary-tree.ts +956 -81
  184. package/src/data-structures/binary-tree/bst.ts +840 -35
  185. package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
  186. package/src/data-structures/binary-tree/segment-tree.ts +498 -249
  187. package/src/data-structures/binary-tree/tree-map.ts +3784 -7
  188. package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
  189. package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
  190. package/src/data-structures/binary-tree/tree-set.ts +3531 -10
  191. package/src/data-structures/graph/abstract-graph.ts +4 -4
  192. package/src/data-structures/graph/directed-graph.ts +429 -47
  193. package/src/data-structures/graph/map-graph.ts +59 -1
  194. package/src/data-structures/graph/undirected-graph.ts +393 -59
  195. package/src/data-structures/hash/hash-map.ts +476 -92
  196. package/src/data-structures/heap/heap.ts +581 -99
  197. package/src/data-structures/heap/max-heap.ts +46 -0
  198. package/src/data-structures/heap/min-heap.ts +59 -0
  199. package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
  200. package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
  201. package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
  202. package/src/data-structures/matrix/matrix.ts +584 -12
  203. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  204. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  205. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  206. package/src/data-structures/queue/deque.ts +592 -70
  207. package/src/data-structures/queue/queue.ts +463 -42
  208. package/src/data-structures/stack/stack.ts +384 -32
  209. package/src/data-structures/trie/trie.ts +470 -48
  210. package/src/interfaces/graph.ts +1 -1
  211. package/src/types/common.ts +2 -2
  212. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  213. package/src/types/data-structures/heap/heap.ts +1 -0
  214. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  215. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  216. package/src/types/utils/validate-type.ts +4 -4
  217. package/vercel.json +6 -0
  218. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  219. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  220. package/dist/leetcode/avl-tree.mjs +0 -2720
  221. package/dist/leetcode/binary-tree.mjs +0 -1594
  222. package/dist/leetcode/bst.mjs +0 -2398
  223. package/dist/leetcode/deque.mjs +0 -683
  224. package/dist/leetcode/directed-graph.mjs +0 -1733
  225. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  226. package/dist/leetcode/hash-map.mjs +0 -493
  227. package/dist/leetcode/heap.mjs +0 -542
  228. package/dist/leetcode/max-heap.mjs +0 -375
  229. package/dist/leetcode/max-priority-queue.mjs +0 -383
  230. package/dist/leetcode/min-heap.mjs +0 -363
  231. package/dist/leetcode/min-priority-queue.mjs +0 -371
  232. package/dist/leetcode/priority-queue.mjs +0 -363
  233. package/dist/leetcode/queue.mjs +0 -943
  234. package/dist/leetcode/red-black-tree.mjs +0 -2765
  235. package/dist/leetcode/singly-linked-list.mjs +0 -754
  236. package/dist/leetcode/stack.mjs +0 -217
  237. package/dist/leetcode/tree-counter.mjs +0 -3039
  238. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  239. package/dist/leetcode/trie.mjs +0 -413
  240. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,4376 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __typeError = (msg) => {
5
+ throw TypeError(msg);
6
+ };
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
10
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
11
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
12
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
14
+
15
+ // src/data-structures/base/iterable-element-base.ts
16
+ var _IterableElementBase = class _IterableElementBase {
17
+ /**
18
+ * Create a new iterable base.
19
+ *
20
+ * @param options Optional behavior overrides. When provided, a `toElementFn`
21
+ * is used to convert a raw element (`R`) into a public element (`E`).
22
+ *
23
+ * @remarks
24
+ * Time O(1), Space O(1).
25
+ */
26
+ constructor(options) {
27
+ /**
28
+ * The converter used to transform a raw element (`R`) into a public element (`E`).
29
+ *
30
+ * @remarks
31
+ * Time O(1), Space O(1).
32
+ */
33
+ __publicField(this, "_toElementFn");
34
+ if (options) {
35
+ const { toElementFn } = options;
36
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
37
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
38
+ }
39
+ }
40
+ /**
41
+ * Exposes the current `toElementFn`, if configured.
42
+ *
43
+ * @returns The converter function or `undefined` when not set.
44
+ * @remarks
45
+ * Time O(1), Space O(1).
46
+ */
47
+ get toElementFn() {
48
+ return this._toElementFn;
49
+ }
50
+ /**
51
+ * Returns an iterator over the structure's elements.
52
+ *
53
+ * @param args Optional iterator arguments forwarded to the internal iterator.
54
+ * @returns An `IterableIterator<E>` that yields the elements in traversal order.
55
+ *
56
+ * @remarks
57
+ * Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
58
+ */
59
+ *[Symbol.iterator](...args) {
60
+ yield* this._getIterator(...args);
61
+ }
62
+ /**
63
+ * Returns an iterator over the values (alias of the default iterator).
64
+ *
65
+ * @returns An `IterableIterator<E>` over all elements.
66
+ * @remarks
67
+ * Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
68
+ */
69
+ *values() {
70
+ for (const item of this) yield item;
71
+ }
72
+ /**
73
+ * Tests whether all elements satisfy the predicate.
74
+ *
75
+ * @template TReturn
76
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
77
+ * @param thisArg Optional `this` binding for the predicate.
78
+ * @returns `true` if every element passes; otherwise `false`.
79
+ *
80
+ * @remarks
81
+ * Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
82
+ */
83
+ every(predicate, thisArg) {
84
+ let index = 0;
85
+ for (const item of this) {
86
+ if (thisArg === void 0) {
87
+ if (!predicate(item, index++, this)) return false;
88
+ } else {
89
+ const fn = predicate;
90
+ if (!fn.call(thisArg, item, index++, this)) return false;
91
+ }
92
+ }
93
+ return true;
94
+ }
95
+ /**
96
+ * Tests whether at least one element satisfies the predicate.
97
+ *
98
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
99
+ * @param thisArg Optional `this` binding for the predicate.
100
+ * @returns `true` if any element passes; otherwise `false`.
101
+ *
102
+ * @remarks
103
+ * Time O(n) in the worst case; may exit early on first success. Space O(1).
104
+ */
105
+ some(predicate, thisArg) {
106
+ let index = 0;
107
+ for (const item of this) {
108
+ if (thisArg === void 0) {
109
+ if (predicate(item, index++, this)) return true;
110
+ } else {
111
+ const fn = predicate;
112
+ if (fn.call(thisArg, item, index++, this)) return true;
113
+ }
114
+ }
115
+ return false;
116
+ }
117
+ /**
118
+ * Invokes a callback for each element in iteration order.
119
+ *
120
+ * @param callbackfn Function invoked per element with signature `(value, index, self)`.
121
+ * @param thisArg Optional `this` binding for the callback.
122
+ * @returns `void`.
123
+ *
124
+ * @remarks
125
+ * Time O(n), Space O(1).
126
+ */
127
+ forEach(callbackfn, thisArg) {
128
+ let index = 0;
129
+ for (const item of this) {
130
+ if (thisArg === void 0) {
131
+ callbackfn(item, index++, this);
132
+ } else {
133
+ const fn = callbackfn;
134
+ fn.call(thisArg, item, index++, this);
135
+ }
136
+ }
137
+ }
138
+ // Implementation signature
139
+ find(predicate, thisArg) {
140
+ let index = 0;
141
+ for (const item of this) {
142
+ if (thisArg === void 0) {
143
+ if (predicate(item, index++, this)) return item;
144
+ } else {
145
+ const fn = predicate;
146
+ if (fn.call(thisArg, item, index++, this)) return item;
147
+ }
148
+ }
149
+ return;
150
+ }
151
+ /**
152
+ * Checks whether a strictly-equal element exists in the structure.
153
+ *
154
+ * @param element The element to test with `===` equality.
155
+ * @returns `true` if an equal element is found; otherwise `false`.
156
+ *
157
+ * @remarks
158
+ * Time O(n) in the worst case. Space O(1).
159
+ */
160
+ has(element) {
161
+ for (const ele of this) if (ele === element) return true;
162
+ return false;
163
+ }
164
+ /**
165
+ * Reduces all elements to a single accumulated value.
166
+ *
167
+ * @overload
168
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
169
+ * @returns The final accumulated value typed as `E`.
170
+ *
171
+ * @overload
172
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
173
+ * @param initialValue The initial accumulator value of type `E`.
174
+ * @returns The final accumulated value typed as `E`.
175
+ *
176
+ * @overload
177
+ * @template U The accumulator type when it differs from `E`.
178
+ * @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
179
+ * @param initialValue The initial accumulator value of type `U`.
180
+ * @returns The final accumulated value typed as `U`.
181
+ *
182
+ * @remarks
183
+ * Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
184
+ */
185
+ reduce(callbackfn, initialValue) {
186
+ let index = 0;
187
+ const iter = this[Symbol.iterator]();
188
+ let acc;
189
+ if (arguments.length >= 2) {
190
+ acc = initialValue;
191
+ } else {
192
+ const first = iter.next();
193
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
194
+ acc = first.value;
195
+ index = 1;
196
+ }
197
+ for (const value of iter) {
198
+ acc = callbackfn(acc, value, index++, this);
199
+ }
200
+ return acc;
201
+ }
202
+ /**
203
+ * Materializes the elements into a new array.
204
+ *
205
+ * @returns A shallow array copy of the iteration order.
206
+ * @remarks
207
+ * Time O(n), Space O(n).
208
+ */
209
+ toArray() {
210
+ return [...this];
211
+ }
212
+ /**
213
+ * Returns a representation of the structure suitable for quick visualization.
214
+ * Defaults to an array of elements; subclasses may override to provide richer visuals.
215
+ *
216
+ * @returns A visual representation (array by default).
217
+ * @remarks
218
+ * Time O(n), Space O(n).
219
+ */
220
+ toVisual() {
221
+ return [...this];
222
+ }
223
+ /**
224
+ * Prints `toVisual()` to the console. Intended for quick debugging.
225
+ *
226
+ * @returns `void`.
227
+ * @remarks
228
+ * Time O(n) due to materialization, Space O(n) for the intermediate representation.
229
+ */
230
+ print() {
231
+ console.log(this.toVisual());
232
+ }
233
+ };
234
+ __name(_IterableElementBase, "IterableElementBase");
235
+ var IterableElementBase = _IterableElementBase;
236
+
237
+ // src/data-structures/base/linear-base.ts
238
+ var _LinkedListNode = class _LinkedListNode {
239
+ /**
240
+ * Initialize a node.
241
+ * @param value - Element value.
242
+ * @remarks Time O(1), Space O(1)
243
+ */
244
+ constructor(value) {
245
+ __publicField(this, "_value");
246
+ __publicField(this, "_next");
247
+ this._value = value;
248
+ this._next = void 0;
249
+ }
250
+ /**
251
+ * Element payload getter.
252
+ * @returns Element value.
253
+ * @remarks Time O(1), Space O(1)
254
+ */
255
+ get value() {
256
+ return this._value;
257
+ }
258
+ /**
259
+ * Element payload setter.
260
+ * @param value - New value.
261
+ * @remarks Time O(1), Space O(1)
262
+ */
263
+ set value(value) {
264
+ this._value = value;
265
+ }
266
+ /**
267
+ * Next node getter.
268
+ * @returns Next node or `undefined`.
269
+ * @remarks Time O(1), Space O(1)
270
+ */
271
+ get next() {
272
+ return this._next;
273
+ }
274
+ /**
275
+ * Next node setter.
276
+ * @param value - Next node or `undefined`.
277
+ * @remarks Time O(1), Space O(1)
278
+ */
279
+ set next(value) {
280
+ this._next = value;
281
+ }
282
+ };
283
+ __name(_LinkedListNode, "LinkedListNode");
284
+ var LinkedListNode = _LinkedListNode;
285
+ var _LinearBase = class _LinearBase extends IterableElementBase {
286
+ /**
287
+ * Construct a linear container with runtime options.
288
+ * @param options - `{ maxLen?, ... }` bounds/behavior options.
289
+ * @remarks Time O(1), Space O(1)
290
+ */
291
+ constructor(options) {
292
+ super(options);
293
+ __publicField(this, "_maxLen", -1);
294
+ if (options) {
295
+ const { maxLen } = options;
296
+ if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
297
+ }
298
+ }
299
+ /**
300
+ * Upper bound for length (if positive), or `-1` when unbounded.
301
+ * @returns Maximum allowed length.
302
+ * @remarks Time O(1), Space O(1)
303
+ */
304
+ get maxLen() {
305
+ return this._maxLen;
306
+ }
307
+ /**
308
+ * First index of a value from the left.
309
+ * @param searchElement - Value to match.
310
+ * @param fromIndex - Start position (supports negative index).
311
+ * @returns Index or `-1` if not found.
312
+ * @remarks Time O(n), Space O(1)
313
+ */
314
+ indexOf(searchElement, fromIndex = 0) {
315
+ if (this.length === 0) return -1;
316
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
317
+ if (fromIndex < 0) fromIndex = 0;
318
+ for (let i = fromIndex; i < this.length; i++) {
319
+ const element = this.at(i);
320
+ if (element === searchElement) return i;
321
+ }
322
+ return -1;
323
+ }
324
+ /**
325
+ * Last index of a value from the right.
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
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
332
+ if (this.length === 0) return -1;
333
+ if (fromIndex >= this.length) fromIndex = this.length - 1;
334
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
335
+ for (let i = fromIndex; i >= 0; i--) {
336
+ const element = this.at(i);
337
+ if (element === searchElement) return i;
338
+ }
339
+ return -1;
340
+ }
341
+ /**
342
+ * Find the first index matching a predicate.
343
+ * @param predicate - `(element, index, self) => boolean`.
344
+ * @param thisArg - Optional `this` for callback.
345
+ * @returns Index or `-1`.
346
+ * @remarks Time O(n), Space O(1)
347
+ */
348
+ findIndex(predicate, thisArg) {
349
+ for (let i = 0; i < this.length; i++) {
350
+ const item = this.at(i);
351
+ if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
352
+ }
353
+ return -1;
354
+ }
355
+ /**
356
+ * Concatenate elements and/or containers.
357
+ * @param items - Elements or other containers.
358
+ * @returns New container with combined elements (`this` type).
359
+ * @remarks Time O(sum(length)), Space O(sum(length))
360
+ */
361
+ concat(...items) {
362
+ const newList = this.clone();
363
+ for (const item of items) {
364
+ if (item instanceof _LinearBase) {
365
+ newList.pushMany(item);
366
+ } else {
367
+ newList.push(item);
368
+ }
369
+ }
370
+ return newList;
371
+ }
372
+ /**
373
+ * In-place stable order via array sort semantics.
374
+ * @param compareFn - Comparator `(a, b) => number`.
375
+ * @returns This container.
376
+ * @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
377
+ */
378
+ sort(compareFn) {
379
+ const arr = this.toArray();
380
+ arr.sort(compareFn);
381
+ this.clear();
382
+ for (const item of arr) this.push(item);
383
+ return this;
384
+ }
385
+ /**
386
+ * Remove and/or insert elements at a position (array-compatible).
387
+ * @param start - Start index (supports negative index).
388
+ * @param deleteCount - How many to remove.
389
+ * @param items - Elements to insert.
390
+ * @returns Removed elements as a new list (`this` type).
391
+ * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
392
+ */
393
+ splice(start, deleteCount = 0, ...items) {
394
+ const removedList = this._createInstance();
395
+ start = start < 0 ? this.length + start : start;
396
+ start = Math.max(0, Math.min(start, this.length));
397
+ deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
398
+ for (let i = 0; i < deleteCount; i++) {
399
+ const removed = this.deleteAt(start);
400
+ if (removed !== void 0) {
401
+ removedList.push(removed);
402
+ }
403
+ }
404
+ for (let i = 0; i < items.length; i++) {
405
+ this.addAt(start + i, items[i]);
406
+ }
407
+ return removedList;
408
+ }
409
+ /**
410
+ * Join all elements into a string.
411
+ * @param separator - Separator string.
412
+ * @returns Concatenated string.
413
+ * @remarks Time O(n), Space O(n)
414
+ */
415
+ join(separator = ",") {
416
+ return this.toArray().join(separator);
417
+ }
418
+ /**
419
+ * Snapshot elements into a reversed array.
420
+ * @returns New reversed array.
421
+ * @remarks Time O(n), Space O(n)
422
+ */
423
+ toReversedArray() {
424
+ const array = [];
425
+ for (let i = this.length - 1; i >= 0; i--) {
426
+ array.push(this.at(i));
427
+ }
428
+ return array;
429
+ }
430
+ reduceRight(callbackfn, initialValue) {
431
+ let accumulator = initialValue != null ? initialValue : 0;
432
+ for (let i = this.length - 1; i >= 0; i--) {
433
+ accumulator = callbackfn(accumulator, this.at(i), i, this);
434
+ }
435
+ return accumulator;
436
+ }
437
+ /**
438
+ * Create a shallow copy of a subrange.
439
+ * @param start - Inclusive start (supports negative index).
440
+ * @param end - Exclusive end (supports negative index).
441
+ * @returns New list with the range (`this` type).
442
+ * @remarks Time O(n), Space O(n)
443
+ */
444
+ slice(start = 0, end = this.length) {
445
+ start = start < 0 ? this.length + start : start;
446
+ end = end < 0 ? this.length + end : end;
447
+ const newList = this._createInstance();
448
+ for (let i = start; i < end; i++) {
449
+ newList.push(this.at(i));
450
+ }
451
+ return newList;
452
+ }
453
+ /**
454
+ * Fill a range with a value.
455
+ * @param value - Value to set.
456
+ * @param start - Inclusive start.
457
+ * @param end - Exclusive end.
458
+ * @returns This list.
459
+ * @remarks Time O(n), Space O(1)
460
+ */
461
+ fill(value, start = 0, end = this.length) {
462
+ start = start < 0 ? this.length + start : start;
463
+ end = end < 0 ? this.length + end : end;
464
+ if (start < 0) start = 0;
465
+ if (end > this.length) end = this.length;
466
+ if (start >= end) return this;
467
+ for (let i = start; i < end; i++) {
468
+ this.setAt(i, value);
469
+ }
470
+ return this;
471
+ }
472
+ };
473
+ __name(_LinearBase, "LinearBase");
474
+ var LinearBase = _LinearBase;
475
+ var _LinearLinkedBase = class _LinearLinkedBase extends LinearBase {
476
+ constructor(options) {
477
+ super(options);
478
+ if (options) {
479
+ const { maxLen } = options;
480
+ if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
481
+ }
482
+ }
483
+ /**
484
+ * Linked-list optimized `indexOf` (forwards scan).
485
+ * @param searchElement - Value to match.
486
+ * @param fromIndex - Start position.
487
+ * @returns Index or `-1`.
488
+ * @remarks Time O(n), Space O(1)
489
+ */
490
+ indexOf(searchElement, fromIndex = 0) {
491
+ const iterator = this._getIterator();
492
+ let current = iterator.next();
493
+ let index = 0;
494
+ while (index < fromIndex) {
495
+ current = iterator.next();
496
+ index++;
497
+ }
498
+ while (!current.done) {
499
+ if (current.value === searchElement) return index;
500
+ current = iterator.next();
501
+ index++;
502
+ }
503
+ return -1;
504
+ }
505
+ /**
506
+ * Linked-list optimized `lastIndexOf` (reverse scan).
507
+ * @param searchElement - Value to match.
508
+ * @param fromIndex - Start position.
509
+ * @returns Index or `-1`.
510
+ * @remarks Time O(n), Space O(1)
511
+ */
512
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
513
+ const iterator = this._getReverseIterator();
514
+ let current = iterator.next();
515
+ let index = this.length - 1;
516
+ while (index > fromIndex) {
517
+ current = iterator.next();
518
+ index--;
519
+ }
520
+ while (!current.done) {
521
+ if (current.value === searchElement) return index;
522
+ current = iterator.next();
523
+ index--;
524
+ }
525
+ return -1;
526
+ }
527
+ /**
528
+ * Concatenate lists/elements preserving order.
529
+ * @param items - Elements or `LinearBase` instances.
530
+ * @returns New list with combined elements (`this` type).
531
+ * @remarks Time O(sum(length)), Space O(sum(length))
532
+ */
533
+ concat(...items) {
534
+ const newList = this.clone();
535
+ for (const item of items) {
536
+ if (item instanceof LinearBase) {
537
+ newList.pushMany(item);
538
+ } else {
539
+ newList.push(item);
540
+ }
541
+ }
542
+ return newList;
543
+ }
544
+ /**
545
+ * Slice via forward iteration (no random access required).
546
+ * @param start - Inclusive start (supports negative index).
547
+ * @param end - Exclusive end (supports negative index).
548
+ * @returns New list (`this` type).
549
+ * @remarks Time O(n), Space O(n)
550
+ */
551
+ slice(start = 0, end = this.length) {
552
+ start = start < 0 ? this.length + start : start;
553
+ end = end < 0 ? this.length + end : end;
554
+ const newList = this._createInstance();
555
+ const iterator = this._getIterator();
556
+ let current = iterator.next();
557
+ let c = 0;
558
+ while (c < start) {
559
+ current = iterator.next();
560
+ c++;
561
+ }
562
+ for (let i = start; i < end; i++) {
563
+ newList.push(current.value);
564
+ current = iterator.next();
565
+ }
566
+ return newList;
567
+ }
568
+ /**
569
+ * Splice by walking node iterators from the start index.
570
+ * @param start - Start index.
571
+ * @param deleteCount - How many elements to remove.
572
+ * @param items - Elements to insert after the splice point.
573
+ * @returns Removed elements as a new list (`this` type).
574
+ * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
575
+ */
576
+ splice(start, deleteCount = 0, ...items) {
577
+ const removedList = this._createInstance();
578
+ start = start < 0 ? this.length + start : start;
579
+ start = Math.max(0, Math.min(start, this.length));
580
+ deleteCount = Math.max(0, deleteCount);
581
+ let currentIndex = 0;
582
+ let currentNode = void 0;
583
+ let previousNode = void 0;
584
+ const iterator = this._getNodeIterator();
585
+ for (const node of iterator) {
586
+ if (currentIndex === start) {
587
+ currentNode = node;
588
+ break;
589
+ }
590
+ previousNode = node;
591
+ currentIndex++;
592
+ }
593
+ for (let i = 0; i < deleteCount && currentNode; i++) {
594
+ removedList.push(currentNode.value);
595
+ const nextNode = currentNode.next;
596
+ this.delete(currentNode);
597
+ currentNode = nextNode;
598
+ }
599
+ for (let i = 0; i < items.length; i++) {
600
+ if (previousNode) {
601
+ this.addAfter(previousNode, items[i]);
602
+ previousNode = previousNode.next;
603
+ } else {
604
+ this.addAt(0, items[i]);
605
+ previousNode = this._getNodeIterator().next().value;
606
+ }
607
+ }
608
+ return removedList;
609
+ }
610
+ reduceRight(callbackfn, initialValue) {
611
+ let accumulator = initialValue != null ? initialValue : 0;
612
+ let index = this.length - 1;
613
+ for (const item of this._getReverseIterator()) {
614
+ accumulator = callbackfn(accumulator, item, index--, this);
615
+ }
616
+ return accumulator;
617
+ }
618
+ };
619
+ __name(_LinearLinkedBase, "LinearLinkedBase");
620
+ var LinearLinkedBase = _LinearLinkedBase;
621
+
622
+ // src/data-structures/linked-list/singly-linked-list.ts
623
+ var _SinglyLinkedListNode = class _SinglyLinkedListNode extends LinkedListNode {
624
+ /**
625
+ * Create a list node.
626
+ * @remarks Time O(1), Space O(1)
627
+ * @param value - Element value to store.
628
+ * @returns New node instance.
629
+ */
630
+ constructor(value) {
631
+ super(value);
632
+ __publicField(this, "_next");
633
+ this._value = value;
634
+ this._next = void 0;
635
+ }
636
+ /**
637
+ * Get the next node.
638
+ * @remarks Time O(1), Space O(1)
639
+ * @returns Next node or undefined.
640
+ */
641
+ get next() {
642
+ return this._next;
643
+ }
644
+ /**
645
+ * Set the next node.
646
+ * @remarks Time O(1), Space O(1)
647
+ * @param value - Next node or undefined.
648
+ * @returns void
649
+ */
650
+ set next(value) {
651
+ this._next = value;
652
+ }
653
+ };
654
+ __name(_SinglyLinkedListNode, "SinglyLinkedListNode");
655
+ var SinglyLinkedListNode = _SinglyLinkedListNode;
656
+ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
657
+ /**
658
+ * Create a SinglyLinkedList and optionally bulk-insert elements.
659
+ * @remarks Time O(N), Space O(N)
660
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
661
+ * @param [options] - Options such as maxLen and toElementFn.
662
+ * @returns New SinglyLinkedList instance.
663
+ */
664
+ constructor(elements = [], options) {
665
+ super(options);
666
+ __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
667
+ __publicField(this, "_head");
668
+ __publicField(this, "_tail");
669
+ __publicField(this, "_length", 0);
670
+ this.pushMany(elements);
671
+ }
672
+ /**
673
+ * Get the head node.
674
+ * @remarks Time O(1), Space O(1)
675
+ * @returns Head node or undefined.
676
+ */
677
+ get head() {
678
+ return this._head;
679
+ }
680
+ /**
681
+ * Get the tail node.
682
+ * @remarks Time O(1), Space O(1)
683
+ * @returns Tail node or undefined.
684
+ */
685
+ get tail() {
686
+ return this._tail;
687
+ }
688
+ /**
689
+ * Get the number of elements.
690
+ * @remarks Time O(1), Space O(1)
691
+ * @returns Current length.
692
+ */
693
+ get length() {
694
+ return this._length;
695
+ }
696
+ /**
697
+ * Get the first element value.
698
+ * @remarks Time O(1), Space O(1)
699
+ * @returns First element or undefined.
700
+ */
701
+ get first() {
702
+ var _a;
703
+ return (_a = this.head) == null ? void 0 : _a.value;
704
+ }
705
+ /**
706
+ * Get the last element value.
707
+ * @remarks Time O(1), Space O(1)
708
+ * @returns Last element or undefined.
709
+ */
710
+ get last() {
711
+ var _a;
712
+ return (_a = this.tail) == null ? void 0 : _a.value;
713
+ }
714
+ /**
715
+ * Create a new list from an iterable of elements.
716
+ * @remarks Time O(N), Space O(N)
717
+ * @template E
718
+ * @template R
719
+ * @template S
720
+ * @param this - The constructor (subclass) to instantiate.
721
+ * @param data - Iterable of elements to insert.
722
+ * @param [options] - Options forwarded to the constructor.
723
+ * @returns A new list populated with the iterable's elements.
724
+ */
725
+ static from(data, options) {
726
+ const list = new this([], options);
727
+ for (const x of data) list.push(x);
728
+ return list;
729
+ }
730
+ /**
731
+ * Append an element/node to the tail.
732
+ * @remarks Time O(1), Space O(1)
733
+ * @param elementOrNode - Element or node to append.
734
+ * @returns True when appended.
735
+
736
+
737
+
738
+
739
+
740
+
741
+
742
+
743
+
744
+
745
+
746
+
747
+
748
+
749
+
750
+
751
+
752
+
753
+
754
+
755
+
756
+
757
+
758
+
759
+
760
+
761
+
762
+
763
+
764
+
765
+
766
+ * @example
767
+ * // basic SinglyLinkedList creation and push operation
768
+ * // Create a simple SinglyLinkedList with initial values
769
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
770
+ *
771
+ * // Verify the list maintains insertion order
772
+ * console.log([...list]); // [1, 2, 3, 4, 5];
773
+ *
774
+ * // Check length
775
+ * console.log(list.length); // 5;
776
+ *
777
+ * // Push a new element to the end
778
+ * list.push(6);
779
+ * console.log(list.length); // 6;
780
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
781
+ */
782
+ push(elementOrNode) {
783
+ const newNode = this._ensureNode(elementOrNode);
784
+ if (!this.head) {
785
+ this._head = this._tail = newNode;
786
+ } else {
787
+ this.tail.next = newNode;
788
+ this._tail = newNode;
789
+ }
790
+ this._length++;
791
+ if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
792
+ return true;
793
+ }
794
+ /**
795
+ * Remove and return the tail element.
796
+ * @remarks Time O(N), Space O(1)
797
+ * @returns Removed element or undefined.
798
+
799
+
800
+
801
+
802
+
803
+
804
+
805
+
806
+
807
+
808
+
809
+
810
+
811
+
812
+
813
+
814
+
815
+
816
+
817
+
818
+
819
+
820
+
821
+
822
+
823
+
824
+
825
+
826
+
827
+
828
+
829
+ * @example
830
+ * // SinglyLinkedList pop and shift operations
831
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
832
+ *
833
+ * // Pop removes from the end
834
+ * const last = list.pop();
835
+ * console.log(last); // 50;
836
+ *
837
+ * // Shift removes from the beginning
838
+ * const first = list.shift();
839
+ * console.log(first); // 10;
840
+ *
841
+ * // Verify remaining elements
842
+ * console.log([...list]); // [20, 30, 40];
843
+ * console.log(list.length); // 3;
844
+ */
845
+ pop() {
846
+ var _a;
847
+ if (!this.head) return void 0;
848
+ if (this.head === this.tail) {
849
+ const value2 = this.head.value;
850
+ this._head = void 0;
851
+ this._tail = void 0;
852
+ this._length--;
853
+ return value2;
854
+ }
855
+ let current = this.head;
856
+ while (current.next && current.next !== this.tail) current = current.next;
857
+ const value = (_a = this.tail) == null ? void 0 : _a.value;
858
+ current.next = void 0;
859
+ this._tail = current;
860
+ this._length--;
861
+ return value;
862
+ }
863
+ /**
864
+ * Remove and return the head element.
865
+ * @remarks Time O(1), Space O(1)
866
+ * @returns Removed element or undefined.
867
+
868
+
869
+
870
+
871
+
872
+
873
+
874
+
875
+
876
+
877
+
878
+
879
+
880
+
881
+
882
+
883
+
884
+
885
+
886
+
887
+
888
+
889
+
890
+
891
+
892
+
893
+
894
+
895
+
896
+
897
+
898
+ * @example
899
+ * // Remove from the front
900
+ * const list = new SinglyLinkedList<number>([10, 20, 30]);
901
+ * console.log(list.shift()); // 10;
902
+ * console.log(list.length); // 2;
903
+ */
904
+ shift() {
905
+ if (!this.head) return void 0;
906
+ const removed = this.head;
907
+ this._head = this.head.next;
908
+ if (!this._head) this._tail = void 0;
909
+ this._length--;
910
+ return removed.value;
911
+ }
912
+ /**
913
+ * Prepend an element/node to the head.
914
+ * @remarks Time O(1), Space O(1)
915
+ * @param elementOrNode - Element or node to prepend.
916
+ * @returns True when prepended.
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
926
+
927
+
928
+
929
+
930
+
931
+
932
+
933
+
934
+
935
+
936
+
937
+
938
+
939
+
940
+
941
+
942
+
943
+
944
+
945
+
946
+
947
+
948
+ * @example
949
+ * // SinglyLinkedList unshift and forward traversal
950
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
951
+ *
952
+ * // Unshift adds to the beginning
953
+ * list.unshift(10);
954
+ * console.log([...list]); // [10, 20, 30, 40];
955
+ *
956
+ * // Access elements (forward traversal only for singly linked)
957
+ * const second = list.at(1);
958
+ * console.log(second); // 20;
959
+ *
960
+ * // SinglyLinkedList allows forward iteration only
961
+ * const elements: number[] = [];
962
+ * for (const item of list) {
963
+ * elements.push(item);
964
+ * }
965
+ * console.log(elements); // [10, 20, 30, 40];
966
+ *
967
+ * console.log(list.length); // 4;
968
+ */
969
+ unshift(elementOrNode) {
970
+ const newNode = this._ensureNode(elementOrNode);
971
+ if (!this.head) {
972
+ this._head = this._tail = newNode;
973
+ } else {
974
+ newNode.next = this.head;
975
+ this._head = newNode;
976
+ }
977
+ this._length++;
978
+ return true;
979
+ }
980
+ /**
981
+ * Append a sequence of elements/nodes.
982
+ * @remarks Time O(N), Space O(1)
983
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
984
+ * @returns Array of per-element success flags.
985
+ */
986
+ pushMany(elements) {
987
+ const ans = [];
988
+ for (const el of elements) {
989
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
990
+ else ans.push(this.push(el));
991
+ }
992
+ return ans;
993
+ }
994
+ /**
995
+ * Prepend a sequence of elements/nodes.
996
+ * @remarks Time O(N), Space O(1)
997
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
998
+ * @returns Array of per-element success flags.
999
+ */
1000
+ unshiftMany(elements) {
1001
+ const ans = [];
1002
+ for (const el of elements) {
1003
+ if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
1004
+ else ans.push(this.unshift(el));
1005
+ }
1006
+ return ans;
1007
+ }
1008
+ /**
1009
+ * Find the first value matching a predicate (by node).
1010
+ * @remarks Time O(N), Space O(1)
1011
+ * @param elementNodeOrPredicate - Element, node, or node predicate to match.
1012
+ * @returns Matched value or undefined.
1013
+ */
1014
+ search(elementNodeOrPredicate) {
1015
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1016
+ let current = this.head;
1017
+ while (current) {
1018
+ if (predicate(current)) return current.value;
1019
+ current = current.next;
1020
+ }
1021
+ return void 0;
1022
+ }
1023
+ /**
1024
+ * Get the element at a given index.
1025
+ * @remarks Time O(N), Space O(1)
1026
+ * @param index - Zero-based index.
1027
+ * @returns Element or undefined.
1028
+
1029
+
1030
+
1031
+
1032
+
1033
+
1034
+
1035
+
1036
+
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+
1057
+
1058
+
1059
+ * @example
1060
+ * // Access element by index
1061
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
1062
+ * console.log(list.at(0)); // 'a';
1063
+ * console.log(list.at(2)); // 'c';
1064
+ * console.log(list.at(3)); // 'd';
1065
+ */
1066
+ at(index) {
1067
+ if (index < 0 || index >= this._length) return void 0;
1068
+ let current = this.head;
1069
+ for (let i = 0; i < index && current; i++) current = current.next;
1070
+ return current == null ? void 0 : current.value;
1071
+ }
1072
+ /**
1073
+ * Type guard: check whether the input is a SinglyLinkedListNode.
1074
+ * @remarks Time O(1), Space O(1)
1075
+ * @param elementNodeOrPredicate - Element, node, or predicate.
1076
+ * @returns True if the value is a SinglyLinkedListNode.
1077
+ */
1078
+ isNode(elementNodeOrPredicate) {
1079
+ return elementNodeOrPredicate instanceof SinglyLinkedListNode;
1080
+ }
1081
+ /**
1082
+ * Get the node reference at a given index.
1083
+ * @remarks Time O(N), Space O(1)
1084
+ * @param index - Zero-based index.
1085
+ * @returns Node or undefined.
1086
+
1087
+
1088
+
1089
+
1090
+
1091
+
1092
+
1093
+
1094
+
1095
+
1096
+
1097
+
1098
+
1099
+
1100
+
1101
+
1102
+
1103
+
1104
+
1105
+
1106
+
1107
+
1108
+
1109
+
1110
+
1111
+
1112
+
1113
+
1114
+ * @example
1115
+ * // Get node at index
1116
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1117
+ * console.log(list.getNodeAt(1)?.value); // 'b';
1118
+ */
1119
+ getNodeAt(index) {
1120
+ if (index < 0 || index >= this._length) return void 0;
1121
+ let current = this.head;
1122
+ for (let i = 0; i < index && current; i++) current = current.next;
1123
+ return current;
1124
+ }
1125
+ /**
1126
+ * Delete the element at an index.
1127
+ * @remarks Time O(N), Space O(1)
1128
+ * @param index - Zero-based index.
1129
+ * @returns Removed element or undefined.
1130
+
1131
+
1132
+
1133
+
1134
+
1135
+
1136
+
1137
+
1138
+
1139
+
1140
+
1141
+
1142
+
1143
+
1144
+
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+ * @example
1159
+ * // Remove by index
1160
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1161
+ * list.deleteAt(1);
1162
+ * console.log(list.toArray()); // ['a', 'c'];
1163
+ */
1164
+ deleteAt(index) {
1165
+ if (index < 0 || index >= this._length) return void 0;
1166
+ if (index === 0) return this.shift();
1167
+ const targetNode = this.getNodeAt(index);
1168
+ const prevNode = this._getPrevNode(targetNode);
1169
+ const value = targetNode.value;
1170
+ prevNode.next = targetNode.next;
1171
+ if (targetNode === this.tail) this._tail = prevNode;
1172
+ this._length--;
1173
+ return value;
1174
+ }
1175
+ /**
1176
+ * Delete the first match by value/node.
1177
+ * @remarks Time O(N), Space O(1)
1178
+ * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
1179
+ * @returns True if removed.
1180
+
1181
+
1182
+
1183
+
1184
+
1185
+
1186
+
1187
+
1188
+
1189
+
1190
+
1191
+
1192
+
1193
+
1194
+
1195
+
1196
+
1197
+
1198
+
1199
+
1200
+
1201
+
1202
+
1203
+
1204
+
1205
+
1206
+
1207
+
1208
+ * @example
1209
+ * // Remove first occurrence
1210
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
1211
+ * list.delete(2);
1212
+ * console.log(list.toArray()); // [1, 3, 2];
1213
+ */
1214
+ delete(elementOrNode) {
1215
+ if (elementOrNode === void 0 || !this.head) return false;
1216
+ const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
1217
+ if (!node) return false;
1218
+ const prevNode = this._getPrevNode(node);
1219
+ if (!prevNode) {
1220
+ this._head = node.next;
1221
+ if (node === this.tail) this._tail = void 0;
1222
+ } else {
1223
+ prevNode.next = node.next;
1224
+ if (node === this.tail) this._tail = prevNode;
1225
+ }
1226
+ this._length--;
1227
+ return true;
1228
+ }
1229
+ /**
1230
+ * Insert a new element/node at an index, shifting following nodes.
1231
+ * @remarks Time O(N), Space O(1)
1232
+ * @param index - Zero-based index.
1233
+ * @param newElementOrNode - Element or node to insert.
1234
+ * @returns True if inserted.
1235
+
1236
+
1237
+
1238
+
1239
+
1240
+
1241
+
1242
+
1243
+
1244
+
1245
+
1246
+
1247
+
1248
+
1249
+
1250
+
1251
+
1252
+
1253
+
1254
+
1255
+
1256
+
1257
+
1258
+
1259
+
1260
+
1261
+
1262
+
1263
+ * @example
1264
+ * // Insert at index
1265
+ * const list = new SinglyLinkedList<number>([1, 3]);
1266
+ * list.addAt(1, 2);
1267
+ * console.log(list.toArray()); // [1, 2, 3];
1268
+ */
1269
+ addAt(index, newElementOrNode) {
1270
+ if (index < 0 || index > this._length) return false;
1271
+ if (index === 0) return this.unshift(newElementOrNode);
1272
+ if (index === this._length) return this.push(newElementOrNode);
1273
+ const newNode = this._ensureNode(newElementOrNode);
1274
+ const prevNode = this.getNodeAt(index - 1);
1275
+ newNode.next = prevNode.next;
1276
+ prevNode.next = newNode;
1277
+ this._length++;
1278
+ return true;
1279
+ }
1280
+ /**
1281
+ * Set the element value at an index.
1282
+ * @remarks Time O(N), Space O(1)
1283
+ * @param index - Zero-based index.
1284
+ * @param value - New value.
1285
+ * @returns True if updated.
1286
+ */
1287
+ setAt(index, value) {
1288
+ const node = this.getNodeAt(index);
1289
+ if (!node) return false;
1290
+ node.value = value;
1291
+ return true;
1292
+ }
1293
+ /**
1294
+ * Check whether the list is empty.
1295
+ * @remarks Time O(1), Space O(1)
1296
+ * @returns True if length is 0.
1297
+
1298
+
1299
+
1300
+
1301
+
1302
+
1303
+
1304
+
1305
+
1306
+
1307
+
1308
+
1309
+
1310
+
1311
+
1312
+
1313
+
1314
+
1315
+
1316
+
1317
+
1318
+
1319
+
1320
+
1321
+
1322
+
1323
+
1324
+
1325
+
1326
+ * @example
1327
+ * // Check empty
1328
+ * console.log(new SinglyLinkedList().isEmpty()); // true;
1329
+ */
1330
+ isEmpty() {
1331
+ return this._length === 0;
1332
+ }
1333
+ /**
1334
+ * Remove all nodes and reset length.
1335
+ * @remarks Time O(N), Space O(1)
1336
+ * @returns void
1337
+
1338
+
1339
+
1340
+
1341
+
1342
+
1343
+
1344
+
1345
+
1346
+
1347
+
1348
+
1349
+
1350
+
1351
+
1352
+
1353
+
1354
+
1355
+
1356
+
1357
+
1358
+
1359
+
1360
+
1361
+
1362
+
1363
+
1364
+
1365
+
1366
+ * @example
1367
+ * // Remove all
1368
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1369
+ * list.clear();
1370
+ * console.log(list.isEmpty()); // true;
1371
+ */
1372
+ clear() {
1373
+ this._head = void 0;
1374
+ this._tail = void 0;
1375
+ this._length = 0;
1376
+ }
1377
+ /**
1378
+ * Reverse the list in place.
1379
+ * @remarks Time O(N), Space O(1)
1380
+ * @returns This list.
1381
+
1382
+
1383
+
1384
+
1385
+
1386
+
1387
+
1388
+
1389
+
1390
+
1391
+
1392
+
1393
+
1394
+
1395
+
1396
+
1397
+
1398
+
1399
+
1400
+
1401
+
1402
+
1403
+
1404
+
1405
+
1406
+
1407
+
1408
+
1409
+
1410
+
1411
+
1412
+ * @example
1413
+ * // Reverse the list in-place
1414
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
1415
+ * list.reverse();
1416
+ * console.log([...list]); // [4, 3, 2, 1];
1417
+ */
1418
+ reverse() {
1419
+ if (!this.head || this.head === this.tail) return this;
1420
+ let prev;
1421
+ let current = this.head;
1422
+ let next;
1423
+ while (current) {
1424
+ next = current.next;
1425
+ current.next = prev;
1426
+ prev = current;
1427
+ current = next;
1428
+ }
1429
+ [this._head, this._tail] = [this.tail, this.head];
1430
+ return this;
1431
+ }
1432
+ /**
1433
+ * Find a node by value, reference, or predicate.
1434
+ * @remarks Time O(N), Space O(1)
1435
+ * @param [elementNodeOrPredicate] - Element, node, or node predicate to match.
1436
+ * @returns Matching node or undefined.
1437
+ */
1438
+ getNode(elementNodeOrPredicate) {
1439
+ if (elementNodeOrPredicate === void 0) return;
1440
+ if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
1441
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1442
+ let current = this.head;
1443
+ while (current) {
1444
+ if (predicate(current)) return current;
1445
+ current = current.next;
1446
+ }
1447
+ return void 0;
1448
+ }
1449
+ /**
1450
+ * Insert a new element/node before an existing one.
1451
+ * @remarks Time O(N), Space O(1)
1452
+ * @param existingElementOrNode - Existing element or node.
1453
+ * @param newElementOrNode - Element or node to insert.
1454
+ * @returns True if inserted.
1455
+ */
1456
+ addBefore(existingElementOrNode, newElementOrNode) {
1457
+ const existingNode = this.getNode(existingElementOrNode);
1458
+ if (!existingNode) return false;
1459
+ const prevNode = this._getPrevNode(existingNode);
1460
+ const newNode = this._ensureNode(newElementOrNode);
1461
+ if (!prevNode) {
1462
+ newNode.next = this._head;
1463
+ this._head = newNode;
1464
+ if (!this._tail) this._tail = newNode;
1465
+ this._length++;
1466
+ } else {
1467
+ prevNode.next = newNode;
1468
+ newNode.next = existingNode;
1469
+ this._length++;
1470
+ }
1471
+ return true;
1472
+ }
1473
+ /**
1474
+ * Insert a new element/node after an existing one.
1475
+ * @remarks Time O(N), Space O(1)
1476
+ * @param existingElementOrNode - Existing element or node.
1477
+ * @param newElementOrNode - Element or node to insert.
1478
+ * @returns True if inserted.
1479
+ */
1480
+ addAfter(existingElementOrNode, newElementOrNode) {
1481
+ const existingNode = this.getNode(existingElementOrNode);
1482
+ if (!existingNode) return false;
1483
+ const newNode = this._ensureNode(newElementOrNode);
1484
+ newNode.next = existingNode.next;
1485
+ existingNode.next = newNode;
1486
+ if (existingNode === this.tail) this._tail = newNode;
1487
+ this._length++;
1488
+ return true;
1489
+ }
1490
+ /**
1491
+ * Remove and/or insert elements at a position (array-like behavior).
1492
+ * @remarks Time O(N + M), Space O(M)
1493
+ * @param start - Start index (clamped to [0, length]).
1494
+ * @param [deleteCount] - Number of elements to remove (default 0).
1495
+ * @param [items] - Elements to insert after `start`.
1496
+ * @returns A new list containing the removed elements (typed as `this`).
1497
+ */
1498
+ splice(start, deleteCount = 0, ...items) {
1499
+ start = Math.max(0, Math.min(start, this.length));
1500
+ deleteCount = Math.max(0, deleteCount);
1501
+ const removedList = this._createInstance();
1502
+ const prevNode = start === 0 ? void 0 : this.getNodeAt(start - 1);
1503
+ let cur = prevNode ? prevNode.next : this.head;
1504
+ let removedCount = 0;
1505
+ while (removedCount < deleteCount && cur) {
1506
+ removedList.push(cur.value);
1507
+ cur = cur.next;
1508
+ removedCount++;
1509
+ }
1510
+ const afterNode = cur;
1511
+ if (prevNode) {
1512
+ prevNode.next = afterNode;
1513
+ } else {
1514
+ this._head = afterNode;
1515
+ }
1516
+ if (!afterNode) this._tail = prevNode;
1517
+ if (items.length > 0) {
1518
+ let firstInserted;
1519
+ let lastInserted;
1520
+ for (const it of items) {
1521
+ const node = this._ensureNode(it);
1522
+ if (!firstInserted) firstInserted = node;
1523
+ if (lastInserted) lastInserted.next = node;
1524
+ lastInserted = node;
1525
+ }
1526
+ if (prevNode) prevNode.next = firstInserted;
1527
+ else this._head = firstInserted;
1528
+ lastInserted.next = afterNode;
1529
+ if (!afterNode) this._tail = lastInserted;
1530
+ }
1531
+ this._length += items.length - removedCount;
1532
+ if (this._length === 0) {
1533
+ this._head = void 0;
1534
+ this._tail = void 0;
1535
+ }
1536
+ return removedList;
1537
+ }
1538
+ /**
1539
+ * Count how many nodes match a value/node/predicate.
1540
+ * @remarks Time O(N), Space O(1)
1541
+ * @param elementOrNode - Element, node, or node predicate to match.
1542
+ * @returns Number of matches in the list.
1543
+ */
1544
+ countOccurrences(elementOrNode) {
1545
+ const predicate = elementOrPredicate(elementOrNode, this._equals);
1546
+ let count = 0;
1547
+ let current = this.head;
1548
+ while (current) {
1549
+ if (predicate(current)) count++;
1550
+ current = current.next;
1551
+ }
1552
+ return count;
1553
+ }
1554
+ /**
1555
+ * Set the equality comparator used to compare values.
1556
+ * @remarks Time O(1), Space O(1)
1557
+ * @param equals - Equality predicate (a, b) → boolean.
1558
+ * @returns This list.
1559
+ */
1560
+ setEquality(equals) {
1561
+ this._equals = equals;
1562
+ return this;
1563
+ }
1564
+ /**
1565
+ * Delete the first node whose value matches a predicate.
1566
+ * @remarks Time O(N), Space O(1)
1567
+ * @param predicate - Predicate (value, index, list) → boolean to decide deletion.
1568
+ * @returns True if a node was removed.
1569
+ */
1570
+ deleteWhere(predicate) {
1571
+ let prev;
1572
+ let current = this.head;
1573
+ let i = 0;
1574
+ while (current) {
1575
+ if (predicate(current.value, i++, this)) {
1576
+ if (!prev) {
1577
+ this._head = current.next;
1578
+ if (current === this._tail) this._tail = void 0;
1579
+ } else {
1580
+ prev.next = current.next;
1581
+ if (current === this._tail) this._tail = prev;
1582
+ }
1583
+ this._length--;
1584
+ return true;
1585
+ }
1586
+ prev = current;
1587
+ current = current.next;
1588
+ }
1589
+ return false;
1590
+ }
1591
+ /**
1592
+ * Deep clone this list (values are copied by reference).
1593
+ * @remarks Time O(N), Space O(N)
1594
+ * @returns A new list with the same element sequence.
1595
+
1596
+
1597
+
1598
+
1599
+
1600
+
1601
+
1602
+
1603
+
1604
+
1605
+
1606
+
1607
+
1608
+
1609
+
1610
+
1611
+
1612
+
1613
+
1614
+
1615
+
1616
+
1617
+
1618
+
1619
+
1620
+
1621
+
1622
+
1623
+
1624
+ * @example
1625
+ * // Deep copy
1626
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1627
+ * const copy = list.clone();
1628
+ * copy.pop();
1629
+ * console.log(list.length); // 3;
1630
+ * console.log(copy.length); // 2;
1631
+ */
1632
+ clone() {
1633
+ const out = this._createInstance();
1634
+ for (const v of this) out.push(v);
1635
+ return out;
1636
+ }
1637
+ /**
1638
+ * Filter values into a new list of the same class.
1639
+ * @remarks Time O(N), Space O(N)
1640
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
1641
+ * @param [thisArg] - Value for `this` inside the callback.
1642
+ * @returns A new list with kept values.
1643
+
1644
+
1645
+
1646
+
1647
+
1648
+
1649
+
1650
+
1651
+
1652
+
1653
+
1654
+
1655
+
1656
+
1657
+
1658
+
1659
+
1660
+
1661
+
1662
+
1663
+
1664
+
1665
+
1666
+
1667
+
1668
+
1669
+
1670
+
1671
+
1672
+
1673
+
1674
+ * @example
1675
+ * // SinglyLinkedList filter and map operations
1676
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
1677
+ *
1678
+ * // Filter even numbers
1679
+ * const filtered = list.filter(value => value % 2 === 0);
1680
+ * console.log(filtered.length); // 2;
1681
+ *
1682
+ * // Map to double values
1683
+ * const doubled = list.map(value => value * 2);
1684
+ * console.log(doubled.length); // 5;
1685
+ *
1686
+ * // Use reduce to sum
1687
+ * const sum = list.reduce((acc, value) => acc + value, 0);
1688
+ * console.log(sum); // 15;
1689
+ */
1690
+ filter(callback, thisArg) {
1691
+ const out = this._createInstance();
1692
+ let index = 0;
1693
+ for (const value of this) if (callback.call(thisArg, value, index++, this)) out.push(value);
1694
+ return out;
1695
+ }
1696
+ /**
1697
+ * Map values into a new list of the same class.
1698
+ * @remarks Time O(N), Space O(N)
1699
+ * @param callback - Mapping function (value, index, list) → newValue.
1700
+ * @param [thisArg] - Value for `this` inside the callback.
1701
+ * @returns A new list with mapped values.
1702
+ */
1703
+ mapSame(callback, thisArg) {
1704
+ const out = this._createInstance();
1705
+ let index = 0;
1706
+ for (const value of this) {
1707
+ const mv = thisArg === void 0 ? callback(value, index++, this) : callback.call(thisArg, value, index++, this);
1708
+ out.push(mv);
1709
+ }
1710
+ return out;
1711
+ }
1712
+ /**
1713
+ * Map values into a new list (possibly different element type).
1714
+ * @remarks Time O(N), Space O(N)
1715
+ * @template EM
1716
+ * @template RM
1717
+ * @param callback - Mapping function (value, index, list) → newElement.
1718
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1719
+ * @param [thisArg] - Value for `this` inside the callback.
1720
+ * @returns A new SinglyLinkedList with mapped values.
1721
+
1722
+
1723
+
1724
+
1725
+
1726
+
1727
+
1728
+
1729
+
1730
+
1731
+
1732
+
1733
+
1734
+
1735
+
1736
+
1737
+
1738
+
1739
+
1740
+
1741
+
1742
+
1743
+
1744
+
1745
+
1746
+
1747
+
1748
+
1749
+
1750
+
1751
+
1752
+ * @example
1753
+ * // Transform elements
1754
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1755
+ * const doubled = list.map(n => n * 2);
1756
+ * console.log([...doubled]); // [2, 4, 6];
1757
+ */
1758
+ map(callback, options, thisArg) {
1759
+ const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
1760
+ let index = 0;
1761
+ for (const value of this) out.push(callback.call(thisArg, value, index++, this));
1762
+ return out;
1763
+ }
1764
+ /**
1765
+ * (Protected) Create a node from a value.
1766
+ * @remarks Time O(1), Space O(1)
1767
+ * @param value - Value to wrap in a node.
1768
+ * @returns A new SinglyLinkedListNode instance.
1769
+ */
1770
+ createNode(value) {
1771
+ return new SinglyLinkedListNode(value);
1772
+ }
1773
+ /**
1774
+ * (Protected) Check if input is a node predicate function.
1775
+ * @remarks Time O(1), Space O(1)
1776
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
1777
+ * @returns True if input is a predicate function.
1778
+ */
1779
+ _isPredicate(elementNodeOrPredicate) {
1780
+ return typeof elementNodeOrPredicate === "function";
1781
+ }
1782
+ /**
1783
+ * (Protected) Normalize input into a node instance.
1784
+ * @remarks Time O(1), Space O(1)
1785
+ * @param elementOrNode - Element or node.
1786
+ * @returns A SinglyLinkedListNode for the provided input.
1787
+ */
1788
+ _ensureNode(elementOrNode) {
1789
+ if (this.isNode(elementOrNode)) return elementOrNode;
1790
+ return this.createNode(elementOrNode);
1791
+ }
1792
+ /**
1793
+ * (Protected) Normalize input into a node predicate.
1794
+ * @remarks Time O(1), Space O(1)
1795
+ * @param elementNodeOrPredicate - Element, node, or predicate.
1796
+ * @returns A predicate taking a node and returning true/false.
1797
+ */
1798
+ _ensurePredicate(elementNodeOrPredicate) {
1799
+ if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
1800
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
1801
+ const value = elementNodeOrPredicate;
1802
+ return (node) => this._equals(node.value, value);
1803
+ }
1804
+ /**
1805
+ * (Protected) Get the previous node of a given node.
1806
+ * @remarks Time O(N), Space O(1)
1807
+ * @param node - A node in the list.
1808
+ * @returns Previous node or undefined.
1809
+ */
1810
+ _getPrevNode(node) {
1811
+ if (!this.head || this.head === node) return void 0;
1812
+ let current = this.head;
1813
+ while (current.next && current.next !== node) current = current.next;
1814
+ return current.next === node ? current : void 0;
1815
+ }
1816
+ /**
1817
+ * (Protected) Iterate values from head to tail.
1818
+ * @remarks Time O(N), Space O(1)
1819
+ * @returns Iterator of values (E).
1820
+ */
1821
+ *_getIterator() {
1822
+ let current = this.head;
1823
+ while (current) {
1824
+ yield current.value;
1825
+ current = current.next;
1826
+ }
1827
+ }
1828
+ /**
1829
+ * (Protected) Iterate values from tail to head.
1830
+ * @remarks Time O(N), Space O(N)
1831
+ * @returns Iterator of values (E).
1832
+ */
1833
+ *_getReverseIterator() {
1834
+ const reversedArr = [...this].reverse();
1835
+ for (const item of reversedArr) yield item;
1836
+ }
1837
+ /**
1838
+ * (Protected) Iterate nodes from head to tail.
1839
+ * @remarks Time O(N), Space O(1)
1840
+ * @returns Iterator of nodes.
1841
+ */
1842
+ *_getNodeIterator() {
1843
+ let current = this.head;
1844
+ while (current) {
1845
+ yield current;
1846
+ current = current.next;
1847
+ }
1848
+ }
1849
+ /**
1850
+ * (Protected) Create an empty instance of the same concrete class.
1851
+ * @remarks Time O(1), Space O(1)
1852
+ * @param [options] - Options forwarded to the constructor.
1853
+ * @returns An empty like-kind list instance.
1854
+ */
1855
+ _createInstance(options) {
1856
+ const Ctor = this.constructor;
1857
+ return new Ctor([], options);
1858
+ }
1859
+ /**
1860
+ * (Protected) Create a like-kind instance and seed it from an iterable.
1861
+ * @remarks Time O(N), Space O(N)
1862
+ * @template EM
1863
+ * @template RM
1864
+ * @param [elements] - Iterable used to seed the new list.
1865
+ * @param [options] - Options forwarded to the constructor.
1866
+ * @returns A like-kind SinglyLinkedList instance.
1867
+ */
1868
+ _createLike(elements = [], options) {
1869
+ const Ctor = this.constructor;
1870
+ return new Ctor(elements, options);
1871
+ }
1872
+ /**
1873
+ * (Protected) Spawn an empty like-kind list instance.
1874
+ * @remarks Time O(1), Space O(1)
1875
+ * @template EM
1876
+ * @template RM
1877
+ * @param [options] - Options forwarded to the constructor.
1878
+ * @returns An empty like-kind SinglyLinkedList instance.
1879
+ */
1880
+ _spawnLike(options) {
1881
+ return this._createLike([], options);
1882
+ }
1883
+ };
1884
+ __name(_SinglyLinkedList, "SinglyLinkedList");
1885
+ var SinglyLinkedList = _SinglyLinkedList;
1886
+ function elementOrPredicate(input, equals) {
1887
+ if (input instanceof SinglyLinkedListNode) return (node) => node === input;
1888
+ if (typeof input === "function") return input;
1889
+ const value = input;
1890
+ return (node) => equals(node.value, value);
1891
+ }
1892
+ __name(elementOrPredicate, "elementOrPredicate");
1893
+
1894
+ // src/data-structures/linked-list/doubly-linked-list.ts
1895
+ var _DoublyLinkedListNode = class _DoublyLinkedListNode extends LinkedListNode {
1896
+ /**
1897
+ * Create a node.
1898
+ * @remarks Time O(1), Space O(1)
1899
+ * @param value - Element value to store.
1900
+ * @returns New node instance.
1901
+ */
1902
+ constructor(value) {
1903
+ super(value);
1904
+ __publicField(this, "_next");
1905
+ __publicField(this, "_prev");
1906
+ this._value = value;
1907
+ this._next = void 0;
1908
+ this._prev = void 0;
1909
+ }
1910
+ /**
1911
+ * Get the next node link.
1912
+ * @remarks Time O(1), Space O(1)
1913
+ * @returns Next node or undefined.
1914
+ */
1915
+ get next() {
1916
+ return this._next;
1917
+ }
1918
+ /**
1919
+ * Set the next node link.
1920
+ * @remarks Time O(1), Space O(1)
1921
+ * @param value - Next node or undefined.
1922
+ * @returns void
1923
+ */
1924
+ set next(value) {
1925
+ this._next = value;
1926
+ }
1927
+ /**
1928
+ * Get the previous node link.
1929
+ * @remarks Time O(1), Space O(1)
1930
+ * @returns Previous node or undefined.
1931
+ */
1932
+ get prev() {
1933
+ return this._prev;
1934
+ }
1935
+ /**
1936
+ * Set the previous node link.
1937
+ * @remarks Time O(1), Space O(1)
1938
+ * @param value - Previous node or undefined.
1939
+ * @returns void
1940
+ */
1941
+ set prev(value) {
1942
+ this._prev = value;
1943
+ }
1944
+ };
1945
+ __name(_DoublyLinkedListNode, "DoublyLinkedListNode");
1946
+ var DoublyLinkedListNode = _DoublyLinkedListNode;
1947
+ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
1948
+ /**
1949
+ * Create a DoublyLinkedList and optionally bulk-insert elements.
1950
+ * @remarks Time O(N), Space O(N)
1951
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
1952
+ * @param [options] - Options such as maxLen and toElementFn.
1953
+ * @returns New DoublyLinkedList instance.
1954
+ */
1955
+ constructor(elements = [], options) {
1956
+ super(options);
1957
+ __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
1958
+ __publicField(this, "_head");
1959
+ __publicField(this, "_tail");
1960
+ __publicField(this, "_length", 0);
1961
+ this._head = void 0;
1962
+ this._tail = void 0;
1963
+ this._length = 0;
1964
+ if ((options == null ? void 0 : options.maxLen) && Number.isInteger(options.maxLen) && options.maxLen > 0) {
1965
+ this._maxLen = options.maxLen;
1966
+ }
1967
+ this.pushMany(elements);
1968
+ }
1969
+ /**
1970
+ * Get the head node.
1971
+ * @remarks Time O(1), Space O(1)
1972
+ * @returns Head node or undefined.
1973
+ */
1974
+ get head() {
1975
+ return this._head;
1976
+ }
1977
+ /**
1978
+ * Get the tail node.
1979
+ * @remarks Time O(1), Space O(1)
1980
+ * @returns Tail node or undefined.
1981
+ */
1982
+ get tail() {
1983
+ return this._tail;
1984
+ }
1985
+ /**
1986
+ * Get the number of elements.
1987
+ * @remarks Time O(1), Space O(1)
1988
+ * @returns Current length.
1989
+ */
1990
+ get length() {
1991
+ return this._length;
1992
+ }
1993
+ /**
1994
+ * Get the first element value.
1995
+ * @remarks Time O(1), Space O(1)
1996
+ * @returns First element or undefined.
1997
+ */
1998
+ get first() {
1999
+ var _a;
2000
+ return (_a = this.head) == null ? void 0 : _a.value;
2001
+ }
2002
+ /**
2003
+ * Get the last element value.
2004
+ * @remarks Time O(1), Space O(1)
2005
+ * @returns Last element or undefined.
2006
+ */
2007
+ get last() {
2008
+ var _a;
2009
+ return (_a = this.tail) == null ? void 0 : _a.value;
2010
+ }
2011
+ /**
2012
+ * Create a new list from an array of elements.
2013
+ * @remarks Time O(N), Space O(N)
2014
+ * @template E
2015
+ * @template R
2016
+ * @param this - The constructor (subclass) to instantiate.
2017
+ * @param data - Array of elements to insert.
2018
+ * @returns A new list populated with the array's elements.
2019
+ */
2020
+ static fromArray(data) {
2021
+ return new this(data);
2022
+ }
2023
+ /**
2024
+ * Type guard: check whether the input is a DoublyLinkedListNode.
2025
+ * @remarks Time O(1), Space O(1)
2026
+ * @param elementNodeOrPredicate - Element, node, or predicate.
2027
+ * @returns True if the value is a DoublyLinkedListNode.
2028
+ */
2029
+ isNode(elementNodeOrPredicate) {
2030
+ return elementNodeOrPredicate instanceof DoublyLinkedListNode;
2031
+ }
2032
+ /**
2033
+ * Append an element/node to the tail.
2034
+ * @remarks Time O(1), Space O(1)
2035
+ * @param elementOrNode - Element or node to append.
2036
+ * @returns True when appended.
2037
+
2038
+
2039
+
2040
+
2041
+
2042
+
2043
+
2044
+
2045
+
2046
+
2047
+
2048
+
2049
+
2050
+
2051
+
2052
+
2053
+
2054
+
2055
+
2056
+
2057
+
2058
+
2059
+
2060
+
2061
+
2062
+
2063
+
2064
+
2065
+
2066
+
2067
+
2068
+ * @example
2069
+ * // basic DoublyLinkedList creation and push operation
2070
+ * // Create a simple DoublyLinkedList with initial values
2071
+ * const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
2072
+ *
2073
+ * // Verify the list maintains insertion order
2074
+ * console.log([...list]); // [1, 2, 3, 4, 5];
2075
+ *
2076
+ * // Check length
2077
+ * console.log(list.length); // 5;
2078
+ *
2079
+ * // Push a new element to the end
2080
+ * list.push(6);
2081
+ * console.log(list.length); // 6;
2082
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
2083
+ */
2084
+ push(elementOrNode) {
2085
+ const newNode = this._ensureNode(elementOrNode);
2086
+ if (!this.head) {
2087
+ this._head = newNode;
2088
+ this._tail = newNode;
2089
+ } else {
2090
+ newNode.prev = this.tail;
2091
+ this.tail.next = newNode;
2092
+ this._tail = newNode;
2093
+ }
2094
+ this._length++;
2095
+ if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
2096
+ return true;
2097
+ }
2098
+ /**
2099
+ * Remove and return the tail element.
2100
+ * @remarks Time O(1), Space O(1)
2101
+ * @returns Removed element or undefined.
2102
+
2103
+
2104
+
2105
+
2106
+
2107
+
2108
+
2109
+
2110
+
2111
+
2112
+
2113
+
2114
+
2115
+
2116
+
2117
+
2118
+
2119
+
2120
+
2121
+
2122
+
2123
+
2124
+
2125
+
2126
+
2127
+
2128
+
2129
+
2130
+
2131
+
2132
+
2133
+ * @example
2134
+ * // DoublyLinkedList pop and shift operations
2135
+ * const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
2136
+ *
2137
+ * // Pop removes from the end
2138
+ * const last = list.pop();
2139
+ * console.log(last); // 50;
2140
+ *
2141
+ * // Shift removes from the beginning
2142
+ * const first = list.shift();
2143
+ * console.log(first); // 10;
2144
+ *
2145
+ * // Verify remaining elements
2146
+ * console.log([...list]); // [20, 30, 40];
2147
+ * console.log(list.length); // 3;
2148
+ */
2149
+ pop() {
2150
+ if (!this.tail) return void 0;
2151
+ const removed = this.tail;
2152
+ if (this.head === this.tail) {
2153
+ this._head = void 0;
2154
+ this._tail = void 0;
2155
+ } else {
2156
+ this._tail = removed.prev;
2157
+ this.tail.next = void 0;
2158
+ }
2159
+ this._length--;
2160
+ return removed.value;
2161
+ }
2162
+ /**
2163
+ * Remove and return the head element.
2164
+ * @remarks Time O(1), Space O(1)
2165
+ * @returns Removed element or undefined.
2166
+
2167
+
2168
+
2169
+
2170
+
2171
+
2172
+
2173
+
2174
+
2175
+
2176
+
2177
+
2178
+
2179
+
2180
+
2181
+
2182
+
2183
+
2184
+
2185
+
2186
+
2187
+
2188
+
2189
+
2190
+
2191
+
2192
+
2193
+
2194
+
2195
+
2196
+
2197
+ * @example
2198
+ * // Remove from the front
2199
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
2200
+ * console.log(list.shift()); // 10;
2201
+ * console.log(list.first); // 20;
2202
+ */
2203
+ shift() {
2204
+ if (!this.head) return void 0;
2205
+ const removed = this.head;
2206
+ if (this.head === this.tail) {
2207
+ this._head = void 0;
2208
+ this._tail = void 0;
2209
+ } else {
2210
+ this._head = removed.next;
2211
+ this.head.prev = void 0;
2212
+ }
2213
+ this._length--;
2214
+ return removed.value;
2215
+ }
2216
+ /**
2217
+ * Prepend an element/node to the head.
2218
+ * @remarks Time O(1), Space O(1)
2219
+ * @param elementOrNode - Element or node to prepend.
2220
+ * @returns True when prepended.
2221
+
2222
+
2223
+
2224
+
2225
+
2226
+
2227
+
2228
+
2229
+
2230
+
2231
+
2232
+
2233
+
2234
+
2235
+
2236
+
2237
+
2238
+
2239
+
2240
+
2241
+
2242
+
2243
+
2244
+
2245
+
2246
+
2247
+
2248
+
2249
+
2250
+
2251
+
2252
+ * @example
2253
+ * // Add to the front
2254
+ * const list = new DoublyLinkedList<number>([2, 3]);
2255
+ * list.unshift(1);
2256
+ * console.log([...list]); // [1, 2, 3];
2257
+ */
2258
+ unshift(elementOrNode) {
2259
+ const newNode = this._ensureNode(elementOrNode);
2260
+ if (!this.head) {
2261
+ this._head = newNode;
2262
+ this._tail = newNode;
2263
+ } else {
2264
+ newNode.next = this.head;
2265
+ this.head.prev = newNode;
2266
+ this._head = newNode;
2267
+ }
2268
+ this._length++;
2269
+ if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
2270
+ return true;
2271
+ }
2272
+ /**
2273
+ * Append a sequence of elements/nodes.
2274
+ * @remarks Time O(N), Space O(1)
2275
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
2276
+ * @returns Array of per-element success flags.
2277
+ */
2278
+ pushMany(elements) {
2279
+ const ans = [];
2280
+ for (const el of elements) {
2281
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
2282
+ else ans.push(this.push(el));
2283
+ }
2284
+ return ans;
2285
+ }
2286
+ /**
2287
+ * Prepend a sequence of elements/nodes.
2288
+ * @remarks Time O(N), Space O(1)
2289
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
2290
+ * @returns Array of per-element success flags.
2291
+ */
2292
+ unshiftMany(elements) {
2293
+ const ans = [];
2294
+ for (const el of elements) {
2295
+ if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
2296
+ else ans.push(this.unshift(el));
2297
+ }
2298
+ return ans;
2299
+ }
2300
+ /**
2301
+ * Get the element at a given index.
2302
+ * @remarks Time O(N), Space O(1)
2303
+ * @param index - Zero-based index.
2304
+ * @returns Element or undefined.
2305
+
2306
+
2307
+
2308
+
2309
+
2310
+
2311
+
2312
+
2313
+
2314
+
2315
+
2316
+
2317
+
2318
+
2319
+
2320
+
2321
+
2322
+
2323
+
2324
+
2325
+
2326
+
2327
+
2328
+
2329
+
2330
+
2331
+
2332
+
2333
+
2334
+
2335
+
2336
+ * @example
2337
+ * // Access by index
2338
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2339
+ * console.log(list.at(1)); // 'b';
2340
+ * console.log(list.at(2)); // 'c';
2341
+ */
2342
+ at(index) {
2343
+ if (index < 0 || index >= this._length) return void 0;
2344
+ let current = this.head;
2345
+ for (let i = 0; i < index && current; i++) current = current.next;
2346
+ return current == null ? void 0 : current.value;
2347
+ }
2348
+ /**
2349
+ * Get the node reference at a given index.
2350
+ * @remarks Time O(N), Space O(1)
2351
+ * @param index - Zero-based index.
2352
+ * @returns Node or undefined.
2353
+
2354
+
2355
+
2356
+
2357
+
2358
+
2359
+
2360
+
2361
+
2362
+
2363
+
2364
+
2365
+
2366
+
2367
+
2368
+
2369
+
2370
+
2371
+
2372
+
2373
+
2374
+
2375
+
2376
+
2377
+
2378
+
2379
+
2380
+
2381
+ * @example
2382
+ * // Get node at index
2383
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2384
+ * console.log(list.getNodeAt(1)?.value); // 'b';
2385
+ */
2386
+ getNodeAt(index) {
2387
+ if (index < 0 || index >= this._length) return void 0;
2388
+ let current = this.head;
2389
+ for (let i = 0; i < index && current; i++) current = current.next;
2390
+ return current;
2391
+ }
2392
+ /**
2393
+ * Find a node by value, reference, or predicate.
2394
+ * @remarks Time O(N), Space O(1)
2395
+ * @param [elementNodeOrPredicate] - Element, node, or predicate to match.
2396
+ * @returns Matching node or undefined.
2397
+ */
2398
+ getNode(elementNodeOrPredicate) {
2399
+ if (elementNodeOrPredicate === void 0) return;
2400
+ if (this.isNode(elementNodeOrPredicate)) {
2401
+ const target = elementNodeOrPredicate;
2402
+ let cur = this.head;
2403
+ while (cur) {
2404
+ if (cur === target) return target;
2405
+ cur = cur.next;
2406
+ }
2407
+ const isMatch = /* @__PURE__ */ __name((node) => this._equals(node.value, target.value), "isMatch");
2408
+ cur = this.head;
2409
+ while (cur) {
2410
+ if (isMatch(cur)) return cur;
2411
+ cur = cur.next;
2412
+ }
2413
+ return void 0;
2414
+ }
2415
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2416
+ let current = this.head;
2417
+ while (current) {
2418
+ if (predicate(current)) return current;
2419
+ current = current.next;
2420
+ }
2421
+ return void 0;
2422
+ }
2423
+ /**
2424
+ * Insert a new element/node at an index, shifting following nodes.
2425
+ * @remarks Time O(N), Space O(1)
2426
+ * @param index - Zero-based index.
2427
+ * @param newElementOrNode - Element or node to insert.
2428
+ * @returns True if inserted.
2429
+
2430
+
2431
+
2432
+
2433
+
2434
+
2435
+
2436
+
2437
+
2438
+
2439
+
2440
+
2441
+
2442
+
2443
+
2444
+
2445
+
2446
+
2447
+
2448
+
2449
+
2450
+
2451
+
2452
+
2453
+
2454
+
2455
+
2456
+
2457
+ * @example
2458
+ * // Insert at position
2459
+ * const list = new DoublyLinkedList<number>([1, 3]);
2460
+ * list.addAt(1, 2);
2461
+ * console.log(list.toArray()); // [1, 2, 3];
2462
+ */
2463
+ addAt(index, newElementOrNode) {
2464
+ if (index < 0 || index > this._length) return false;
2465
+ if (index === 0) return this.unshift(newElementOrNode);
2466
+ if (index === this._length) return this.push(newElementOrNode);
2467
+ const newNode = this._ensureNode(newElementOrNode);
2468
+ const prevNode = this.getNodeAt(index - 1);
2469
+ const nextNode = prevNode.next;
2470
+ newNode.prev = prevNode;
2471
+ newNode.next = nextNode;
2472
+ prevNode.next = newNode;
2473
+ nextNode.prev = newNode;
2474
+ this._length++;
2475
+ return true;
2476
+ }
2477
+ /**
2478
+ * Insert a new element/node before an existing one.
2479
+ * @remarks Time O(N), Space O(1)
2480
+ * @param existingElementOrNode - Existing element or node.
2481
+ * @param newElementOrNode - Element or node to insert.
2482
+ * @returns True if inserted.
2483
+ */
2484
+ addBefore(existingElementOrNode, newElementOrNode) {
2485
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2486
+ if (!existingNode) return false;
2487
+ const newNode = this._ensureNode(newElementOrNode);
2488
+ newNode.prev = existingNode.prev;
2489
+ if (existingNode.prev) existingNode.prev.next = newNode;
2490
+ newNode.next = existingNode;
2491
+ existingNode.prev = newNode;
2492
+ if (existingNode === this.head) this._head = newNode;
2493
+ this._length++;
2494
+ return true;
2495
+ }
2496
+ /**
2497
+ * Insert a new element/node after an existing one.
2498
+ * @remarks Time O(N), Space O(1)
2499
+ * @param existingElementOrNode - Existing element or node.
2500
+ * @param newElementOrNode - Element or node to insert.
2501
+ * @returns True if inserted.
2502
+ */
2503
+ addAfter(existingElementOrNode, newElementOrNode) {
2504
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2505
+ if (!existingNode) return false;
2506
+ const newNode = this._ensureNode(newElementOrNode);
2507
+ newNode.next = existingNode.next;
2508
+ if (existingNode.next) existingNode.next.prev = newNode;
2509
+ newNode.prev = existingNode;
2510
+ existingNode.next = newNode;
2511
+ if (existingNode === this.tail) this._tail = newNode;
2512
+ this._length++;
2513
+ return true;
2514
+ }
2515
+ /**
2516
+ * Set the element value at an index.
2517
+ * @remarks Time O(N), Space O(1)
2518
+ * @param index - Zero-based index.
2519
+ * @param value - New value.
2520
+ * @returns True if updated.
2521
+ */
2522
+ setAt(index, value) {
2523
+ const node = this.getNodeAt(index);
2524
+ if (!node) return false;
2525
+ node.value = value;
2526
+ return true;
2527
+ }
2528
+ /**
2529
+ * Delete the element at an index.
2530
+ * @remarks Time O(N), Space O(1)
2531
+ * @param index - Zero-based index.
2532
+ * @returns Removed element or undefined.
2533
+
2534
+
2535
+
2536
+
2537
+
2538
+
2539
+
2540
+
2541
+
2542
+
2543
+
2544
+
2545
+
2546
+
2547
+
2548
+
2549
+
2550
+
2551
+
2552
+
2553
+
2554
+
2555
+
2556
+
2557
+
2558
+
2559
+
2560
+
2561
+ * @example
2562
+ * // Remove by index
2563
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2564
+ * list.deleteAt(1);
2565
+ * console.log(list.toArray()); // ['a', 'c'];
2566
+ */
2567
+ deleteAt(index) {
2568
+ if (index < 0 || index >= this._length) return;
2569
+ if (index === 0) return this.shift();
2570
+ if (index === this._length - 1) return this.pop();
2571
+ const removedNode = this.getNodeAt(index);
2572
+ const prevNode = removedNode.prev;
2573
+ const nextNode = removedNode.next;
2574
+ prevNode.next = nextNode;
2575
+ nextNode.prev = prevNode;
2576
+ this._length--;
2577
+ return removedNode.value;
2578
+ }
2579
+ /**
2580
+ * Delete the first match by value/node.
2581
+ * @remarks Time O(N), Space O(1)
2582
+ * @param [elementOrNode] - Element or node to remove.
2583
+ * @returns True if removed.
2584
+
2585
+
2586
+
2587
+
2588
+
2589
+
2590
+
2591
+
2592
+
2593
+
2594
+
2595
+
2596
+
2597
+
2598
+
2599
+
2600
+
2601
+
2602
+
2603
+
2604
+
2605
+
2606
+
2607
+
2608
+
2609
+
2610
+
2611
+
2612
+ * @example
2613
+ * // Remove first occurrence
2614
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 2]);
2615
+ * list.delete(2);
2616
+ * console.log(list.toArray()); // [1, 3, 2];
2617
+ */
2618
+ delete(elementOrNode) {
2619
+ const node = this.getNode(elementOrNode);
2620
+ if (!node) return false;
2621
+ if (node === this.head) this.shift();
2622
+ else if (node === this.tail) this.pop();
2623
+ else {
2624
+ const prevNode = node.prev;
2625
+ const nextNode = node.next;
2626
+ prevNode.next = nextNode;
2627
+ nextNode.prev = prevNode;
2628
+ this._length--;
2629
+ }
2630
+ return true;
2631
+ }
2632
+ /**
2633
+ * Check whether the list is empty.
2634
+ * @remarks Time O(1), Space O(1)
2635
+ * @returns True if length is 0.
2636
+
2637
+
2638
+
2639
+
2640
+
2641
+
2642
+
2643
+
2644
+
2645
+
2646
+
2647
+
2648
+
2649
+
2650
+
2651
+
2652
+
2653
+
2654
+
2655
+
2656
+
2657
+
2658
+
2659
+
2660
+
2661
+
2662
+
2663
+
2664
+
2665
+ * @example
2666
+ * // Check empty
2667
+ * console.log(new DoublyLinkedList().isEmpty()); // true;
2668
+ */
2669
+ isEmpty() {
2670
+ return this._length === 0;
2671
+ }
2672
+ /**
2673
+ * Remove all nodes and reset length.
2674
+ * @remarks Time O(N), Space O(1)
2675
+ * @returns void
2676
+
2677
+
2678
+
2679
+
2680
+
2681
+
2682
+
2683
+
2684
+
2685
+
2686
+
2687
+
2688
+
2689
+
2690
+
2691
+
2692
+
2693
+
2694
+
2695
+
2696
+
2697
+
2698
+
2699
+
2700
+
2701
+
2702
+
2703
+
2704
+
2705
+ * @example
2706
+ * // Remove all
2707
+ * const list = new DoublyLinkedList<number>([1, 2]);
2708
+ * list.clear();
2709
+ * console.log(list.isEmpty()); // true;
2710
+ */
2711
+ clear() {
2712
+ this._head = void 0;
2713
+ this._tail = void 0;
2714
+ this._length = 0;
2715
+ }
2716
+ /**
2717
+ * Find the first value matching a predicate scanning forward.
2718
+ * @remarks Time O(N), Space O(1)
2719
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
2720
+ * @returns Matched value or undefined.
2721
+
2722
+
2723
+
2724
+
2725
+
2726
+
2727
+
2728
+
2729
+
2730
+
2731
+
2732
+
2733
+
2734
+
2735
+
2736
+
2737
+
2738
+
2739
+
2740
+
2741
+
2742
+
2743
+
2744
+
2745
+
2746
+
2747
+
2748
+
2749
+ * @example
2750
+ * // Search with predicate
2751
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
2752
+ * const found = list.search(node => node.value > 15);
2753
+ * console.log(found); // 20;
2754
+ */
2755
+ search(elementNodeOrPredicate) {
2756
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2757
+ let current = this.head;
2758
+ while (current) {
2759
+ if (predicate(current)) return current.value;
2760
+ current = current.next;
2761
+ }
2762
+ return void 0;
2763
+ }
2764
+ /**
2765
+ * Find the first value matching a predicate scanning backward.
2766
+ * @remarks Time O(N), Space O(1)
2767
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
2768
+ * @returns Matched value or undefined.
2769
+
2770
+
2771
+
2772
+
2773
+
2774
+
2775
+
2776
+
2777
+
2778
+
2779
+
2780
+
2781
+
2782
+
2783
+
2784
+
2785
+
2786
+
2787
+
2788
+
2789
+
2790
+
2791
+
2792
+
2793
+
2794
+
2795
+
2796
+
2797
+ * @example
2798
+ * // Find value scanning from tail
2799
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
2800
+ * // getBackward scans from tail to head, returns first match
2801
+ * const found = list.getBackward(node => node.value < 4);
2802
+ * console.log(found); // 3;
2803
+ */
2804
+ getBackward(elementNodeOrPredicate) {
2805
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2806
+ let current = this.tail;
2807
+ while (current) {
2808
+ if (predicate(current)) return current.value;
2809
+ current = current.prev;
2810
+ }
2811
+ return void 0;
2812
+ }
2813
+ /**
2814
+ * Reverse the list in place.
2815
+ * @remarks Time O(N), Space O(1)
2816
+ * @returns This list.
2817
+
2818
+
2819
+
2820
+
2821
+
2822
+
2823
+
2824
+
2825
+
2826
+
2827
+
2828
+
2829
+
2830
+
2831
+
2832
+
2833
+
2834
+
2835
+
2836
+
2837
+
2838
+
2839
+
2840
+
2841
+
2842
+
2843
+
2844
+
2845
+
2846
+
2847
+
2848
+ * @example
2849
+ * // Reverse in-place
2850
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
2851
+ * list.reverse();
2852
+ * console.log([...list]); // [3, 2, 1];
2853
+ */
2854
+ reverse() {
2855
+ let current = this.head;
2856
+ [this._head, this._tail] = [this.tail, this.head];
2857
+ while (current) {
2858
+ const next = current.next;
2859
+ [current.prev, current.next] = [current.next, current.prev];
2860
+ current = next;
2861
+ }
2862
+ return this;
2863
+ }
2864
+ /**
2865
+ * Set the equality comparator used to compare values.
2866
+ * @remarks Time O(1), Space O(1)
2867
+ * @param equals - Equality predicate (a, b) → boolean.
2868
+ * @returns This list.
2869
+ */
2870
+ setEquality(equals) {
2871
+ this._equals = equals;
2872
+ return this;
2873
+ }
2874
+ /**
2875
+ * Deep clone this list (values are copied by reference).
2876
+ * @remarks Time O(N), Space O(N)
2877
+ * @returns A new list with the same element sequence.
2878
+
2879
+
2880
+
2881
+
2882
+
2883
+
2884
+
2885
+
2886
+
2887
+
2888
+
2889
+
2890
+
2891
+
2892
+
2893
+
2894
+
2895
+
2896
+
2897
+
2898
+
2899
+
2900
+
2901
+
2902
+
2903
+
2904
+
2905
+
2906
+
2907
+ * @example
2908
+ * // Deep copy
2909
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
2910
+ * const copy = list.clone();
2911
+ * copy.pop();
2912
+ * console.log(list.length); // 3;
2913
+ */
2914
+ clone() {
2915
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
2916
+ for (const v of this) out.push(v);
2917
+ return out;
2918
+ }
2919
+ /**
2920
+ * Filter values into a new list of the same class.
2921
+ * @remarks Time O(N), Space O(N)
2922
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
2923
+ * @param [thisArg] - Value for `this` inside the callback.
2924
+ * @returns A new list with kept values.
2925
+
2926
+
2927
+
2928
+
2929
+
2930
+
2931
+
2932
+
2933
+
2934
+
2935
+
2936
+
2937
+
2938
+
2939
+
2940
+
2941
+
2942
+
2943
+
2944
+
2945
+
2946
+
2947
+
2948
+
2949
+
2950
+
2951
+
2952
+
2953
+
2954
+
2955
+
2956
+ * @example
2957
+ * // Filter elements
2958
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
2959
+ * const evens = list.filter(n => n % 2 === 0);
2960
+ * console.log([...evens]); // [2, 4];
2961
+ */
2962
+ filter(callback, thisArg) {
2963
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
2964
+ let index = 0;
2965
+ for (const v of this) if (callback.call(thisArg, v, index++, this)) out.push(v);
2966
+ return out;
2967
+ }
2968
+ /**
2969
+ * Map values into a new list of the same class.
2970
+ * @remarks Time O(N), Space O(N)
2971
+ * @param callback - Mapping function (value, index, list) → newValue.
2972
+ * @param [thisArg] - Value for `this` inside the callback.
2973
+ * @returns A new list with mapped values.
2974
+ */
2975
+ mapSame(callback, thisArg) {
2976
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
2977
+ let index = 0;
2978
+ for (const v of this) {
2979
+ const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
2980
+ out.push(mv);
2981
+ }
2982
+ return out;
2983
+ }
2984
+ /**
2985
+ * Map values into a new list (possibly different element type).
2986
+ * @remarks Time O(N), Space O(N)
2987
+ * @template EM
2988
+ * @template RM
2989
+ * @param callback - Mapping function (value, index, list) → newElement.
2990
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
2991
+ * @param [thisArg] - Value for `this` inside the callback.
2992
+ * @returns A new DoublyLinkedList with mapped values.
2993
+
2994
+
2995
+
2996
+
2997
+
2998
+
2999
+
3000
+
3001
+
3002
+
3003
+
3004
+
3005
+
3006
+
3007
+
3008
+
3009
+
3010
+
3011
+
3012
+
3013
+
3014
+
3015
+
3016
+
3017
+
3018
+
3019
+
3020
+
3021
+
3022
+
3023
+
3024
+ * @example
3025
+ * // DoublyLinkedList for...of iteration and map operation
3026
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
3027
+ *
3028
+ * // Iterate through list
3029
+ * const doubled = list.map(value => value * 2);
3030
+ * console.log(doubled.length); // 5;
3031
+ *
3032
+ * // Use for...of loop
3033
+ * const result: number[] = [];
3034
+ * for (const item of list) {
3035
+ * result.push(item);
3036
+ * }
3037
+ * console.log(result); // [1, 2, 3, 4, 5];
3038
+ */
3039
+ map(callback, options, thisArg) {
3040
+ const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
3041
+ let index = 0;
3042
+ for (const v of this) out.push(callback.call(thisArg, v, index++, this));
3043
+ return out;
3044
+ }
3045
+ /**
3046
+ * (Protected) Create or return a node for the given input (node or raw element).
3047
+ * @remarks Time O(1), Space O(1)
3048
+ * @param elementOrNode - Element value or node to normalize.
3049
+ * @returns A DoublyLinkedListNode for the provided input.
3050
+ */
3051
+ _ensureNode(elementOrNode) {
3052
+ if (this.isNode(elementOrNode)) return elementOrNode;
3053
+ return new DoublyLinkedListNode(elementOrNode);
3054
+ }
3055
+ /**
3056
+ * (Protected) Normalize input into a predicate over nodes.
3057
+ * @remarks Time O(1), Space O(1)
3058
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
3059
+ * @returns A predicate function taking a node and returning true/false.
3060
+ */
3061
+ _ensurePredicate(elementNodeOrPredicate) {
3062
+ if (this.isNode(elementNodeOrPredicate)) {
3063
+ const target = elementNodeOrPredicate;
3064
+ return (node) => node === target;
3065
+ }
3066
+ if (typeof elementNodeOrPredicate === "function") {
3067
+ return elementNodeOrPredicate;
3068
+ }
3069
+ const value = elementNodeOrPredicate;
3070
+ return (node) => this._equals(node.value, value);
3071
+ }
3072
+ /**
3073
+ * (Protected) Get the previous node of a given node.
3074
+ * @remarks Time O(1), Space O(1)
3075
+ * @param node - A node in the list.
3076
+ * @returns Previous node or undefined.
3077
+ */
3078
+ _getPrevNode(node) {
3079
+ return node.prev;
3080
+ }
3081
+ /**
3082
+ * (Protected) Create an empty instance of the same concrete class.
3083
+ * @remarks Time O(1), Space O(1)
3084
+ * @param [options] - Options forwarded to the constructor.
3085
+ * @returns An empty like-kind list instance.
3086
+ */
3087
+ _createInstance(options) {
3088
+ const Ctor = this.constructor;
3089
+ return new Ctor([], options);
3090
+ }
3091
+ /**
3092
+ * (Protected) Create a like-kind instance and seed it from an iterable.
3093
+ * @remarks Time O(N), Space O(N)
3094
+ * @template EM
3095
+ * @template RM
3096
+ * @param [elements] - Iterable used to seed the new list.
3097
+ * @param [options] - Options forwarded to the constructor.
3098
+ * @returns A like-kind DoublyLinkedList instance.
3099
+ */
3100
+ _createLike(elements = [], options) {
3101
+ const Ctor = this.constructor;
3102
+ return new Ctor(elements, options);
3103
+ }
3104
+ *_getIterator() {
3105
+ let current = this.head;
3106
+ while (current) {
3107
+ yield current.value;
3108
+ current = current.next;
3109
+ }
3110
+ }
3111
+ *_getReverseIterator() {
3112
+ let current = this.tail;
3113
+ while (current) {
3114
+ yield current.value;
3115
+ current = current.prev;
3116
+ }
3117
+ }
3118
+ *_getNodeIterator() {
3119
+ let current = this.head;
3120
+ while (current) {
3121
+ yield current;
3122
+ current = current.next;
3123
+ }
3124
+ }
3125
+ };
3126
+ __name(_DoublyLinkedList, "DoublyLinkedList");
3127
+ var DoublyLinkedList = _DoublyLinkedList;
3128
+
3129
+ // src/common/error.ts
3130
+ var ERR = {
3131
+ // Range / index
3132
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
3133
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
3134
+ // Type / argument
3135
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
3136
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
3137
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
3138
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
3139
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
3140
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
3141
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
3142
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
3143
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
3144
+ // State / operation
3145
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
3146
+ // Matrix
3147
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
3148
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
3149
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
3150
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
3151
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
3152
+ };
3153
+
3154
+ // src/data-structures/base/iterable-entry-base.ts
3155
+ var _IterableEntryBase = class _IterableEntryBase {
3156
+ /**
3157
+ * Default iterator yielding `[key, value]` entries.
3158
+ * @returns Iterator of `[K, V]`.
3159
+ * @remarks Time O(n) to iterate, Space O(1)
3160
+ */
3161
+ *[Symbol.iterator](...args) {
3162
+ yield* this._getIterator(...args);
3163
+ }
3164
+ /**
3165
+ * Iterate over `[key, value]` pairs (may yield `undefined` values).
3166
+ * @returns Iterator of `[K, V | undefined]`.
3167
+ * @remarks Time O(n), Space O(1)
3168
+ */
3169
+ *entries() {
3170
+ for (const item of this) {
3171
+ yield item;
3172
+ }
3173
+ }
3174
+ /**
3175
+ * Iterate over keys only.
3176
+ * @returns Iterator of keys.
3177
+ * @remarks Time O(n), Space O(1)
3178
+ */
3179
+ *keys() {
3180
+ for (const item of this) {
3181
+ yield item[0];
3182
+ }
3183
+ }
3184
+ /**
3185
+ * Iterate over values only.
3186
+ * @returns Iterator of values.
3187
+ * @remarks Time O(n), Space O(1)
3188
+ */
3189
+ *values() {
3190
+ for (const item of this) {
3191
+ yield item[1];
3192
+ }
3193
+ }
3194
+ /**
3195
+ * Test whether all entries satisfy the predicate.
3196
+ * @param predicate - `(key, value, index, self) => boolean`.
3197
+ * @param thisArg - Optional `this` for callback.
3198
+ * @returns `true` if all pass; otherwise `false`.
3199
+ * @remarks Time O(n), Space O(1)
3200
+ */
3201
+ every(predicate, thisArg) {
3202
+ let index = 0;
3203
+ for (const item of this) {
3204
+ if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
3205
+ return false;
3206
+ }
3207
+ }
3208
+ return true;
3209
+ }
3210
+ /**
3211
+ * Test whether any entry satisfies the predicate.
3212
+ * @param predicate - `(key, value, index, self) => boolean`.
3213
+ * @param thisArg - Optional `this` for callback.
3214
+ * @returns `true` if any passes; otherwise `false`.
3215
+ * @remarks Time O(n), Space O(1)
3216
+ */
3217
+ some(predicate, thisArg) {
3218
+ let index = 0;
3219
+ for (const item of this) {
3220
+ if (predicate.call(thisArg, item[1], item[0], index++, this)) {
3221
+ return true;
3222
+ }
3223
+ }
3224
+ return false;
3225
+ }
3226
+ /**
3227
+ * Visit each entry, left-to-right.
3228
+ * @param callbackfn - `(key, value, index, self) => void`.
3229
+ * @param thisArg - Optional `this` for callback.
3230
+ * @remarks Time O(n), Space O(1)
3231
+ */
3232
+ forEach(callbackfn, thisArg) {
3233
+ let index = 0;
3234
+ for (const item of this) {
3235
+ const [key, value] = item;
3236
+ callbackfn.call(thisArg, value, key, index++, this);
3237
+ }
3238
+ }
3239
+ /**
3240
+ * Find the first entry that matches a predicate.
3241
+ * @param callbackfn - `(key, value, index, self) => boolean`.
3242
+ * @param thisArg - Optional `this` for callback.
3243
+ * @returns Matching `[key, value]` or `undefined`.
3244
+ * @remarks Time O(n), Space O(1)
3245
+ */
3246
+ find(callbackfn, thisArg) {
3247
+ let index = 0;
3248
+ for (const item of this) {
3249
+ const [key, value] = item;
3250
+ if (callbackfn.call(thisArg, value, key, index++, this)) return item;
3251
+ }
3252
+ return;
3253
+ }
3254
+ /**
3255
+ * Whether the given key exists.
3256
+ * @param key - Key to test.
3257
+ * @returns `true` if found; otherwise `false`.
3258
+ * @remarks Time O(n) generic, Space O(1)
3259
+ */
3260
+ has(key) {
3261
+ for (const item of this) {
3262
+ const [itemKey] = item;
3263
+ if (itemKey === key) return true;
3264
+ }
3265
+ return false;
3266
+ }
3267
+ /**
3268
+ * Whether there exists an entry with the given value.
3269
+ * @param value - Value to test.
3270
+ * @returns `true` if found; otherwise `false`.
3271
+ * @remarks Time O(n), Space O(1)
3272
+ */
3273
+ hasValue(value) {
3274
+ for (const [, elementValue] of this) {
3275
+ if (elementValue === value) return true;
3276
+ }
3277
+ return false;
3278
+ }
3279
+ /**
3280
+ * Get the value under a key.
3281
+ * @param key - Key to look up.
3282
+ * @returns Value or `undefined`.
3283
+ * @remarks Time O(n) generic, Space O(1)
3284
+ */
3285
+ get(key) {
3286
+ for (const item of this) {
3287
+ const [itemKey, value] = item;
3288
+ if (itemKey === key) return value;
3289
+ }
3290
+ return;
3291
+ }
3292
+ /**
3293
+ * Reduce entries into a single accumulator.
3294
+ * @param callbackfn - `(acc, value, key, index, self) => acc`.
3295
+ * @param initialValue - Initial accumulator.
3296
+ * @returns Final accumulator.
3297
+ * @remarks Time O(n), Space O(1)
3298
+ */
3299
+ reduce(callbackfn, initialValue) {
3300
+ let accumulator = initialValue;
3301
+ let index = 0;
3302
+ for (const item of this) {
3303
+ const [key, value] = item;
3304
+ accumulator = callbackfn(accumulator, value, key, index++, this);
3305
+ }
3306
+ return accumulator;
3307
+ }
3308
+ /**
3309
+ * Converts data structure to `[key, value]` pairs.
3310
+ * @returns Array of entries.
3311
+ * @remarks Time O(n), Space O(n)
3312
+ */
3313
+ toArray() {
3314
+ return [...this];
3315
+ }
3316
+ /**
3317
+ * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
3318
+ * @returns Array of entries (default) or a string.
3319
+ * @remarks Time O(n), Space O(n)
3320
+ */
3321
+ toVisual() {
3322
+ return [...this];
3323
+ }
3324
+ /**
3325
+ * Print a human-friendly representation to the console.
3326
+ * @remarks Time O(n), Space O(n)
3327
+ */
3328
+ print() {
3329
+ console.log(this.toVisual());
3330
+ }
3331
+ };
3332
+ __name(_IterableEntryBase, "IterableEntryBase");
3333
+ var IterableEntryBase = _IterableEntryBase;
3334
+
3335
+ // src/data-structures/linked-list/skip-linked-list.ts
3336
+ var _SkipListNode = class _SkipListNode {
3337
+ constructor(key, value, level) {
3338
+ __publicField(this, "key");
3339
+ __publicField(this, "value");
3340
+ __publicField(this, "forward");
3341
+ this.key = key;
3342
+ this.value = value;
3343
+ this.forward = new Array(level).fill(void 0);
3344
+ }
3345
+ };
3346
+ __name(_SkipListNode, "SkipListNode");
3347
+ var SkipListNode = _SkipListNode;
3348
+ var _comparator, _isDefaultComparator;
3349
+ var _SkipList = class _SkipList extends IterableEntryBase {
3350
+ constructor(entries = [], options = {}) {
3351
+ super();
3352
+ __privateAdd(this, _comparator);
3353
+ __privateAdd(this, _isDefaultComparator);
3354
+ // ─── Internal state ──────────────────────────────────────────
3355
+ __publicField(this, "_head");
3356
+ __publicField(this, "_level", 0);
3357
+ __publicField(this, "_size", 0);
3358
+ __publicField(this, "_maxLevel", 16);
3359
+ __publicField(this, "_probability", 0.5);
3360
+ const { comparator, toEntryFn, maxLevel, probability } = options;
3361
+ if (typeof maxLevel === "number" && maxLevel > 0) this._maxLevel = maxLevel;
3362
+ if (typeof probability === "number" && probability > 0 && probability < 1) this._probability = probability;
3363
+ __privateSet(this, _isDefaultComparator, comparator === void 0);
3364
+ __privateSet(this, _comparator, comparator != null ? comparator : _SkipList.createDefaultComparator());
3365
+ this._head = new SkipListNode(void 0, void 0, this._maxLevel);
3366
+ for (const item of entries) {
3367
+ let k;
3368
+ let v;
3369
+ if (toEntryFn) {
3370
+ [k, v] = toEntryFn(item);
3371
+ } else {
3372
+ if (!Array.isArray(item) || item.length < 2) {
3373
+ throw new TypeError(ERR.invalidEntry("SkipList"));
3374
+ }
3375
+ [k, v] = item;
3376
+ }
3377
+ this.set(k, v);
3378
+ }
3379
+ }
3380
+ /**
3381
+ * Creates a default comparator supporting number, string, Date, and bigint.
3382
+ */
3383
+ static createDefaultComparator() {
3384
+ return (a, b) => {
3385
+ if (typeof a === "number" && typeof b === "number") {
3386
+ if (Number.isNaN(a) || Number.isNaN(b)) throw new TypeError(ERR.invalidNaN("SkipList"));
3387
+ return a - b;
3388
+ }
3389
+ if (typeof a === "string" && typeof b === "string") {
3390
+ return a < b ? -1 : a > b ? 1 : 0;
3391
+ }
3392
+ if (a instanceof Date && b instanceof Date) {
3393
+ const ta = a.getTime(), tb = b.getTime();
3394
+ if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("SkipList"));
3395
+ return ta - tb;
3396
+ }
3397
+ if (typeof a === "bigint" && typeof b === "bigint") {
3398
+ return a < b ? -1 : a > b ? 1 : 0;
3399
+ }
3400
+ throw new TypeError(ERR.comparatorRequired("SkipList"));
3401
+ };
3402
+ }
3403
+ // ─── Size & lifecycle ────────────────────────────────────────
3404
+ get size() {
3405
+ return this._size;
3406
+ }
3407
+ get maxLevel() {
3408
+ return this._maxLevel;
3409
+ }
3410
+ get probability() {
3411
+ return this._probability;
3412
+ }
3413
+ get comparator() {
3414
+ return __privateGet(this, _comparator);
3415
+ }
3416
+ /**
3417
+ * Check if empty
3418
+
3419
+
3420
+
3421
+
3422
+
3423
+
3424
+
3425
+
3426
+
3427
+
3428
+
3429
+
3430
+
3431
+
3432
+
3433
+
3434
+
3435
+
3436
+
3437
+
3438
+
3439
+
3440
+
3441
+
3442
+
3443
+
3444
+
3445
+
3446
+ * @example
3447
+ * // Check if empty
3448
+ * const sl = new SkipList<number, string>();
3449
+ * console.log(sl.isEmpty()); // true;
3450
+ */
3451
+ isEmpty() {
3452
+ return this._size === 0;
3453
+ }
3454
+ /**
3455
+ * Remove all entries
3456
+
3457
+
3458
+
3459
+
3460
+
3461
+
3462
+
3463
+
3464
+
3465
+
3466
+
3467
+
3468
+
3469
+
3470
+
3471
+
3472
+
3473
+
3474
+
3475
+
3476
+
3477
+
3478
+
3479
+
3480
+
3481
+
3482
+
3483
+
3484
+ * @example
3485
+ * // Remove all entries
3486
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
3487
+ * sl.clear();
3488
+ * console.log(sl.isEmpty()); // true;
3489
+ */
3490
+ clear() {
3491
+ this._head = new SkipListNode(void 0, void 0, this._maxLevel);
3492
+ this._level = 0;
3493
+ this._size = 0;
3494
+ }
3495
+ /**
3496
+ * Create independent copy
3497
+
3498
+
3499
+
3500
+
3501
+
3502
+
3503
+
3504
+
3505
+
3506
+
3507
+
3508
+
3509
+
3510
+
3511
+
3512
+
3513
+
3514
+
3515
+
3516
+
3517
+
3518
+
3519
+
3520
+
3521
+
3522
+
3523
+
3524
+
3525
+ * @example
3526
+ * // Create independent copy
3527
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
3528
+ * const copy = sl.clone();
3529
+ * copy.delete(1);
3530
+ * console.log(sl.has(1)); // true;
3531
+ */
3532
+ clone() {
3533
+ return new _SkipList(this, {
3534
+ comparator: __privateGet(this, _isDefaultComparator) ? void 0 : __privateGet(this, _comparator),
3535
+ maxLevel: this._maxLevel,
3536
+ probability: this._probability
3537
+ });
3538
+ }
3539
+ // ─── Core CRUD ───────────────────────────────────────────────
3540
+ /**
3541
+ * Insert or update a key-value pair. Returns `this` for chaining.
3542
+ * Unique keys only — if key exists, value is updated in place.
3543
+
3544
+
3545
+
3546
+
3547
+
3548
+
3549
+
3550
+
3551
+
3552
+
3553
+
3554
+
3555
+
3556
+
3557
+
3558
+
3559
+
3560
+
3561
+
3562
+
3563
+
3564
+
3565
+
3566
+
3567
+
3568
+
3569
+
3570
+
3571
+
3572
+
3573
+
3574
+ * @example
3575
+ * // In-memory sorted key-value store
3576
+ * const store = new SkipList<number, string>();
3577
+ *
3578
+ * store.set(3, 'three');
3579
+ * store.set(1, 'one');
3580
+ * store.set(5, 'five');
3581
+ * store.set(2, 'two');
3582
+ *
3583
+ * console.log(store.get(3)); // 'three';
3584
+ * console.log(store.get(1)); // 'one';
3585
+ * console.log(store.get(5)); // 'five';
3586
+ *
3587
+ * // Update existing key
3588
+ * store.set(3, 'THREE');
3589
+ * console.log(store.get(3)); // 'THREE';
3590
+ */
3591
+ set(key, value) {
3592
+ const cmp = __privateGet(this, _comparator);
3593
+ const update = this._findUpdate(key);
3594
+ const existing = update[0].forward[0];
3595
+ if (existing && cmp(existing.key, key) === 0) {
3596
+ existing.value = value;
3597
+ return this;
3598
+ }
3599
+ const newLevel = this._randomLevel();
3600
+ const newNode = new SkipListNode(key, value, newLevel);
3601
+ if (newLevel > this._level) {
3602
+ for (let i = this._level; i < newLevel; i++) {
3603
+ update[i] = this._head;
3604
+ }
3605
+ this._level = newLevel;
3606
+ }
3607
+ for (let i = 0; i < newLevel; i++) {
3608
+ newNode.forward[i] = update[i].forward[i];
3609
+ update[i].forward[i] = newNode;
3610
+ }
3611
+ this._size++;
3612
+ return this;
3613
+ }
3614
+ /**
3615
+ * Get the value for a key, or `undefined` if not found.
3616
+ * Overrides base O(n) with O(log n) skip-list search.
3617
+
3618
+
3619
+
3620
+
3621
+
3622
+
3623
+
3624
+
3625
+
3626
+
3627
+
3628
+
3629
+
3630
+
3631
+
3632
+
3633
+
3634
+
3635
+
3636
+
3637
+
3638
+
3639
+
3640
+
3641
+
3642
+
3643
+
3644
+
3645
+
3646
+
3647
+
3648
+ * @example
3649
+ * // Building a sorted index
3650
+ * type Product = { id: number; name: string; price: number };
3651
+ * const products: Product[] = [
3652
+ * { id: 1, name: 'Widget', price: 25 },
3653
+ * { id: 2, name: 'Gadget', price: 50 },
3654
+ * { id: 3, name: 'Doohickey', price: 15 }
3655
+ * ];
3656
+ *
3657
+ * const index = new SkipList<number, Product, Product>(products, {
3658
+ * toEntryFn: (p: Product) => [p.price, p]
3659
+ * });
3660
+ *
3661
+ * // Iterate in sorted order by price
3662
+ * const names = [...index.values()].map(p => p!.name);
3663
+ * console.log(names); // ['Doohickey', 'Widget', 'Gadget'];
3664
+ *
3665
+ * // Range search: products between $20 and $60
3666
+ * const range = index.rangeSearch([20, 60]);
3667
+ * console.log(range.map(([, p]) => p!.name)); // ['Widget', 'Gadget'];
3668
+ */
3669
+ get(key) {
3670
+ const node = this._findNode(key);
3671
+ return node ? node.value : void 0;
3672
+ }
3673
+ /**
3674
+ * Check if a key exists.
3675
+ * Overrides base O(n) with O(log n) skip-list search.
3676
+
3677
+
3678
+
3679
+
3680
+
3681
+
3682
+
3683
+
3684
+
3685
+
3686
+
3687
+
3688
+
3689
+
3690
+
3691
+
3692
+
3693
+
3694
+
3695
+
3696
+
3697
+
3698
+
3699
+
3700
+
3701
+
3702
+
3703
+
3704
+
3705
+
3706
+
3707
+ * @example
3708
+ * // Check key existence
3709
+ * const sl = new SkipList<number, string>([[1, 'a'], [3, 'c'], [5, 'e']]);
3710
+ * console.log(sl.has(3)); // true;
3711
+ * console.log(sl.has(4)); // false;
3712
+ */
3713
+ has(key) {
3714
+ return this._findNode(key) !== void 0;
3715
+ }
3716
+ /**
3717
+ * Delete a key. Returns `true` if the key was found and removed.
3718
+
3719
+
3720
+
3721
+
3722
+
3723
+
3724
+
3725
+
3726
+
3727
+
3728
+
3729
+
3730
+
3731
+
3732
+
3733
+
3734
+
3735
+
3736
+
3737
+
3738
+
3739
+
3740
+
3741
+
3742
+
3743
+
3744
+
3745
+
3746
+
3747
+
3748
+
3749
+ * @example
3750
+ * // Fast lookup with deletion
3751
+ * const cache = new SkipList<string, number>();
3752
+ *
3753
+ * cache.set('alpha', 1);
3754
+ * cache.set('beta', 2);
3755
+ * cache.set('gamma', 3);
3756
+ *
3757
+ * console.log(cache.has('beta')); // true;
3758
+ * cache.delete('beta');
3759
+ * console.log(cache.has('beta')); // false;
3760
+ * console.log(cache.size); // 2;
3761
+ */
3762
+ delete(key) {
3763
+ const cmp = __privateGet(this, _comparator);
3764
+ const update = this._findUpdate(key);
3765
+ const target = update[0].forward[0];
3766
+ if (!target || cmp(target.key, key) !== 0) return false;
3767
+ for (let i = 0; i < this._level; i++) {
3768
+ if (update[i].forward[i] !== target) break;
3769
+ update[i].forward[i] = target.forward[i];
3770
+ }
3771
+ while (this._level > 0 && !this._head.forward[this._level - 1]) {
3772
+ this._level--;
3773
+ }
3774
+ this._size--;
3775
+ return true;
3776
+ }
3777
+ // ─── Navigation ──────────────────────────────────────────────
3778
+ /**
3779
+ * Returns the first (smallest key) entry, or `undefined` if empty.
3780
+
3781
+
3782
+
3783
+
3784
+
3785
+
3786
+
3787
+
3788
+
3789
+
3790
+
3791
+
3792
+
3793
+
3794
+
3795
+
3796
+
3797
+
3798
+
3799
+
3800
+
3801
+
3802
+
3803
+
3804
+
3805
+
3806
+
3807
+
3808
+
3809
+
3810
+
3811
+ * @example
3812
+ * // Access the minimum entry
3813
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
3814
+ * console.log(sl.first()); // [1, 'a'];
3815
+ */
3816
+ first() {
3817
+ const node = this._head.forward[0];
3818
+ return node ? [node.key, node.value] : void 0;
3819
+ }
3820
+ /**
3821
+ * Returns the last (largest key) entry, or `undefined` if empty.
3822
+
3823
+
3824
+
3825
+
3826
+
3827
+
3828
+
3829
+
3830
+
3831
+
3832
+
3833
+
3834
+
3835
+
3836
+
3837
+
3838
+
3839
+
3840
+
3841
+
3842
+
3843
+
3844
+
3845
+
3846
+
3847
+
3848
+
3849
+
3850
+
3851
+
3852
+
3853
+ * @example
3854
+ * // Access the maximum entry
3855
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
3856
+ * console.log(sl.last()); // [5, 'e'];
3857
+ */
3858
+ last() {
3859
+ let current = this._head;
3860
+ for (let i = this._level - 1; i >= 0; i--) {
3861
+ while (current.forward[i]) {
3862
+ current = current.forward[i];
3863
+ }
3864
+ }
3865
+ return current === this._head ? void 0 : [current.key, current.value];
3866
+ }
3867
+ /**
3868
+ * Remove and return the first (smallest key) entry.
3869
+
3870
+
3871
+
3872
+
3873
+
3874
+
3875
+
3876
+
3877
+
3878
+
3879
+
3880
+
3881
+
3882
+
3883
+
3884
+
3885
+
3886
+
3887
+
3888
+
3889
+
3890
+
3891
+
3892
+
3893
+
3894
+
3895
+
3896
+
3897
+ * @example
3898
+ * // Remove and return smallest
3899
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
3900
+ * console.log(sl.pollFirst()); // [1, 'a'];
3901
+ * console.log(sl.size); // 2;
3902
+ */
3903
+ pollFirst() {
3904
+ const entry = this.first();
3905
+ if (!entry) return void 0;
3906
+ this.delete(entry[0]);
3907
+ return entry;
3908
+ }
3909
+ /**
3910
+ * Remove and return the last (largest key) entry.
3911
+
3912
+
3913
+
3914
+
3915
+
3916
+
3917
+
3918
+
3919
+
3920
+
3921
+
3922
+
3923
+
3924
+
3925
+
3926
+
3927
+
3928
+
3929
+
3930
+
3931
+
3932
+
3933
+
3934
+
3935
+
3936
+
3937
+
3938
+
3939
+ * @example
3940
+ * // Remove and return largest
3941
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
3942
+ * console.log(sl.pollLast()); // [3, 'c'];
3943
+ * console.log(sl.size); // 2;
3944
+ */
3945
+ pollLast() {
3946
+ const entry = this.last();
3947
+ if (!entry) return void 0;
3948
+ this.delete(entry[0]);
3949
+ return entry;
3950
+ }
3951
+ /**
3952
+ * Least entry ≥ key, or `undefined`.
3953
+
3954
+
3955
+
3956
+
3957
+
3958
+
3959
+
3960
+
3961
+
3962
+
3963
+
3964
+
3965
+
3966
+
3967
+
3968
+
3969
+
3970
+
3971
+
3972
+
3973
+
3974
+
3975
+
3976
+
3977
+
3978
+
3979
+
3980
+
3981
+
3982
+
3983
+
3984
+ * @example
3985
+ * // Least entry ≥ key
3986
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
3987
+ * console.log(sl.ceiling(15)); // [20, 'b'];
3988
+ * console.log(sl.ceiling(20)); // [20, 'b'];
3989
+ */
3990
+ ceiling(key) {
3991
+ const cmp = __privateGet(this, _comparator);
3992
+ let current = this._head;
3993
+ for (let i = this._level - 1; i >= 0; i--) {
3994
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
3995
+ current = current.forward[i];
3996
+ }
3997
+ }
3998
+ const node = current.forward[0];
3999
+ return node ? [node.key, node.value] : void 0;
4000
+ }
4001
+ /**
4002
+ * Greatest entry ≤ key, or `undefined`.
4003
+
4004
+
4005
+
4006
+
4007
+
4008
+
4009
+
4010
+
4011
+
4012
+
4013
+
4014
+
4015
+
4016
+
4017
+
4018
+
4019
+
4020
+
4021
+
4022
+
4023
+
4024
+
4025
+
4026
+
4027
+
4028
+
4029
+
4030
+
4031
+
4032
+
4033
+
4034
+ * @example
4035
+ * // Greatest entry ≤ key
4036
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4037
+ * console.log(sl.floor(25)); // [20, 'b'];
4038
+ * console.log(sl.floor(5)); // undefined;
4039
+ */
4040
+ floor(key) {
4041
+ const cmp = __privateGet(this, _comparator);
4042
+ let current = this._head;
4043
+ for (let i = this._level - 1; i >= 0; i--) {
4044
+ while (current.forward[i] && cmp(current.forward[i].key, key) <= 0) {
4045
+ current = current.forward[i];
4046
+ }
4047
+ }
4048
+ const result = current === this._head ? void 0 : current;
4049
+ if (result && cmp(result.key, key) <= 0) return [result.key, result.value];
4050
+ return void 0;
4051
+ }
4052
+ /**
4053
+ * Least entry strictly > key, or `undefined`.
4054
+
4055
+
4056
+
4057
+
4058
+
4059
+
4060
+
4061
+
4062
+
4063
+
4064
+
4065
+
4066
+
4067
+
4068
+
4069
+
4070
+
4071
+
4072
+
4073
+
4074
+
4075
+
4076
+
4077
+
4078
+
4079
+
4080
+
4081
+
4082
+ * @example
4083
+ * // Strictly greater entry
4084
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4085
+ * console.log(sl.higher(15)); // [20, 'b'];
4086
+ * console.log(sl.higher(30)); // undefined;
4087
+ */
4088
+ higher(key) {
4089
+ const cmp = __privateGet(this, _comparator);
4090
+ let current = this._head;
4091
+ for (let i = this._level - 1; i >= 0; i--) {
4092
+ while (current.forward[i] && cmp(current.forward[i].key, key) <= 0) {
4093
+ current = current.forward[i];
4094
+ }
4095
+ }
4096
+ const node = current.forward[0];
4097
+ return node ? [node.key, node.value] : void 0;
4098
+ }
4099
+ /**
4100
+ * Greatest entry strictly < key, or `undefined`.
4101
+
4102
+
4103
+
4104
+
4105
+
4106
+
4107
+
4108
+
4109
+
4110
+
4111
+
4112
+
4113
+
4114
+
4115
+
4116
+
4117
+
4118
+
4119
+
4120
+
4121
+
4122
+
4123
+
4124
+
4125
+
4126
+
4127
+
4128
+
4129
+ * @example
4130
+ * // Strictly less entry
4131
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4132
+ * console.log(sl.lower(25)); // [20, 'b'];
4133
+ * console.log(sl.lower(10)); // undefined;
4134
+ */
4135
+ lower(key) {
4136
+ const cmp = __privateGet(this, _comparator);
4137
+ let current = this._head;
4138
+ let result;
4139
+ for (let i = this._level - 1; i >= 0; i--) {
4140
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4141
+ current = current.forward[i];
4142
+ }
4143
+ if (current !== this._head && cmp(current.key, key) < 0) {
4144
+ result = current;
4145
+ }
4146
+ }
4147
+ return result ? [result.key, result.value] : void 0;
4148
+ }
4149
+ /**
4150
+ * Returns entries within the given key range.
4151
+
4152
+
4153
+
4154
+
4155
+
4156
+
4157
+
4158
+
4159
+
4160
+
4161
+
4162
+
4163
+
4164
+
4165
+
4166
+
4167
+
4168
+
4169
+
4170
+
4171
+
4172
+
4173
+
4174
+
4175
+
4176
+
4177
+
4178
+
4179
+
4180
+
4181
+
4182
+ * @example
4183
+ * // Find entries in a range
4184
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e']]);
4185
+ * const result = sl.rangeSearch([2, 4]);
4186
+ * console.log(result); // [[2, 'b'], [3, 'c'], [4, 'd']];
4187
+ */
4188
+ rangeSearch(range, options = {}) {
4189
+ const { lowInclusive = true, highInclusive = true } = options;
4190
+ const [low, high] = range;
4191
+ const cmp = __privateGet(this, _comparator);
4192
+ const out = [];
4193
+ let current = this._head;
4194
+ for (let i = this._level - 1; i >= 0; i--) {
4195
+ while (current.forward[i] && cmp(current.forward[i].key, low) < 0) {
4196
+ current = current.forward[i];
4197
+ }
4198
+ }
4199
+ current = current.forward[0];
4200
+ while (current) {
4201
+ const cmpHigh = cmp(current.key, high);
4202
+ if (cmpHigh > 0) break;
4203
+ if (cmpHigh === 0 && !highInclusive) break;
4204
+ const cmpLow = cmp(current.key, low);
4205
+ if (cmpLow > 0 || cmpLow === 0 && lowInclusive) {
4206
+ out.push([current.key, current.value]);
4207
+ }
4208
+ current = current.forward[0];
4209
+ }
4210
+ return out;
4211
+ }
4212
+ // ─── Functional (overrides) ──────────────────────────────────
4213
+ /**
4214
+ * Creates a new SkipList with entries transformed by callback.
4215
+
4216
+
4217
+
4218
+
4219
+
4220
+
4221
+
4222
+
4223
+
4224
+
4225
+
4226
+
4227
+
4228
+
4229
+
4230
+
4231
+
4232
+
4233
+
4234
+
4235
+
4236
+
4237
+
4238
+
4239
+
4240
+
4241
+
4242
+
4243
+ * @example
4244
+ * // Transform entries
4245
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
4246
+ * const mapped = sl.map((v, k) => [k, v?.toUpperCase()] as [number, string]);
4247
+ * console.log([...mapped.values()]); // ['A', 'B'];
4248
+ */
4249
+ map(callback, options) {
4250
+ const out = new _SkipList([], options != null ? options : {});
4251
+ let i = 0;
4252
+ for (const [k, v] of this) {
4253
+ const [nk, nv] = callback(v, k, i++, this);
4254
+ out.set(nk, nv);
4255
+ }
4256
+ return out;
4257
+ }
4258
+ /**
4259
+ * Creates a new SkipList with entries that pass the predicate.
4260
+
4261
+
4262
+
4263
+
4264
+
4265
+
4266
+
4267
+
4268
+
4269
+
4270
+
4271
+
4272
+
4273
+
4274
+
4275
+
4276
+
4277
+
4278
+
4279
+
4280
+
4281
+
4282
+
4283
+
4284
+
4285
+
4286
+
4287
+
4288
+ * @example
4289
+ * // Filter entries
4290
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
4291
+ * const result = sl.filter((v, k) => k > 1);
4292
+ * console.log(result.size); // 2;
4293
+ */
4294
+ filter(callbackfn, thisArg) {
4295
+ const out = new _SkipList([], {
4296
+ comparator: __privateGet(this, _isDefaultComparator) ? void 0 : __privateGet(this, _comparator),
4297
+ maxLevel: this._maxLevel,
4298
+ probability: this._probability
4299
+ });
4300
+ let i = 0;
4301
+ for (const [k, v] of this) {
4302
+ const ok = callbackfn.call(thisArg, v, k, i++, this);
4303
+ if (ok) out.set(k, v);
4304
+ }
4305
+ return out;
4306
+ }
4307
+ // ─── Iterator (required by IterableEntryBase) ────────────────
4308
+ _getIterator() {
4309
+ const head = this._head;
4310
+ return (function* () {
4311
+ let node = head.forward[0];
4312
+ while (node) {
4313
+ yield [node.key, node.value];
4314
+ node = node.forward[0];
4315
+ }
4316
+ })();
4317
+ }
4318
+ // ─── Internal helpers ────────────────────────────────────────
4319
+ /**
4320
+ * Finds the update array (predecessors at each level) for a given key.
4321
+ */
4322
+ _findUpdate(key) {
4323
+ const cmp = __privateGet(this, _comparator);
4324
+ const update = new Array(this._maxLevel).fill(this._head);
4325
+ let current = this._head;
4326
+ for (let i = this._level - 1; i >= 0; i--) {
4327
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4328
+ current = current.forward[i];
4329
+ }
4330
+ update[i] = current;
4331
+ }
4332
+ return update;
4333
+ }
4334
+ /**
4335
+ * Finds the node for a given key, or undefined.
4336
+ */
4337
+ _findNode(key) {
4338
+ const cmp = __privateGet(this, _comparator);
4339
+ let current = this._head;
4340
+ for (let i = this._level - 1; i >= 0; i--) {
4341
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4342
+ current = current.forward[i];
4343
+ }
4344
+ }
4345
+ const candidate = current.forward[0];
4346
+ if (candidate && cmp(candidate.key, key) === 0) return candidate;
4347
+ return void 0;
4348
+ }
4349
+ _randomLevel() {
4350
+ let level = 1;
4351
+ while (Math.random() < this._probability && level < this._maxLevel) {
4352
+ level++;
4353
+ }
4354
+ return level;
4355
+ }
4356
+ };
4357
+ _comparator = new WeakMap();
4358
+ _isDefaultComparator = new WeakMap();
4359
+ __name(_SkipList, "SkipList");
4360
+ var SkipList = _SkipList;
4361
+ /**
4362
+ * data-structure-typed
4363
+ *
4364
+ * @author Pablo Zeng
4365
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
4366
+ * @license MIT License
4367
+ */
4368
+
4369
+ exports.DoublyLinkedList = DoublyLinkedList;
4370
+ exports.DoublyLinkedListNode = DoublyLinkedListNode;
4371
+ exports.SinglyLinkedList = SinglyLinkedList;
4372
+ exports.SinglyLinkedListNode = SinglyLinkedListNode;
4373
+ exports.SkipList = SkipList;
4374
+ exports.SkipListNode = SkipListNode;
4375
+ //# sourceMappingURL=linked-list.cjs.map
4376
+ //# sourceMappingURL=linked-list.cjs.map