data-structure-typed 2.5.0 → 2.5.1

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