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