data-structure-typed 2.5.0 → 2.5.2

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