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