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,4369 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __typeError = (msg) => {
3
+ throw TypeError(msg);
4
+ };
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
9
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
10
+ 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);
11
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
12
+
13
+ // src/data-structures/base/iterable-element-base.ts
14
+ var _IterableElementBase = class _IterableElementBase {
15
+ /**
16
+ * Create a new iterable base.
17
+ *
18
+ * @param options Optional behavior overrides. When provided, a `toElementFn`
19
+ * is used to convert a raw element (`R`) into a public element (`E`).
20
+ *
21
+ * @remarks
22
+ * Time O(1), Space O(1).
23
+ */
24
+ constructor(options) {
25
+ /**
26
+ * The converter used to transform a raw element (`R`) into a public element (`E`).
27
+ *
28
+ * @remarks
29
+ * Time O(1), Space O(1).
30
+ */
31
+ __publicField(this, "_toElementFn");
32
+ if (options) {
33
+ const { toElementFn } = options;
34
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
35
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
36
+ }
37
+ }
38
+ /**
39
+ * Exposes the current `toElementFn`, if configured.
40
+ *
41
+ * @returns The converter function or `undefined` when not set.
42
+ * @remarks
43
+ * Time O(1), Space O(1).
44
+ */
45
+ get toElementFn() {
46
+ return this._toElementFn;
47
+ }
48
+ /**
49
+ * Returns an iterator over the structure's elements.
50
+ *
51
+ * @param args Optional iterator arguments forwarded to the internal iterator.
52
+ * @returns An `IterableIterator<E>` that yields the elements in traversal order.
53
+ *
54
+ * @remarks
55
+ * Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
56
+ */
57
+ *[Symbol.iterator](...args) {
58
+ yield* this._getIterator(...args);
59
+ }
60
+ /**
61
+ * Returns an iterator over the values (alias of the default iterator).
62
+ *
63
+ * @returns An `IterableIterator<E>` over all elements.
64
+ * @remarks
65
+ * Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
66
+ */
67
+ *values() {
68
+ for (const item of this) yield item;
69
+ }
70
+ /**
71
+ * Tests whether all elements satisfy the predicate.
72
+ *
73
+ * @template TReturn
74
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
75
+ * @param thisArg Optional `this` binding for the predicate.
76
+ * @returns `true` if every element passes; otherwise `false`.
77
+ *
78
+ * @remarks
79
+ * Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
80
+ */
81
+ every(predicate, thisArg) {
82
+ let index = 0;
83
+ for (const item of this) {
84
+ if (thisArg === void 0) {
85
+ if (!predicate(item, index++, this)) return false;
86
+ } else {
87
+ const fn = predicate;
88
+ if (!fn.call(thisArg, item, index++, this)) return false;
89
+ }
90
+ }
91
+ return true;
92
+ }
93
+ /**
94
+ * Tests whether at least one element satisfies the predicate.
95
+ *
96
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
97
+ * @param thisArg Optional `this` binding for the predicate.
98
+ * @returns `true` if any element passes; otherwise `false`.
99
+ *
100
+ * @remarks
101
+ * Time O(n) in the worst case; may exit early on first success. Space O(1).
102
+ */
103
+ some(predicate, thisArg) {
104
+ let index = 0;
105
+ for (const item of this) {
106
+ if (thisArg === void 0) {
107
+ if (predicate(item, index++, this)) return true;
108
+ } else {
109
+ const fn = predicate;
110
+ if (fn.call(thisArg, item, index++, this)) return true;
111
+ }
112
+ }
113
+ return false;
114
+ }
115
+ /**
116
+ * Invokes a callback for each element in iteration order.
117
+ *
118
+ * @param callbackfn Function invoked per element with signature `(value, index, self)`.
119
+ * @param thisArg Optional `this` binding for the callback.
120
+ * @returns `void`.
121
+ *
122
+ * @remarks
123
+ * Time O(n), Space O(1).
124
+ */
125
+ forEach(callbackfn, thisArg) {
126
+ let index = 0;
127
+ for (const item of this) {
128
+ if (thisArg === void 0) {
129
+ callbackfn(item, index++, this);
130
+ } else {
131
+ const fn = callbackfn;
132
+ fn.call(thisArg, item, index++, this);
133
+ }
134
+ }
135
+ }
136
+ // Implementation signature
137
+ find(predicate, thisArg) {
138
+ let index = 0;
139
+ for (const item of this) {
140
+ if (thisArg === void 0) {
141
+ if (predicate(item, index++, this)) return item;
142
+ } else {
143
+ const fn = predicate;
144
+ if (fn.call(thisArg, item, index++, this)) return item;
145
+ }
146
+ }
147
+ return;
148
+ }
149
+ /**
150
+ * Checks whether a strictly-equal element exists in the structure.
151
+ *
152
+ * @param element The element to test with `===` equality.
153
+ * @returns `true` if an equal element is found; otherwise `false`.
154
+ *
155
+ * @remarks
156
+ * Time O(n) in the worst case. Space O(1).
157
+ */
158
+ has(element) {
159
+ for (const ele of this) if (ele === element) return true;
160
+ return false;
161
+ }
162
+ /**
163
+ * Reduces all elements to a single accumulated value.
164
+ *
165
+ * @overload
166
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
167
+ * @returns The final accumulated value typed as `E`.
168
+ *
169
+ * @overload
170
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
171
+ * @param initialValue The initial accumulator value of type `E`.
172
+ * @returns The final accumulated value typed as `E`.
173
+ *
174
+ * @overload
175
+ * @template U The accumulator type when it differs from `E`.
176
+ * @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
177
+ * @param initialValue The initial accumulator value of type `U`.
178
+ * @returns The final accumulated value typed as `U`.
179
+ *
180
+ * @remarks
181
+ * Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
182
+ */
183
+ reduce(callbackfn, initialValue) {
184
+ let index = 0;
185
+ const iter = this[Symbol.iterator]();
186
+ let acc;
187
+ if (arguments.length >= 2) {
188
+ acc = initialValue;
189
+ } else {
190
+ const first = iter.next();
191
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
192
+ acc = first.value;
193
+ index = 1;
194
+ }
195
+ for (const value of iter) {
196
+ acc = callbackfn(acc, value, index++, this);
197
+ }
198
+ return acc;
199
+ }
200
+ /**
201
+ * Materializes the elements into a new array.
202
+ *
203
+ * @returns A shallow array copy of the iteration order.
204
+ * @remarks
205
+ * Time O(n), Space O(n).
206
+ */
207
+ toArray() {
208
+ return [...this];
209
+ }
210
+ /**
211
+ * Returns a representation of the structure suitable for quick visualization.
212
+ * Defaults to an array of elements; subclasses may override to provide richer visuals.
213
+ *
214
+ * @returns A visual representation (array by default).
215
+ * @remarks
216
+ * Time O(n), Space O(n).
217
+ */
218
+ toVisual() {
219
+ return [...this];
220
+ }
221
+ /**
222
+ * Prints `toVisual()` to the console. Intended for quick debugging.
223
+ *
224
+ * @returns `void`.
225
+ * @remarks
226
+ * Time O(n) due to materialization, Space O(n) for the intermediate representation.
227
+ */
228
+ print() {
229
+ console.log(this.toVisual());
230
+ }
231
+ };
232
+ __name(_IterableElementBase, "IterableElementBase");
233
+ var IterableElementBase = _IterableElementBase;
234
+
235
+ // src/data-structures/base/linear-base.ts
236
+ var _LinkedListNode = class _LinkedListNode {
237
+ /**
238
+ * Initialize a node.
239
+ * @param value - Element value.
240
+ * @remarks Time O(1), Space O(1)
241
+ */
242
+ constructor(value) {
243
+ __publicField(this, "_value");
244
+ __publicField(this, "_next");
245
+ this._value = value;
246
+ this._next = void 0;
247
+ }
248
+ /**
249
+ * Element payload getter.
250
+ * @returns Element value.
251
+ * @remarks Time O(1), Space O(1)
252
+ */
253
+ get value() {
254
+ return this._value;
255
+ }
256
+ /**
257
+ * Element payload setter.
258
+ * @param value - New value.
259
+ * @remarks Time O(1), Space O(1)
260
+ */
261
+ set value(value) {
262
+ this._value = value;
263
+ }
264
+ /**
265
+ * Next node getter.
266
+ * @returns Next node or `undefined`.
267
+ * @remarks Time O(1), Space O(1)
268
+ */
269
+ get next() {
270
+ return this._next;
271
+ }
272
+ /**
273
+ * Next node setter.
274
+ * @param value - Next node or `undefined`.
275
+ * @remarks Time O(1), Space O(1)
276
+ */
277
+ set next(value) {
278
+ this._next = value;
279
+ }
280
+ };
281
+ __name(_LinkedListNode, "LinkedListNode");
282
+ var LinkedListNode = _LinkedListNode;
283
+ var _LinearBase = class _LinearBase extends IterableElementBase {
284
+ /**
285
+ * Construct a linear container with runtime options.
286
+ * @param options - `{ maxLen?, ... }` bounds/behavior options.
287
+ * @remarks Time O(1), Space O(1)
288
+ */
289
+ constructor(options) {
290
+ super(options);
291
+ __publicField(this, "_maxLen", -1);
292
+ if (options) {
293
+ const { maxLen } = options;
294
+ if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
295
+ }
296
+ }
297
+ /**
298
+ * Upper bound for length (if positive), or `-1` when unbounded.
299
+ * @returns Maximum allowed length.
300
+ * @remarks Time O(1), Space O(1)
301
+ */
302
+ get maxLen() {
303
+ return this._maxLen;
304
+ }
305
+ /**
306
+ * First index of a value from the left.
307
+ * @param searchElement - Value to match.
308
+ * @param fromIndex - Start position (supports negative index).
309
+ * @returns Index or `-1` if not found.
310
+ * @remarks Time O(n), Space O(1)
311
+ */
312
+ indexOf(searchElement, fromIndex = 0) {
313
+ if (this.length === 0) return -1;
314
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
315
+ if (fromIndex < 0) fromIndex = 0;
316
+ for (let i = fromIndex; i < this.length; i++) {
317
+ const element = this.at(i);
318
+ if (element === searchElement) return i;
319
+ }
320
+ return -1;
321
+ }
322
+ /**
323
+ * Last index of a value from the right.
324
+ * @param searchElement - Value to match.
325
+ * @param fromIndex - Start position (supports negative index).
326
+ * @returns Index or `-1` if not found.
327
+ * @remarks Time O(n), Space O(1)
328
+ */
329
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
330
+ if (this.length === 0) return -1;
331
+ if (fromIndex >= this.length) fromIndex = this.length - 1;
332
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
333
+ for (let i = fromIndex; i >= 0; i--) {
334
+ const element = this.at(i);
335
+ if (element === searchElement) return i;
336
+ }
337
+ return -1;
338
+ }
339
+ /**
340
+ * Find the first index matching a predicate.
341
+ * @param predicate - `(element, index, self) => boolean`.
342
+ * @param thisArg - Optional `this` for callback.
343
+ * @returns Index or `-1`.
344
+ * @remarks Time O(n), Space O(1)
345
+ */
346
+ findIndex(predicate, thisArg) {
347
+ for (let i = 0; i < this.length; i++) {
348
+ const item = this.at(i);
349
+ if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
350
+ }
351
+ return -1;
352
+ }
353
+ /**
354
+ * Concatenate elements and/or containers.
355
+ * @param items - Elements or other containers.
356
+ * @returns New container with combined elements (`this` type).
357
+ * @remarks Time O(sum(length)), Space O(sum(length))
358
+ */
359
+ concat(...items) {
360
+ const newList = this.clone();
361
+ for (const item of items) {
362
+ if (item instanceof _LinearBase) {
363
+ newList.pushMany(item);
364
+ } else {
365
+ newList.push(item);
366
+ }
367
+ }
368
+ return newList;
369
+ }
370
+ /**
371
+ * In-place stable order via array sort semantics.
372
+ * @param compareFn - Comparator `(a, b) => number`.
373
+ * @returns This container.
374
+ * @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
375
+ */
376
+ sort(compareFn) {
377
+ const arr = this.toArray();
378
+ arr.sort(compareFn);
379
+ this.clear();
380
+ for (const item of arr) this.push(item);
381
+ return this;
382
+ }
383
+ /**
384
+ * Remove and/or insert elements at a position (array-compatible).
385
+ * @param start - Start index (supports negative index).
386
+ * @param deleteCount - How many to remove.
387
+ * @param items - Elements to insert.
388
+ * @returns Removed elements as a new list (`this` type).
389
+ * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
390
+ */
391
+ splice(start, deleteCount = 0, ...items) {
392
+ const removedList = this._createInstance();
393
+ start = start < 0 ? this.length + start : start;
394
+ start = Math.max(0, Math.min(start, this.length));
395
+ deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
396
+ for (let i = 0; i < deleteCount; i++) {
397
+ const removed = this.deleteAt(start);
398
+ if (removed !== void 0) {
399
+ removedList.push(removed);
400
+ }
401
+ }
402
+ for (let i = 0; i < items.length; i++) {
403
+ this.addAt(start + i, items[i]);
404
+ }
405
+ return removedList;
406
+ }
407
+ /**
408
+ * Join all elements into a string.
409
+ * @param separator - Separator string.
410
+ * @returns Concatenated string.
411
+ * @remarks Time O(n), Space O(n)
412
+ */
413
+ join(separator = ",") {
414
+ return this.toArray().join(separator);
415
+ }
416
+ /**
417
+ * Snapshot elements into a reversed array.
418
+ * @returns New reversed array.
419
+ * @remarks Time O(n), Space O(n)
420
+ */
421
+ toReversedArray() {
422
+ const array = [];
423
+ for (let i = this.length - 1; i >= 0; i--) {
424
+ array.push(this.at(i));
425
+ }
426
+ return array;
427
+ }
428
+ reduceRight(callbackfn, initialValue) {
429
+ let accumulator = initialValue != null ? initialValue : 0;
430
+ for (let i = this.length - 1; i >= 0; i--) {
431
+ accumulator = callbackfn(accumulator, this.at(i), i, this);
432
+ }
433
+ return accumulator;
434
+ }
435
+ /**
436
+ * Create a shallow copy of a subrange.
437
+ * @param start - Inclusive start (supports negative index).
438
+ * @param end - Exclusive end (supports negative index).
439
+ * @returns New list with the range (`this` type).
440
+ * @remarks Time O(n), Space O(n)
441
+ */
442
+ slice(start = 0, end = this.length) {
443
+ start = start < 0 ? this.length + start : start;
444
+ end = end < 0 ? this.length + end : end;
445
+ const newList = this._createInstance();
446
+ for (let i = start; i < end; i++) {
447
+ newList.push(this.at(i));
448
+ }
449
+ return newList;
450
+ }
451
+ /**
452
+ * Fill a range with a value.
453
+ * @param value - Value to set.
454
+ * @param start - Inclusive start.
455
+ * @param end - Exclusive end.
456
+ * @returns This list.
457
+ * @remarks Time O(n), Space O(1)
458
+ */
459
+ fill(value, start = 0, end = this.length) {
460
+ start = start < 0 ? this.length + start : start;
461
+ end = end < 0 ? this.length + end : end;
462
+ if (start < 0) start = 0;
463
+ if (end > this.length) end = this.length;
464
+ if (start >= end) return this;
465
+ for (let i = start; i < end; i++) {
466
+ this.setAt(i, value);
467
+ }
468
+ return this;
469
+ }
470
+ };
471
+ __name(_LinearBase, "LinearBase");
472
+ var LinearBase = _LinearBase;
473
+ var _LinearLinkedBase = class _LinearLinkedBase extends LinearBase {
474
+ constructor(options) {
475
+ super(options);
476
+ if (options) {
477
+ const { maxLen } = options;
478
+ if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
479
+ }
480
+ }
481
+ /**
482
+ * Linked-list optimized `indexOf` (forwards scan).
483
+ * @param searchElement - Value to match.
484
+ * @param fromIndex - Start position.
485
+ * @returns Index or `-1`.
486
+ * @remarks Time O(n), Space O(1)
487
+ */
488
+ indexOf(searchElement, fromIndex = 0) {
489
+ const iterator = this._getIterator();
490
+ let current = iterator.next();
491
+ let index = 0;
492
+ while (index < fromIndex) {
493
+ current = iterator.next();
494
+ index++;
495
+ }
496
+ while (!current.done) {
497
+ if (current.value === searchElement) return index;
498
+ current = iterator.next();
499
+ index++;
500
+ }
501
+ return -1;
502
+ }
503
+ /**
504
+ * Linked-list optimized `lastIndexOf` (reverse scan).
505
+ * @param searchElement - Value to match.
506
+ * @param fromIndex - Start position.
507
+ * @returns Index or `-1`.
508
+ * @remarks Time O(n), Space O(1)
509
+ */
510
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
511
+ const iterator = this._getReverseIterator();
512
+ let current = iterator.next();
513
+ let index = this.length - 1;
514
+ while (index > fromIndex) {
515
+ current = iterator.next();
516
+ index--;
517
+ }
518
+ while (!current.done) {
519
+ if (current.value === searchElement) return index;
520
+ current = iterator.next();
521
+ index--;
522
+ }
523
+ return -1;
524
+ }
525
+ /**
526
+ * Concatenate lists/elements preserving order.
527
+ * @param items - Elements or `LinearBase` instances.
528
+ * @returns New list with combined elements (`this` type).
529
+ * @remarks Time O(sum(length)), Space O(sum(length))
530
+ */
531
+ concat(...items) {
532
+ const newList = this.clone();
533
+ for (const item of items) {
534
+ if (item instanceof LinearBase) {
535
+ newList.pushMany(item);
536
+ } else {
537
+ newList.push(item);
538
+ }
539
+ }
540
+ return newList;
541
+ }
542
+ /**
543
+ * Slice via forward iteration (no random access required).
544
+ * @param start - Inclusive start (supports negative index).
545
+ * @param end - Exclusive end (supports negative index).
546
+ * @returns New list (`this` type).
547
+ * @remarks Time O(n), Space O(n)
548
+ */
549
+ slice(start = 0, end = this.length) {
550
+ start = start < 0 ? this.length + start : start;
551
+ end = end < 0 ? this.length + end : end;
552
+ const newList = this._createInstance();
553
+ const iterator = this._getIterator();
554
+ let current = iterator.next();
555
+ let c = 0;
556
+ while (c < start) {
557
+ current = iterator.next();
558
+ c++;
559
+ }
560
+ for (let i = start; i < end; i++) {
561
+ newList.push(current.value);
562
+ current = iterator.next();
563
+ }
564
+ return newList;
565
+ }
566
+ /**
567
+ * Splice by walking node iterators from the start index.
568
+ * @param start - Start index.
569
+ * @param deleteCount - How many elements to remove.
570
+ * @param items - Elements to insert after the splice point.
571
+ * @returns Removed elements as a new list (`this` type).
572
+ * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
573
+ */
574
+ splice(start, deleteCount = 0, ...items) {
575
+ const removedList = this._createInstance();
576
+ start = start < 0 ? this.length + start : start;
577
+ start = Math.max(0, Math.min(start, this.length));
578
+ deleteCount = Math.max(0, deleteCount);
579
+ let currentIndex = 0;
580
+ let currentNode = void 0;
581
+ let previousNode = void 0;
582
+ const iterator = this._getNodeIterator();
583
+ for (const node of iterator) {
584
+ if (currentIndex === start) {
585
+ currentNode = node;
586
+ break;
587
+ }
588
+ previousNode = node;
589
+ currentIndex++;
590
+ }
591
+ for (let i = 0; i < deleteCount && currentNode; i++) {
592
+ removedList.push(currentNode.value);
593
+ const nextNode = currentNode.next;
594
+ this.delete(currentNode);
595
+ currentNode = nextNode;
596
+ }
597
+ for (let i = 0; i < items.length; i++) {
598
+ if (previousNode) {
599
+ this.addAfter(previousNode, items[i]);
600
+ previousNode = previousNode.next;
601
+ } else {
602
+ this.addAt(0, items[i]);
603
+ previousNode = this._getNodeIterator().next().value;
604
+ }
605
+ }
606
+ return removedList;
607
+ }
608
+ reduceRight(callbackfn, initialValue) {
609
+ let accumulator = initialValue != null ? initialValue : 0;
610
+ let index = this.length - 1;
611
+ for (const item of this._getReverseIterator()) {
612
+ accumulator = callbackfn(accumulator, item, index--, this);
613
+ }
614
+ return accumulator;
615
+ }
616
+ };
617
+ __name(_LinearLinkedBase, "LinearLinkedBase");
618
+ var LinearLinkedBase = _LinearLinkedBase;
619
+
620
+ // src/data-structures/linked-list/singly-linked-list.ts
621
+ var _SinglyLinkedListNode = class _SinglyLinkedListNode extends LinkedListNode {
622
+ /**
623
+ * Create a list node.
624
+ * @remarks Time O(1), Space O(1)
625
+ * @param value - Element value to store.
626
+ * @returns New node instance.
627
+ */
628
+ constructor(value) {
629
+ super(value);
630
+ __publicField(this, "_next");
631
+ this._value = value;
632
+ this._next = void 0;
633
+ }
634
+ /**
635
+ * Get the next node.
636
+ * @remarks Time O(1), Space O(1)
637
+ * @returns Next node or undefined.
638
+ */
639
+ get next() {
640
+ return this._next;
641
+ }
642
+ /**
643
+ * Set the next node.
644
+ * @remarks Time O(1), Space O(1)
645
+ * @param value - Next node or undefined.
646
+ * @returns void
647
+ */
648
+ set next(value) {
649
+ this._next = value;
650
+ }
651
+ };
652
+ __name(_SinglyLinkedListNode, "SinglyLinkedListNode");
653
+ var SinglyLinkedListNode = _SinglyLinkedListNode;
654
+ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
655
+ /**
656
+ * Create a SinglyLinkedList and optionally bulk-insert elements.
657
+ * @remarks Time O(N), Space O(N)
658
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
659
+ * @param [options] - Options such as maxLen and toElementFn.
660
+ * @returns New SinglyLinkedList instance.
661
+ */
662
+ constructor(elements = [], options) {
663
+ super(options);
664
+ __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
665
+ __publicField(this, "_head");
666
+ __publicField(this, "_tail");
667
+ __publicField(this, "_length", 0);
668
+ this.pushMany(elements);
669
+ }
670
+ /**
671
+ * Get the head node.
672
+ * @remarks Time O(1), Space O(1)
673
+ * @returns Head node or undefined.
674
+ */
675
+ get head() {
676
+ return this._head;
677
+ }
678
+ /**
679
+ * Get the tail node.
680
+ * @remarks Time O(1), Space O(1)
681
+ * @returns Tail node or undefined.
682
+ */
683
+ get tail() {
684
+ return this._tail;
685
+ }
686
+ /**
687
+ * Get the number of elements.
688
+ * @remarks Time O(1), Space O(1)
689
+ * @returns Current length.
690
+ */
691
+ get length() {
692
+ return this._length;
693
+ }
694
+ /**
695
+ * Get the first element value.
696
+ * @remarks Time O(1), Space O(1)
697
+ * @returns First element or undefined.
698
+ */
699
+ get first() {
700
+ var _a;
701
+ return (_a = this.head) == null ? void 0 : _a.value;
702
+ }
703
+ /**
704
+ * Get the last element value.
705
+ * @remarks Time O(1), Space O(1)
706
+ * @returns Last element or undefined.
707
+ */
708
+ get last() {
709
+ var _a;
710
+ return (_a = this.tail) == null ? void 0 : _a.value;
711
+ }
712
+ /**
713
+ * Create a new list from an iterable of elements.
714
+ * @remarks Time O(N), Space O(N)
715
+ * @template E
716
+ * @template R
717
+ * @template S
718
+ * @param this - The constructor (subclass) to instantiate.
719
+ * @param data - Iterable of elements to insert.
720
+ * @param [options] - Options forwarded to the constructor.
721
+ * @returns A new list populated with the iterable's elements.
722
+ */
723
+ static from(data, options) {
724
+ const list = new this([], options);
725
+ for (const x of data) list.push(x);
726
+ return list;
727
+ }
728
+ /**
729
+ * Append an element/node to the tail.
730
+ * @remarks Time O(1), Space O(1)
731
+ * @param elementOrNode - Element or node to append.
732
+ * @returns True when appended.
733
+
734
+
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
+ * @example
765
+ * // basic SinglyLinkedList creation and push operation
766
+ * // Create a simple SinglyLinkedList with initial values
767
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
768
+ *
769
+ * // Verify the list maintains insertion order
770
+ * console.log([...list]); // [1, 2, 3, 4, 5];
771
+ *
772
+ * // Check length
773
+ * console.log(list.length); // 5;
774
+ *
775
+ * // Push a new element to the end
776
+ * list.push(6);
777
+ * console.log(list.length); // 6;
778
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
779
+ */
780
+ push(elementOrNode) {
781
+ const newNode = this._ensureNode(elementOrNode);
782
+ if (!this.head) {
783
+ this._head = this._tail = newNode;
784
+ } else {
785
+ this.tail.next = newNode;
786
+ this._tail = newNode;
787
+ }
788
+ this._length++;
789
+ if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
790
+ return true;
791
+ }
792
+ /**
793
+ * Remove and return the tail element.
794
+ * @remarks Time O(N), Space O(1)
795
+ * @returns Removed element or undefined.
796
+
797
+
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
+ * @example
828
+ * // SinglyLinkedList pop and shift operations
829
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
830
+ *
831
+ * // Pop removes from the end
832
+ * const last = list.pop();
833
+ * console.log(last); // 50;
834
+ *
835
+ * // Shift removes from the beginning
836
+ * const first = list.shift();
837
+ * console.log(first); // 10;
838
+ *
839
+ * // Verify remaining elements
840
+ * console.log([...list]); // [20, 30, 40];
841
+ * console.log(list.length); // 3;
842
+ */
843
+ pop() {
844
+ var _a;
845
+ if (!this.head) return void 0;
846
+ if (this.head === this.tail) {
847
+ const value2 = this.head.value;
848
+ this._head = void 0;
849
+ this._tail = void 0;
850
+ this._length--;
851
+ return value2;
852
+ }
853
+ let current = this.head;
854
+ while (current.next && current.next !== this.tail) current = current.next;
855
+ const value = (_a = this.tail) == null ? void 0 : _a.value;
856
+ current.next = void 0;
857
+ this._tail = current;
858
+ this._length--;
859
+ return value;
860
+ }
861
+ /**
862
+ * Remove and return the head element.
863
+ * @remarks Time O(1), Space O(1)
864
+ * @returns Removed element or undefined.
865
+
866
+
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
+ * @example
897
+ * // Remove from the front
898
+ * const list = new SinglyLinkedList<number>([10, 20, 30]);
899
+ * console.log(list.shift()); // 10;
900
+ * console.log(list.length); // 2;
901
+ */
902
+ shift() {
903
+ if (!this.head) return void 0;
904
+ const removed = this.head;
905
+ this._head = this.head.next;
906
+ if (!this._head) this._tail = void 0;
907
+ this._length--;
908
+ return removed.value;
909
+ }
910
+ /**
911
+ * Prepend an element/node to the head.
912
+ * @remarks Time O(1), Space O(1)
913
+ * @param elementOrNode - Element or node to prepend.
914
+ * @returns True when prepended.
915
+
916
+
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
+ * @example
947
+ * // SinglyLinkedList unshift and forward traversal
948
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
949
+ *
950
+ * // Unshift adds to the beginning
951
+ * list.unshift(10);
952
+ * console.log([...list]); // [10, 20, 30, 40];
953
+ *
954
+ * // Access elements (forward traversal only for singly linked)
955
+ * const second = list.at(1);
956
+ * console.log(second); // 20;
957
+ *
958
+ * // SinglyLinkedList allows forward iteration only
959
+ * const elements: number[] = [];
960
+ * for (const item of list) {
961
+ * elements.push(item);
962
+ * }
963
+ * console.log(elements); // [10, 20, 30, 40];
964
+ *
965
+ * console.log(list.length); // 4;
966
+ */
967
+ unshift(elementOrNode) {
968
+ const newNode = this._ensureNode(elementOrNode);
969
+ if (!this.head) {
970
+ this._head = this._tail = newNode;
971
+ } else {
972
+ newNode.next = this.head;
973
+ this._head = newNode;
974
+ }
975
+ this._length++;
976
+ return true;
977
+ }
978
+ /**
979
+ * Append a sequence of elements/nodes.
980
+ * @remarks Time O(N), Space O(1)
981
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
982
+ * @returns Array of per-element success flags.
983
+ */
984
+ pushMany(elements) {
985
+ const ans = [];
986
+ for (const el of elements) {
987
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
988
+ else ans.push(this.push(el));
989
+ }
990
+ return ans;
991
+ }
992
+ /**
993
+ * Prepend a sequence of elements/nodes.
994
+ * @remarks Time O(N), Space O(1)
995
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
996
+ * @returns Array of per-element success flags.
997
+ */
998
+ unshiftMany(elements) {
999
+ const ans = [];
1000
+ for (const el of elements) {
1001
+ if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
1002
+ else ans.push(this.unshift(el));
1003
+ }
1004
+ return ans;
1005
+ }
1006
+ /**
1007
+ * Find the first value matching a predicate (by node).
1008
+ * @remarks Time O(N), Space O(1)
1009
+ * @param elementNodeOrPredicate - Element, node, or node predicate to match.
1010
+ * @returns Matched value or undefined.
1011
+ */
1012
+ search(elementNodeOrPredicate) {
1013
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1014
+ let current = this.head;
1015
+ while (current) {
1016
+ if (predicate(current)) return current.value;
1017
+ current = current.next;
1018
+ }
1019
+ return void 0;
1020
+ }
1021
+ /**
1022
+ * Get the element at a given index.
1023
+ * @remarks Time O(N), Space O(1)
1024
+ * @param index - Zero-based index.
1025
+ * @returns Element or undefined.
1026
+
1027
+
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
+ * @example
1058
+ * // Access element by index
1059
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
1060
+ * console.log(list.at(0)); // 'a';
1061
+ * console.log(list.at(2)); // 'c';
1062
+ * console.log(list.at(3)); // 'd';
1063
+ */
1064
+ at(index) {
1065
+ if (index < 0 || index >= this._length) return void 0;
1066
+ let current = this.head;
1067
+ for (let i = 0; i < index && current; i++) current = current.next;
1068
+ return current == null ? void 0 : current.value;
1069
+ }
1070
+ /**
1071
+ * Type guard: check whether the input is a SinglyLinkedListNode.
1072
+ * @remarks Time O(1), Space O(1)
1073
+ * @param elementNodeOrPredicate - Element, node, or predicate.
1074
+ * @returns True if the value is a SinglyLinkedListNode.
1075
+ */
1076
+ isNode(elementNodeOrPredicate) {
1077
+ return elementNodeOrPredicate instanceof SinglyLinkedListNode;
1078
+ }
1079
+ /**
1080
+ * Get the node reference at a given index.
1081
+ * @remarks Time O(N), Space O(1)
1082
+ * @param index - Zero-based index.
1083
+ * @returns Node or undefined.
1084
+
1085
+
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
+ * @example
1113
+ * // Get node at index
1114
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1115
+ * console.log(list.getNodeAt(1)?.value); // 'b';
1116
+ */
1117
+ getNodeAt(index) {
1118
+ if (index < 0 || index >= this._length) return void 0;
1119
+ let current = this.head;
1120
+ for (let i = 0; i < index && current; i++) current = current.next;
1121
+ return current;
1122
+ }
1123
+ /**
1124
+ * Delete the element at an index.
1125
+ * @remarks Time O(N), Space O(1)
1126
+ * @param index - Zero-based index.
1127
+ * @returns Removed element or undefined.
1128
+
1129
+
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
+ * @example
1157
+ * // Remove by index
1158
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1159
+ * list.deleteAt(1);
1160
+ * console.log(list.toArray()); // ['a', 'c'];
1161
+ */
1162
+ deleteAt(index) {
1163
+ if (index < 0 || index >= this._length) return void 0;
1164
+ if (index === 0) return this.shift();
1165
+ const targetNode = this.getNodeAt(index);
1166
+ const prevNode = this._getPrevNode(targetNode);
1167
+ const value = targetNode.value;
1168
+ prevNode.next = targetNode.next;
1169
+ if (targetNode === this.tail) this._tail = prevNode;
1170
+ this._length--;
1171
+ return value;
1172
+ }
1173
+ /**
1174
+ * Delete the first match by value/node.
1175
+ * @remarks Time O(N), Space O(1)
1176
+ * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
1177
+ * @returns True if removed.
1178
+
1179
+
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
+ * @example
1207
+ * // Remove first occurrence
1208
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
1209
+ * list.delete(2);
1210
+ * console.log(list.toArray()); // [1, 3, 2];
1211
+ */
1212
+ delete(elementOrNode) {
1213
+ if (elementOrNode === void 0 || !this.head) return false;
1214
+ const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
1215
+ if (!node) return false;
1216
+ const prevNode = this._getPrevNode(node);
1217
+ if (!prevNode) {
1218
+ this._head = node.next;
1219
+ if (node === this.tail) this._tail = void 0;
1220
+ } else {
1221
+ prevNode.next = node.next;
1222
+ if (node === this.tail) this._tail = prevNode;
1223
+ }
1224
+ this._length--;
1225
+ return true;
1226
+ }
1227
+ /**
1228
+ * Insert a new element/node at an index, shifting following nodes.
1229
+ * @remarks Time O(N), Space O(1)
1230
+ * @param index - Zero-based index.
1231
+ * @param newElementOrNode - Element or node to insert.
1232
+ * @returns True if inserted.
1233
+
1234
+
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
+ * @example
1262
+ * // Insert at index
1263
+ * const list = new SinglyLinkedList<number>([1, 3]);
1264
+ * list.addAt(1, 2);
1265
+ * console.log(list.toArray()); // [1, 2, 3];
1266
+ */
1267
+ addAt(index, newElementOrNode) {
1268
+ if (index < 0 || index > this._length) return false;
1269
+ if (index === 0) return this.unshift(newElementOrNode);
1270
+ if (index === this._length) return this.push(newElementOrNode);
1271
+ const newNode = this._ensureNode(newElementOrNode);
1272
+ const prevNode = this.getNodeAt(index - 1);
1273
+ newNode.next = prevNode.next;
1274
+ prevNode.next = newNode;
1275
+ this._length++;
1276
+ return true;
1277
+ }
1278
+ /**
1279
+ * Set the element value at an index.
1280
+ * @remarks Time O(N), Space O(1)
1281
+ * @param index - Zero-based index.
1282
+ * @param value - New value.
1283
+ * @returns True if updated.
1284
+ */
1285
+ setAt(index, value) {
1286
+ const node = this.getNodeAt(index);
1287
+ if (!node) return false;
1288
+ node.value = value;
1289
+ return true;
1290
+ }
1291
+ /**
1292
+ * Check whether the list is empty.
1293
+ * @remarks Time O(1), Space O(1)
1294
+ * @returns True if length is 0.
1295
+
1296
+
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
+ * @example
1325
+ * // Check empty
1326
+ * console.log(new SinglyLinkedList().isEmpty()); // true;
1327
+ */
1328
+ isEmpty() {
1329
+ return this._length === 0;
1330
+ }
1331
+ /**
1332
+ * Remove all nodes and reset length.
1333
+ * @remarks Time O(N), Space O(1)
1334
+ * @returns void
1335
+
1336
+
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
+ * @example
1365
+ * // Remove all
1366
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1367
+ * list.clear();
1368
+ * console.log(list.isEmpty()); // true;
1369
+ */
1370
+ clear() {
1371
+ this._head = void 0;
1372
+ this._tail = void 0;
1373
+ this._length = 0;
1374
+ }
1375
+ /**
1376
+ * Reverse the list in place.
1377
+ * @remarks Time O(N), Space O(1)
1378
+ * @returns This list.
1379
+
1380
+
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
+ * @example
1411
+ * // Reverse the list in-place
1412
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
1413
+ * list.reverse();
1414
+ * console.log([...list]); // [4, 3, 2, 1];
1415
+ */
1416
+ reverse() {
1417
+ if (!this.head || this.head === this.tail) return this;
1418
+ let prev;
1419
+ let current = this.head;
1420
+ let next;
1421
+ while (current) {
1422
+ next = current.next;
1423
+ current.next = prev;
1424
+ prev = current;
1425
+ current = next;
1426
+ }
1427
+ [this._head, this._tail] = [this.tail, this.head];
1428
+ return this;
1429
+ }
1430
+ /**
1431
+ * Find a node by value, reference, or predicate.
1432
+ * @remarks Time O(N), Space O(1)
1433
+ * @param [elementNodeOrPredicate] - Element, node, or node predicate to match.
1434
+ * @returns Matching node or undefined.
1435
+ */
1436
+ getNode(elementNodeOrPredicate) {
1437
+ if (elementNodeOrPredicate === void 0) return;
1438
+ if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
1439
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1440
+ let current = this.head;
1441
+ while (current) {
1442
+ if (predicate(current)) return current;
1443
+ current = current.next;
1444
+ }
1445
+ return void 0;
1446
+ }
1447
+ /**
1448
+ * Insert a new element/node before an existing one.
1449
+ * @remarks Time O(N), Space O(1)
1450
+ * @param existingElementOrNode - Existing element or node.
1451
+ * @param newElementOrNode - Element or node to insert.
1452
+ * @returns True if inserted.
1453
+ */
1454
+ addBefore(existingElementOrNode, newElementOrNode) {
1455
+ const existingNode = this.getNode(existingElementOrNode);
1456
+ if (!existingNode) return false;
1457
+ const prevNode = this._getPrevNode(existingNode);
1458
+ const newNode = this._ensureNode(newElementOrNode);
1459
+ if (!prevNode) {
1460
+ newNode.next = this._head;
1461
+ this._head = newNode;
1462
+ if (!this._tail) this._tail = newNode;
1463
+ this._length++;
1464
+ } else {
1465
+ prevNode.next = newNode;
1466
+ newNode.next = existingNode;
1467
+ this._length++;
1468
+ }
1469
+ return true;
1470
+ }
1471
+ /**
1472
+ * Insert a new element/node after an existing one.
1473
+ * @remarks Time O(N), Space O(1)
1474
+ * @param existingElementOrNode - Existing element or node.
1475
+ * @param newElementOrNode - Element or node to insert.
1476
+ * @returns True if inserted.
1477
+ */
1478
+ addAfter(existingElementOrNode, newElementOrNode) {
1479
+ const existingNode = this.getNode(existingElementOrNode);
1480
+ if (!existingNode) return false;
1481
+ const newNode = this._ensureNode(newElementOrNode);
1482
+ newNode.next = existingNode.next;
1483
+ existingNode.next = newNode;
1484
+ if (existingNode === this.tail) this._tail = newNode;
1485
+ this._length++;
1486
+ return true;
1487
+ }
1488
+ /**
1489
+ * Remove and/or insert elements at a position (array-like behavior).
1490
+ * @remarks Time O(N + M), Space O(M)
1491
+ * @param start - Start index (clamped to [0, length]).
1492
+ * @param [deleteCount] - Number of elements to remove (default 0).
1493
+ * @param [items] - Elements to insert after `start`.
1494
+ * @returns A new list containing the removed elements (typed as `this`).
1495
+ */
1496
+ splice(start, deleteCount = 0, ...items) {
1497
+ start = Math.max(0, Math.min(start, this.length));
1498
+ deleteCount = Math.max(0, deleteCount);
1499
+ const removedList = this._createInstance();
1500
+ const prevNode = start === 0 ? void 0 : this.getNodeAt(start - 1);
1501
+ let cur = prevNode ? prevNode.next : this.head;
1502
+ let removedCount = 0;
1503
+ while (removedCount < deleteCount && cur) {
1504
+ removedList.push(cur.value);
1505
+ cur = cur.next;
1506
+ removedCount++;
1507
+ }
1508
+ const afterNode = cur;
1509
+ if (prevNode) {
1510
+ prevNode.next = afterNode;
1511
+ } else {
1512
+ this._head = afterNode;
1513
+ }
1514
+ if (!afterNode) this._tail = prevNode;
1515
+ if (items.length > 0) {
1516
+ let firstInserted;
1517
+ let lastInserted;
1518
+ for (const it of items) {
1519
+ const node = this._ensureNode(it);
1520
+ if (!firstInserted) firstInserted = node;
1521
+ if (lastInserted) lastInserted.next = node;
1522
+ lastInserted = node;
1523
+ }
1524
+ if (prevNode) prevNode.next = firstInserted;
1525
+ else this._head = firstInserted;
1526
+ lastInserted.next = afterNode;
1527
+ if (!afterNode) this._tail = lastInserted;
1528
+ }
1529
+ this._length += items.length - removedCount;
1530
+ if (this._length === 0) {
1531
+ this._head = void 0;
1532
+ this._tail = void 0;
1533
+ }
1534
+ return removedList;
1535
+ }
1536
+ /**
1537
+ * Count how many nodes match a value/node/predicate.
1538
+ * @remarks Time O(N), Space O(1)
1539
+ * @param elementOrNode - Element, node, or node predicate to match.
1540
+ * @returns Number of matches in the list.
1541
+ */
1542
+ countOccurrences(elementOrNode) {
1543
+ const predicate = elementOrPredicate(elementOrNode, this._equals);
1544
+ let count = 0;
1545
+ let current = this.head;
1546
+ while (current) {
1547
+ if (predicate(current)) count++;
1548
+ current = current.next;
1549
+ }
1550
+ return count;
1551
+ }
1552
+ /**
1553
+ * Set the equality comparator used to compare values.
1554
+ * @remarks Time O(1), Space O(1)
1555
+ * @param equals - Equality predicate (a, b) → boolean.
1556
+ * @returns This list.
1557
+ */
1558
+ setEquality(equals) {
1559
+ this._equals = equals;
1560
+ return this;
1561
+ }
1562
+ /**
1563
+ * Delete the first node whose value matches a predicate.
1564
+ * @remarks Time O(N), Space O(1)
1565
+ * @param predicate - Predicate (value, index, list) → boolean to decide deletion.
1566
+ * @returns True if a node was removed.
1567
+ */
1568
+ deleteWhere(predicate) {
1569
+ let prev;
1570
+ let current = this.head;
1571
+ let i = 0;
1572
+ while (current) {
1573
+ if (predicate(current.value, i++, this)) {
1574
+ if (!prev) {
1575
+ this._head = current.next;
1576
+ if (current === this._tail) this._tail = void 0;
1577
+ } else {
1578
+ prev.next = current.next;
1579
+ if (current === this._tail) this._tail = prev;
1580
+ }
1581
+ this._length--;
1582
+ return true;
1583
+ }
1584
+ prev = current;
1585
+ current = current.next;
1586
+ }
1587
+ return false;
1588
+ }
1589
+ /**
1590
+ * Deep clone this list (values are copied by reference).
1591
+ * @remarks Time O(N), Space O(N)
1592
+ * @returns A new list with the same element sequence.
1593
+
1594
+
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
+ * @example
1623
+ * // Deep copy
1624
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1625
+ * const copy = list.clone();
1626
+ * copy.pop();
1627
+ * console.log(list.length); // 3;
1628
+ * console.log(copy.length); // 2;
1629
+ */
1630
+ clone() {
1631
+ const out = this._createInstance();
1632
+ for (const v of this) out.push(v);
1633
+ return out;
1634
+ }
1635
+ /**
1636
+ * Filter values into a new list of the same class.
1637
+ * @remarks Time O(N), Space O(N)
1638
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
1639
+ * @param [thisArg] - Value for `this` inside the callback.
1640
+ * @returns A new list with kept values.
1641
+
1642
+
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
+ * @example
1673
+ * // SinglyLinkedList filter and map operations
1674
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
1675
+ *
1676
+ * // Filter even numbers
1677
+ * const filtered = list.filter(value => value % 2 === 0);
1678
+ * console.log(filtered.length); // 2;
1679
+ *
1680
+ * // Map to double values
1681
+ * const doubled = list.map(value => value * 2);
1682
+ * console.log(doubled.length); // 5;
1683
+ *
1684
+ * // Use reduce to sum
1685
+ * const sum = list.reduce((acc, value) => acc + value, 0);
1686
+ * console.log(sum); // 15;
1687
+ */
1688
+ filter(callback, thisArg) {
1689
+ const out = this._createInstance();
1690
+ let index = 0;
1691
+ for (const value of this) if (callback.call(thisArg, value, index++, this)) out.push(value);
1692
+ return out;
1693
+ }
1694
+ /**
1695
+ * Map values into a new list of the same class.
1696
+ * @remarks Time O(N), Space O(N)
1697
+ * @param callback - Mapping function (value, index, list) → newValue.
1698
+ * @param [thisArg] - Value for `this` inside the callback.
1699
+ * @returns A new list with mapped values.
1700
+ */
1701
+ mapSame(callback, thisArg) {
1702
+ const out = this._createInstance();
1703
+ let index = 0;
1704
+ for (const value of this) {
1705
+ const mv = thisArg === void 0 ? callback(value, index++, this) : callback.call(thisArg, value, index++, this);
1706
+ out.push(mv);
1707
+ }
1708
+ return out;
1709
+ }
1710
+ /**
1711
+ * Map values into a new list (possibly different element type).
1712
+ * @remarks Time O(N), Space O(N)
1713
+ * @template EM
1714
+ * @template RM
1715
+ * @param callback - Mapping function (value, index, list) → newElement.
1716
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1717
+ * @param [thisArg] - Value for `this` inside the callback.
1718
+ * @returns A new SinglyLinkedList with mapped values.
1719
+
1720
+
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
+ * @example
1751
+ * // Transform elements
1752
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1753
+ * const doubled = list.map(n => n * 2);
1754
+ * console.log([...doubled]); // [2, 4, 6];
1755
+ */
1756
+ map(callback, options, thisArg) {
1757
+ const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
1758
+ let index = 0;
1759
+ for (const value of this) out.push(callback.call(thisArg, value, index++, this));
1760
+ return out;
1761
+ }
1762
+ /**
1763
+ * (Protected) Create a node from a value.
1764
+ * @remarks Time O(1), Space O(1)
1765
+ * @param value - Value to wrap in a node.
1766
+ * @returns A new SinglyLinkedListNode instance.
1767
+ */
1768
+ createNode(value) {
1769
+ return new SinglyLinkedListNode(value);
1770
+ }
1771
+ /**
1772
+ * (Protected) Check if input is a node predicate function.
1773
+ * @remarks Time O(1), Space O(1)
1774
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
1775
+ * @returns True if input is a predicate function.
1776
+ */
1777
+ _isPredicate(elementNodeOrPredicate) {
1778
+ return typeof elementNodeOrPredicate === "function";
1779
+ }
1780
+ /**
1781
+ * (Protected) Normalize input into a node instance.
1782
+ * @remarks Time O(1), Space O(1)
1783
+ * @param elementOrNode - Element or node.
1784
+ * @returns A SinglyLinkedListNode for the provided input.
1785
+ */
1786
+ _ensureNode(elementOrNode) {
1787
+ if (this.isNode(elementOrNode)) return elementOrNode;
1788
+ return this.createNode(elementOrNode);
1789
+ }
1790
+ /**
1791
+ * (Protected) Normalize input into a node predicate.
1792
+ * @remarks Time O(1), Space O(1)
1793
+ * @param elementNodeOrPredicate - Element, node, or predicate.
1794
+ * @returns A predicate taking a node and returning true/false.
1795
+ */
1796
+ _ensurePredicate(elementNodeOrPredicate) {
1797
+ if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
1798
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
1799
+ const value = elementNodeOrPredicate;
1800
+ return (node) => this._equals(node.value, value);
1801
+ }
1802
+ /**
1803
+ * (Protected) Get the previous node of a given node.
1804
+ * @remarks Time O(N), Space O(1)
1805
+ * @param node - A node in the list.
1806
+ * @returns Previous node or undefined.
1807
+ */
1808
+ _getPrevNode(node) {
1809
+ if (!this.head || this.head === node) return void 0;
1810
+ let current = this.head;
1811
+ while (current.next && current.next !== node) current = current.next;
1812
+ return current.next === node ? current : void 0;
1813
+ }
1814
+ /**
1815
+ * (Protected) Iterate values from head to tail.
1816
+ * @remarks Time O(N), Space O(1)
1817
+ * @returns Iterator of values (E).
1818
+ */
1819
+ *_getIterator() {
1820
+ let current = this.head;
1821
+ while (current) {
1822
+ yield current.value;
1823
+ current = current.next;
1824
+ }
1825
+ }
1826
+ /**
1827
+ * (Protected) Iterate values from tail to head.
1828
+ * @remarks Time O(N), Space O(N)
1829
+ * @returns Iterator of values (E).
1830
+ */
1831
+ *_getReverseIterator() {
1832
+ const reversedArr = [...this].reverse();
1833
+ for (const item of reversedArr) yield item;
1834
+ }
1835
+ /**
1836
+ * (Protected) Iterate nodes from head to tail.
1837
+ * @remarks Time O(N), Space O(1)
1838
+ * @returns Iterator of nodes.
1839
+ */
1840
+ *_getNodeIterator() {
1841
+ let current = this.head;
1842
+ while (current) {
1843
+ yield current;
1844
+ current = current.next;
1845
+ }
1846
+ }
1847
+ /**
1848
+ * (Protected) Create an empty instance of the same concrete class.
1849
+ * @remarks Time O(1), Space O(1)
1850
+ * @param [options] - Options forwarded to the constructor.
1851
+ * @returns An empty like-kind list instance.
1852
+ */
1853
+ _createInstance(options) {
1854
+ const Ctor = this.constructor;
1855
+ return new Ctor([], options);
1856
+ }
1857
+ /**
1858
+ * (Protected) Create a like-kind instance and seed it from an iterable.
1859
+ * @remarks Time O(N), Space O(N)
1860
+ * @template EM
1861
+ * @template RM
1862
+ * @param [elements] - Iterable used to seed the new list.
1863
+ * @param [options] - Options forwarded to the constructor.
1864
+ * @returns A like-kind SinglyLinkedList instance.
1865
+ */
1866
+ _createLike(elements = [], options) {
1867
+ const Ctor = this.constructor;
1868
+ return new Ctor(elements, options);
1869
+ }
1870
+ /**
1871
+ * (Protected) Spawn an empty like-kind list instance.
1872
+ * @remarks Time O(1), Space O(1)
1873
+ * @template EM
1874
+ * @template RM
1875
+ * @param [options] - Options forwarded to the constructor.
1876
+ * @returns An empty like-kind SinglyLinkedList instance.
1877
+ */
1878
+ _spawnLike(options) {
1879
+ return this._createLike([], options);
1880
+ }
1881
+ };
1882
+ __name(_SinglyLinkedList, "SinglyLinkedList");
1883
+ var SinglyLinkedList = _SinglyLinkedList;
1884
+ function elementOrPredicate(input, equals) {
1885
+ if (input instanceof SinglyLinkedListNode) return (node) => node === input;
1886
+ if (typeof input === "function") return input;
1887
+ const value = input;
1888
+ return (node) => equals(node.value, value);
1889
+ }
1890
+ __name(elementOrPredicate, "elementOrPredicate");
1891
+
1892
+ // src/data-structures/linked-list/doubly-linked-list.ts
1893
+ var _DoublyLinkedListNode = class _DoublyLinkedListNode extends LinkedListNode {
1894
+ /**
1895
+ * Create a node.
1896
+ * @remarks Time O(1), Space O(1)
1897
+ * @param value - Element value to store.
1898
+ * @returns New node instance.
1899
+ */
1900
+ constructor(value) {
1901
+ super(value);
1902
+ __publicField(this, "_next");
1903
+ __publicField(this, "_prev");
1904
+ this._value = value;
1905
+ this._next = void 0;
1906
+ this._prev = void 0;
1907
+ }
1908
+ /**
1909
+ * Get the next node link.
1910
+ * @remarks Time O(1), Space O(1)
1911
+ * @returns Next node or undefined.
1912
+ */
1913
+ get next() {
1914
+ return this._next;
1915
+ }
1916
+ /**
1917
+ * Set the next node link.
1918
+ * @remarks Time O(1), Space O(1)
1919
+ * @param value - Next node or undefined.
1920
+ * @returns void
1921
+ */
1922
+ set next(value) {
1923
+ this._next = value;
1924
+ }
1925
+ /**
1926
+ * Get the previous node link.
1927
+ * @remarks Time O(1), Space O(1)
1928
+ * @returns Previous node or undefined.
1929
+ */
1930
+ get prev() {
1931
+ return this._prev;
1932
+ }
1933
+ /**
1934
+ * Set the previous node link.
1935
+ * @remarks Time O(1), Space O(1)
1936
+ * @param value - Previous node or undefined.
1937
+ * @returns void
1938
+ */
1939
+ set prev(value) {
1940
+ this._prev = value;
1941
+ }
1942
+ };
1943
+ __name(_DoublyLinkedListNode, "DoublyLinkedListNode");
1944
+ var DoublyLinkedListNode = _DoublyLinkedListNode;
1945
+ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
1946
+ /**
1947
+ * Create a DoublyLinkedList and optionally bulk-insert elements.
1948
+ * @remarks Time O(N), Space O(N)
1949
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
1950
+ * @param [options] - Options such as maxLen and toElementFn.
1951
+ * @returns New DoublyLinkedList instance.
1952
+ */
1953
+ constructor(elements = [], options) {
1954
+ super(options);
1955
+ __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
1956
+ __publicField(this, "_head");
1957
+ __publicField(this, "_tail");
1958
+ __publicField(this, "_length", 0);
1959
+ this._head = void 0;
1960
+ this._tail = void 0;
1961
+ this._length = 0;
1962
+ if ((options == null ? void 0 : options.maxLen) && Number.isInteger(options.maxLen) && options.maxLen > 0) {
1963
+ this._maxLen = options.maxLen;
1964
+ }
1965
+ this.pushMany(elements);
1966
+ }
1967
+ /**
1968
+ * Get the head node.
1969
+ * @remarks Time O(1), Space O(1)
1970
+ * @returns Head node or undefined.
1971
+ */
1972
+ get head() {
1973
+ return this._head;
1974
+ }
1975
+ /**
1976
+ * Get the tail node.
1977
+ * @remarks Time O(1), Space O(1)
1978
+ * @returns Tail node or undefined.
1979
+ */
1980
+ get tail() {
1981
+ return this._tail;
1982
+ }
1983
+ /**
1984
+ * Get the number of elements.
1985
+ * @remarks Time O(1), Space O(1)
1986
+ * @returns Current length.
1987
+ */
1988
+ get length() {
1989
+ return this._length;
1990
+ }
1991
+ /**
1992
+ * Get the first element value.
1993
+ * @remarks Time O(1), Space O(1)
1994
+ * @returns First element or undefined.
1995
+ */
1996
+ get first() {
1997
+ var _a;
1998
+ return (_a = this.head) == null ? void 0 : _a.value;
1999
+ }
2000
+ /**
2001
+ * Get the last element value.
2002
+ * @remarks Time O(1), Space O(1)
2003
+ * @returns Last element or undefined.
2004
+ */
2005
+ get last() {
2006
+ var _a;
2007
+ return (_a = this.tail) == null ? void 0 : _a.value;
2008
+ }
2009
+ /**
2010
+ * Create a new list from an array of elements.
2011
+ * @remarks Time O(N), Space O(N)
2012
+ * @template E
2013
+ * @template R
2014
+ * @param this - The constructor (subclass) to instantiate.
2015
+ * @param data - Array of elements to insert.
2016
+ * @returns A new list populated with the array's elements.
2017
+ */
2018
+ static fromArray(data) {
2019
+ return new this(data);
2020
+ }
2021
+ /**
2022
+ * Type guard: check whether the input is a DoublyLinkedListNode.
2023
+ * @remarks Time O(1), Space O(1)
2024
+ * @param elementNodeOrPredicate - Element, node, or predicate.
2025
+ * @returns True if the value is a DoublyLinkedListNode.
2026
+ */
2027
+ isNode(elementNodeOrPredicate) {
2028
+ return elementNodeOrPredicate instanceof DoublyLinkedListNode;
2029
+ }
2030
+ /**
2031
+ * Append an element/node to the tail.
2032
+ * @remarks Time O(1), Space O(1)
2033
+ * @param elementOrNode - Element or node to append.
2034
+ * @returns True when appended.
2035
+
2036
+
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
+ * @example
2067
+ * // basic DoublyLinkedList creation and push operation
2068
+ * // Create a simple DoublyLinkedList with initial values
2069
+ * const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
2070
+ *
2071
+ * // Verify the list maintains insertion order
2072
+ * console.log([...list]); // [1, 2, 3, 4, 5];
2073
+ *
2074
+ * // Check length
2075
+ * console.log(list.length); // 5;
2076
+ *
2077
+ * // Push a new element to the end
2078
+ * list.push(6);
2079
+ * console.log(list.length); // 6;
2080
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
2081
+ */
2082
+ push(elementOrNode) {
2083
+ const newNode = this._ensureNode(elementOrNode);
2084
+ if (!this.head) {
2085
+ this._head = newNode;
2086
+ this._tail = newNode;
2087
+ } else {
2088
+ newNode.prev = this.tail;
2089
+ this.tail.next = newNode;
2090
+ this._tail = newNode;
2091
+ }
2092
+ this._length++;
2093
+ if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
2094
+ return true;
2095
+ }
2096
+ /**
2097
+ * Remove and return the tail element.
2098
+ * @remarks Time O(1), Space O(1)
2099
+ * @returns Removed element or undefined.
2100
+
2101
+
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
+ * @example
2132
+ * // DoublyLinkedList pop and shift operations
2133
+ * const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
2134
+ *
2135
+ * // Pop removes from the end
2136
+ * const last = list.pop();
2137
+ * console.log(last); // 50;
2138
+ *
2139
+ * // Shift removes from the beginning
2140
+ * const first = list.shift();
2141
+ * console.log(first); // 10;
2142
+ *
2143
+ * // Verify remaining elements
2144
+ * console.log([...list]); // [20, 30, 40];
2145
+ * console.log(list.length); // 3;
2146
+ */
2147
+ pop() {
2148
+ if (!this.tail) return void 0;
2149
+ const removed = this.tail;
2150
+ if (this.head === this.tail) {
2151
+ this._head = void 0;
2152
+ this._tail = void 0;
2153
+ } else {
2154
+ this._tail = removed.prev;
2155
+ this.tail.next = void 0;
2156
+ }
2157
+ this._length--;
2158
+ return removed.value;
2159
+ }
2160
+ /**
2161
+ * Remove and return the head element.
2162
+ * @remarks Time O(1), Space O(1)
2163
+ * @returns Removed element or undefined.
2164
+
2165
+
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
+ * @example
2196
+ * // Remove from the front
2197
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
2198
+ * console.log(list.shift()); // 10;
2199
+ * console.log(list.first); // 20;
2200
+ */
2201
+ shift() {
2202
+ if (!this.head) return void 0;
2203
+ const removed = this.head;
2204
+ if (this.head === this.tail) {
2205
+ this._head = void 0;
2206
+ this._tail = void 0;
2207
+ } else {
2208
+ this._head = removed.next;
2209
+ this.head.prev = void 0;
2210
+ }
2211
+ this._length--;
2212
+ return removed.value;
2213
+ }
2214
+ /**
2215
+ * Prepend an element/node to the head.
2216
+ * @remarks Time O(1), Space O(1)
2217
+ * @param elementOrNode - Element or node to prepend.
2218
+ * @returns True when prepended.
2219
+
2220
+
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
+ * @example
2251
+ * // Add to the front
2252
+ * const list = new DoublyLinkedList<number>([2, 3]);
2253
+ * list.unshift(1);
2254
+ * console.log([...list]); // [1, 2, 3];
2255
+ */
2256
+ unshift(elementOrNode) {
2257
+ const newNode = this._ensureNode(elementOrNode);
2258
+ if (!this.head) {
2259
+ this._head = newNode;
2260
+ this._tail = newNode;
2261
+ } else {
2262
+ newNode.next = this.head;
2263
+ this.head.prev = newNode;
2264
+ this._head = newNode;
2265
+ }
2266
+ this._length++;
2267
+ if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
2268
+ return true;
2269
+ }
2270
+ /**
2271
+ * Append a sequence of elements/nodes.
2272
+ * @remarks Time O(N), Space O(1)
2273
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
2274
+ * @returns Array of per-element success flags.
2275
+ */
2276
+ pushMany(elements) {
2277
+ const ans = [];
2278
+ for (const el of elements) {
2279
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
2280
+ else ans.push(this.push(el));
2281
+ }
2282
+ return ans;
2283
+ }
2284
+ /**
2285
+ * Prepend a sequence of elements/nodes.
2286
+ * @remarks Time O(N), Space O(1)
2287
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
2288
+ * @returns Array of per-element success flags.
2289
+ */
2290
+ unshiftMany(elements) {
2291
+ const ans = [];
2292
+ for (const el of elements) {
2293
+ if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
2294
+ else ans.push(this.unshift(el));
2295
+ }
2296
+ return ans;
2297
+ }
2298
+ /**
2299
+ * Get the element at a given index.
2300
+ * @remarks Time O(N), Space O(1)
2301
+ * @param index - Zero-based index.
2302
+ * @returns Element or undefined.
2303
+
2304
+
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
+ * @example
2335
+ * // Access by index
2336
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2337
+ * console.log(list.at(1)); // 'b';
2338
+ * console.log(list.at(2)); // 'c';
2339
+ */
2340
+ at(index) {
2341
+ if (index < 0 || index >= this._length) return void 0;
2342
+ let current = this.head;
2343
+ for (let i = 0; i < index && current; i++) current = current.next;
2344
+ return current == null ? void 0 : current.value;
2345
+ }
2346
+ /**
2347
+ * Get the node reference at a given index.
2348
+ * @remarks Time O(N), Space O(1)
2349
+ * @param index - Zero-based index.
2350
+ * @returns Node or undefined.
2351
+
2352
+
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
+ * @example
2380
+ * // Get node at index
2381
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2382
+ * console.log(list.getNodeAt(1)?.value); // 'b';
2383
+ */
2384
+ getNodeAt(index) {
2385
+ if (index < 0 || index >= this._length) return void 0;
2386
+ let current = this.head;
2387
+ for (let i = 0; i < index && current; i++) current = current.next;
2388
+ return current;
2389
+ }
2390
+ /**
2391
+ * Find a node by value, reference, or predicate.
2392
+ * @remarks Time O(N), Space O(1)
2393
+ * @param [elementNodeOrPredicate] - Element, node, or predicate to match.
2394
+ * @returns Matching node or undefined.
2395
+ */
2396
+ getNode(elementNodeOrPredicate) {
2397
+ if (elementNodeOrPredicate === void 0) return;
2398
+ if (this.isNode(elementNodeOrPredicate)) {
2399
+ const target = elementNodeOrPredicate;
2400
+ let cur = this.head;
2401
+ while (cur) {
2402
+ if (cur === target) return target;
2403
+ cur = cur.next;
2404
+ }
2405
+ const isMatch = /* @__PURE__ */ __name((node) => this._equals(node.value, target.value), "isMatch");
2406
+ cur = this.head;
2407
+ while (cur) {
2408
+ if (isMatch(cur)) return cur;
2409
+ cur = cur.next;
2410
+ }
2411
+ return void 0;
2412
+ }
2413
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2414
+ let current = this.head;
2415
+ while (current) {
2416
+ if (predicate(current)) return current;
2417
+ current = current.next;
2418
+ }
2419
+ return void 0;
2420
+ }
2421
+ /**
2422
+ * Insert a new element/node at an index, shifting following nodes.
2423
+ * @remarks Time O(N), Space O(1)
2424
+ * @param index - Zero-based index.
2425
+ * @param newElementOrNode - Element or node to insert.
2426
+ * @returns True if inserted.
2427
+
2428
+
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
+ * @example
2456
+ * // Insert at position
2457
+ * const list = new DoublyLinkedList<number>([1, 3]);
2458
+ * list.addAt(1, 2);
2459
+ * console.log(list.toArray()); // [1, 2, 3];
2460
+ */
2461
+ addAt(index, newElementOrNode) {
2462
+ if (index < 0 || index > this._length) return false;
2463
+ if (index === 0) return this.unshift(newElementOrNode);
2464
+ if (index === this._length) return this.push(newElementOrNode);
2465
+ const newNode = this._ensureNode(newElementOrNode);
2466
+ const prevNode = this.getNodeAt(index - 1);
2467
+ const nextNode = prevNode.next;
2468
+ newNode.prev = prevNode;
2469
+ newNode.next = nextNode;
2470
+ prevNode.next = newNode;
2471
+ nextNode.prev = newNode;
2472
+ this._length++;
2473
+ return true;
2474
+ }
2475
+ /**
2476
+ * Insert a new element/node before an existing one.
2477
+ * @remarks Time O(N), Space O(1)
2478
+ * @param existingElementOrNode - Existing element or node.
2479
+ * @param newElementOrNode - Element or node to insert.
2480
+ * @returns True if inserted.
2481
+ */
2482
+ addBefore(existingElementOrNode, newElementOrNode) {
2483
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2484
+ if (!existingNode) return false;
2485
+ const newNode = this._ensureNode(newElementOrNode);
2486
+ newNode.prev = existingNode.prev;
2487
+ if (existingNode.prev) existingNode.prev.next = newNode;
2488
+ newNode.next = existingNode;
2489
+ existingNode.prev = newNode;
2490
+ if (existingNode === this.head) this._head = newNode;
2491
+ this._length++;
2492
+ return true;
2493
+ }
2494
+ /**
2495
+ * Insert a new element/node after an existing one.
2496
+ * @remarks Time O(N), Space O(1)
2497
+ * @param existingElementOrNode - Existing element or node.
2498
+ * @param newElementOrNode - Element or node to insert.
2499
+ * @returns True if inserted.
2500
+ */
2501
+ addAfter(existingElementOrNode, newElementOrNode) {
2502
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2503
+ if (!existingNode) return false;
2504
+ const newNode = this._ensureNode(newElementOrNode);
2505
+ newNode.next = existingNode.next;
2506
+ if (existingNode.next) existingNode.next.prev = newNode;
2507
+ newNode.prev = existingNode;
2508
+ existingNode.next = newNode;
2509
+ if (existingNode === this.tail) this._tail = newNode;
2510
+ this._length++;
2511
+ return true;
2512
+ }
2513
+ /**
2514
+ * Set the element value at an index.
2515
+ * @remarks Time O(N), Space O(1)
2516
+ * @param index - Zero-based index.
2517
+ * @param value - New value.
2518
+ * @returns True if updated.
2519
+ */
2520
+ setAt(index, value) {
2521
+ const node = this.getNodeAt(index);
2522
+ if (!node) return false;
2523
+ node.value = value;
2524
+ return true;
2525
+ }
2526
+ /**
2527
+ * Delete the element at an index.
2528
+ * @remarks Time O(N), Space O(1)
2529
+ * @param index - Zero-based index.
2530
+ * @returns Removed element or undefined.
2531
+
2532
+
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
+ * @example
2560
+ * // Remove by index
2561
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2562
+ * list.deleteAt(1);
2563
+ * console.log(list.toArray()); // ['a', 'c'];
2564
+ */
2565
+ deleteAt(index) {
2566
+ if (index < 0 || index >= this._length) return;
2567
+ if (index === 0) return this.shift();
2568
+ if (index === this._length - 1) return this.pop();
2569
+ const removedNode = this.getNodeAt(index);
2570
+ const prevNode = removedNode.prev;
2571
+ const nextNode = removedNode.next;
2572
+ prevNode.next = nextNode;
2573
+ nextNode.prev = prevNode;
2574
+ this._length--;
2575
+ return removedNode.value;
2576
+ }
2577
+ /**
2578
+ * Delete the first match by value/node.
2579
+ * @remarks Time O(N), Space O(1)
2580
+ * @param [elementOrNode] - Element or node to remove.
2581
+ * @returns True if removed.
2582
+
2583
+
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
+ * @example
2611
+ * // Remove first occurrence
2612
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 2]);
2613
+ * list.delete(2);
2614
+ * console.log(list.toArray()); // [1, 3, 2];
2615
+ */
2616
+ delete(elementOrNode) {
2617
+ const node = this.getNode(elementOrNode);
2618
+ if (!node) return false;
2619
+ if (node === this.head) this.shift();
2620
+ else if (node === this.tail) this.pop();
2621
+ else {
2622
+ const prevNode = node.prev;
2623
+ const nextNode = node.next;
2624
+ prevNode.next = nextNode;
2625
+ nextNode.prev = prevNode;
2626
+ this._length--;
2627
+ }
2628
+ return true;
2629
+ }
2630
+ /**
2631
+ * Check whether the list is empty.
2632
+ * @remarks Time O(1), Space O(1)
2633
+ * @returns True if length is 0.
2634
+
2635
+
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
+ * @example
2664
+ * // Check empty
2665
+ * console.log(new DoublyLinkedList().isEmpty()); // true;
2666
+ */
2667
+ isEmpty() {
2668
+ return this._length === 0;
2669
+ }
2670
+ /**
2671
+ * Remove all nodes and reset length.
2672
+ * @remarks Time O(N), Space O(1)
2673
+ * @returns void
2674
+
2675
+
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
+ * @example
2704
+ * // Remove all
2705
+ * const list = new DoublyLinkedList<number>([1, 2]);
2706
+ * list.clear();
2707
+ * console.log(list.isEmpty()); // true;
2708
+ */
2709
+ clear() {
2710
+ this._head = void 0;
2711
+ this._tail = void 0;
2712
+ this._length = 0;
2713
+ }
2714
+ /**
2715
+ * Find the first value matching a predicate scanning forward.
2716
+ * @remarks Time O(N), Space O(1)
2717
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
2718
+ * @returns Matched value or undefined.
2719
+
2720
+
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
+ * @example
2748
+ * // Search with predicate
2749
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
2750
+ * const found = list.search(node => node.value > 15);
2751
+ * console.log(found); // 20;
2752
+ */
2753
+ search(elementNodeOrPredicate) {
2754
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2755
+ let current = this.head;
2756
+ while (current) {
2757
+ if (predicate(current)) return current.value;
2758
+ current = current.next;
2759
+ }
2760
+ return void 0;
2761
+ }
2762
+ /**
2763
+ * Find the first value matching a predicate scanning backward.
2764
+ * @remarks Time O(N), Space O(1)
2765
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
2766
+ * @returns Matched value or undefined.
2767
+
2768
+
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
+ * @example
2796
+ * // Find value scanning from tail
2797
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
2798
+ * // getBackward scans from tail to head, returns first match
2799
+ * const found = list.getBackward(node => node.value < 4);
2800
+ * console.log(found); // 3;
2801
+ */
2802
+ getBackward(elementNodeOrPredicate) {
2803
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2804
+ let current = this.tail;
2805
+ while (current) {
2806
+ if (predicate(current)) return current.value;
2807
+ current = current.prev;
2808
+ }
2809
+ return void 0;
2810
+ }
2811
+ /**
2812
+ * Reverse the list in place.
2813
+ * @remarks Time O(N), Space O(1)
2814
+ * @returns This list.
2815
+
2816
+
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
+ * @example
2847
+ * // Reverse in-place
2848
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
2849
+ * list.reverse();
2850
+ * console.log([...list]); // [3, 2, 1];
2851
+ */
2852
+ reverse() {
2853
+ let current = this.head;
2854
+ [this._head, this._tail] = [this.tail, this.head];
2855
+ while (current) {
2856
+ const next = current.next;
2857
+ [current.prev, current.next] = [current.next, current.prev];
2858
+ current = next;
2859
+ }
2860
+ return this;
2861
+ }
2862
+ /**
2863
+ * Set the equality comparator used to compare values.
2864
+ * @remarks Time O(1), Space O(1)
2865
+ * @param equals - Equality predicate (a, b) → boolean.
2866
+ * @returns This list.
2867
+ */
2868
+ setEquality(equals) {
2869
+ this._equals = equals;
2870
+ return this;
2871
+ }
2872
+ /**
2873
+ * Deep clone this list (values are copied by reference).
2874
+ * @remarks Time O(N), Space O(N)
2875
+ * @returns A new list with the same element sequence.
2876
+
2877
+
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
+ * @example
2906
+ * // Deep copy
2907
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
2908
+ * const copy = list.clone();
2909
+ * copy.pop();
2910
+ * console.log(list.length); // 3;
2911
+ */
2912
+ clone() {
2913
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
2914
+ for (const v of this) out.push(v);
2915
+ return out;
2916
+ }
2917
+ /**
2918
+ * Filter values into a new list of the same class.
2919
+ * @remarks Time O(N), Space O(N)
2920
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
2921
+ * @param [thisArg] - Value for `this` inside the callback.
2922
+ * @returns A new list with kept values.
2923
+
2924
+
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
+ * @example
2955
+ * // Filter elements
2956
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
2957
+ * const evens = list.filter(n => n % 2 === 0);
2958
+ * console.log([...evens]); // [2, 4];
2959
+ */
2960
+ filter(callback, thisArg) {
2961
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
2962
+ let index = 0;
2963
+ for (const v of this) if (callback.call(thisArg, v, index++, this)) out.push(v);
2964
+ return out;
2965
+ }
2966
+ /**
2967
+ * Map values into a new list of the same class.
2968
+ * @remarks Time O(N), Space O(N)
2969
+ * @param callback - Mapping function (value, index, list) → newValue.
2970
+ * @param [thisArg] - Value for `this` inside the callback.
2971
+ * @returns A new list with mapped values.
2972
+ */
2973
+ mapSame(callback, thisArg) {
2974
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
2975
+ let index = 0;
2976
+ for (const v of this) {
2977
+ const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
2978
+ out.push(mv);
2979
+ }
2980
+ return out;
2981
+ }
2982
+ /**
2983
+ * Map values into a new list (possibly different element type).
2984
+ * @remarks Time O(N), Space O(N)
2985
+ * @template EM
2986
+ * @template RM
2987
+ * @param callback - Mapping function (value, index, list) → newElement.
2988
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
2989
+ * @param [thisArg] - Value for `this` inside the callback.
2990
+ * @returns A new DoublyLinkedList with mapped values.
2991
+
2992
+
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
+ * @example
3023
+ * // DoublyLinkedList for...of iteration and map operation
3024
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
3025
+ *
3026
+ * // Iterate through list
3027
+ * const doubled = list.map(value => value * 2);
3028
+ * console.log(doubled.length); // 5;
3029
+ *
3030
+ * // Use for...of loop
3031
+ * const result: number[] = [];
3032
+ * for (const item of list) {
3033
+ * result.push(item);
3034
+ * }
3035
+ * console.log(result); // [1, 2, 3, 4, 5];
3036
+ */
3037
+ map(callback, options, thisArg) {
3038
+ const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
3039
+ let index = 0;
3040
+ for (const v of this) out.push(callback.call(thisArg, v, index++, this));
3041
+ return out;
3042
+ }
3043
+ /**
3044
+ * (Protected) Create or return a node for the given input (node or raw element).
3045
+ * @remarks Time O(1), Space O(1)
3046
+ * @param elementOrNode - Element value or node to normalize.
3047
+ * @returns A DoublyLinkedListNode for the provided input.
3048
+ */
3049
+ _ensureNode(elementOrNode) {
3050
+ if (this.isNode(elementOrNode)) return elementOrNode;
3051
+ return new DoublyLinkedListNode(elementOrNode);
3052
+ }
3053
+ /**
3054
+ * (Protected) Normalize input into a predicate over nodes.
3055
+ * @remarks Time O(1), Space O(1)
3056
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
3057
+ * @returns A predicate function taking a node and returning true/false.
3058
+ */
3059
+ _ensurePredicate(elementNodeOrPredicate) {
3060
+ if (this.isNode(elementNodeOrPredicate)) {
3061
+ const target = elementNodeOrPredicate;
3062
+ return (node) => node === target;
3063
+ }
3064
+ if (typeof elementNodeOrPredicate === "function") {
3065
+ return elementNodeOrPredicate;
3066
+ }
3067
+ const value = elementNodeOrPredicate;
3068
+ return (node) => this._equals(node.value, value);
3069
+ }
3070
+ /**
3071
+ * (Protected) Get the previous node of a given node.
3072
+ * @remarks Time O(1), Space O(1)
3073
+ * @param node - A node in the list.
3074
+ * @returns Previous node or undefined.
3075
+ */
3076
+ _getPrevNode(node) {
3077
+ return node.prev;
3078
+ }
3079
+ /**
3080
+ * (Protected) Create an empty instance of the same concrete class.
3081
+ * @remarks Time O(1), Space O(1)
3082
+ * @param [options] - Options forwarded to the constructor.
3083
+ * @returns An empty like-kind list instance.
3084
+ */
3085
+ _createInstance(options) {
3086
+ const Ctor = this.constructor;
3087
+ return new Ctor([], options);
3088
+ }
3089
+ /**
3090
+ * (Protected) Create a like-kind instance and seed it from an iterable.
3091
+ * @remarks Time O(N), Space O(N)
3092
+ * @template EM
3093
+ * @template RM
3094
+ * @param [elements] - Iterable used to seed the new list.
3095
+ * @param [options] - Options forwarded to the constructor.
3096
+ * @returns A like-kind DoublyLinkedList instance.
3097
+ */
3098
+ _createLike(elements = [], options) {
3099
+ const Ctor = this.constructor;
3100
+ return new Ctor(elements, options);
3101
+ }
3102
+ *_getIterator() {
3103
+ let current = this.head;
3104
+ while (current) {
3105
+ yield current.value;
3106
+ current = current.next;
3107
+ }
3108
+ }
3109
+ *_getReverseIterator() {
3110
+ let current = this.tail;
3111
+ while (current) {
3112
+ yield current.value;
3113
+ current = current.prev;
3114
+ }
3115
+ }
3116
+ *_getNodeIterator() {
3117
+ let current = this.head;
3118
+ while (current) {
3119
+ yield current;
3120
+ current = current.next;
3121
+ }
3122
+ }
3123
+ };
3124
+ __name(_DoublyLinkedList, "DoublyLinkedList");
3125
+ var DoublyLinkedList = _DoublyLinkedList;
3126
+
3127
+ // src/common/error.ts
3128
+ var ERR = {
3129
+ // Range / index
3130
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
3131
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
3132
+ // Type / argument
3133
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
3134
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
3135
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
3136
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
3137
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
3138
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
3139
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
3140
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
3141
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
3142
+ // State / operation
3143
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
3144
+ // Matrix
3145
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
3146
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
3147
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
3148
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
3149
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
3150
+ };
3151
+
3152
+ // src/data-structures/base/iterable-entry-base.ts
3153
+ var _IterableEntryBase = class _IterableEntryBase {
3154
+ /**
3155
+ * Default iterator yielding `[key, value]` entries.
3156
+ * @returns Iterator of `[K, V]`.
3157
+ * @remarks Time O(n) to iterate, Space O(1)
3158
+ */
3159
+ *[Symbol.iterator](...args) {
3160
+ yield* this._getIterator(...args);
3161
+ }
3162
+ /**
3163
+ * Iterate over `[key, value]` pairs (may yield `undefined` values).
3164
+ * @returns Iterator of `[K, V | undefined]`.
3165
+ * @remarks Time O(n), Space O(1)
3166
+ */
3167
+ *entries() {
3168
+ for (const item of this) {
3169
+ yield item;
3170
+ }
3171
+ }
3172
+ /**
3173
+ * Iterate over keys only.
3174
+ * @returns Iterator of keys.
3175
+ * @remarks Time O(n), Space O(1)
3176
+ */
3177
+ *keys() {
3178
+ for (const item of this) {
3179
+ yield item[0];
3180
+ }
3181
+ }
3182
+ /**
3183
+ * Iterate over values only.
3184
+ * @returns Iterator of values.
3185
+ * @remarks Time O(n), Space O(1)
3186
+ */
3187
+ *values() {
3188
+ for (const item of this) {
3189
+ yield item[1];
3190
+ }
3191
+ }
3192
+ /**
3193
+ * Test whether all entries satisfy the predicate.
3194
+ * @param predicate - `(key, value, index, self) => boolean`.
3195
+ * @param thisArg - Optional `this` for callback.
3196
+ * @returns `true` if all pass; otherwise `false`.
3197
+ * @remarks Time O(n), Space O(1)
3198
+ */
3199
+ every(predicate, thisArg) {
3200
+ let index = 0;
3201
+ for (const item of this) {
3202
+ if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
3203
+ return false;
3204
+ }
3205
+ }
3206
+ return true;
3207
+ }
3208
+ /**
3209
+ * Test whether any entry satisfies the predicate.
3210
+ * @param predicate - `(key, value, index, self) => boolean`.
3211
+ * @param thisArg - Optional `this` for callback.
3212
+ * @returns `true` if any passes; otherwise `false`.
3213
+ * @remarks Time O(n), Space O(1)
3214
+ */
3215
+ some(predicate, thisArg) {
3216
+ let index = 0;
3217
+ for (const item of this) {
3218
+ if (predicate.call(thisArg, item[1], item[0], index++, this)) {
3219
+ return true;
3220
+ }
3221
+ }
3222
+ return false;
3223
+ }
3224
+ /**
3225
+ * Visit each entry, left-to-right.
3226
+ * @param callbackfn - `(key, value, index, self) => void`.
3227
+ * @param thisArg - Optional `this` for callback.
3228
+ * @remarks Time O(n), Space O(1)
3229
+ */
3230
+ forEach(callbackfn, thisArg) {
3231
+ let index = 0;
3232
+ for (const item of this) {
3233
+ const [key, value] = item;
3234
+ callbackfn.call(thisArg, value, key, index++, this);
3235
+ }
3236
+ }
3237
+ /**
3238
+ * Find the first entry that matches a predicate.
3239
+ * @param callbackfn - `(key, value, index, self) => boolean`.
3240
+ * @param thisArg - Optional `this` for callback.
3241
+ * @returns Matching `[key, value]` or `undefined`.
3242
+ * @remarks Time O(n), Space O(1)
3243
+ */
3244
+ find(callbackfn, thisArg) {
3245
+ let index = 0;
3246
+ for (const item of this) {
3247
+ const [key, value] = item;
3248
+ if (callbackfn.call(thisArg, value, key, index++, this)) return item;
3249
+ }
3250
+ return;
3251
+ }
3252
+ /**
3253
+ * Whether the given key exists.
3254
+ * @param key - Key to test.
3255
+ * @returns `true` if found; otherwise `false`.
3256
+ * @remarks Time O(n) generic, Space O(1)
3257
+ */
3258
+ has(key) {
3259
+ for (const item of this) {
3260
+ const [itemKey] = item;
3261
+ if (itemKey === key) return true;
3262
+ }
3263
+ return false;
3264
+ }
3265
+ /**
3266
+ * Whether there exists an entry with the given value.
3267
+ * @param value - Value to test.
3268
+ * @returns `true` if found; otherwise `false`.
3269
+ * @remarks Time O(n), Space O(1)
3270
+ */
3271
+ hasValue(value) {
3272
+ for (const [, elementValue] of this) {
3273
+ if (elementValue === value) return true;
3274
+ }
3275
+ return false;
3276
+ }
3277
+ /**
3278
+ * Get the value under a key.
3279
+ * @param key - Key to look up.
3280
+ * @returns Value or `undefined`.
3281
+ * @remarks Time O(n) generic, Space O(1)
3282
+ */
3283
+ get(key) {
3284
+ for (const item of this) {
3285
+ const [itemKey, value] = item;
3286
+ if (itemKey === key) return value;
3287
+ }
3288
+ return;
3289
+ }
3290
+ /**
3291
+ * Reduce entries into a single accumulator.
3292
+ * @param callbackfn - `(acc, value, key, index, self) => acc`.
3293
+ * @param initialValue - Initial accumulator.
3294
+ * @returns Final accumulator.
3295
+ * @remarks Time O(n), Space O(1)
3296
+ */
3297
+ reduce(callbackfn, initialValue) {
3298
+ let accumulator = initialValue;
3299
+ let index = 0;
3300
+ for (const item of this) {
3301
+ const [key, value] = item;
3302
+ accumulator = callbackfn(accumulator, value, key, index++, this);
3303
+ }
3304
+ return accumulator;
3305
+ }
3306
+ /**
3307
+ * Converts data structure to `[key, value]` pairs.
3308
+ * @returns Array of entries.
3309
+ * @remarks Time O(n), Space O(n)
3310
+ */
3311
+ toArray() {
3312
+ return [...this];
3313
+ }
3314
+ /**
3315
+ * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
3316
+ * @returns Array of entries (default) or a string.
3317
+ * @remarks Time O(n), Space O(n)
3318
+ */
3319
+ toVisual() {
3320
+ return [...this];
3321
+ }
3322
+ /**
3323
+ * Print a human-friendly representation to the console.
3324
+ * @remarks Time O(n), Space O(n)
3325
+ */
3326
+ print() {
3327
+ console.log(this.toVisual());
3328
+ }
3329
+ };
3330
+ __name(_IterableEntryBase, "IterableEntryBase");
3331
+ var IterableEntryBase = _IterableEntryBase;
3332
+
3333
+ // src/data-structures/linked-list/skip-linked-list.ts
3334
+ var _SkipListNode = class _SkipListNode {
3335
+ constructor(key, value, level) {
3336
+ __publicField(this, "key");
3337
+ __publicField(this, "value");
3338
+ __publicField(this, "forward");
3339
+ this.key = key;
3340
+ this.value = value;
3341
+ this.forward = new Array(level).fill(void 0);
3342
+ }
3343
+ };
3344
+ __name(_SkipListNode, "SkipListNode");
3345
+ var SkipListNode = _SkipListNode;
3346
+ var _comparator, _isDefaultComparator;
3347
+ var _SkipList = class _SkipList extends IterableEntryBase {
3348
+ constructor(entries = [], options = {}) {
3349
+ super();
3350
+ __privateAdd(this, _comparator);
3351
+ __privateAdd(this, _isDefaultComparator);
3352
+ // ─── Internal state ──────────────────────────────────────────
3353
+ __publicField(this, "_head");
3354
+ __publicField(this, "_level", 0);
3355
+ __publicField(this, "_size", 0);
3356
+ __publicField(this, "_maxLevel", 16);
3357
+ __publicField(this, "_probability", 0.5);
3358
+ const { comparator, toEntryFn, maxLevel, probability } = options;
3359
+ if (typeof maxLevel === "number" && maxLevel > 0) this._maxLevel = maxLevel;
3360
+ if (typeof probability === "number" && probability > 0 && probability < 1) this._probability = probability;
3361
+ __privateSet(this, _isDefaultComparator, comparator === void 0);
3362
+ __privateSet(this, _comparator, comparator != null ? comparator : _SkipList.createDefaultComparator());
3363
+ this._head = new SkipListNode(void 0, void 0, this._maxLevel);
3364
+ for (const item of entries) {
3365
+ let k;
3366
+ let v;
3367
+ if (toEntryFn) {
3368
+ [k, v] = toEntryFn(item);
3369
+ } else {
3370
+ if (!Array.isArray(item) || item.length < 2) {
3371
+ throw new TypeError(ERR.invalidEntry("SkipList"));
3372
+ }
3373
+ [k, v] = item;
3374
+ }
3375
+ this.set(k, v);
3376
+ }
3377
+ }
3378
+ /**
3379
+ * Creates a default comparator supporting number, string, Date, and bigint.
3380
+ */
3381
+ static createDefaultComparator() {
3382
+ return (a, b) => {
3383
+ if (typeof a === "number" && typeof b === "number") {
3384
+ if (Number.isNaN(a) || Number.isNaN(b)) throw new TypeError(ERR.invalidNaN("SkipList"));
3385
+ return a - b;
3386
+ }
3387
+ if (typeof a === "string" && typeof b === "string") {
3388
+ return a < b ? -1 : a > b ? 1 : 0;
3389
+ }
3390
+ if (a instanceof Date && b instanceof Date) {
3391
+ const ta = a.getTime(), tb = b.getTime();
3392
+ if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("SkipList"));
3393
+ return ta - tb;
3394
+ }
3395
+ if (typeof a === "bigint" && typeof b === "bigint") {
3396
+ return a < b ? -1 : a > b ? 1 : 0;
3397
+ }
3398
+ throw new TypeError(ERR.comparatorRequired("SkipList"));
3399
+ };
3400
+ }
3401
+ // ─── Size & lifecycle ────────────────────────────────────────
3402
+ get size() {
3403
+ return this._size;
3404
+ }
3405
+ get maxLevel() {
3406
+ return this._maxLevel;
3407
+ }
3408
+ get probability() {
3409
+ return this._probability;
3410
+ }
3411
+ get comparator() {
3412
+ return __privateGet(this, _comparator);
3413
+ }
3414
+ /**
3415
+ * Check if empty
3416
+
3417
+
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
+ * @example
3445
+ * // Check if empty
3446
+ * const sl = new SkipList<number, string>();
3447
+ * console.log(sl.isEmpty()); // true;
3448
+ */
3449
+ isEmpty() {
3450
+ return this._size === 0;
3451
+ }
3452
+ /**
3453
+ * Remove all entries
3454
+
3455
+
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
+ * @example
3483
+ * // Remove all entries
3484
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
3485
+ * sl.clear();
3486
+ * console.log(sl.isEmpty()); // true;
3487
+ */
3488
+ clear() {
3489
+ this._head = new SkipListNode(void 0, void 0, this._maxLevel);
3490
+ this._level = 0;
3491
+ this._size = 0;
3492
+ }
3493
+ /**
3494
+ * Create independent copy
3495
+
3496
+
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
+ * @example
3524
+ * // Create independent copy
3525
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
3526
+ * const copy = sl.clone();
3527
+ * copy.delete(1);
3528
+ * console.log(sl.has(1)); // true;
3529
+ */
3530
+ clone() {
3531
+ return new _SkipList(this, {
3532
+ comparator: __privateGet(this, _isDefaultComparator) ? void 0 : __privateGet(this, _comparator),
3533
+ maxLevel: this._maxLevel,
3534
+ probability: this._probability
3535
+ });
3536
+ }
3537
+ // ─── Core CRUD ───────────────────────────────────────────────
3538
+ /**
3539
+ * Insert or update a key-value pair. Returns `this` for chaining.
3540
+ * Unique keys only — if key exists, value is updated in place.
3541
+
3542
+
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
+ * @example
3573
+ * // In-memory sorted key-value store
3574
+ * const store = new SkipList<number, string>();
3575
+ *
3576
+ * store.set(3, 'three');
3577
+ * store.set(1, 'one');
3578
+ * store.set(5, 'five');
3579
+ * store.set(2, 'two');
3580
+ *
3581
+ * console.log(store.get(3)); // 'three';
3582
+ * console.log(store.get(1)); // 'one';
3583
+ * console.log(store.get(5)); // 'five';
3584
+ *
3585
+ * // Update existing key
3586
+ * store.set(3, 'THREE');
3587
+ * console.log(store.get(3)); // 'THREE';
3588
+ */
3589
+ set(key, value) {
3590
+ const cmp = __privateGet(this, _comparator);
3591
+ const update = this._findUpdate(key);
3592
+ const existing = update[0].forward[0];
3593
+ if (existing && cmp(existing.key, key) === 0) {
3594
+ existing.value = value;
3595
+ return this;
3596
+ }
3597
+ const newLevel = this._randomLevel();
3598
+ const newNode = new SkipListNode(key, value, newLevel);
3599
+ if (newLevel > this._level) {
3600
+ for (let i = this._level; i < newLevel; i++) {
3601
+ update[i] = this._head;
3602
+ }
3603
+ this._level = newLevel;
3604
+ }
3605
+ for (let i = 0; i < newLevel; i++) {
3606
+ newNode.forward[i] = update[i].forward[i];
3607
+ update[i].forward[i] = newNode;
3608
+ }
3609
+ this._size++;
3610
+ return this;
3611
+ }
3612
+ /**
3613
+ * Get the value for a key, or `undefined` if not found.
3614
+ * Overrides base O(n) with O(log n) skip-list search.
3615
+
3616
+
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
+ * @example
3647
+ * // Building a sorted index
3648
+ * type Product = { id: number; name: string; price: number };
3649
+ * const products: Product[] = [
3650
+ * { id: 1, name: 'Widget', price: 25 },
3651
+ * { id: 2, name: 'Gadget', price: 50 },
3652
+ * { id: 3, name: 'Doohickey', price: 15 }
3653
+ * ];
3654
+ *
3655
+ * const index = new SkipList<number, Product, Product>(products, {
3656
+ * toEntryFn: (p: Product) => [p.price, p]
3657
+ * });
3658
+ *
3659
+ * // Iterate in sorted order by price
3660
+ * const names = [...index.values()].map(p => p!.name);
3661
+ * console.log(names); // ['Doohickey', 'Widget', 'Gadget'];
3662
+ *
3663
+ * // Range search: products between $20 and $60
3664
+ * const range = index.rangeSearch([20, 60]);
3665
+ * console.log(range.map(([, p]) => p!.name)); // ['Widget', 'Gadget'];
3666
+ */
3667
+ get(key) {
3668
+ const node = this._findNode(key);
3669
+ return node ? node.value : void 0;
3670
+ }
3671
+ /**
3672
+ * Check if a key exists.
3673
+ * Overrides base O(n) with O(log n) skip-list search.
3674
+
3675
+
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
+ * @example
3706
+ * // Check key existence
3707
+ * const sl = new SkipList<number, string>([[1, 'a'], [3, 'c'], [5, 'e']]);
3708
+ * console.log(sl.has(3)); // true;
3709
+ * console.log(sl.has(4)); // false;
3710
+ */
3711
+ has(key) {
3712
+ return this._findNode(key) !== void 0;
3713
+ }
3714
+ /**
3715
+ * Delete a key. Returns `true` if the key was found and removed.
3716
+
3717
+
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
+ * @example
3748
+ * // Fast lookup with deletion
3749
+ * const cache = new SkipList<string, number>();
3750
+ *
3751
+ * cache.set('alpha', 1);
3752
+ * cache.set('beta', 2);
3753
+ * cache.set('gamma', 3);
3754
+ *
3755
+ * console.log(cache.has('beta')); // true;
3756
+ * cache.delete('beta');
3757
+ * console.log(cache.has('beta')); // false;
3758
+ * console.log(cache.size); // 2;
3759
+ */
3760
+ delete(key) {
3761
+ const cmp = __privateGet(this, _comparator);
3762
+ const update = this._findUpdate(key);
3763
+ const target = update[0].forward[0];
3764
+ if (!target || cmp(target.key, key) !== 0) return false;
3765
+ for (let i = 0; i < this._level; i++) {
3766
+ if (update[i].forward[i] !== target) break;
3767
+ update[i].forward[i] = target.forward[i];
3768
+ }
3769
+ while (this._level > 0 && !this._head.forward[this._level - 1]) {
3770
+ this._level--;
3771
+ }
3772
+ this._size--;
3773
+ return true;
3774
+ }
3775
+ // ─── Navigation ──────────────────────────────────────────────
3776
+ /**
3777
+ * Returns the first (smallest key) entry, or `undefined` if empty.
3778
+
3779
+
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
+ * @example
3810
+ * // Access the minimum entry
3811
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
3812
+ * console.log(sl.first()); // [1, 'a'];
3813
+ */
3814
+ first() {
3815
+ const node = this._head.forward[0];
3816
+ return node ? [node.key, node.value] : void 0;
3817
+ }
3818
+ /**
3819
+ * Returns the last (largest key) entry, or `undefined` if empty.
3820
+
3821
+
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
+ * @example
3852
+ * // Access the maximum entry
3853
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
3854
+ * console.log(sl.last()); // [5, 'e'];
3855
+ */
3856
+ last() {
3857
+ let current = this._head;
3858
+ for (let i = this._level - 1; i >= 0; i--) {
3859
+ while (current.forward[i]) {
3860
+ current = current.forward[i];
3861
+ }
3862
+ }
3863
+ return current === this._head ? void 0 : [current.key, current.value];
3864
+ }
3865
+ /**
3866
+ * Remove and return the first (smallest key) entry.
3867
+
3868
+
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
+ * @example
3896
+ * // Remove and return smallest
3897
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
3898
+ * console.log(sl.pollFirst()); // [1, 'a'];
3899
+ * console.log(sl.size); // 2;
3900
+ */
3901
+ pollFirst() {
3902
+ const entry = this.first();
3903
+ if (!entry) return void 0;
3904
+ this.delete(entry[0]);
3905
+ return entry;
3906
+ }
3907
+ /**
3908
+ * Remove and return the last (largest key) entry.
3909
+
3910
+
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
+ * @example
3938
+ * // Remove and return largest
3939
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
3940
+ * console.log(sl.pollLast()); // [3, 'c'];
3941
+ * console.log(sl.size); // 2;
3942
+ */
3943
+ pollLast() {
3944
+ const entry = this.last();
3945
+ if (!entry) return void 0;
3946
+ this.delete(entry[0]);
3947
+ return entry;
3948
+ }
3949
+ /**
3950
+ * Least entry ≥ key, or `undefined`.
3951
+
3952
+
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
+ * @example
3983
+ * // Least entry ≥ key
3984
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
3985
+ * console.log(sl.ceiling(15)); // [20, 'b'];
3986
+ * console.log(sl.ceiling(20)); // [20, 'b'];
3987
+ */
3988
+ ceiling(key) {
3989
+ const cmp = __privateGet(this, _comparator);
3990
+ let current = this._head;
3991
+ for (let i = this._level - 1; i >= 0; i--) {
3992
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
3993
+ current = current.forward[i];
3994
+ }
3995
+ }
3996
+ const node = current.forward[0];
3997
+ return node ? [node.key, node.value] : void 0;
3998
+ }
3999
+ /**
4000
+ * Greatest entry ≤ key, or `undefined`.
4001
+
4002
+
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
+ * @example
4033
+ * // Greatest entry ≤ key
4034
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4035
+ * console.log(sl.floor(25)); // [20, 'b'];
4036
+ * console.log(sl.floor(5)); // undefined;
4037
+ */
4038
+ floor(key) {
4039
+ const cmp = __privateGet(this, _comparator);
4040
+ let current = this._head;
4041
+ for (let i = this._level - 1; i >= 0; i--) {
4042
+ while (current.forward[i] && cmp(current.forward[i].key, key) <= 0) {
4043
+ current = current.forward[i];
4044
+ }
4045
+ }
4046
+ const result = current === this._head ? void 0 : current;
4047
+ if (result && cmp(result.key, key) <= 0) return [result.key, result.value];
4048
+ return void 0;
4049
+ }
4050
+ /**
4051
+ * Least entry strictly > key, or `undefined`.
4052
+
4053
+
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
+ * @example
4081
+ * // Strictly greater entry
4082
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4083
+ * console.log(sl.higher(15)); // [20, 'b'];
4084
+ * console.log(sl.higher(30)); // undefined;
4085
+ */
4086
+ higher(key) {
4087
+ const cmp = __privateGet(this, _comparator);
4088
+ let current = this._head;
4089
+ for (let i = this._level - 1; i >= 0; i--) {
4090
+ while (current.forward[i] && cmp(current.forward[i].key, key) <= 0) {
4091
+ current = current.forward[i];
4092
+ }
4093
+ }
4094
+ const node = current.forward[0];
4095
+ return node ? [node.key, node.value] : void 0;
4096
+ }
4097
+ /**
4098
+ * Greatest entry strictly < key, or `undefined`.
4099
+
4100
+
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
+ * @example
4128
+ * // Strictly less entry
4129
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4130
+ * console.log(sl.lower(25)); // [20, 'b'];
4131
+ * console.log(sl.lower(10)); // undefined;
4132
+ */
4133
+ lower(key) {
4134
+ const cmp = __privateGet(this, _comparator);
4135
+ let current = this._head;
4136
+ let result;
4137
+ for (let i = this._level - 1; i >= 0; i--) {
4138
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4139
+ current = current.forward[i];
4140
+ }
4141
+ if (current !== this._head && cmp(current.key, key) < 0) {
4142
+ result = current;
4143
+ }
4144
+ }
4145
+ return result ? [result.key, result.value] : void 0;
4146
+ }
4147
+ /**
4148
+ * Returns entries within the given key range.
4149
+
4150
+
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
+ * @example
4181
+ * // Find entries in a range
4182
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e']]);
4183
+ * const result = sl.rangeSearch([2, 4]);
4184
+ * console.log(result); // [[2, 'b'], [3, 'c'], [4, 'd']];
4185
+ */
4186
+ rangeSearch(range, options = {}) {
4187
+ const { lowInclusive = true, highInclusive = true } = options;
4188
+ const [low, high] = range;
4189
+ const cmp = __privateGet(this, _comparator);
4190
+ const out = [];
4191
+ let current = this._head;
4192
+ for (let i = this._level - 1; i >= 0; i--) {
4193
+ while (current.forward[i] && cmp(current.forward[i].key, low) < 0) {
4194
+ current = current.forward[i];
4195
+ }
4196
+ }
4197
+ current = current.forward[0];
4198
+ while (current) {
4199
+ const cmpHigh = cmp(current.key, high);
4200
+ if (cmpHigh > 0) break;
4201
+ if (cmpHigh === 0 && !highInclusive) break;
4202
+ const cmpLow = cmp(current.key, low);
4203
+ if (cmpLow > 0 || cmpLow === 0 && lowInclusive) {
4204
+ out.push([current.key, current.value]);
4205
+ }
4206
+ current = current.forward[0];
4207
+ }
4208
+ return out;
4209
+ }
4210
+ // ─── Functional (overrides) ──────────────────────────────────
4211
+ /**
4212
+ * Creates a new SkipList with entries transformed by callback.
4213
+
4214
+
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
+ * @example
4242
+ * // Transform entries
4243
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
4244
+ * const mapped = sl.map((v, k) => [k, v?.toUpperCase()] as [number, string]);
4245
+ * console.log([...mapped.values()]); // ['A', 'B'];
4246
+ */
4247
+ map(callback, options) {
4248
+ const out = new _SkipList([], options != null ? options : {});
4249
+ let i = 0;
4250
+ for (const [k, v] of this) {
4251
+ const [nk, nv] = callback(v, k, i++, this);
4252
+ out.set(nk, nv);
4253
+ }
4254
+ return out;
4255
+ }
4256
+ /**
4257
+ * Creates a new SkipList with entries that pass the predicate.
4258
+
4259
+
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
+ * @example
4287
+ * // Filter entries
4288
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
4289
+ * const result = sl.filter((v, k) => k > 1);
4290
+ * console.log(result.size); // 2;
4291
+ */
4292
+ filter(callbackfn, thisArg) {
4293
+ const out = new _SkipList([], {
4294
+ comparator: __privateGet(this, _isDefaultComparator) ? void 0 : __privateGet(this, _comparator),
4295
+ maxLevel: this._maxLevel,
4296
+ probability: this._probability
4297
+ });
4298
+ let i = 0;
4299
+ for (const [k, v] of this) {
4300
+ const ok = callbackfn.call(thisArg, v, k, i++, this);
4301
+ if (ok) out.set(k, v);
4302
+ }
4303
+ return out;
4304
+ }
4305
+ // ─── Iterator (required by IterableEntryBase) ────────────────
4306
+ _getIterator() {
4307
+ const head = this._head;
4308
+ return (function* () {
4309
+ let node = head.forward[0];
4310
+ while (node) {
4311
+ yield [node.key, node.value];
4312
+ node = node.forward[0];
4313
+ }
4314
+ })();
4315
+ }
4316
+ // ─── Internal helpers ────────────────────────────────────────
4317
+ /**
4318
+ * Finds the update array (predecessors at each level) for a given key.
4319
+ */
4320
+ _findUpdate(key) {
4321
+ const cmp = __privateGet(this, _comparator);
4322
+ const update = new Array(this._maxLevel).fill(this._head);
4323
+ let current = this._head;
4324
+ for (let i = this._level - 1; i >= 0; i--) {
4325
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4326
+ current = current.forward[i];
4327
+ }
4328
+ update[i] = current;
4329
+ }
4330
+ return update;
4331
+ }
4332
+ /**
4333
+ * Finds the node for a given key, or undefined.
4334
+ */
4335
+ _findNode(key) {
4336
+ const cmp = __privateGet(this, _comparator);
4337
+ let current = this._head;
4338
+ for (let i = this._level - 1; i >= 0; i--) {
4339
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4340
+ current = current.forward[i];
4341
+ }
4342
+ }
4343
+ const candidate = current.forward[0];
4344
+ if (candidate && cmp(candidate.key, key) === 0) return candidate;
4345
+ return void 0;
4346
+ }
4347
+ _randomLevel() {
4348
+ let level = 1;
4349
+ while (Math.random() < this._probability && level < this._maxLevel) {
4350
+ level++;
4351
+ }
4352
+ return level;
4353
+ }
4354
+ };
4355
+ _comparator = new WeakMap();
4356
+ _isDefaultComparator = new WeakMap();
4357
+ __name(_SkipList, "SkipList");
4358
+ var SkipList = _SkipList;
4359
+ /**
4360
+ * data-structure-typed
4361
+ *
4362
+ * @author Pablo Zeng
4363
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
4364
+ * @license MIT License
4365
+ */
4366
+
4367
+ export { DoublyLinkedList, DoublyLinkedListNode, SinglyLinkedList, SinglyLinkedListNode, SkipList, SkipListNode };
4368
+ //# sourceMappingURL=linked-list.mjs.map
4369
+ //# sourceMappingURL=linked-list.mjs.map