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,4576 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+
6
+ // src/common/error.ts
7
+ function raise(ErrorClass, message) {
8
+ throw new ErrorClass(message);
9
+ }
10
+ __name(raise, "raise");
11
+ var ERR = {
12
+ // Range / index
13
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
14
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
15
+ // Type / argument
16
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
17
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
18
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
19
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
20
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
21
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
22
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
23
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
24
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
25
+ // State / operation
26
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
27
+ // Matrix
28
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
29
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
30
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
31
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
32
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
33
+ // Order statistic
34
+ orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
35
+ };
36
+
37
+ // src/data-structures/base/iterable-element-base.ts
38
+ var IterableElementBase = class {
39
+ static {
40
+ __name(this, "IterableElementBase");
41
+ }
42
+ /**
43
+ * Create a new iterable base.
44
+ *
45
+ * @param options Optional behavior overrides. When provided, a `toElementFn`
46
+ * is used to convert a raw element (`R`) into a public element (`E`).
47
+ *
48
+ * @remarks
49
+ * Time O(1), Space O(1).
50
+ */
51
+ constructor(options) {
52
+ if (options) {
53
+ const { toElementFn } = options;
54
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
55
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
56
+ }
57
+ }
58
+ /**
59
+ * The converter used to transform a raw element (`R`) into a public element (`E`).
60
+ *
61
+ * @remarks
62
+ * Time O(1), Space O(1).
63
+ */
64
+ _toElementFn;
65
+ /**
66
+ * Exposes the current `toElementFn`, if configured.
67
+ *
68
+ * @returns The converter function or `undefined` when not set.
69
+ * @remarks
70
+ * Time O(1), Space O(1).
71
+ */
72
+ get toElementFn() {
73
+ return this._toElementFn;
74
+ }
75
+ /**
76
+ * Returns an iterator over the structure's elements.
77
+ *
78
+ * @param args Optional iterator arguments forwarded to the internal iterator.
79
+ * @returns An `IterableIterator<E>` that yields the elements in traversal order.
80
+ *
81
+ * @remarks
82
+ * Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
83
+ */
84
+ *[Symbol.iterator](...args) {
85
+ yield* this._getIterator(...args);
86
+ }
87
+ /**
88
+ * Returns an iterator over the values (alias of the default iterator).
89
+ *
90
+ * @returns An `IterableIterator<E>` over all elements.
91
+ * @remarks
92
+ * Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
93
+ */
94
+ *values() {
95
+ for (const item of this) yield item;
96
+ }
97
+ /**
98
+ * Tests whether all elements satisfy the predicate.
99
+ *
100
+ * @template TReturn
101
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
102
+ * @param thisArg Optional `this` binding for the predicate.
103
+ * @returns `true` if every element passes; otherwise `false`.
104
+ *
105
+ * @remarks
106
+ * Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
107
+ */
108
+ every(predicate, thisArg) {
109
+ let index = 0;
110
+ for (const item of this) {
111
+ if (thisArg === void 0) {
112
+ if (!predicate(item, index++, this)) return false;
113
+ } else {
114
+ const fn = predicate;
115
+ if (!fn.call(thisArg, item, index++, this)) return false;
116
+ }
117
+ }
118
+ return true;
119
+ }
120
+ /**
121
+ * Tests whether at least one element satisfies the predicate.
122
+ *
123
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
124
+ * @param thisArg Optional `this` binding for the predicate.
125
+ * @returns `true` if any element passes; otherwise `false`.
126
+ *
127
+ * @remarks
128
+ * Time O(n) in the worst case; may exit early on first success. Space O(1).
129
+ */
130
+ some(predicate, thisArg) {
131
+ let index = 0;
132
+ for (const item of this) {
133
+ if (thisArg === void 0) {
134
+ if (predicate(item, index++, this)) return true;
135
+ } else {
136
+ const fn = predicate;
137
+ if (fn.call(thisArg, item, index++, this)) return true;
138
+ }
139
+ }
140
+ return false;
141
+ }
142
+ /**
143
+ * Invokes a callback for each element in iteration order.
144
+ *
145
+ * @param callbackfn Function invoked per element with signature `(value, index, self)`.
146
+ * @param thisArg Optional `this` binding for the callback.
147
+ * @returns `void`.
148
+ *
149
+ * @remarks
150
+ * Time O(n), Space O(1).
151
+ */
152
+ forEach(callbackfn, thisArg) {
153
+ let index = 0;
154
+ for (const item of this) {
155
+ if (thisArg === void 0) {
156
+ callbackfn(item, index++, this);
157
+ } else {
158
+ const fn = callbackfn;
159
+ fn.call(thisArg, item, index++, this);
160
+ }
161
+ }
162
+ }
163
+ // Implementation signature
164
+ find(predicate, thisArg) {
165
+ let index = 0;
166
+ for (const item of this) {
167
+ if (thisArg === void 0) {
168
+ if (predicate(item, index++, this)) return item;
169
+ } else {
170
+ const fn = predicate;
171
+ if (fn.call(thisArg, item, index++, this)) return item;
172
+ }
173
+ }
174
+ return;
175
+ }
176
+ /**
177
+ * Checks whether a strictly-equal element exists in the structure.
178
+ *
179
+ * @param element The element to test with `===` equality.
180
+ * @returns `true` if an equal element is found; otherwise `false`.
181
+ *
182
+ * @remarks
183
+ * Time O(n) in the worst case. Space O(1).
184
+ */
185
+ has(element) {
186
+ for (const ele of this) if (ele === element) return true;
187
+ return false;
188
+ }
189
+ /**
190
+ * Reduces all elements to a single accumulated value.
191
+ *
192
+ * @overload
193
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
194
+ * @returns The final accumulated value typed as `E`.
195
+ *
196
+ * @overload
197
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
198
+ * @param initialValue The initial accumulator value of type `E`.
199
+ * @returns The final accumulated value typed as `E`.
200
+ *
201
+ * @overload
202
+ * @template U The accumulator type when it differs from `E`.
203
+ * @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
204
+ * @param initialValue The initial accumulator value of type `U`.
205
+ * @returns The final accumulated value typed as `U`.
206
+ *
207
+ * @remarks
208
+ * Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
209
+ */
210
+ reduce(callbackfn, initialValue) {
211
+ let index = 0;
212
+ const iter = this[Symbol.iterator]();
213
+ let acc;
214
+ if (arguments.length >= 2) {
215
+ acc = initialValue;
216
+ } else {
217
+ const first = iter.next();
218
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
219
+ acc = first.value;
220
+ index = 1;
221
+ }
222
+ for (const value of iter) {
223
+ acc = callbackfn(acc, value, index++, this);
224
+ }
225
+ return acc;
226
+ }
227
+ /**
228
+ * Materializes the elements into a new array.
229
+ *
230
+ * @returns A shallow array copy of the iteration order.
231
+ * @remarks
232
+ * Time O(n), Space O(n).
233
+ */
234
+ toArray() {
235
+ return [...this];
236
+ }
237
+ /**
238
+ * Returns a representation of the structure suitable for quick visualization.
239
+ * Defaults to an array of elements; subclasses may override to provide richer visuals.
240
+ *
241
+ * @returns A visual representation (array by default).
242
+ * @remarks
243
+ * Time O(n), Space O(n).
244
+ */
245
+ toVisual() {
246
+ return [...this];
247
+ }
248
+ /**
249
+ * Prints `toVisual()` to the console. Intended for quick debugging.
250
+ *
251
+ * @returns `void`.
252
+ * @remarks
253
+ * Time O(n) due to materialization, Space O(n) for the intermediate representation.
254
+ */
255
+ print() {
256
+ console.log(this.toVisual());
257
+ }
258
+ };
259
+
260
+ // src/data-structures/base/linear-base.ts
261
+ var LinkedListNode = class {
262
+ static {
263
+ __name(this, "LinkedListNode");
264
+ }
265
+ /**
266
+ * Initialize a node.
267
+ * @param value - Element value.
268
+ * @remarks Time O(1), Space O(1)
269
+ */
270
+ constructor(value) {
271
+ this._value = value;
272
+ this._next = void 0;
273
+ }
274
+ _value;
275
+ /**
276
+ * Element payload getter.
277
+ * @returns Element value.
278
+ * @remarks Time O(1), Space O(1)
279
+ */
280
+ get value() {
281
+ return this._value;
282
+ }
283
+ /**
284
+ * Element payload setter.
285
+ * @param value - New value.
286
+ * @remarks Time O(1), Space O(1)
287
+ */
288
+ set value(value) {
289
+ this._value = value;
290
+ }
291
+ _next;
292
+ /**
293
+ * Next node getter.
294
+ * @returns Next node or `undefined`.
295
+ * @remarks Time O(1), Space O(1)
296
+ */
297
+ get next() {
298
+ return this._next;
299
+ }
300
+ /**
301
+ * Next node setter.
302
+ * @param value - Next node or `undefined`.
303
+ * @remarks Time O(1), Space O(1)
304
+ */
305
+ set next(value) {
306
+ this._next = value;
307
+ }
308
+ };
309
+ var LinearBase = class _LinearBase extends IterableElementBase {
310
+ static {
311
+ __name(this, "LinearBase");
312
+ }
313
+ /**
314
+ * Construct a linear container with runtime options.
315
+ * @param options - `{ maxLen?, ... }` bounds/behavior options.
316
+ * @remarks Time O(1), Space O(1)
317
+ */
318
+ constructor(options) {
319
+ super(options);
320
+ if (options) {
321
+ const { maxLen } = options;
322
+ if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
323
+ }
324
+ }
325
+ _maxLen = -1;
326
+ /**
327
+ * Upper bound for length (if positive), or `-1` when unbounded.
328
+ * @returns Maximum allowed length.
329
+ * @remarks Time O(1), Space O(1)
330
+ */
331
+ get maxLen() {
332
+ return this._maxLen;
333
+ }
334
+ /**
335
+ * First index of a value from the left.
336
+ * @param searchElement - Value to match.
337
+ * @param fromIndex - Start position (supports negative index).
338
+ * @returns Index or `-1` if not found.
339
+ * @remarks Time O(n), Space O(1)
340
+ */
341
+ indexOf(searchElement, fromIndex = 0) {
342
+ if (this.length === 0) return -1;
343
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
344
+ if (fromIndex < 0) fromIndex = 0;
345
+ for (let i = fromIndex; i < this.length; i++) {
346
+ const element = this.at(i);
347
+ if (element === searchElement) return i;
348
+ }
349
+ return -1;
350
+ }
351
+ /**
352
+ * Last index of a value from the right.
353
+ * @param searchElement - Value to match.
354
+ * @param fromIndex - Start position (supports negative index).
355
+ * @returns Index or `-1` if not found.
356
+ * @remarks Time O(n), Space O(1)
357
+ */
358
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
359
+ if (this.length === 0) return -1;
360
+ if (fromIndex >= this.length) fromIndex = this.length - 1;
361
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
362
+ for (let i = fromIndex; i >= 0; i--) {
363
+ const element = this.at(i);
364
+ if (element === searchElement) return i;
365
+ }
366
+ return -1;
367
+ }
368
+ /**
369
+ * Find the first index matching a predicate.
370
+ * @param predicate - `(element, index, self) => boolean`.
371
+ * @param thisArg - Optional `this` for callback.
372
+ * @returns Index or `-1`.
373
+ * @remarks Time O(n), Space O(1)
374
+ */
375
+ findIndex(predicate, thisArg) {
376
+ for (let i = 0; i < this.length; i++) {
377
+ const item = this.at(i);
378
+ if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
379
+ }
380
+ return -1;
381
+ }
382
+ /**
383
+ * Concatenate elements and/or containers.
384
+ * @param items - Elements or other containers.
385
+ * @returns New container with combined elements (`this` type).
386
+ * @remarks Time O(sum(length)), Space O(sum(length))
387
+ */
388
+ concat(...items) {
389
+ const newList = this.clone();
390
+ for (const item of items) {
391
+ if (item instanceof _LinearBase) {
392
+ newList.pushMany(item);
393
+ } else {
394
+ newList.push(item);
395
+ }
396
+ }
397
+ return newList;
398
+ }
399
+ /**
400
+ * In-place stable order via array sort semantics.
401
+ * @param compareFn - Comparator `(a, b) => number`.
402
+ * @returns This container.
403
+ * @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
404
+ */
405
+ sort(compareFn) {
406
+ const arr = this.toArray();
407
+ arr.sort(compareFn);
408
+ this.clear();
409
+ for (const item of arr) this.push(item);
410
+ return this;
411
+ }
412
+ /**
413
+ * Remove and/or insert elements at a position (array-compatible).
414
+ * @param start - Start index (supports negative index).
415
+ * @param deleteCount - How many to remove.
416
+ * @param items - Elements to insert.
417
+ * @returns Removed elements as a new list (`this` type).
418
+ * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
419
+ */
420
+ splice(start, deleteCount = 0, ...items) {
421
+ const removedList = this._createInstance();
422
+ start = start < 0 ? this.length + start : start;
423
+ start = Math.max(0, Math.min(start, this.length));
424
+ deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
425
+ for (let i = 0; i < deleteCount; i++) {
426
+ const removed = this.deleteAt(start);
427
+ if (removed !== void 0) {
428
+ removedList.push(removed);
429
+ }
430
+ }
431
+ for (let i = 0; i < items.length; i++) {
432
+ this.addAt(start + i, items[i]);
433
+ }
434
+ return removedList;
435
+ }
436
+ /**
437
+ * Join all elements into a string.
438
+ * @param separator - Separator string.
439
+ * @returns Concatenated string.
440
+ * @remarks Time O(n), Space O(n)
441
+ */
442
+ join(separator = ",") {
443
+ return this.toArray().join(separator);
444
+ }
445
+ /**
446
+ * Snapshot elements into a reversed array.
447
+ * @returns New reversed array.
448
+ * @remarks Time O(n), Space O(n)
449
+ */
450
+ toReversedArray() {
451
+ const array = [];
452
+ for (let i = this.length - 1; i >= 0; i--) {
453
+ array.push(this.at(i));
454
+ }
455
+ return array;
456
+ }
457
+ reduceRight(callbackfn, initialValue) {
458
+ let accumulator = initialValue ?? 0;
459
+ for (let i = this.length - 1; i >= 0; i--) {
460
+ accumulator = callbackfn(accumulator, this.at(i), i, this);
461
+ }
462
+ return accumulator;
463
+ }
464
+ /**
465
+ * Create a shallow copy of a subrange.
466
+ * @param start - Inclusive start (supports negative index).
467
+ * @param end - Exclusive end (supports negative index).
468
+ * @returns New list with the range (`this` type).
469
+ * @remarks Time O(n), Space O(n)
470
+ */
471
+ slice(start = 0, end = this.length) {
472
+ start = start < 0 ? this.length + start : start;
473
+ end = end < 0 ? this.length + end : end;
474
+ const newList = this._createInstance();
475
+ for (let i = start; i < end; i++) {
476
+ newList.push(this.at(i));
477
+ }
478
+ return newList;
479
+ }
480
+ /**
481
+ * Fill a range with a value.
482
+ * @param value - Value to set.
483
+ * @param start - Inclusive start.
484
+ * @param end - Exclusive end.
485
+ * @returns This list.
486
+ * @remarks Time O(n), Space O(1)
487
+ */
488
+ fill(value, start = 0, end = this.length) {
489
+ start = start < 0 ? this.length + start : start;
490
+ end = end < 0 ? this.length + end : end;
491
+ if (start < 0) start = 0;
492
+ if (end > this.length) end = this.length;
493
+ if (start >= end) return this;
494
+ for (let i = start; i < end; i++) {
495
+ this.setAt(i, value);
496
+ }
497
+ return this;
498
+ }
499
+ };
500
+ var LinearLinkedBase = class extends LinearBase {
501
+ static {
502
+ __name(this, "LinearLinkedBase");
503
+ }
504
+ constructor(options) {
505
+ super(options);
506
+ if (options) {
507
+ const { maxLen } = options;
508
+ if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
509
+ }
510
+ }
511
+ /**
512
+ * Linked-list optimized `indexOf` (forwards scan).
513
+ * @param searchElement - Value to match.
514
+ * @param fromIndex - Start position.
515
+ * @returns Index or `-1`.
516
+ * @remarks Time O(n), Space O(1)
517
+ */
518
+ indexOf(searchElement, fromIndex = 0) {
519
+ const iterator = this._getIterator();
520
+ let current = iterator.next();
521
+ let index = 0;
522
+ while (index < fromIndex) {
523
+ current = iterator.next();
524
+ index++;
525
+ }
526
+ while (!current.done) {
527
+ if (current.value === searchElement) return index;
528
+ current = iterator.next();
529
+ index++;
530
+ }
531
+ return -1;
532
+ }
533
+ /**
534
+ * Linked-list optimized `lastIndexOf` (reverse scan).
535
+ * @param searchElement - Value to match.
536
+ * @param fromIndex - Start position.
537
+ * @returns Index or `-1`.
538
+ * @remarks Time O(n), Space O(1)
539
+ */
540
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
541
+ const iterator = this._getReverseIterator();
542
+ let current = iterator.next();
543
+ let index = this.length - 1;
544
+ while (index > fromIndex) {
545
+ current = iterator.next();
546
+ index--;
547
+ }
548
+ while (!current.done) {
549
+ if (current.value === searchElement) return index;
550
+ current = iterator.next();
551
+ index--;
552
+ }
553
+ return -1;
554
+ }
555
+ /**
556
+ * Concatenate lists/elements preserving order.
557
+ * @param items - Elements or `LinearBase` instances.
558
+ * @returns New list with combined elements (`this` type).
559
+ * @remarks Time O(sum(length)), Space O(sum(length))
560
+ */
561
+ concat(...items) {
562
+ const newList = this.clone();
563
+ for (const item of items) {
564
+ if (item instanceof LinearBase) {
565
+ newList.pushMany(item);
566
+ } else {
567
+ newList.push(item);
568
+ }
569
+ }
570
+ return newList;
571
+ }
572
+ /**
573
+ * Slice via forward iteration (no random access required).
574
+ * @param start - Inclusive start (supports negative index).
575
+ * @param end - Exclusive end (supports negative index).
576
+ * @returns New list (`this` type).
577
+ * @remarks Time O(n), Space O(n)
578
+ */
579
+ slice(start = 0, end = this.length) {
580
+ start = start < 0 ? this.length + start : start;
581
+ end = end < 0 ? this.length + end : end;
582
+ const newList = this._createInstance();
583
+ const iterator = this._getIterator();
584
+ let current = iterator.next();
585
+ let c = 0;
586
+ while (c < start) {
587
+ current = iterator.next();
588
+ c++;
589
+ }
590
+ for (let i = start; i < end; i++) {
591
+ newList.push(current.value);
592
+ current = iterator.next();
593
+ }
594
+ return newList;
595
+ }
596
+ /**
597
+ * Splice by walking node iterators from the start index.
598
+ * @param start - Start index.
599
+ * @param deleteCount - How many elements to remove.
600
+ * @param items - Elements to insert after the splice point.
601
+ * @returns Removed elements as a new list (`this` type).
602
+ * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
603
+ */
604
+ splice(start, deleteCount = 0, ...items) {
605
+ const removedList = this._createInstance();
606
+ start = start < 0 ? this.length + start : start;
607
+ start = Math.max(0, Math.min(start, this.length));
608
+ deleteCount = Math.max(0, deleteCount);
609
+ let currentIndex = 0;
610
+ let currentNode = void 0;
611
+ let previousNode = void 0;
612
+ const iterator = this._getNodeIterator();
613
+ for (const node of iterator) {
614
+ if (currentIndex === start) {
615
+ currentNode = node;
616
+ break;
617
+ }
618
+ previousNode = node;
619
+ currentIndex++;
620
+ }
621
+ for (let i = 0; i < deleteCount && currentNode; i++) {
622
+ removedList.push(currentNode.value);
623
+ const nextNode = currentNode.next;
624
+ this.delete(currentNode);
625
+ currentNode = nextNode;
626
+ }
627
+ for (let i = 0; i < items.length; i++) {
628
+ if (previousNode) {
629
+ this.addAfter(previousNode, items[i]);
630
+ previousNode = previousNode.next;
631
+ } else {
632
+ this.addAt(0, items[i]);
633
+ previousNode = this._getNodeIterator().next().value;
634
+ }
635
+ }
636
+ return removedList;
637
+ }
638
+ reduceRight(callbackfn, initialValue) {
639
+ let accumulator = initialValue ?? 0;
640
+ let index = this.length - 1;
641
+ for (const item of this._getReverseIterator()) {
642
+ accumulator = callbackfn(accumulator, item, index--, this);
643
+ }
644
+ return accumulator;
645
+ }
646
+ };
647
+
648
+ // src/data-structures/linked-list/singly-linked-list.ts
649
+ var SinglyLinkedListNode = class extends LinkedListNode {
650
+ static {
651
+ __name(this, "SinglyLinkedListNode");
652
+ }
653
+ /**
654
+ * Create a list node.
655
+ * @remarks Time O(1), Space O(1)
656
+ * @param value - Element value to store.
657
+ * @returns New node instance.
658
+ */
659
+ constructor(value) {
660
+ super(value);
661
+ this._value = value;
662
+ this._next = void 0;
663
+ }
664
+ _next;
665
+ /**
666
+ * Get the next node.
667
+ * @remarks Time O(1), Space O(1)
668
+ * @returns Next node or undefined.
669
+ */
670
+ get next() {
671
+ return this._next;
672
+ }
673
+ /**
674
+ * Set the next node.
675
+ * @remarks Time O(1), Space O(1)
676
+ * @param value - Next node or undefined.
677
+ * @returns void
678
+ */
679
+ set next(value) {
680
+ this._next = value;
681
+ }
682
+ };
683
+ var SinglyLinkedList = class extends LinearLinkedBase {
684
+ static {
685
+ __name(this, "SinglyLinkedList");
686
+ }
687
+ _equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
688
+ /**
689
+ * Create a SinglyLinkedList and optionally bulk-insert elements.
690
+ * @remarks Time O(N), Space O(N)
691
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
692
+ * @param [options] - Options such as maxLen and toElementFn.
693
+ * @returns New SinglyLinkedList instance.
694
+ */
695
+ constructor(elements = [], options) {
696
+ super(options);
697
+ this.pushMany(elements);
698
+ }
699
+ _head;
700
+ /**
701
+ * Get the head node.
702
+ * @remarks Time O(1), Space O(1)
703
+ * @returns Head node or undefined.
704
+ */
705
+ get head() {
706
+ return this._head;
707
+ }
708
+ _tail;
709
+ /**
710
+ * Get the tail node.
711
+ * @remarks Time O(1), Space O(1)
712
+ * @returns Tail node or undefined.
713
+ */
714
+ get tail() {
715
+ return this._tail;
716
+ }
717
+ _length = 0;
718
+ /**
719
+ * Get the number of elements.
720
+ * @remarks Time O(1), Space O(1)
721
+ * @returns Current length.
722
+ */
723
+ get length() {
724
+ return this._length;
725
+ }
726
+ /**
727
+ * Get the first element value.
728
+ * @remarks Time O(1), Space O(1)
729
+ * @returns First element or undefined.
730
+ */
731
+ get first() {
732
+ return this.head?.value;
733
+ }
734
+ /**
735
+ * Get the last element value.
736
+ * @remarks Time O(1), Space O(1)
737
+ * @returns Last element or undefined.
738
+ */
739
+ get last() {
740
+ return this.tail?.value;
741
+ }
742
+ /**
743
+ * Create a new list from an iterable of elements.
744
+ * @remarks Time O(N), Space O(N)
745
+ * @template E
746
+ * @template R
747
+ * @template S
748
+ * @param this - The constructor (subclass) to instantiate.
749
+ * @param data - Iterable of elements to insert.
750
+ * @param [options] - Options forwarded to the constructor.
751
+ * @returns A new list populated with the iterable's elements.
752
+ */
753
+ static from(data, options) {
754
+ const list = new this([], options);
755
+ for (const x of data) list.push(x);
756
+ return list;
757
+ }
758
+ /**
759
+ * Append an element/node to the tail.
760
+ * @remarks Time O(1), Space O(1)
761
+ * @param elementOrNode - Element or node to append.
762
+ * @returns True when appended.
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
+
797
+
798
+ * @example
799
+ * // basic SinglyLinkedList creation and push operation
800
+ * // Create a simple SinglyLinkedList with initial values
801
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
802
+ *
803
+ * // Verify the list maintains insertion order
804
+ * console.log([...list]); // [1, 2, 3, 4, 5];
805
+ *
806
+ * // Check length
807
+ * console.log(list.length); // 5;
808
+ *
809
+ * // Push a new element to the end
810
+ * list.push(6);
811
+ * console.log(list.length); // 6;
812
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
813
+ */
814
+ push(elementOrNode) {
815
+ const newNode = this._ensureNode(elementOrNode);
816
+ if (!this.head) {
817
+ this._head = this._tail = newNode;
818
+ } else {
819
+ this.tail.next = newNode;
820
+ this._tail = newNode;
821
+ }
822
+ this._length++;
823
+ if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
824
+ return true;
825
+ }
826
+ /**
827
+ * Remove and return the tail element.
828
+ * @remarks Time O(N), Space O(1)
829
+ * @returns Removed element or undefined.
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
+
864
+
865
+ * @example
866
+ * // SinglyLinkedList pop and shift operations
867
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
868
+ *
869
+ * // Pop removes from the end
870
+ * const last = list.pop();
871
+ * console.log(last); // 50;
872
+ *
873
+ * // Shift removes from the beginning
874
+ * const first = list.shift();
875
+ * console.log(first); // 10;
876
+ *
877
+ * // Verify remaining elements
878
+ * console.log([...list]); // [20, 30, 40];
879
+ * console.log(list.length); // 3;
880
+ */
881
+ pop() {
882
+ if (!this.head) return void 0;
883
+ if (this.head === this.tail) {
884
+ const value2 = this.head.value;
885
+ this._head = void 0;
886
+ this._tail = void 0;
887
+ this._length--;
888
+ return value2;
889
+ }
890
+ let current = this.head;
891
+ while (current.next && current.next !== this.tail) current = current.next;
892
+ const value = this.tail?.value;
893
+ current.next = void 0;
894
+ this._tail = current;
895
+ this._length--;
896
+ return value;
897
+ }
898
+ /**
899
+ * Remove and return the head element.
900
+ * @remarks Time O(1), Space O(1)
901
+ * @returns Removed element or undefined.
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
+
936
+
937
+ * @example
938
+ * // Remove from the front
939
+ * const list = new SinglyLinkedList<number>([10, 20, 30]);
940
+ * console.log(list.shift()); // 10;
941
+ * console.log(list.length); // 2;
942
+ */
943
+ shift() {
944
+ if (!this.head) return void 0;
945
+ const removed = this.head;
946
+ this._head = this.head.next;
947
+ if (!this._head) this._tail = void 0;
948
+ this._length--;
949
+ return removed.value;
950
+ }
951
+ /**
952
+ * Prepend an element/node to the head.
953
+ * @remarks Time O(1), Space O(1)
954
+ * @param elementOrNode - Element or node to prepend.
955
+ * @returns True when prepended.
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
+
990
+
991
+ * @example
992
+ * // SinglyLinkedList unshift and forward traversal
993
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
994
+ *
995
+ * // Unshift adds to the beginning
996
+ * list.unshift(10);
997
+ * console.log([...list]); // [10, 20, 30, 40];
998
+ *
999
+ * // Access elements (forward traversal only for singly linked)
1000
+ * const second = list.at(1);
1001
+ * console.log(second); // 20;
1002
+ *
1003
+ * // SinglyLinkedList allows forward iteration only
1004
+ * const elements: number[] = [];
1005
+ * for (const item of list) {
1006
+ * elements.push(item);
1007
+ * }
1008
+ * console.log(elements); // [10, 20, 30, 40];
1009
+ *
1010
+ * console.log(list.length); // 4;
1011
+ */
1012
+ unshift(elementOrNode) {
1013
+ const newNode = this._ensureNode(elementOrNode);
1014
+ if (!this.head) {
1015
+ this._head = this._tail = newNode;
1016
+ } else {
1017
+ newNode.next = this.head;
1018
+ this._head = newNode;
1019
+ }
1020
+ this._length++;
1021
+ return true;
1022
+ }
1023
+ /**
1024
+ * Append a sequence of elements/nodes.
1025
+ * @remarks Time O(N), Space O(1)
1026
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
1027
+ * @returns Array of per-element success flags.
1028
+ */
1029
+ pushMany(elements) {
1030
+ const ans = [];
1031
+ for (const el of elements) {
1032
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
1033
+ else ans.push(this.push(el));
1034
+ }
1035
+ return ans;
1036
+ }
1037
+ /**
1038
+ * Prepend a sequence of elements/nodes.
1039
+ * @remarks Time O(N), Space O(1)
1040
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
1041
+ * @returns Array of per-element success flags.
1042
+ */
1043
+ unshiftMany(elements) {
1044
+ const ans = [];
1045
+ for (const el of elements) {
1046
+ if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
1047
+ else ans.push(this.unshift(el));
1048
+ }
1049
+ return ans;
1050
+ }
1051
+ /**
1052
+ * Find the first value matching a predicate (by node).
1053
+ * @remarks Time O(N), Space O(1)
1054
+ * @param elementNodeOrPredicate - Element, node, or node predicate to match.
1055
+ * @returns Matched value or undefined.
1056
+ */
1057
+ search(elementNodeOrPredicate) {
1058
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1059
+ let current = this.head;
1060
+ while (current) {
1061
+ if (predicate(current)) return current.value;
1062
+ current = current.next;
1063
+ }
1064
+ return void 0;
1065
+ }
1066
+ /**
1067
+ * Get the element at a given index.
1068
+ * @remarks Time O(N), Space O(1)
1069
+ * @param index - Zero-based index.
1070
+ * @returns Element or undefined.
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
+
1105
+
1106
+ * @example
1107
+ * // Access element by index
1108
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
1109
+ * console.log(list.at(0)); // 'a';
1110
+ * console.log(list.at(2)); // 'c';
1111
+ * console.log(list.at(3)); // 'd';
1112
+ */
1113
+ at(index) {
1114
+ if (index < 0 || index >= this._length) return void 0;
1115
+ let current = this.head;
1116
+ for (let i = 0; i < index && current; i++) current = current.next;
1117
+ return current?.value;
1118
+ }
1119
+ /**
1120
+ * Type guard: check whether the input is a SinglyLinkedListNode.
1121
+ * @remarks Time O(1), Space O(1)
1122
+ * @param elementNodeOrPredicate - Element, node, or predicate.
1123
+ * @returns True if the value is a SinglyLinkedListNode.
1124
+ */
1125
+ isNode(elementNodeOrPredicate) {
1126
+ return elementNodeOrPredicate instanceof SinglyLinkedListNode;
1127
+ }
1128
+ /**
1129
+ * Get the node reference at a given index.
1130
+ * @remarks Time O(N), Space O(1)
1131
+ * @param index - Zero-based index.
1132
+ * @returns Node or undefined.
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
+
1164
+
1165
+ * @example
1166
+ * // Get node at index
1167
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1168
+ * console.log(list.getNodeAt(1)?.value); // 'b';
1169
+ */
1170
+ getNodeAt(index) {
1171
+ if (index < 0 || index >= this._length) return void 0;
1172
+ let current = this.head;
1173
+ for (let i = 0; i < index && current; i++) current = current.next;
1174
+ return current;
1175
+ }
1176
+ /**
1177
+ * Delete the element at an index.
1178
+ * @remarks Time O(N), Space O(1)
1179
+ * @param index - Zero-based index.
1180
+ * @returns Removed element or undefined.
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
+
1212
+
1213
+ * @example
1214
+ * // Remove by index
1215
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1216
+ * list.deleteAt(1);
1217
+ * console.log(list.toArray()); // ['a', 'c'];
1218
+ */
1219
+ deleteAt(index) {
1220
+ if (index < 0 || index >= this._length) return void 0;
1221
+ if (index === 0) return this.shift();
1222
+ const targetNode = this.getNodeAt(index);
1223
+ const prevNode = this._getPrevNode(targetNode);
1224
+ const value = targetNode.value;
1225
+ prevNode.next = targetNode.next;
1226
+ if (targetNode === this.tail) this._tail = prevNode;
1227
+ this._length--;
1228
+ return value;
1229
+ }
1230
+ /**
1231
+ * Delete the first match by value/node.
1232
+ * @remarks Time O(N), Space O(1)
1233
+ * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
1234
+ * @returns True if removed.
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
+
1266
+
1267
+ * @example
1268
+ * // Remove first occurrence
1269
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
1270
+ * list.delete(2);
1271
+ * console.log(list.toArray()); // [1, 3, 2];
1272
+ */
1273
+ delete(elementOrNode) {
1274
+ if (elementOrNode === void 0 || !this.head) return false;
1275
+ const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
1276
+ if (!node) return false;
1277
+ const prevNode = this._getPrevNode(node);
1278
+ if (!prevNode) {
1279
+ this._head = node.next;
1280
+ if (node === this.tail) this._tail = void 0;
1281
+ } else {
1282
+ prevNode.next = node.next;
1283
+ if (node === this.tail) this._tail = prevNode;
1284
+ }
1285
+ this._length--;
1286
+ return true;
1287
+ }
1288
+ /**
1289
+ * Insert a new element/node at an index, shifting following nodes.
1290
+ * @remarks Time O(N), Space O(1)
1291
+ * @param index - Zero-based index.
1292
+ * @param newElementOrNode - Element or node to insert.
1293
+ * @returns True if inserted.
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
+
1325
+
1326
+ * @example
1327
+ * // Insert at index
1328
+ * const list = new SinglyLinkedList<number>([1, 3]);
1329
+ * list.addAt(1, 2);
1330
+ * console.log(list.toArray()); // [1, 2, 3];
1331
+ */
1332
+ addAt(index, newElementOrNode) {
1333
+ if (index < 0 || index > this._length) return false;
1334
+ if (index === 0) return this.unshift(newElementOrNode);
1335
+ if (index === this._length) return this.push(newElementOrNode);
1336
+ const newNode = this._ensureNode(newElementOrNode);
1337
+ const prevNode = this.getNodeAt(index - 1);
1338
+ newNode.next = prevNode.next;
1339
+ prevNode.next = newNode;
1340
+ this._length++;
1341
+ return true;
1342
+ }
1343
+ /**
1344
+ * Set the element value at an index.
1345
+ * @remarks Time O(N), Space O(1)
1346
+ * @param index - Zero-based index.
1347
+ * @param value - New value.
1348
+ * @returns True if updated.
1349
+ */
1350
+ setAt(index, value) {
1351
+ const node = this.getNodeAt(index);
1352
+ if (!node) return false;
1353
+ node.value = value;
1354
+ return true;
1355
+ }
1356
+ /**
1357
+ * Check whether the list is empty.
1358
+ * @remarks Time O(1), Space O(1)
1359
+ * @returns True if length is 0.
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
+
1392
+
1393
+ * @example
1394
+ * // Check empty
1395
+ * console.log(new SinglyLinkedList().isEmpty()); // true;
1396
+ */
1397
+ isEmpty() {
1398
+ return this._length === 0;
1399
+ }
1400
+ /**
1401
+ * Remove all nodes and reset length.
1402
+ * @remarks Time O(N), Space O(1)
1403
+ * @returns void
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
+
1436
+
1437
+ * @example
1438
+ * // Remove all
1439
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1440
+ * list.clear();
1441
+ * console.log(list.isEmpty()); // true;
1442
+ */
1443
+ clear() {
1444
+ this._head = void 0;
1445
+ this._tail = void 0;
1446
+ this._length = 0;
1447
+ }
1448
+ /**
1449
+ * Reverse the list in place.
1450
+ * @remarks Time O(N), Space O(1)
1451
+ * @returns This list.
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
+
1486
+
1487
+ * @example
1488
+ * // Reverse the list in-place
1489
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
1490
+ * list.reverse();
1491
+ * console.log([...list]); // [4, 3, 2, 1];
1492
+ */
1493
+ reverse() {
1494
+ if (!this.head || this.head === this.tail) return this;
1495
+ let prev;
1496
+ let current = this.head;
1497
+ let next;
1498
+ while (current) {
1499
+ next = current.next;
1500
+ current.next = prev;
1501
+ prev = current;
1502
+ current = next;
1503
+ }
1504
+ [this._head, this._tail] = [this.tail, this.head];
1505
+ return this;
1506
+ }
1507
+ /**
1508
+ * Find a node by value, reference, or predicate.
1509
+ * @remarks Time O(N), Space O(1)
1510
+ * @param [elementNodeOrPredicate] - Element, node, or node predicate to match.
1511
+ * @returns Matching node or undefined.
1512
+ */
1513
+ getNode(elementNodeOrPredicate) {
1514
+ if (elementNodeOrPredicate === void 0) return;
1515
+ if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
1516
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1517
+ let current = this.head;
1518
+ while (current) {
1519
+ if (predicate(current)) return current;
1520
+ current = current.next;
1521
+ }
1522
+ return void 0;
1523
+ }
1524
+ /**
1525
+ * Insert a new element/node before an existing one.
1526
+ * @remarks Time O(N), Space O(1)
1527
+ * @param existingElementOrNode - Existing element or node.
1528
+ * @param newElementOrNode - Element or node to insert.
1529
+ * @returns True if inserted.
1530
+ */
1531
+ addBefore(existingElementOrNode, newElementOrNode) {
1532
+ const existingNode = this.getNode(existingElementOrNode);
1533
+ if (!existingNode) return false;
1534
+ const prevNode = this._getPrevNode(existingNode);
1535
+ const newNode = this._ensureNode(newElementOrNode);
1536
+ if (!prevNode) {
1537
+ newNode.next = this._head;
1538
+ this._head = newNode;
1539
+ if (!this._tail) this._tail = newNode;
1540
+ this._length++;
1541
+ } else {
1542
+ prevNode.next = newNode;
1543
+ newNode.next = existingNode;
1544
+ this._length++;
1545
+ }
1546
+ return true;
1547
+ }
1548
+ /**
1549
+ * Insert a new element/node after an existing one.
1550
+ * @remarks Time O(N), Space O(1)
1551
+ * @param existingElementOrNode - Existing element or node.
1552
+ * @param newElementOrNode - Element or node to insert.
1553
+ * @returns True if inserted.
1554
+ */
1555
+ addAfter(existingElementOrNode, newElementOrNode) {
1556
+ const existingNode = this.getNode(existingElementOrNode);
1557
+ if (!existingNode) return false;
1558
+ const newNode = this._ensureNode(newElementOrNode);
1559
+ newNode.next = existingNode.next;
1560
+ existingNode.next = newNode;
1561
+ if (existingNode === this.tail) this._tail = newNode;
1562
+ this._length++;
1563
+ return true;
1564
+ }
1565
+ /**
1566
+ * Remove and/or insert elements at a position (array-like behavior).
1567
+ * @remarks Time O(N + M), Space O(M)
1568
+ * @param start - Start index (clamped to [0, length]).
1569
+ * @param [deleteCount] - Number of elements to remove (default 0).
1570
+ * @param [items] - Elements to insert after `start`.
1571
+ * @returns A new list containing the removed elements (typed as `this`).
1572
+ */
1573
+ splice(start, deleteCount = 0, ...items) {
1574
+ start = Math.max(0, Math.min(start, this.length));
1575
+ deleteCount = Math.max(0, deleteCount);
1576
+ const removedList = this._createInstance();
1577
+ const prevNode = start === 0 ? void 0 : this.getNodeAt(start - 1);
1578
+ let cur = prevNode ? prevNode.next : this.head;
1579
+ let removedCount = 0;
1580
+ while (removedCount < deleteCount && cur) {
1581
+ removedList.push(cur.value);
1582
+ cur = cur.next;
1583
+ removedCount++;
1584
+ }
1585
+ const afterNode = cur;
1586
+ if (prevNode) {
1587
+ prevNode.next = afterNode;
1588
+ } else {
1589
+ this._head = afterNode;
1590
+ }
1591
+ if (!afterNode) this._tail = prevNode;
1592
+ if (items.length > 0) {
1593
+ let firstInserted;
1594
+ let lastInserted;
1595
+ for (const it of items) {
1596
+ const node = this._ensureNode(it);
1597
+ if (!firstInserted) firstInserted = node;
1598
+ if (lastInserted) lastInserted.next = node;
1599
+ lastInserted = node;
1600
+ }
1601
+ if (prevNode) prevNode.next = firstInserted;
1602
+ else this._head = firstInserted;
1603
+ lastInserted.next = afterNode;
1604
+ if (!afterNode) this._tail = lastInserted;
1605
+ }
1606
+ this._length += items.length - removedCount;
1607
+ if (this._length === 0) {
1608
+ this._head = void 0;
1609
+ this._tail = void 0;
1610
+ }
1611
+ return removedList;
1612
+ }
1613
+ /**
1614
+ * Count how many nodes match a value/node/predicate.
1615
+ * @remarks Time O(N), Space O(1)
1616
+ * @param elementOrNode - Element, node, or node predicate to match.
1617
+ * @returns Number of matches in the list.
1618
+ */
1619
+ countOccurrences(elementOrNode) {
1620
+ const predicate = elementOrPredicate(elementOrNode, this._equals);
1621
+ let count = 0;
1622
+ let current = this.head;
1623
+ while (current) {
1624
+ if (predicate(current)) count++;
1625
+ current = current.next;
1626
+ }
1627
+ return count;
1628
+ }
1629
+ /**
1630
+ * Set the equality comparator used to compare values.
1631
+ * @remarks Time O(1), Space O(1)
1632
+ * @param equals - Equality predicate (a, b) → boolean.
1633
+ * @returns This list.
1634
+ */
1635
+ setEquality(equals) {
1636
+ this._equals = equals;
1637
+ return this;
1638
+ }
1639
+ /**
1640
+ * Delete the first node whose value matches a predicate.
1641
+ * @remarks Time O(N), Space O(1)
1642
+ * @param predicate - Predicate (value, index, list) → boolean to decide deletion.
1643
+ * @returns True if a node was removed.
1644
+ */
1645
+ deleteWhere(predicate) {
1646
+ let prev;
1647
+ let current = this.head;
1648
+ let i = 0;
1649
+ while (current) {
1650
+ if (predicate(current.value, i++, this)) {
1651
+ if (!prev) {
1652
+ this._head = current.next;
1653
+ if (current === this._tail) this._tail = void 0;
1654
+ } else {
1655
+ prev.next = current.next;
1656
+ if (current === this._tail) this._tail = prev;
1657
+ }
1658
+ this._length--;
1659
+ return true;
1660
+ }
1661
+ prev = current;
1662
+ current = current.next;
1663
+ }
1664
+ return false;
1665
+ }
1666
+ /**
1667
+ * Deep clone this list (values are copied by reference).
1668
+ * @remarks Time O(N), Space O(N)
1669
+ * @returns A new list with the same element sequence.
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
+
1702
+
1703
+ * @example
1704
+ * // Deep copy
1705
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1706
+ * const copy = list.clone();
1707
+ * copy.pop();
1708
+ * console.log(list.length); // 3;
1709
+ * console.log(copy.length); // 2;
1710
+ */
1711
+ clone() {
1712
+ const out = this._createInstance();
1713
+ for (const v of this) out.push(v);
1714
+ return out;
1715
+ }
1716
+ /**
1717
+ * Filter values into a new list of the same class.
1718
+ * @remarks Time O(N), Space O(N)
1719
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
1720
+ * @param [thisArg] - Value for `this` inside the callback.
1721
+ * @returns A new list with kept values.
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
+
1756
+
1757
+ * @example
1758
+ * // SinglyLinkedList filter and map operations
1759
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
1760
+ *
1761
+ * // Filter even numbers
1762
+ * const filtered = list.filter(value => value % 2 === 0);
1763
+ * console.log(filtered.length); // 2;
1764
+ *
1765
+ * // Map to double values
1766
+ * const doubled = list.map(value => value * 2);
1767
+ * console.log(doubled.length); // 5;
1768
+ *
1769
+ * // Use reduce to sum
1770
+ * const sum = list.reduce((acc, value) => acc + value, 0);
1771
+ * console.log(sum); // 15;
1772
+ */
1773
+ filter(callback, thisArg) {
1774
+ const out = this._createInstance();
1775
+ let index = 0;
1776
+ for (const value of this) if (callback.call(thisArg, value, index++, this)) out.push(value);
1777
+ return out;
1778
+ }
1779
+ /**
1780
+ * Map values into a new list of the same class.
1781
+ * @remarks Time O(N), Space O(N)
1782
+ * @param callback - Mapping function (value, index, list) → newValue.
1783
+ * @param [thisArg] - Value for `this` inside the callback.
1784
+ * @returns A new list with mapped values.
1785
+ */
1786
+ mapSame(callback, thisArg) {
1787
+ const out = this._createInstance();
1788
+ let index = 0;
1789
+ for (const value of this) {
1790
+ const mv = thisArg === void 0 ? callback(value, index++, this) : callback.call(thisArg, value, index++, this);
1791
+ out.push(mv);
1792
+ }
1793
+ return out;
1794
+ }
1795
+ /**
1796
+ * Map values into a new list (possibly different element type).
1797
+ * @remarks Time O(N), Space O(N)
1798
+ * @template EM
1799
+ * @template RM
1800
+ * @param callback - Mapping function (value, index, list) → newElement.
1801
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1802
+ * @param [thisArg] - Value for `this` inside the callback.
1803
+ * @returns A new SinglyLinkedList with mapped values.
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
+
1838
+
1839
+ * @example
1840
+ * // Transform elements
1841
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1842
+ * const doubled = list.map(n => n * 2);
1843
+ * console.log([...doubled]); // [2, 4, 6];
1844
+ */
1845
+ map(callback, options, thisArg) {
1846
+ const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
1847
+ let index = 0;
1848
+ for (const value of this) out.push(callback.call(thisArg, value, index++, this));
1849
+ return out;
1850
+ }
1851
+ /**
1852
+ * (Protected) Create a node from a value.
1853
+ * @remarks Time O(1), Space O(1)
1854
+ * @param value - Value to wrap in a node.
1855
+ * @returns A new SinglyLinkedListNode instance.
1856
+ */
1857
+ createNode(value) {
1858
+ return new SinglyLinkedListNode(value);
1859
+ }
1860
+ /**
1861
+ * (Protected) Check if input is a node predicate function.
1862
+ * @remarks Time O(1), Space O(1)
1863
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
1864
+ * @returns True if input is a predicate function.
1865
+ */
1866
+ _isPredicate(elementNodeOrPredicate) {
1867
+ return typeof elementNodeOrPredicate === "function";
1868
+ }
1869
+ /**
1870
+ * (Protected) Normalize input into a node instance.
1871
+ * @remarks Time O(1), Space O(1)
1872
+ * @param elementOrNode - Element or node.
1873
+ * @returns A SinglyLinkedListNode for the provided input.
1874
+ */
1875
+ _ensureNode(elementOrNode) {
1876
+ if (this.isNode(elementOrNode)) return elementOrNode;
1877
+ return this.createNode(elementOrNode);
1878
+ }
1879
+ /**
1880
+ * (Protected) Normalize input into a node predicate.
1881
+ * @remarks Time O(1), Space O(1)
1882
+ * @param elementNodeOrPredicate - Element, node, or predicate.
1883
+ * @returns A predicate taking a node and returning true/false.
1884
+ */
1885
+ _ensurePredicate(elementNodeOrPredicate) {
1886
+ if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
1887
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
1888
+ const value = elementNodeOrPredicate;
1889
+ return (node) => this._equals(node.value, value);
1890
+ }
1891
+ /**
1892
+ * (Protected) Get the previous node of a given node.
1893
+ * @remarks Time O(N), Space O(1)
1894
+ * @param node - A node in the list.
1895
+ * @returns Previous node or undefined.
1896
+ */
1897
+ _getPrevNode(node) {
1898
+ if (!this.head || this.head === node) return void 0;
1899
+ let current = this.head;
1900
+ while (current.next && current.next !== node) current = current.next;
1901
+ return current.next === node ? current : void 0;
1902
+ }
1903
+ /**
1904
+ * (Protected) Iterate values from head to tail.
1905
+ * @remarks Time O(N), Space O(1)
1906
+ * @returns Iterator of values (E).
1907
+ */
1908
+ *_getIterator() {
1909
+ let current = this.head;
1910
+ while (current) {
1911
+ yield current.value;
1912
+ current = current.next;
1913
+ }
1914
+ }
1915
+ /**
1916
+ * (Protected) Iterate values from tail to head.
1917
+ * @remarks Time O(N), Space O(N)
1918
+ * @returns Iterator of values (E).
1919
+ */
1920
+ *_getReverseIterator() {
1921
+ const reversedArr = [...this].reverse();
1922
+ for (const item of reversedArr) yield item;
1923
+ }
1924
+ /**
1925
+ * (Protected) Iterate nodes from head to tail.
1926
+ * @remarks Time O(N), Space O(1)
1927
+ * @returns Iterator of nodes.
1928
+ */
1929
+ *_getNodeIterator() {
1930
+ let current = this.head;
1931
+ while (current) {
1932
+ yield current;
1933
+ current = current.next;
1934
+ }
1935
+ }
1936
+ /**
1937
+ * (Protected) Create an empty instance of the same concrete class.
1938
+ * @remarks Time O(1), Space O(1)
1939
+ * @param [options] - Options forwarded to the constructor.
1940
+ * @returns An empty like-kind list instance.
1941
+ */
1942
+ _createInstance(options) {
1943
+ const Ctor = this.constructor;
1944
+ return new Ctor([], options);
1945
+ }
1946
+ /**
1947
+ * (Protected) Create a like-kind instance and seed it from an iterable.
1948
+ * @remarks Time O(N), Space O(N)
1949
+ * @template EM
1950
+ * @template RM
1951
+ * @param [elements] - Iterable used to seed the new list.
1952
+ * @param [options] - Options forwarded to the constructor.
1953
+ * @returns A like-kind SinglyLinkedList instance.
1954
+ */
1955
+ _createLike(elements = [], options) {
1956
+ const Ctor = this.constructor;
1957
+ return new Ctor(elements, options);
1958
+ }
1959
+ /**
1960
+ * (Protected) Spawn an empty like-kind list instance.
1961
+ * @remarks Time O(1), Space O(1)
1962
+ * @template EM
1963
+ * @template RM
1964
+ * @param [options] - Options forwarded to the constructor.
1965
+ * @returns An empty like-kind SinglyLinkedList instance.
1966
+ */
1967
+ _spawnLike(options) {
1968
+ return this._createLike([], options);
1969
+ }
1970
+ };
1971
+ function elementOrPredicate(input, equals) {
1972
+ if (input instanceof SinglyLinkedListNode) return (node) => node === input;
1973
+ if (typeof input === "function") return input;
1974
+ const value = input;
1975
+ return (node) => equals(node.value, value);
1976
+ }
1977
+ __name(elementOrPredicate, "elementOrPredicate");
1978
+
1979
+ // src/data-structures/linked-list/doubly-linked-list.ts
1980
+ var DoublyLinkedListNode = class extends LinkedListNode {
1981
+ static {
1982
+ __name(this, "DoublyLinkedListNode");
1983
+ }
1984
+ /**
1985
+ * Create a node.
1986
+ * @remarks Time O(1), Space O(1)
1987
+ * @param value - Element value to store.
1988
+ * @returns New node instance.
1989
+ */
1990
+ constructor(value) {
1991
+ super(value);
1992
+ this._value = value;
1993
+ this._next = void 0;
1994
+ this._prev = void 0;
1995
+ }
1996
+ _next;
1997
+ /**
1998
+ * Get the next node link.
1999
+ * @remarks Time O(1), Space O(1)
2000
+ * @returns Next node or undefined.
2001
+ */
2002
+ get next() {
2003
+ return this._next;
2004
+ }
2005
+ /**
2006
+ * Set the next node link.
2007
+ * @remarks Time O(1), Space O(1)
2008
+ * @param value - Next node or undefined.
2009
+ * @returns void
2010
+ */
2011
+ set next(value) {
2012
+ this._next = value;
2013
+ }
2014
+ _prev;
2015
+ /**
2016
+ * Get the previous node link.
2017
+ * @remarks Time O(1), Space O(1)
2018
+ * @returns Previous node or undefined.
2019
+ */
2020
+ get prev() {
2021
+ return this._prev;
2022
+ }
2023
+ /**
2024
+ * Set the previous node link.
2025
+ * @remarks Time O(1), Space O(1)
2026
+ * @param value - Previous node or undefined.
2027
+ * @returns void
2028
+ */
2029
+ set prev(value) {
2030
+ this._prev = value;
2031
+ }
2032
+ };
2033
+ var DoublyLinkedList = class extends LinearLinkedBase {
2034
+ static {
2035
+ __name(this, "DoublyLinkedList");
2036
+ }
2037
+ _equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
2038
+ /**
2039
+ * Create a DoublyLinkedList and optionally bulk-insert elements.
2040
+ * @remarks Time O(N), Space O(N)
2041
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
2042
+ * @param [options] - Options such as maxLen and toElementFn.
2043
+ * @returns New DoublyLinkedList instance.
2044
+ */
2045
+ constructor(elements = [], options) {
2046
+ super(options);
2047
+ this._head = void 0;
2048
+ this._tail = void 0;
2049
+ this._length = 0;
2050
+ if (options?.maxLen && Number.isInteger(options.maxLen) && options.maxLen > 0) {
2051
+ this._maxLen = options.maxLen;
2052
+ }
2053
+ this.pushMany(elements);
2054
+ }
2055
+ _head;
2056
+ /**
2057
+ * Get the head node.
2058
+ * @remarks Time O(1), Space O(1)
2059
+ * @returns Head node or undefined.
2060
+ */
2061
+ get head() {
2062
+ return this._head;
2063
+ }
2064
+ _tail;
2065
+ /**
2066
+ * Get the tail node.
2067
+ * @remarks Time O(1), Space O(1)
2068
+ * @returns Tail node or undefined.
2069
+ */
2070
+ get tail() {
2071
+ return this._tail;
2072
+ }
2073
+ _length = 0;
2074
+ /**
2075
+ * Get the number of elements.
2076
+ * @remarks Time O(1), Space O(1)
2077
+ * @returns Current length.
2078
+ */
2079
+ get length() {
2080
+ return this._length;
2081
+ }
2082
+ /**
2083
+ * Get the first element value.
2084
+ * @remarks Time O(1), Space O(1)
2085
+ * @returns First element or undefined.
2086
+ */
2087
+ get first() {
2088
+ return this.head?.value;
2089
+ }
2090
+ /**
2091
+ * Get the last element value.
2092
+ * @remarks Time O(1), Space O(1)
2093
+ * @returns Last element or undefined.
2094
+ */
2095
+ get last() {
2096
+ return this.tail?.value;
2097
+ }
2098
+ /**
2099
+ * Create a new list from an array of elements.
2100
+ * @remarks Time O(N), Space O(N)
2101
+ * @template E
2102
+ * @template R
2103
+ * @param this - The constructor (subclass) to instantiate.
2104
+ * @param data - Array of elements to insert.
2105
+ * @returns A new list populated with the array's elements.
2106
+ */
2107
+ static fromArray(data) {
2108
+ return new this(data);
2109
+ }
2110
+ /**
2111
+ * Type guard: check whether the input is a DoublyLinkedListNode.
2112
+ * @remarks Time O(1), Space O(1)
2113
+ * @param elementNodeOrPredicate - Element, node, or predicate.
2114
+ * @returns True if the value is a DoublyLinkedListNode.
2115
+ */
2116
+ isNode(elementNodeOrPredicate) {
2117
+ return elementNodeOrPredicate instanceof DoublyLinkedListNode;
2118
+ }
2119
+ /**
2120
+ * Append an element/node to the tail.
2121
+ * @remarks Time O(1), Space O(1)
2122
+ * @param elementOrNode - Element or node to append.
2123
+ * @returns True when appended.
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
+
2158
+
2159
+ * @example
2160
+ * // basic DoublyLinkedList creation and push operation
2161
+ * // Create a simple DoublyLinkedList with initial values
2162
+ * const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
2163
+ *
2164
+ * // Verify the list maintains insertion order
2165
+ * console.log([...list]); // [1, 2, 3, 4, 5];
2166
+ *
2167
+ * // Check length
2168
+ * console.log(list.length); // 5;
2169
+ *
2170
+ * // Push a new element to the end
2171
+ * list.push(6);
2172
+ * console.log(list.length); // 6;
2173
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
2174
+ */
2175
+ push(elementOrNode) {
2176
+ const newNode = this._ensureNode(elementOrNode);
2177
+ if (!this.head) {
2178
+ this._head = newNode;
2179
+ this._tail = newNode;
2180
+ } else {
2181
+ newNode.prev = this.tail;
2182
+ this.tail.next = newNode;
2183
+ this._tail = newNode;
2184
+ }
2185
+ this._length++;
2186
+ if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
2187
+ return true;
2188
+ }
2189
+ /**
2190
+ * Remove and return the tail element.
2191
+ * @remarks Time O(1), Space O(1)
2192
+ * @returns Removed element or undefined.
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
+
2227
+
2228
+ * @example
2229
+ * // DoublyLinkedList pop and shift operations
2230
+ * const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
2231
+ *
2232
+ * // Pop removes from the end
2233
+ * const last = list.pop();
2234
+ * console.log(last); // 50;
2235
+ *
2236
+ * // Shift removes from the beginning
2237
+ * const first = list.shift();
2238
+ * console.log(first); // 10;
2239
+ *
2240
+ * // Verify remaining elements
2241
+ * console.log([...list]); // [20, 30, 40];
2242
+ * console.log(list.length); // 3;
2243
+ */
2244
+ pop() {
2245
+ if (!this.tail) return void 0;
2246
+ const removed = this.tail;
2247
+ if (this.head === this.tail) {
2248
+ this._head = void 0;
2249
+ this._tail = void 0;
2250
+ } else {
2251
+ this._tail = removed.prev;
2252
+ this.tail.next = void 0;
2253
+ }
2254
+ this._length--;
2255
+ return removed.value;
2256
+ }
2257
+ /**
2258
+ * Remove and return the head element.
2259
+ * @remarks Time O(1), Space O(1)
2260
+ * @returns Removed element or undefined.
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
+
2295
+
2296
+ * @example
2297
+ * // Remove from the front
2298
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
2299
+ * console.log(list.shift()); // 10;
2300
+ * console.log(list.first); // 20;
2301
+ */
2302
+ shift() {
2303
+ if (!this.head) return void 0;
2304
+ const removed = this.head;
2305
+ if (this.head === this.tail) {
2306
+ this._head = void 0;
2307
+ this._tail = void 0;
2308
+ } else {
2309
+ this._head = removed.next;
2310
+ this.head.prev = void 0;
2311
+ }
2312
+ this._length--;
2313
+ return removed.value;
2314
+ }
2315
+ /**
2316
+ * Prepend an element/node to the head.
2317
+ * @remarks Time O(1), Space O(1)
2318
+ * @param elementOrNode - Element or node to prepend.
2319
+ * @returns True when prepended.
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
+
2354
+
2355
+ * @example
2356
+ * // Add to the front
2357
+ * const list = new DoublyLinkedList<number>([2, 3]);
2358
+ * list.unshift(1);
2359
+ * console.log([...list]); // [1, 2, 3];
2360
+ */
2361
+ unshift(elementOrNode) {
2362
+ const newNode = this._ensureNode(elementOrNode);
2363
+ if (!this.head) {
2364
+ this._head = newNode;
2365
+ this._tail = newNode;
2366
+ } else {
2367
+ newNode.next = this.head;
2368
+ this.head.prev = newNode;
2369
+ this._head = newNode;
2370
+ }
2371
+ this._length++;
2372
+ if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
2373
+ return true;
2374
+ }
2375
+ /**
2376
+ * Append a sequence of elements/nodes.
2377
+ * @remarks Time O(N), Space O(1)
2378
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
2379
+ * @returns Array of per-element success flags.
2380
+ */
2381
+ pushMany(elements) {
2382
+ const ans = [];
2383
+ for (const el of elements) {
2384
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
2385
+ else ans.push(this.push(el));
2386
+ }
2387
+ return ans;
2388
+ }
2389
+ /**
2390
+ * Prepend a sequence of elements/nodes.
2391
+ * @remarks Time O(N), Space O(1)
2392
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
2393
+ * @returns Array of per-element success flags.
2394
+ */
2395
+ unshiftMany(elements) {
2396
+ const ans = [];
2397
+ for (const el of elements) {
2398
+ if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
2399
+ else ans.push(this.unshift(el));
2400
+ }
2401
+ return ans;
2402
+ }
2403
+ /**
2404
+ * Get the element at a given index.
2405
+ * @remarks Time O(N), Space O(1)
2406
+ * @param index - Zero-based index.
2407
+ * @returns Element or undefined.
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
+
2442
+
2443
+ * @example
2444
+ * // Access by index
2445
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2446
+ * console.log(list.at(1)); // 'b';
2447
+ * console.log(list.at(2)); // 'c';
2448
+ */
2449
+ at(index) {
2450
+ if (index < 0 || index >= this._length) return void 0;
2451
+ let current = this.head;
2452
+ for (let i = 0; i < index && current; i++) current = current.next;
2453
+ return current?.value;
2454
+ }
2455
+ /**
2456
+ * Get the node reference at a given index.
2457
+ * @remarks Time O(N), Space O(1)
2458
+ * @param index - Zero-based index.
2459
+ * @returns Node or undefined.
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
+
2491
+
2492
+ * @example
2493
+ * // Get node at index
2494
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2495
+ * console.log(list.getNodeAt(1)?.value); // 'b';
2496
+ */
2497
+ getNodeAt(index) {
2498
+ if (index < 0 || index >= this._length) return void 0;
2499
+ let current = this.head;
2500
+ for (let i = 0; i < index && current; i++) current = current.next;
2501
+ return current;
2502
+ }
2503
+ /**
2504
+ * Find a node by value, reference, or predicate.
2505
+ * @remarks Time O(N), Space O(1)
2506
+ * @param [elementNodeOrPredicate] - Element, node, or predicate to match.
2507
+ * @returns Matching node or undefined.
2508
+ */
2509
+ getNode(elementNodeOrPredicate) {
2510
+ if (elementNodeOrPredicate === void 0) return;
2511
+ if (this.isNode(elementNodeOrPredicate)) {
2512
+ const target = elementNodeOrPredicate;
2513
+ let cur = this.head;
2514
+ while (cur) {
2515
+ if (cur === target) return target;
2516
+ cur = cur.next;
2517
+ }
2518
+ const isMatch = /* @__PURE__ */ __name((node) => this._equals(node.value, target.value), "isMatch");
2519
+ cur = this.head;
2520
+ while (cur) {
2521
+ if (isMatch(cur)) return cur;
2522
+ cur = cur.next;
2523
+ }
2524
+ return void 0;
2525
+ }
2526
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2527
+ let current = this.head;
2528
+ while (current) {
2529
+ if (predicate(current)) return current;
2530
+ current = current.next;
2531
+ }
2532
+ return void 0;
2533
+ }
2534
+ /**
2535
+ * Insert a new element/node at an index, shifting following nodes.
2536
+ * @remarks Time O(N), Space O(1)
2537
+ * @param index - Zero-based index.
2538
+ * @param newElementOrNode - Element or node to insert.
2539
+ * @returns True if inserted.
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
+
2571
+
2572
+ * @example
2573
+ * // Insert at position
2574
+ * const list = new DoublyLinkedList<number>([1, 3]);
2575
+ * list.addAt(1, 2);
2576
+ * console.log(list.toArray()); // [1, 2, 3];
2577
+ */
2578
+ addAt(index, newElementOrNode) {
2579
+ if (index < 0 || index > this._length) return false;
2580
+ if (index === 0) return this.unshift(newElementOrNode);
2581
+ if (index === this._length) return this.push(newElementOrNode);
2582
+ const newNode = this._ensureNode(newElementOrNode);
2583
+ const prevNode = this.getNodeAt(index - 1);
2584
+ const nextNode = prevNode.next;
2585
+ newNode.prev = prevNode;
2586
+ newNode.next = nextNode;
2587
+ prevNode.next = newNode;
2588
+ nextNode.prev = newNode;
2589
+ this._length++;
2590
+ return true;
2591
+ }
2592
+ /**
2593
+ * Insert a new element/node before an existing one.
2594
+ * @remarks Time O(N), Space O(1)
2595
+ * @param existingElementOrNode - Existing element or node.
2596
+ * @param newElementOrNode - Element or node to insert.
2597
+ * @returns True if inserted.
2598
+ */
2599
+ addBefore(existingElementOrNode, newElementOrNode) {
2600
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2601
+ if (!existingNode) return false;
2602
+ const newNode = this._ensureNode(newElementOrNode);
2603
+ newNode.prev = existingNode.prev;
2604
+ if (existingNode.prev) existingNode.prev.next = newNode;
2605
+ newNode.next = existingNode;
2606
+ existingNode.prev = newNode;
2607
+ if (existingNode === this.head) this._head = newNode;
2608
+ this._length++;
2609
+ return true;
2610
+ }
2611
+ /**
2612
+ * Insert a new element/node after an existing one.
2613
+ * @remarks Time O(N), Space O(1)
2614
+ * @param existingElementOrNode - Existing element or node.
2615
+ * @param newElementOrNode - Element or node to insert.
2616
+ * @returns True if inserted.
2617
+ */
2618
+ addAfter(existingElementOrNode, newElementOrNode) {
2619
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2620
+ if (!existingNode) return false;
2621
+ const newNode = this._ensureNode(newElementOrNode);
2622
+ newNode.next = existingNode.next;
2623
+ if (existingNode.next) existingNode.next.prev = newNode;
2624
+ newNode.prev = existingNode;
2625
+ existingNode.next = newNode;
2626
+ if (existingNode === this.tail) this._tail = newNode;
2627
+ this._length++;
2628
+ return true;
2629
+ }
2630
+ /**
2631
+ * Set the element value at an index.
2632
+ * @remarks Time O(N), Space O(1)
2633
+ * @param index - Zero-based index.
2634
+ * @param value - New value.
2635
+ * @returns True if updated.
2636
+ */
2637
+ setAt(index, value) {
2638
+ const node = this.getNodeAt(index);
2639
+ if (!node) return false;
2640
+ node.value = value;
2641
+ return true;
2642
+ }
2643
+ /**
2644
+ * Delete the element at an index.
2645
+ * @remarks Time O(N), Space O(1)
2646
+ * @param index - Zero-based index.
2647
+ * @returns Removed element or undefined.
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
+
2679
+
2680
+ * @example
2681
+ * // Remove by index
2682
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2683
+ * list.deleteAt(1);
2684
+ * console.log(list.toArray()); // ['a', 'c'];
2685
+ */
2686
+ deleteAt(index) {
2687
+ if (index < 0 || index >= this._length) return;
2688
+ if (index === 0) return this.shift();
2689
+ if (index === this._length - 1) return this.pop();
2690
+ const removedNode = this.getNodeAt(index);
2691
+ const prevNode = removedNode.prev;
2692
+ const nextNode = removedNode.next;
2693
+ prevNode.next = nextNode;
2694
+ nextNode.prev = prevNode;
2695
+ this._length--;
2696
+ return removedNode.value;
2697
+ }
2698
+ /**
2699
+ * Delete the first match by value/node.
2700
+ * @remarks Time O(N), Space O(1)
2701
+ * @param [elementOrNode] - Element or node to remove.
2702
+ * @returns True if removed.
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
+
2734
+
2735
+ * @example
2736
+ * // Remove first occurrence
2737
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 2]);
2738
+ * list.delete(2);
2739
+ * console.log(list.toArray()); // [1, 3, 2];
2740
+ */
2741
+ delete(elementOrNode) {
2742
+ const node = this.getNode(elementOrNode);
2743
+ if (!node) return false;
2744
+ if (node === this.head) this.shift();
2745
+ else if (node === this.tail) this.pop();
2746
+ else {
2747
+ const prevNode = node.prev;
2748
+ const nextNode = node.next;
2749
+ prevNode.next = nextNode;
2750
+ nextNode.prev = prevNode;
2751
+ this._length--;
2752
+ }
2753
+ return true;
2754
+ }
2755
+ /**
2756
+ * Check whether the list is empty.
2757
+ * @remarks Time O(1), Space O(1)
2758
+ * @returns True if length is 0.
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
+
2791
+
2792
+ * @example
2793
+ * // Check empty
2794
+ * console.log(new DoublyLinkedList().isEmpty()); // true;
2795
+ */
2796
+ isEmpty() {
2797
+ return this._length === 0;
2798
+ }
2799
+ /**
2800
+ * Remove all nodes and reset length.
2801
+ * @remarks Time O(N), Space O(1)
2802
+ * @returns void
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
+
2835
+
2836
+ * @example
2837
+ * // Remove all
2838
+ * const list = new DoublyLinkedList<number>([1, 2]);
2839
+ * list.clear();
2840
+ * console.log(list.isEmpty()); // true;
2841
+ */
2842
+ clear() {
2843
+ this._head = void 0;
2844
+ this._tail = void 0;
2845
+ this._length = 0;
2846
+ }
2847
+ /**
2848
+ * Find the first value matching a predicate scanning forward.
2849
+ * @remarks Time O(N), Space O(1)
2850
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
2851
+ * @returns Matched value or undefined.
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
+
2883
+
2884
+ * @example
2885
+ * // Search with predicate
2886
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
2887
+ * const found = list.search(node => node.value > 15);
2888
+ * console.log(found); // 20;
2889
+ */
2890
+ search(elementNodeOrPredicate) {
2891
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2892
+ let current = this.head;
2893
+ while (current) {
2894
+ if (predicate(current)) return current.value;
2895
+ current = current.next;
2896
+ }
2897
+ return void 0;
2898
+ }
2899
+ /**
2900
+ * Find the first value matching a predicate scanning backward.
2901
+ * @remarks Time O(N), Space O(1)
2902
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
2903
+ * @returns Matched value or undefined.
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
+
2935
+
2936
+ * @example
2937
+ * // Find value scanning from tail
2938
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
2939
+ * // getBackward scans from tail to head, returns first match
2940
+ * const found = list.getBackward(node => node.value < 4);
2941
+ * console.log(found); // 3;
2942
+ */
2943
+ getBackward(elementNodeOrPredicate) {
2944
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2945
+ let current = this.tail;
2946
+ while (current) {
2947
+ if (predicate(current)) return current.value;
2948
+ current = current.prev;
2949
+ }
2950
+ return void 0;
2951
+ }
2952
+ /**
2953
+ * Reverse the list in place.
2954
+ * @remarks Time O(N), Space O(1)
2955
+ * @returns This list.
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
+
2990
+
2991
+ * @example
2992
+ * // Reverse in-place
2993
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
2994
+ * list.reverse();
2995
+ * console.log([...list]); // [3, 2, 1];
2996
+ */
2997
+ reverse() {
2998
+ let current = this.head;
2999
+ [this._head, this._tail] = [this.tail, this.head];
3000
+ while (current) {
3001
+ const next = current.next;
3002
+ [current.prev, current.next] = [current.next, current.prev];
3003
+ current = next;
3004
+ }
3005
+ return this;
3006
+ }
3007
+ /**
3008
+ * Set the equality comparator used to compare values.
3009
+ * @remarks Time O(1), Space O(1)
3010
+ * @param equals - Equality predicate (a, b) → boolean.
3011
+ * @returns This list.
3012
+ */
3013
+ setEquality(equals) {
3014
+ this._equals = equals;
3015
+ return this;
3016
+ }
3017
+ /**
3018
+ * Deep clone this list (values are copied by reference).
3019
+ * @remarks Time O(N), Space O(N)
3020
+ * @returns A new list with the same element sequence.
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
+
3053
+
3054
+ * @example
3055
+ * // Deep copy
3056
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
3057
+ * const copy = list.clone();
3058
+ * copy.pop();
3059
+ * console.log(list.length); // 3;
3060
+ */
3061
+ clone() {
3062
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
3063
+ for (const v of this) out.push(v);
3064
+ return out;
3065
+ }
3066
+ /**
3067
+ * Filter values into a new list of the same class.
3068
+ * @remarks Time O(N), Space O(N)
3069
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
3070
+ * @param [thisArg] - Value for `this` inside the callback.
3071
+ * @returns A new list with kept values.
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
+
3106
+
3107
+ * @example
3108
+ * // Filter elements
3109
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
3110
+ * const evens = list.filter(n => n % 2 === 0);
3111
+ * console.log([...evens]); // [2, 4];
3112
+ */
3113
+ filter(callback, thisArg) {
3114
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
3115
+ let index = 0;
3116
+ for (const v of this) if (callback.call(thisArg, v, index++, this)) out.push(v);
3117
+ return out;
3118
+ }
3119
+ /**
3120
+ * Map values into a new list of the same class.
3121
+ * @remarks Time O(N), Space O(N)
3122
+ * @param callback - Mapping function (value, index, list) → newValue.
3123
+ * @param [thisArg] - Value for `this` inside the callback.
3124
+ * @returns A new list with mapped values.
3125
+ */
3126
+ mapSame(callback, thisArg) {
3127
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
3128
+ let index = 0;
3129
+ for (const v of this) {
3130
+ const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
3131
+ out.push(mv);
3132
+ }
3133
+ return out;
3134
+ }
3135
+ /**
3136
+ * Map values into a new list (possibly different element type).
3137
+ * @remarks Time O(N), Space O(N)
3138
+ * @template EM
3139
+ * @template RM
3140
+ * @param callback - Mapping function (value, index, list) → newElement.
3141
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
3142
+ * @param [thisArg] - Value for `this` inside the callback.
3143
+ * @returns A new DoublyLinkedList with mapped values.
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
+
3178
+
3179
+ * @example
3180
+ * // DoublyLinkedList for...of iteration and map operation
3181
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
3182
+ *
3183
+ * // Iterate through list
3184
+ * const doubled = list.map(value => value * 2);
3185
+ * console.log(doubled.length); // 5;
3186
+ *
3187
+ * // Use for...of loop
3188
+ * const result: number[] = [];
3189
+ * for (const item of list) {
3190
+ * result.push(item);
3191
+ * }
3192
+ * console.log(result); // [1, 2, 3, 4, 5];
3193
+ */
3194
+ map(callback, options, thisArg) {
3195
+ const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
3196
+ let index = 0;
3197
+ for (const v of this) out.push(callback.call(thisArg, v, index++, this));
3198
+ return out;
3199
+ }
3200
+ /**
3201
+ * (Protected) Create or return a node for the given input (node or raw element).
3202
+ * @remarks Time O(1), Space O(1)
3203
+ * @param elementOrNode - Element value or node to normalize.
3204
+ * @returns A DoublyLinkedListNode for the provided input.
3205
+ */
3206
+ _ensureNode(elementOrNode) {
3207
+ if (this.isNode(elementOrNode)) return elementOrNode;
3208
+ return new DoublyLinkedListNode(elementOrNode);
3209
+ }
3210
+ /**
3211
+ * (Protected) Normalize input into a predicate over nodes.
3212
+ * @remarks Time O(1), Space O(1)
3213
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
3214
+ * @returns A predicate function taking a node and returning true/false.
3215
+ */
3216
+ _ensurePredicate(elementNodeOrPredicate) {
3217
+ if (this.isNode(elementNodeOrPredicate)) {
3218
+ const target = elementNodeOrPredicate;
3219
+ return (node) => node === target;
3220
+ }
3221
+ if (typeof elementNodeOrPredicate === "function") {
3222
+ return elementNodeOrPredicate;
3223
+ }
3224
+ const value = elementNodeOrPredicate;
3225
+ return (node) => this._equals(node.value, value);
3226
+ }
3227
+ /**
3228
+ * (Protected) Get the previous node of a given node.
3229
+ * @remarks Time O(1), Space O(1)
3230
+ * @param node - A node in the list.
3231
+ * @returns Previous node or undefined.
3232
+ */
3233
+ _getPrevNode(node) {
3234
+ return node.prev;
3235
+ }
3236
+ /**
3237
+ * (Protected) Create an empty instance of the same concrete class.
3238
+ * @remarks Time O(1), Space O(1)
3239
+ * @param [options] - Options forwarded to the constructor.
3240
+ * @returns An empty like-kind list instance.
3241
+ */
3242
+ _createInstance(options) {
3243
+ const Ctor = this.constructor;
3244
+ return new Ctor([], options);
3245
+ }
3246
+ /**
3247
+ * (Protected) Create a like-kind instance and seed it from an iterable.
3248
+ * @remarks Time O(N), Space O(N)
3249
+ * @template EM
3250
+ * @template RM
3251
+ * @param [elements] - Iterable used to seed the new list.
3252
+ * @param [options] - Options forwarded to the constructor.
3253
+ * @returns A like-kind DoublyLinkedList instance.
3254
+ */
3255
+ _createLike(elements = [], options) {
3256
+ const Ctor = this.constructor;
3257
+ return new Ctor(elements, options);
3258
+ }
3259
+ *_getIterator() {
3260
+ let current = this.head;
3261
+ while (current) {
3262
+ yield current.value;
3263
+ current = current.next;
3264
+ }
3265
+ }
3266
+ *_getReverseIterator() {
3267
+ let current = this.tail;
3268
+ while (current) {
3269
+ yield current.value;
3270
+ current = current.prev;
3271
+ }
3272
+ }
3273
+ *_getNodeIterator() {
3274
+ let current = this.head;
3275
+ while (current) {
3276
+ yield current;
3277
+ current = current.next;
3278
+ }
3279
+ }
3280
+ };
3281
+
3282
+ // src/data-structures/base/iterable-entry-base.ts
3283
+ var IterableEntryBase = class {
3284
+ static {
3285
+ __name(this, "IterableEntryBase");
3286
+ }
3287
+ /**
3288
+ * Default iterator yielding `[key, value]` entries.
3289
+ * @returns Iterator of `[K, V]`.
3290
+ * @remarks Time O(n) to iterate, Space O(1)
3291
+ */
3292
+ *[Symbol.iterator](...args) {
3293
+ yield* this._getIterator(...args);
3294
+ }
3295
+ /**
3296
+ * Iterate over `[key, value]` pairs (may yield `undefined` values).
3297
+ * @returns Iterator of `[K, V | undefined]`.
3298
+ * @remarks Time O(n), Space O(1)
3299
+ */
3300
+ *entries() {
3301
+ for (const item of this) {
3302
+ yield item;
3303
+ }
3304
+ }
3305
+ /**
3306
+ * Iterate over keys only.
3307
+ * @returns Iterator of keys.
3308
+ * @remarks Time O(n), Space O(1)
3309
+ */
3310
+ *keys() {
3311
+ for (const item of this) {
3312
+ yield item[0];
3313
+ }
3314
+ }
3315
+ /**
3316
+ * Iterate over values only.
3317
+ * @returns Iterator of values.
3318
+ * @remarks Time O(n), Space O(1)
3319
+ */
3320
+ *values() {
3321
+ for (const item of this) {
3322
+ yield item[1];
3323
+ }
3324
+ }
3325
+ /**
3326
+ * Test whether all entries satisfy the predicate.
3327
+ * @param predicate - `(key, value, index, self) => boolean`.
3328
+ * @param thisArg - Optional `this` for callback.
3329
+ * @returns `true` if all pass; otherwise `false`.
3330
+ * @remarks Time O(n), Space O(1)
3331
+ */
3332
+ every(predicate, thisArg) {
3333
+ let index = 0;
3334
+ for (const item of this) {
3335
+ if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
3336
+ return false;
3337
+ }
3338
+ }
3339
+ return true;
3340
+ }
3341
+ /**
3342
+ * Test whether any entry satisfies the predicate.
3343
+ * @param predicate - `(key, value, index, self) => boolean`.
3344
+ * @param thisArg - Optional `this` for callback.
3345
+ * @returns `true` if any passes; otherwise `false`.
3346
+ * @remarks Time O(n), Space O(1)
3347
+ */
3348
+ some(predicate, thisArg) {
3349
+ let index = 0;
3350
+ for (const item of this) {
3351
+ if (predicate.call(thisArg, item[1], item[0], index++, this)) {
3352
+ return true;
3353
+ }
3354
+ }
3355
+ return false;
3356
+ }
3357
+ /**
3358
+ * Visit each entry, left-to-right.
3359
+ * @param callbackfn - `(key, value, index, self) => void`.
3360
+ * @param thisArg - Optional `this` for callback.
3361
+ * @remarks Time O(n), Space O(1)
3362
+ */
3363
+ forEach(callbackfn, thisArg) {
3364
+ let index = 0;
3365
+ for (const item of this) {
3366
+ const [key, value] = item;
3367
+ callbackfn.call(thisArg, value, key, index++, this);
3368
+ }
3369
+ }
3370
+ /**
3371
+ * Find the first entry that matches a predicate.
3372
+ * @param callbackfn - `(key, value, index, self) => boolean`.
3373
+ * @param thisArg - Optional `this` for callback.
3374
+ * @returns Matching `[key, value]` or `undefined`.
3375
+ * @remarks Time O(n), Space O(1)
3376
+ */
3377
+ find(callbackfn, thisArg) {
3378
+ let index = 0;
3379
+ for (const item of this) {
3380
+ const [key, value] = item;
3381
+ if (callbackfn.call(thisArg, value, key, index++, this)) return item;
3382
+ }
3383
+ return;
3384
+ }
3385
+ /**
3386
+ * Whether the given key exists.
3387
+ * @param key - Key to test.
3388
+ * @returns `true` if found; otherwise `false`.
3389
+ * @remarks Time O(n) generic, Space O(1)
3390
+ */
3391
+ has(key) {
3392
+ for (const item of this) {
3393
+ const [itemKey] = item;
3394
+ if (itemKey === key) return true;
3395
+ }
3396
+ return false;
3397
+ }
3398
+ /**
3399
+ * Whether there exists an entry with the given value.
3400
+ * @param value - Value to test.
3401
+ * @returns `true` if found; otherwise `false`.
3402
+ * @remarks Time O(n), Space O(1)
3403
+ */
3404
+ hasValue(value) {
3405
+ for (const [, elementValue] of this) {
3406
+ if (elementValue === value) return true;
3407
+ }
3408
+ return false;
3409
+ }
3410
+ /**
3411
+ * Get the value under a key.
3412
+ * @param key - Key to look up.
3413
+ * @returns Value or `undefined`.
3414
+ * @remarks Time O(n) generic, Space O(1)
3415
+ */
3416
+ get(key) {
3417
+ for (const item of this) {
3418
+ const [itemKey, value] = item;
3419
+ if (itemKey === key) return value;
3420
+ }
3421
+ return;
3422
+ }
3423
+ /**
3424
+ * Reduce entries into a single accumulator.
3425
+ * @param callbackfn - `(acc, value, key, index, self) => acc`.
3426
+ * @param initialValue - Initial accumulator.
3427
+ * @returns Final accumulator.
3428
+ * @remarks Time O(n), Space O(1)
3429
+ */
3430
+ reduce(callbackfn, initialValue) {
3431
+ let accumulator = initialValue;
3432
+ let index = 0;
3433
+ for (const item of this) {
3434
+ const [key, value] = item;
3435
+ accumulator = callbackfn(accumulator, value, key, index++, this);
3436
+ }
3437
+ return accumulator;
3438
+ }
3439
+ /**
3440
+ * Converts data structure to `[key, value]` pairs.
3441
+ * @returns Array of entries.
3442
+ * @remarks Time O(n), Space O(n)
3443
+ */
3444
+ toArray() {
3445
+ return [...this];
3446
+ }
3447
+ /**
3448
+ * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
3449
+ * @returns Array of entries (default) or a string.
3450
+ * @remarks Time O(n), Space O(n)
3451
+ */
3452
+ toVisual() {
3453
+ return [...this];
3454
+ }
3455
+ /**
3456
+ * Print a human-friendly representation to the console.
3457
+ * @remarks Time O(n), Space O(n)
3458
+ */
3459
+ print() {
3460
+ console.log(this.toVisual());
3461
+ }
3462
+ };
3463
+
3464
+ // src/data-structures/linked-list/skip-linked-list.ts
3465
+ var SkipListNode = class {
3466
+ static {
3467
+ __name(this, "SkipListNode");
3468
+ }
3469
+ key;
3470
+ value;
3471
+ forward;
3472
+ constructor(key, value, level) {
3473
+ this.key = key;
3474
+ this.value = value;
3475
+ this.forward = new Array(level).fill(void 0);
3476
+ }
3477
+ };
3478
+ var SkipList = class _SkipList extends IterableEntryBase {
3479
+ static {
3480
+ __name(this, "SkipList");
3481
+ }
3482
+ #comparator;
3483
+ #isDefaultComparator;
3484
+ constructor(entries = [], options = {}) {
3485
+ super();
3486
+ const { comparator, toEntryFn, maxLevel, probability } = options;
3487
+ if (typeof maxLevel === "number" && maxLevel > 0) this._maxLevel = maxLevel;
3488
+ if (typeof probability === "number" && probability > 0 && probability < 1) this._probability = probability;
3489
+ this.#isDefaultComparator = comparator === void 0;
3490
+ this.#comparator = comparator ?? _SkipList.createDefaultComparator();
3491
+ this._head = new SkipListNode(void 0, void 0, this._maxLevel);
3492
+ for (const item of entries) {
3493
+ let k;
3494
+ let v;
3495
+ if (toEntryFn) {
3496
+ [k, v] = toEntryFn(item);
3497
+ } else {
3498
+ if (!Array.isArray(item) || item.length < 2) {
3499
+ raise(TypeError, ERR.invalidEntry("SkipList"));
3500
+ }
3501
+ [k, v] = item;
3502
+ }
3503
+ this.set(k, v);
3504
+ }
3505
+ }
3506
+ /**
3507
+ * Creates a default comparator supporting number, string, Date, and bigint.
3508
+ */
3509
+ static createDefaultComparator() {
3510
+ return (a, b) => {
3511
+ if (typeof a === "number" && typeof b === "number") {
3512
+ if (Number.isNaN(a) || Number.isNaN(b)) raise(TypeError, ERR.invalidNaN("SkipList"));
3513
+ return a - b;
3514
+ }
3515
+ if (typeof a === "string" && typeof b === "string") {
3516
+ return a < b ? -1 : a > b ? 1 : 0;
3517
+ }
3518
+ if (a instanceof Date && b instanceof Date) {
3519
+ const ta = a.getTime(), tb = b.getTime();
3520
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("SkipList"));
3521
+ return ta - tb;
3522
+ }
3523
+ if (typeof a === "bigint" && typeof b === "bigint") {
3524
+ return a < b ? -1 : a > b ? 1 : 0;
3525
+ }
3526
+ raise(TypeError, ERR.comparatorRequired("SkipList"));
3527
+ };
3528
+ }
3529
+ // ─── Internal state ──────────────────────────────────────────
3530
+ _head;
3531
+ _level = 0;
3532
+ _size = 0;
3533
+ _maxLevel = 16;
3534
+ _probability = 0.5;
3535
+ // ─── Size & lifecycle ────────────────────────────────────────
3536
+ get size() {
3537
+ return this._size;
3538
+ }
3539
+ get maxLevel() {
3540
+ return this._maxLevel;
3541
+ }
3542
+ get probability() {
3543
+ return this._probability;
3544
+ }
3545
+ get comparator() {
3546
+ return this.#comparator;
3547
+ }
3548
+ /**
3549
+ * Check if empty
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
+
3581
+
3582
+ * @example
3583
+ * // Check if empty
3584
+ * const sl = new SkipList<number, string>();
3585
+ * console.log(sl.isEmpty()); // true;
3586
+ */
3587
+ isEmpty() {
3588
+ return this._size === 0;
3589
+ }
3590
+ /**
3591
+ * Remove all entries
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
+
3623
+
3624
+ * @example
3625
+ * // Remove all entries
3626
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
3627
+ * sl.clear();
3628
+ * console.log(sl.isEmpty()); // true;
3629
+ */
3630
+ clear() {
3631
+ this._head = new SkipListNode(void 0, void 0, this._maxLevel);
3632
+ this._level = 0;
3633
+ this._size = 0;
3634
+ }
3635
+ /**
3636
+ * Create independent copy
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
+
3668
+
3669
+ * @example
3670
+ * // Create independent copy
3671
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
3672
+ * const copy = sl.clone();
3673
+ * copy.delete(1);
3674
+ * console.log(sl.has(1)); // true;
3675
+ */
3676
+ clone() {
3677
+ return new _SkipList(this, {
3678
+ comparator: this.#isDefaultComparator ? void 0 : this.#comparator,
3679
+ maxLevel: this._maxLevel,
3680
+ probability: this._probability
3681
+ });
3682
+ }
3683
+ // ─── Core CRUD ───────────────────────────────────────────────
3684
+ /**
3685
+ * Insert or update a key-value pair. Returns `this` for chaining.
3686
+ * Unique keys only — if key exists, value is updated in place.
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
+
3721
+
3722
+ * @example
3723
+ * // In-memory sorted key-value store
3724
+ * const store = new SkipList<number, string>();
3725
+ *
3726
+ * store.set(3, 'three');
3727
+ * store.set(1, 'one');
3728
+ * store.set(5, 'five');
3729
+ * store.set(2, 'two');
3730
+ *
3731
+ * console.log(store.get(3)); // 'three';
3732
+ * console.log(store.get(1)); // 'one';
3733
+ * console.log(store.get(5)); // 'five';
3734
+ *
3735
+ * // Update existing key
3736
+ * store.set(3, 'THREE');
3737
+ * console.log(store.get(3)); // 'THREE';
3738
+ */
3739
+ set(key, value) {
3740
+ const cmp = this.#comparator;
3741
+ const update = this._findUpdate(key);
3742
+ const existing = update[0].forward[0];
3743
+ if (existing && cmp(existing.key, key) === 0) {
3744
+ existing.value = value;
3745
+ return this;
3746
+ }
3747
+ const newLevel = this._randomLevel();
3748
+ const newNode = new SkipListNode(key, value, newLevel);
3749
+ if (newLevel > this._level) {
3750
+ for (let i = this._level; i < newLevel; i++) {
3751
+ update[i] = this._head;
3752
+ }
3753
+ this._level = newLevel;
3754
+ }
3755
+ for (let i = 0; i < newLevel; i++) {
3756
+ newNode.forward[i] = update[i].forward[i];
3757
+ update[i].forward[i] = newNode;
3758
+ }
3759
+ this._size++;
3760
+ return this;
3761
+ }
3762
+ /**
3763
+ * Get the value for a key, or `undefined` if not found.
3764
+ * Overrides base O(n) with O(log n) skip-list search.
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
+
3799
+
3800
+ * @example
3801
+ * // Building a sorted index
3802
+ * type Product = { id: number; name: string; price: number };
3803
+ * const products: Product[] = [
3804
+ * { id: 1, name: 'Widget', price: 25 },
3805
+ * { id: 2, name: 'Gadget', price: 50 },
3806
+ * { id: 3, name: 'Doohickey', price: 15 }
3807
+ * ];
3808
+ *
3809
+ * const index = new SkipList<number, Product, Product>(products, {
3810
+ * toEntryFn: (p: Product) => [p.price, p]
3811
+ * });
3812
+ *
3813
+ * // Iterate in sorted order by price
3814
+ * const names = [...index.values()].map(p => p!.name);
3815
+ * console.log(names); // ['Doohickey', 'Widget', 'Gadget'];
3816
+ *
3817
+ * // Range search: products between $20 and $60
3818
+ * const range = index.rangeSearch([20, 60]);
3819
+ * console.log(range.map(([, p]) => p!.name)); // ['Widget', 'Gadget'];
3820
+ */
3821
+ get(key) {
3822
+ const node = this._findNode(key);
3823
+ return node ? node.value : void 0;
3824
+ }
3825
+ /**
3826
+ * Check if a key exists.
3827
+ * Overrides base O(n) with O(log n) skip-list search.
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
+
3862
+
3863
+ * @example
3864
+ * // Check key existence
3865
+ * const sl = new SkipList<number, string>([[1, 'a'], [3, 'c'], [5, 'e']]);
3866
+ * console.log(sl.has(3)); // true;
3867
+ * console.log(sl.has(4)); // false;
3868
+ */
3869
+ has(key) {
3870
+ return this._findNode(key) !== void 0;
3871
+ }
3872
+ /**
3873
+ * Delete a key. Returns `true` if the key was found and removed.
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
+
3908
+
3909
+ * @example
3910
+ * // Fast lookup with deletion
3911
+ * const cache = new SkipList<string, number>();
3912
+ *
3913
+ * cache.set('alpha', 1);
3914
+ * cache.set('beta', 2);
3915
+ * cache.set('gamma', 3);
3916
+ *
3917
+ * console.log(cache.has('beta')); // true;
3918
+ * cache.delete('beta');
3919
+ * console.log(cache.has('beta')); // false;
3920
+ * console.log(cache.size); // 2;
3921
+ */
3922
+ delete(key) {
3923
+ const cmp = this.#comparator;
3924
+ const update = this._findUpdate(key);
3925
+ const target = update[0].forward[0];
3926
+ if (!target || cmp(target.key, key) !== 0) return false;
3927
+ for (let i = 0; i < this._level; i++) {
3928
+ if (update[i].forward[i] !== target) break;
3929
+ update[i].forward[i] = target.forward[i];
3930
+ }
3931
+ while (this._level > 0 && !this._head.forward[this._level - 1]) {
3932
+ this._level--;
3933
+ }
3934
+ this._size--;
3935
+ return true;
3936
+ }
3937
+ // ─── Navigation ──────────────────────────────────────────────
3938
+ /**
3939
+ * Returns the first (smallest key) entry, or `undefined` if empty.
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
+
3974
+
3975
+ * @example
3976
+ * // Access the minimum entry
3977
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
3978
+ * console.log(sl.first()); // [1, 'a'];
3979
+ */
3980
+ first() {
3981
+ const node = this._head.forward[0];
3982
+ return node ? [node.key, node.value] : void 0;
3983
+ }
3984
+ /**
3985
+ * Returns the last (largest key) entry, or `undefined` if empty.
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
+
4020
+
4021
+ * @example
4022
+ * // Access the maximum entry
4023
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
4024
+ * console.log(sl.last()); // [5, 'e'];
4025
+ */
4026
+ last() {
4027
+ let current = this._head;
4028
+ for (let i = this._level - 1; i >= 0; i--) {
4029
+ while (current.forward[i]) {
4030
+ current = current.forward[i];
4031
+ }
4032
+ }
4033
+ return current === this._head ? void 0 : [current.key, current.value];
4034
+ }
4035
+ /**
4036
+ * Remove and return the first (smallest key) entry.
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
+
4068
+
4069
+ * @example
4070
+ * // Remove and return smallest
4071
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
4072
+ * console.log(sl.pollFirst()); // [1, 'a'];
4073
+ * console.log(sl.size); // 2;
4074
+ */
4075
+ pollFirst() {
4076
+ const entry = this.first();
4077
+ if (!entry) return void 0;
4078
+ this.delete(entry[0]);
4079
+ return entry;
4080
+ }
4081
+ /**
4082
+ * Remove and return the last (largest key) entry.
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
+
4114
+
4115
+ * @example
4116
+ * // Remove and return largest
4117
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
4118
+ * console.log(sl.pollLast()); // [3, 'c'];
4119
+ * console.log(sl.size); // 2;
4120
+ */
4121
+ pollLast() {
4122
+ const entry = this.last();
4123
+ if (!entry) return void 0;
4124
+ this.delete(entry[0]);
4125
+ return entry;
4126
+ }
4127
+ /**
4128
+ * Least entry ≥ key, or `undefined`.
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
+
4163
+
4164
+ * @example
4165
+ * // Least entry ≥ key
4166
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4167
+ * console.log(sl.ceiling(15)); // [20, 'b'];
4168
+ * console.log(sl.ceiling(20)); // [20, 'b'];
4169
+ */
4170
+ ceiling(key) {
4171
+ const cmp = this.#comparator;
4172
+ let current = this._head;
4173
+ for (let i = this._level - 1; i >= 0; i--) {
4174
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4175
+ current = current.forward[i];
4176
+ }
4177
+ }
4178
+ const node = current.forward[0];
4179
+ return node ? [node.key, node.value] : void 0;
4180
+ }
4181
+ /**
4182
+ * Greatest entry ≤ key, or `undefined`.
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
+
4217
+
4218
+ * @example
4219
+ * // Greatest entry ≤ key
4220
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4221
+ * console.log(sl.floor(25)); // [20, 'b'];
4222
+ * console.log(sl.floor(5)); // undefined;
4223
+ */
4224
+ floor(key) {
4225
+ const cmp = this.#comparator;
4226
+ let current = this._head;
4227
+ for (let i = this._level - 1; i >= 0; i--) {
4228
+ while (current.forward[i] && cmp(current.forward[i].key, key) <= 0) {
4229
+ current = current.forward[i];
4230
+ }
4231
+ }
4232
+ const result = current === this._head ? void 0 : current;
4233
+ if (result && cmp(result.key, key) <= 0) return [result.key, result.value];
4234
+ return void 0;
4235
+ }
4236
+ /**
4237
+ * Least entry strictly > key, or `undefined`.
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
+
4269
+
4270
+ * @example
4271
+ * // Strictly greater entry
4272
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4273
+ * console.log(sl.higher(15)); // [20, 'b'];
4274
+ * console.log(sl.higher(30)); // undefined;
4275
+ */
4276
+ higher(key) {
4277
+ const cmp = this.#comparator;
4278
+ let current = this._head;
4279
+ for (let i = this._level - 1; i >= 0; i--) {
4280
+ while (current.forward[i] && cmp(current.forward[i].key, key) <= 0) {
4281
+ current = current.forward[i];
4282
+ }
4283
+ }
4284
+ const node = current.forward[0];
4285
+ return node ? [node.key, node.value] : void 0;
4286
+ }
4287
+ /**
4288
+ * Greatest entry strictly < key, or `undefined`.
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
+
4320
+
4321
+ * @example
4322
+ * // Strictly less entry
4323
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4324
+ * console.log(sl.lower(25)); // [20, 'b'];
4325
+ * console.log(sl.lower(10)); // undefined;
4326
+ */
4327
+ lower(key) {
4328
+ const cmp = this.#comparator;
4329
+ let current = this._head;
4330
+ let result;
4331
+ for (let i = this._level - 1; i >= 0; i--) {
4332
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4333
+ current = current.forward[i];
4334
+ }
4335
+ if (current !== this._head && cmp(current.key, key) < 0) {
4336
+ result = current;
4337
+ }
4338
+ }
4339
+ return result ? [result.key, result.value] : void 0;
4340
+ }
4341
+ /**
4342
+ * Returns entries within the given key range.
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
+
4377
+
4378
+ * @example
4379
+ * // Find entries in a range
4380
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e']]);
4381
+ * const result = sl.rangeSearch([2, 4]);
4382
+ * console.log(result); // [[2, 'b'], [3, 'c'], [4, 'd']];
4383
+ */
4384
+ rangeSearch(range, options = {}) {
4385
+ const { lowInclusive = true, highInclusive = true } = options;
4386
+ const [low, high] = range;
4387
+ const cmp = this.#comparator;
4388
+ const out = [];
4389
+ let current = this._head;
4390
+ for (let i = this._level - 1; i >= 0; i--) {
4391
+ while (current.forward[i] && cmp(current.forward[i].key, low) < 0) {
4392
+ current = current.forward[i];
4393
+ }
4394
+ }
4395
+ current = current.forward[0];
4396
+ while (current) {
4397
+ const cmpHigh = cmp(current.key, high);
4398
+ if (cmpHigh > 0) break;
4399
+ if (cmpHigh === 0 && !highInclusive) break;
4400
+ const cmpLow = cmp(current.key, low);
4401
+ if (cmpLow > 0 || cmpLow === 0 && lowInclusive) {
4402
+ out.push([current.key, current.value]);
4403
+ }
4404
+ current = current.forward[0];
4405
+ }
4406
+ return out;
4407
+ }
4408
+ // ─── Functional (overrides) ──────────────────────────────────
4409
+ /**
4410
+ * Creates a new SkipList with entries transformed by callback.
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
+
4442
+
4443
+ * @example
4444
+ * // Transform entries
4445
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
4446
+ * const mapped = sl.map((v, k) => [k, v?.toUpperCase()] as [number, string]);
4447
+ * console.log([...mapped.values()]); // ['A', 'B'];
4448
+ */
4449
+ map(callback, options) {
4450
+ const out = new _SkipList([], options ?? {});
4451
+ let i = 0;
4452
+ for (const [k, v] of this) {
4453
+ const [nk, nv] = callback(v, k, i++, this);
4454
+ out.set(nk, nv);
4455
+ }
4456
+ return out;
4457
+ }
4458
+ /**
4459
+ * Creates a new SkipList with entries that pass the predicate.
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
+
4491
+
4492
+ * @example
4493
+ * // Filter entries
4494
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
4495
+ * const result = sl.filter((v, k) => k > 1);
4496
+ * console.log(result.size); // 2;
4497
+ */
4498
+ filter(callbackfn, thisArg) {
4499
+ const out = new _SkipList([], {
4500
+ comparator: this.#isDefaultComparator ? void 0 : this.#comparator,
4501
+ maxLevel: this._maxLevel,
4502
+ probability: this._probability
4503
+ });
4504
+ let i = 0;
4505
+ for (const [k, v] of this) {
4506
+ const ok = callbackfn.call(thisArg, v, k, i++, this);
4507
+ if (ok) out.set(k, v);
4508
+ }
4509
+ return out;
4510
+ }
4511
+ // ─── Iterator (required by IterableEntryBase) ────────────────
4512
+ _getIterator() {
4513
+ const head = this._head;
4514
+ return (function* () {
4515
+ let node = head.forward[0];
4516
+ while (node) {
4517
+ yield [node.key, node.value];
4518
+ node = node.forward[0];
4519
+ }
4520
+ })();
4521
+ }
4522
+ // ─── Internal helpers ────────────────────────────────────────
4523
+ /**
4524
+ * Finds the update array (predecessors at each level) for a given key.
4525
+ */
4526
+ _findUpdate(key) {
4527
+ const cmp = this.#comparator;
4528
+ const update = new Array(this._maxLevel).fill(this._head);
4529
+ let current = this._head;
4530
+ for (let i = this._level - 1; i >= 0; i--) {
4531
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4532
+ current = current.forward[i];
4533
+ }
4534
+ update[i] = current;
4535
+ }
4536
+ return update;
4537
+ }
4538
+ /**
4539
+ * Finds the node for a given key, or undefined.
4540
+ */
4541
+ _findNode(key) {
4542
+ const cmp = this.#comparator;
4543
+ let current = this._head;
4544
+ for (let i = this._level - 1; i >= 0; i--) {
4545
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4546
+ current = current.forward[i];
4547
+ }
4548
+ }
4549
+ const candidate = current.forward[0];
4550
+ if (candidate && cmp(candidate.key, key) === 0) return candidate;
4551
+ return void 0;
4552
+ }
4553
+ _randomLevel() {
4554
+ let level = 1;
4555
+ while (Math.random() < this._probability && level < this._maxLevel) {
4556
+ level++;
4557
+ }
4558
+ return level;
4559
+ }
4560
+ };
4561
+ /**
4562
+ * data-structure-typed
4563
+ *
4564
+ * @author Pablo Zeng
4565
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
4566
+ * @license MIT License
4567
+ */
4568
+
4569
+ exports.DoublyLinkedList = DoublyLinkedList;
4570
+ exports.DoublyLinkedListNode = DoublyLinkedListNode;
4571
+ exports.SinglyLinkedList = SinglyLinkedList;
4572
+ exports.SinglyLinkedListNode = SinglyLinkedListNode;
4573
+ exports.SkipList = SkipList;
4574
+ exports.SkipListNode = SkipListNode;
4575
+ //# sourceMappingURL=linked-list.cjs.map
4576
+ //# sourceMappingURL=linked-list.cjs.map