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,4370 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+
6
+ // src/data-structures/base/iterable-element-base.ts
7
+ var IterableElementBase = class {
8
+ static {
9
+ __name(this, "IterableElementBase");
10
+ }
11
+ /**
12
+ * Create a new iterable base.
13
+ *
14
+ * @param options Optional behavior overrides. When provided, a `toElementFn`
15
+ * is used to convert a raw element (`R`) into a public element (`E`).
16
+ *
17
+ * @remarks
18
+ * Time O(1), Space O(1).
19
+ */
20
+ constructor(options) {
21
+ if (options) {
22
+ const { toElementFn } = options;
23
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
24
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
25
+ }
26
+ }
27
+ /**
28
+ * The converter used to transform a raw element (`R`) into a public element (`E`).
29
+ *
30
+ * @remarks
31
+ * Time O(1), Space O(1).
32
+ */
33
+ _toElementFn;
34
+ /**
35
+ * Exposes the current `toElementFn`, if configured.
36
+ *
37
+ * @returns The converter function or `undefined` when not set.
38
+ * @remarks
39
+ * Time O(1), Space O(1).
40
+ */
41
+ get toElementFn() {
42
+ return this._toElementFn;
43
+ }
44
+ /**
45
+ * Returns an iterator over the structure's elements.
46
+ *
47
+ * @param args Optional iterator arguments forwarded to the internal iterator.
48
+ * @returns An `IterableIterator<E>` that yields the elements in traversal order.
49
+ *
50
+ * @remarks
51
+ * Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
52
+ */
53
+ *[Symbol.iterator](...args) {
54
+ yield* this._getIterator(...args);
55
+ }
56
+ /**
57
+ * Returns an iterator over the values (alias of the default iterator).
58
+ *
59
+ * @returns An `IterableIterator<E>` over all elements.
60
+ * @remarks
61
+ * Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
62
+ */
63
+ *values() {
64
+ for (const item of this) yield item;
65
+ }
66
+ /**
67
+ * Tests whether all elements satisfy the predicate.
68
+ *
69
+ * @template TReturn
70
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
71
+ * @param thisArg Optional `this` binding for the predicate.
72
+ * @returns `true` if every element passes; otherwise `false`.
73
+ *
74
+ * @remarks
75
+ * Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
76
+ */
77
+ every(predicate, thisArg) {
78
+ let index = 0;
79
+ for (const item of this) {
80
+ if (thisArg === void 0) {
81
+ if (!predicate(item, index++, this)) return false;
82
+ } else {
83
+ const fn = predicate;
84
+ if (!fn.call(thisArg, item, index++, this)) return false;
85
+ }
86
+ }
87
+ return true;
88
+ }
89
+ /**
90
+ * Tests whether at least one element satisfies the predicate.
91
+ *
92
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
93
+ * @param thisArg Optional `this` binding for the predicate.
94
+ * @returns `true` if any element passes; otherwise `false`.
95
+ *
96
+ * @remarks
97
+ * Time O(n) in the worst case; may exit early on first success. Space O(1).
98
+ */
99
+ some(predicate, thisArg) {
100
+ let index = 0;
101
+ for (const item of this) {
102
+ if (thisArg === void 0) {
103
+ if (predicate(item, index++, this)) return true;
104
+ } else {
105
+ const fn = predicate;
106
+ if (fn.call(thisArg, item, index++, this)) return true;
107
+ }
108
+ }
109
+ return false;
110
+ }
111
+ /**
112
+ * Invokes a callback for each element in iteration order.
113
+ *
114
+ * @param callbackfn Function invoked per element with signature `(value, index, self)`.
115
+ * @param thisArg Optional `this` binding for the callback.
116
+ * @returns `void`.
117
+ *
118
+ * @remarks
119
+ * Time O(n), Space O(1).
120
+ */
121
+ forEach(callbackfn, thisArg) {
122
+ let index = 0;
123
+ for (const item of this) {
124
+ if (thisArg === void 0) {
125
+ callbackfn(item, index++, this);
126
+ } else {
127
+ const fn = callbackfn;
128
+ fn.call(thisArg, item, index++, this);
129
+ }
130
+ }
131
+ }
132
+ // Implementation signature
133
+ find(predicate, thisArg) {
134
+ let index = 0;
135
+ for (const item of this) {
136
+ if (thisArg === void 0) {
137
+ if (predicate(item, index++, this)) return item;
138
+ } else {
139
+ const fn = predicate;
140
+ if (fn.call(thisArg, item, index++, this)) return item;
141
+ }
142
+ }
143
+ return;
144
+ }
145
+ /**
146
+ * Checks whether a strictly-equal element exists in the structure.
147
+ *
148
+ * @param element The element to test with `===` equality.
149
+ * @returns `true` if an equal element is found; otherwise `false`.
150
+ *
151
+ * @remarks
152
+ * Time O(n) in the worst case. Space O(1).
153
+ */
154
+ has(element) {
155
+ for (const ele of this) if (ele === element) return true;
156
+ return false;
157
+ }
158
+ /**
159
+ * Reduces all elements to a single accumulated value.
160
+ *
161
+ * @overload
162
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
163
+ * @returns The final accumulated value typed as `E`.
164
+ *
165
+ * @overload
166
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
167
+ * @param initialValue The initial accumulator value of type `E`.
168
+ * @returns The final accumulated value typed as `E`.
169
+ *
170
+ * @overload
171
+ * @template U The accumulator type when it differs from `E`.
172
+ * @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
173
+ * @param initialValue The initial accumulator value of type `U`.
174
+ * @returns The final accumulated value typed as `U`.
175
+ *
176
+ * @remarks
177
+ * Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
178
+ */
179
+ reduce(callbackfn, initialValue) {
180
+ let index = 0;
181
+ const iter = this[Symbol.iterator]();
182
+ let acc;
183
+ if (arguments.length >= 2) {
184
+ acc = initialValue;
185
+ } else {
186
+ const first = iter.next();
187
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
188
+ acc = first.value;
189
+ index = 1;
190
+ }
191
+ for (const value of iter) {
192
+ acc = callbackfn(acc, value, index++, this);
193
+ }
194
+ return acc;
195
+ }
196
+ /**
197
+ * Materializes the elements into a new array.
198
+ *
199
+ * @returns A shallow array copy of the iteration order.
200
+ * @remarks
201
+ * Time O(n), Space O(n).
202
+ */
203
+ toArray() {
204
+ return [...this];
205
+ }
206
+ /**
207
+ * Returns a representation of the structure suitable for quick visualization.
208
+ * Defaults to an array of elements; subclasses may override to provide richer visuals.
209
+ *
210
+ * @returns A visual representation (array by default).
211
+ * @remarks
212
+ * Time O(n), Space O(n).
213
+ */
214
+ toVisual() {
215
+ return [...this];
216
+ }
217
+ /**
218
+ * Prints `toVisual()` to the console. Intended for quick debugging.
219
+ *
220
+ * @returns `void`.
221
+ * @remarks
222
+ * Time O(n) due to materialization, Space O(n) for the intermediate representation.
223
+ */
224
+ print() {
225
+ console.log(this.toVisual());
226
+ }
227
+ };
228
+
229
+ // src/data-structures/base/linear-base.ts
230
+ var LinkedListNode = class {
231
+ static {
232
+ __name(this, "LinkedListNode");
233
+ }
234
+ /**
235
+ * Initialize a node.
236
+ * @param value - Element value.
237
+ * @remarks Time O(1), Space O(1)
238
+ */
239
+ constructor(value) {
240
+ this._value = value;
241
+ this._next = void 0;
242
+ }
243
+ _value;
244
+ /**
245
+ * Element payload getter.
246
+ * @returns Element value.
247
+ * @remarks Time O(1), Space O(1)
248
+ */
249
+ get value() {
250
+ return this._value;
251
+ }
252
+ /**
253
+ * Element payload setter.
254
+ * @param value - New value.
255
+ * @remarks Time O(1), Space O(1)
256
+ */
257
+ set value(value) {
258
+ this._value = value;
259
+ }
260
+ _next;
261
+ /**
262
+ * Next node getter.
263
+ * @returns Next node or `undefined`.
264
+ * @remarks Time O(1), Space O(1)
265
+ */
266
+ get next() {
267
+ return this._next;
268
+ }
269
+ /**
270
+ * Next node setter.
271
+ * @param value - Next node or `undefined`.
272
+ * @remarks Time O(1), Space O(1)
273
+ */
274
+ set next(value) {
275
+ this._next = value;
276
+ }
277
+ };
278
+ var LinearBase = class _LinearBase extends IterableElementBase {
279
+ static {
280
+ __name(this, "LinearBase");
281
+ }
282
+ /**
283
+ * Construct a linear container with runtime options.
284
+ * @param options - `{ maxLen?, ... }` bounds/behavior options.
285
+ * @remarks Time O(1), Space O(1)
286
+ */
287
+ constructor(options) {
288
+ super(options);
289
+ if (options) {
290
+ const { maxLen } = options;
291
+ if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
292
+ }
293
+ }
294
+ _maxLen = -1;
295
+ /**
296
+ * Upper bound for length (if positive), or `-1` when unbounded.
297
+ * @returns Maximum allowed length.
298
+ * @remarks Time O(1), Space O(1)
299
+ */
300
+ get maxLen() {
301
+ return this._maxLen;
302
+ }
303
+ /**
304
+ * First index of a value from the left.
305
+ * @param searchElement - Value to match.
306
+ * @param fromIndex - Start position (supports negative index).
307
+ * @returns Index or `-1` if not found.
308
+ * @remarks Time O(n), Space O(1)
309
+ */
310
+ indexOf(searchElement, fromIndex = 0) {
311
+ if (this.length === 0) return -1;
312
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
313
+ if (fromIndex < 0) fromIndex = 0;
314
+ for (let i = fromIndex; i < this.length; i++) {
315
+ const element = this.at(i);
316
+ if (element === searchElement) return i;
317
+ }
318
+ return -1;
319
+ }
320
+ /**
321
+ * Last index of a value from the right.
322
+ * @param searchElement - Value to match.
323
+ * @param fromIndex - Start position (supports negative index).
324
+ * @returns Index or `-1` if not found.
325
+ * @remarks Time O(n), Space O(1)
326
+ */
327
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
328
+ if (this.length === 0) return -1;
329
+ if (fromIndex >= this.length) fromIndex = this.length - 1;
330
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
331
+ for (let i = fromIndex; i >= 0; i--) {
332
+ const element = this.at(i);
333
+ if (element === searchElement) return i;
334
+ }
335
+ return -1;
336
+ }
337
+ /**
338
+ * Find the first index matching a predicate.
339
+ * @param predicate - `(element, index, self) => boolean`.
340
+ * @param thisArg - Optional `this` for callback.
341
+ * @returns Index or `-1`.
342
+ * @remarks Time O(n), Space O(1)
343
+ */
344
+ findIndex(predicate, thisArg) {
345
+ for (let i = 0; i < this.length; i++) {
346
+ const item = this.at(i);
347
+ if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
348
+ }
349
+ return -1;
350
+ }
351
+ /**
352
+ * Concatenate elements and/or containers.
353
+ * @param items - Elements or other containers.
354
+ * @returns New container with combined elements (`this` type).
355
+ * @remarks Time O(sum(length)), Space O(sum(length))
356
+ */
357
+ concat(...items) {
358
+ const newList = this.clone();
359
+ for (const item of items) {
360
+ if (item instanceof _LinearBase) {
361
+ newList.pushMany(item);
362
+ } else {
363
+ newList.push(item);
364
+ }
365
+ }
366
+ return newList;
367
+ }
368
+ /**
369
+ * In-place stable order via array sort semantics.
370
+ * @param compareFn - Comparator `(a, b) => number`.
371
+ * @returns This container.
372
+ * @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
373
+ */
374
+ sort(compareFn) {
375
+ const arr = this.toArray();
376
+ arr.sort(compareFn);
377
+ this.clear();
378
+ for (const item of arr) this.push(item);
379
+ return this;
380
+ }
381
+ /**
382
+ * Remove and/or insert elements at a position (array-compatible).
383
+ * @param start - Start index (supports negative index).
384
+ * @param deleteCount - How many to remove.
385
+ * @param items - Elements to insert.
386
+ * @returns Removed elements as a new list (`this` type).
387
+ * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
388
+ */
389
+ splice(start, deleteCount = 0, ...items) {
390
+ const removedList = this._createInstance();
391
+ start = start < 0 ? this.length + start : start;
392
+ start = Math.max(0, Math.min(start, this.length));
393
+ deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
394
+ for (let i = 0; i < deleteCount; i++) {
395
+ const removed = this.deleteAt(start);
396
+ if (removed !== void 0) {
397
+ removedList.push(removed);
398
+ }
399
+ }
400
+ for (let i = 0; i < items.length; i++) {
401
+ this.addAt(start + i, items[i]);
402
+ }
403
+ return removedList;
404
+ }
405
+ /**
406
+ * Join all elements into a string.
407
+ * @param separator - Separator string.
408
+ * @returns Concatenated string.
409
+ * @remarks Time O(n), Space O(n)
410
+ */
411
+ join(separator = ",") {
412
+ return this.toArray().join(separator);
413
+ }
414
+ /**
415
+ * Snapshot elements into a reversed array.
416
+ * @returns New reversed array.
417
+ * @remarks Time O(n), Space O(n)
418
+ */
419
+ toReversedArray() {
420
+ const array = [];
421
+ for (let i = this.length - 1; i >= 0; i--) {
422
+ array.push(this.at(i));
423
+ }
424
+ return array;
425
+ }
426
+ reduceRight(callbackfn, initialValue) {
427
+ let accumulator = initialValue ?? 0;
428
+ for (let i = this.length - 1; i >= 0; i--) {
429
+ accumulator = callbackfn(accumulator, this.at(i), i, this);
430
+ }
431
+ return accumulator;
432
+ }
433
+ /**
434
+ * Create a shallow copy of a subrange.
435
+ * @param start - Inclusive start (supports negative index).
436
+ * @param end - Exclusive end (supports negative index).
437
+ * @returns New list with the range (`this` type).
438
+ * @remarks Time O(n), Space O(n)
439
+ */
440
+ slice(start = 0, end = this.length) {
441
+ start = start < 0 ? this.length + start : start;
442
+ end = end < 0 ? this.length + end : end;
443
+ const newList = this._createInstance();
444
+ for (let i = start; i < end; i++) {
445
+ newList.push(this.at(i));
446
+ }
447
+ return newList;
448
+ }
449
+ /**
450
+ * Fill a range with a value.
451
+ * @param value - Value to set.
452
+ * @param start - Inclusive start.
453
+ * @param end - Exclusive end.
454
+ * @returns This list.
455
+ * @remarks Time O(n), Space O(1)
456
+ */
457
+ fill(value, start = 0, end = this.length) {
458
+ start = start < 0 ? this.length + start : start;
459
+ end = end < 0 ? this.length + end : end;
460
+ if (start < 0) start = 0;
461
+ if (end > this.length) end = this.length;
462
+ if (start >= end) return this;
463
+ for (let i = start; i < end; i++) {
464
+ this.setAt(i, value);
465
+ }
466
+ return this;
467
+ }
468
+ };
469
+ var LinearLinkedBase = class extends LinearBase {
470
+ static {
471
+ __name(this, "LinearLinkedBase");
472
+ }
473
+ constructor(options) {
474
+ super(options);
475
+ if (options) {
476
+ const { maxLen } = options;
477
+ if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
478
+ }
479
+ }
480
+ /**
481
+ * Linked-list optimized `indexOf` (forwards scan).
482
+ * @param searchElement - Value to match.
483
+ * @param fromIndex - Start position.
484
+ * @returns Index or `-1`.
485
+ * @remarks Time O(n), Space O(1)
486
+ */
487
+ indexOf(searchElement, fromIndex = 0) {
488
+ const iterator = this._getIterator();
489
+ let current = iterator.next();
490
+ let index = 0;
491
+ while (index < fromIndex) {
492
+ current = iterator.next();
493
+ index++;
494
+ }
495
+ while (!current.done) {
496
+ if (current.value === searchElement) return index;
497
+ current = iterator.next();
498
+ index++;
499
+ }
500
+ return -1;
501
+ }
502
+ /**
503
+ * Linked-list optimized `lastIndexOf` (reverse scan).
504
+ * @param searchElement - Value to match.
505
+ * @param fromIndex - Start position.
506
+ * @returns Index or `-1`.
507
+ * @remarks Time O(n), Space O(1)
508
+ */
509
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
510
+ const iterator = this._getReverseIterator();
511
+ let current = iterator.next();
512
+ let index = this.length - 1;
513
+ while (index > fromIndex) {
514
+ current = iterator.next();
515
+ index--;
516
+ }
517
+ while (!current.done) {
518
+ if (current.value === searchElement) return index;
519
+ current = iterator.next();
520
+ index--;
521
+ }
522
+ return -1;
523
+ }
524
+ /**
525
+ * Concatenate lists/elements preserving order.
526
+ * @param items - Elements or `LinearBase` instances.
527
+ * @returns New list with combined elements (`this` type).
528
+ * @remarks Time O(sum(length)), Space O(sum(length))
529
+ */
530
+ concat(...items) {
531
+ const newList = this.clone();
532
+ for (const item of items) {
533
+ if (item instanceof LinearBase) {
534
+ newList.pushMany(item);
535
+ } else {
536
+ newList.push(item);
537
+ }
538
+ }
539
+ return newList;
540
+ }
541
+ /**
542
+ * Slice via forward iteration (no random access required).
543
+ * @param start - Inclusive start (supports negative index).
544
+ * @param end - Exclusive end (supports negative index).
545
+ * @returns New list (`this` type).
546
+ * @remarks Time O(n), Space O(n)
547
+ */
548
+ slice(start = 0, end = this.length) {
549
+ start = start < 0 ? this.length + start : start;
550
+ end = end < 0 ? this.length + end : end;
551
+ const newList = this._createInstance();
552
+ const iterator = this._getIterator();
553
+ let current = iterator.next();
554
+ let c = 0;
555
+ while (c < start) {
556
+ current = iterator.next();
557
+ c++;
558
+ }
559
+ for (let i = start; i < end; i++) {
560
+ newList.push(current.value);
561
+ current = iterator.next();
562
+ }
563
+ return newList;
564
+ }
565
+ /**
566
+ * Splice by walking node iterators from the start index.
567
+ * @param start - Start index.
568
+ * @param deleteCount - How many elements to remove.
569
+ * @param items - Elements to insert after the splice point.
570
+ * @returns Removed elements as a new list (`this` type).
571
+ * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
572
+ */
573
+ splice(start, deleteCount = 0, ...items) {
574
+ const removedList = this._createInstance();
575
+ start = start < 0 ? this.length + start : start;
576
+ start = Math.max(0, Math.min(start, this.length));
577
+ deleteCount = Math.max(0, deleteCount);
578
+ let currentIndex = 0;
579
+ let currentNode = void 0;
580
+ let previousNode = void 0;
581
+ const iterator = this._getNodeIterator();
582
+ for (const node of iterator) {
583
+ if (currentIndex === start) {
584
+ currentNode = node;
585
+ break;
586
+ }
587
+ previousNode = node;
588
+ currentIndex++;
589
+ }
590
+ for (let i = 0; i < deleteCount && currentNode; i++) {
591
+ removedList.push(currentNode.value);
592
+ const nextNode = currentNode.next;
593
+ this.delete(currentNode);
594
+ currentNode = nextNode;
595
+ }
596
+ for (let i = 0; i < items.length; i++) {
597
+ if (previousNode) {
598
+ this.addAfter(previousNode, items[i]);
599
+ previousNode = previousNode.next;
600
+ } else {
601
+ this.addAt(0, items[i]);
602
+ previousNode = this._getNodeIterator().next().value;
603
+ }
604
+ }
605
+ return removedList;
606
+ }
607
+ reduceRight(callbackfn, initialValue) {
608
+ let accumulator = initialValue ?? 0;
609
+ let index = this.length - 1;
610
+ for (const item of this._getReverseIterator()) {
611
+ accumulator = callbackfn(accumulator, item, index--, this);
612
+ }
613
+ return accumulator;
614
+ }
615
+ };
616
+
617
+ // src/data-structures/linked-list/singly-linked-list.ts
618
+ var SinglyLinkedListNode = class extends LinkedListNode {
619
+ static {
620
+ __name(this, "SinglyLinkedListNode");
621
+ }
622
+ /**
623
+ * Create a list node.
624
+ * @remarks Time O(1), Space O(1)
625
+ * @param value - Element value to store.
626
+ * @returns New node instance.
627
+ */
628
+ constructor(value) {
629
+ super(value);
630
+ this._value = value;
631
+ this._next = void 0;
632
+ }
633
+ _next;
634
+ /**
635
+ * Get the next node.
636
+ * @remarks Time O(1), Space O(1)
637
+ * @returns Next node or undefined.
638
+ */
639
+ get next() {
640
+ return this._next;
641
+ }
642
+ /**
643
+ * Set the next node.
644
+ * @remarks Time O(1), Space O(1)
645
+ * @param value - Next node or undefined.
646
+ * @returns void
647
+ */
648
+ set next(value) {
649
+ this._next = value;
650
+ }
651
+ };
652
+ var SinglyLinkedList = class extends LinearLinkedBase {
653
+ static {
654
+ __name(this, "SinglyLinkedList");
655
+ }
656
+ _equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
657
+ /**
658
+ * Create a SinglyLinkedList and optionally bulk-insert elements.
659
+ * @remarks Time O(N), Space O(N)
660
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
661
+ * @param [options] - Options such as maxLen and toElementFn.
662
+ * @returns New SinglyLinkedList instance.
663
+ */
664
+ constructor(elements = [], options) {
665
+ super(options);
666
+ this.pushMany(elements);
667
+ }
668
+ _head;
669
+ /**
670
+ * Get the head node.
671
+ * @remarks Time O(1), Space O(1)
672
+ * @returns Head node or undefined.
673
+ */
674
+ get head() {
675
+ return this._head;
676
+ }
677
+ _tail;
678
+ /**
679
+ * Get the tail node.
680
+ * @remarks Time O(1), Space O(1)
681
+ * @returns Tail node or undefined.
682
+ */
683
+ get tail() {
684
+ return this._tail;
685
+ }
686
+ _length = 0;
687
+ /**
688
+ * Get the number of elements.
689
+ * @remarks Time O(1), Space O(1)
690
+ * @returns Current length.
691
+ */
692
+ get length() {
693
+ return this._length;
694
+ }
695
+ /**
696
+ * Get the first element value.
697
+ * @remarks Time O(1), Space O(1)
698
+ * @returns First element or undefined.
699
+ */
700
+ get first() {
701
+ return this.head?.value;
702
+ }
703
+ /**
704
+ * Get the last element value.
705
+ * @remarks Time O(1), Space O(1)
706
+ * @returns Last element or undefined.
707
+ */
708
+ get last() {
709
+ return this.tail?.value;
710
+ }
711
+ /**
712
+ * Create a new list from an iterable of elements.
713
+ * @remarks Time O(N), Space O(N)
714
+ * @template E
715
+ * @template R
716
+ * @template S
717
+ * @param this - The constructor (subclass) to instantiate.
718
+ * @param data - Iterable of elements to insert.
719
+ * @param [options] - Options forwarded to the constructor.
720
+ * @returns A new list populated with the iterable's elements.
721
+ */
722
+ static from(data, options) {
723
+ const list = new this([], options);
724
+ for (const x of data) list.push(x);
725
+ return list;
726
+ }
727
+ /**
728
+ * Append an element/node to the tail.
729
+ * @remarks Time O(1), Space O(1)
730
+ * @param elementOrNode - Element or node to append.
731
+ * @returns True when appended.
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
+
762
+
763
+ * @example
764
+ * // basic SinglyLinkedList creation and push operation
765
+ * // Create a simple SinglyLinkedList with initial values
766
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
767
+ *
768
+ * // Verify the list maintains insertion order
769
+ * console.log([...list]); // [1, 2, 3, 4, 5];
770
+ *
771
+ * // Check length
772
+ * console.log(list.length); // 5;
773
+ *
774
+ * // Push a new element to the end
775
+ * list.push(6);
776
+ * console.log(list.length); // 6;
777
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
778
+ */
779
+ push(elementOrNode) {
780
+ const newNode = this._ensureNode(elementOrNode);
781
+ if (!this.head) {
782
+ this._head = this._tail = newNode;
783
+ } else {
784
+ this.tail.next = newNode;
785
+ this._tail = newNode;
786
+ }
787
+ this._length++;
788
+ if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
789
+ return true;
790
+ }
791
+ /**
792
+ * Remove and return the tail element.
793
+ * @remarks Time O(N), Space O(1)
794
+ * @returns Removed element or undefined.
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
+
825
+
826
+ * @example
827
+ * // SinglyLinkedList pop and shift operations
828
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
829
+ *
830
+ * // Pop removes from the end
831
+ * const last = list.pop();
832
+ * console.log(last); // 50;
833
+ *
834
+ * // Shift removes from the beginning
835
+ * const first = list.shift();
836
+ * console.log(first); // 10;
837
+ *
838
+ * // Verify remaining elements
839
+ * console.log([...list]); // [20, 30, 40];
840
+ * console.log(list.length); // 3;
841
+ */
842
+ pop() {
843
+ if (!this.head) return void 0;
844
+ if (this.head === this.tail) {
845
+ const value2 = this.head.value;
846
+ this._head = void 0;
847
+ this._tail = void 0;
848
+ this._length--;
849
+ return value2;
850
+ }
851
+ let current = this.head;
852
+ while (current.next && current.next !== this.tail) current = current.next;
853
+ const value = this.tail?.value;
854
+ current.next = void 0;
855
+ this._tail = current;
856
+ this._length--;
857
+ return value;
858
+ }
859
+ /**
860
+ * Remove and return the head element.
861
+ * @remarks Time O(1), Space O(1)
862
+ * @returns Removed element or undefined.
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
+
893
+
894
+ * @example
895
+ * // Remove from the front
896
+ * const list = new SinglyLinkedList<number>([10, 20, 30]);
897
+ * console.log(list.shift()); // 10;
898
+ * console.log(list.length); // 2;
899
+ */
900
+ shift() {
901
+ if (!this.head) return void 0;
902
+ const removed = this.head;
903
+ this._head = this.head.next;
904
+ if (!this._head) this._tail = void 0;
905
+ this._length--;
906
+ return removed.value;
907
+ }
908
+ /**
909
+ * Prepend an element/node to the head.
910
+ * @remarks Time O(1), Space O(1)
911
+ * @param elementOrNode - Element or node to prepend.
912
+ * @returns True when prepended.
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
+
943
+
944
+ * @example
945
+ * // SinglyLinkedList unshift and forward traversal
946
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
947
+ *
948
+ * // Unshift adds to the beginning
949
+ * list.unshift(10);
950
+ * console.log([...list]); // [10, 20, 30, 40];
951
+ *
952
+ * // Access elements (forward traversal only for singly linked)
953
+ * const second = list.at(1);
954
+ * console.log(second); // 20;
955
+ *
956
+ * // SinglyLinkedList allows forward iteration only
957
+ * const elements: number[] = [];
958
+ * for (const item of list) {
959
+ * elements.push(item);
960
+ * }
961
+ * console.log(elements); // [10, 20, 30, 40];
962
+ *
963
+ * console.log(list.length); // 4;
964
+ */
965
+ unshift(elementOrNode) {
966
+ const newNode = this._ensureNode(elementOrNode);
967
+ if (!this.head) {
968
+ this._head = this._tail = newNode;
969
+ } else {
970
+ newNode.next = this.head;
971
+ this._head = newNode;
972
+ }
973
+ this._length++;
974
+ return true;
975
+ }
976
+ /**
977
+ * Append a sequence of elements/nodes.
978
+ * @remarks Time O(N), Space O(1)
979
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
980
+ * @returns Array of per-element success flags.
981
+ */
982
+ pushMany(elements) {
983
+ const ans = [];
984
+ for (const el of elements) {
985
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
986
+ else ans.push(this.push(el));
987
+ }
988
+ return ans;
989
+ }
990
+ /**
991
+ * Prepend a sequence of elements/nodes.
992
+ * @remarks Time O(N), Space O(1)
993
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
994
+ * @returns Array of per-element success flags.
995
+ */
996
+ unshiftMany(elements) {
997
+ const ans = [];
998
+ for (const el of elements) {
999
+ if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
1000
+ else ans.push(this.unshift(el));
1001
+ }
1002
+ return ans;
1003
+ }
1004
+ /**
1005
+ * Find the first value matching a predicate (by node).
1006
+ * @remarks Time O(N), Space O(1)
1007
+ * @param elementNodeOrPredicate - Element, node, or node predicate to match.
1008
+ * @returns Matched value or undefined.
1009
+ */
1010
+ search(elementNodeOrPredicate) {
1011
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1012
+ let current = this.head;
1013
+ while (current) {
1014
+ if (predicate(current)) return current.value;
1015
+ current = current.next;
1016
+ }
1017
+ return void 0;
1018
+ }
1019
+ /**
1020
+ * Get the element at a given index.
1021
+ * @remarks Time O(N), Space O(1)
1022
+ * @param index - Zero-based index.
1023
+ * @returns Element or undefined.
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
+
1054
+
1055
+ * @example
1056
+ * // Access element by index
1057
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
1058
+ * console.log(list.at(0)); // 'a';
1059
+ * console.log(list.at(2)); // 'c';
1060
+ * console.log(list.at(3)); // 'd';
1061
+ */
1062
+ at(index) {
1063
+ if (index < 0 || index >= this._length) return void 0;
1064
+ let current = this.head;
1065
+ for (let i = 0; i < index && current; i++) current = current.next;
1066
+ return current?.value;
1067
+ }
1068
+ /**
1069
+ * Type guard: check whether the input is a SinglyLinkedListNode.
1070
+ * @remarks Time O(1), Space O(1)
1071
+ * @param elementNodeOrPredicate - Element, node, or predicate.
1072
+ * @returns True if the value is a SinglyLinkedListNode.
1073
+ */
1074
+ isNode(elementNodeOrPredicate) {
1075
+ return elementNodeOrPredicate instanceof SinglyLinkedListNode;
1076
+ }
1077
+ /**
1078
+ * Get the node reference at a given index.
1079
+ * @remarks Time O(N), Space O(1)
1080
+ * @param index - Zero-based index.
1081
+ * @returns Node or undefined.
1082
+
1083
+
1084
+
1085
+
1086
+
1087
+
1088
+
1089
+
1090
+
1091
+
1092
+
1093
+
1094
+
1095
+
1096
+
1097
+
1098
+
1099
+
1100
+
1101
+
1102
+
1103
+
1104
+
1105
+
1106
+
1107
+
1108
+
1109
+
1110
+ * @example
1111
+ * // Get node at index
1112
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1113
+ * console.log(list.getNodeAt(1)?.value); // 'b';
1114
+ */
1115
+ getNodeAt(index) {
1116
+ if (index < 0 || index >= this._length) return void 0;
1117
+ let current = this.head;
1118
+ for (let i = 0; i < index && current; i++) current = current.next;
1119
+ return current;
1120
+ }
1121
+ /**
1122
+ * Delete the element at an index.
1123
+ * @remarks Time O(N), Space O(1)
1124
+ * @param index - Zero-based index.
1125
+ * @returns Removed element or undefined.
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
+
1153
+
1154
+ * @example
1155
+ * // Remove by index
1156
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1157
+ * list.deleteAt(1);
1158
+ * console.log(list.toArray()); // ['a', 'c'];
1159
+ */
1160
+ deleteAt(index) {
1161
+ if (index < 0 || index >= this._length) return void 0;
1162
+ if (index === 0) return this.shift();
1163
+ const targetNode = this.getNodeAt(index);
1164
+ const prevNode = this._getPrevNode(targetNode);
1165
+ const value = targetNode.value;
1166
+ prevNode.next = targetNode.next;
1167
+ if (targetNode === this.tail) this._tail = prevNode;
1168
+ this._length--;
1169
+ return value;
1170
+ }
1171
+ /**
1172
+ * Delete the first match by value/node.
1173
+ * @remarks Time O(N), Space O(1)
1174
+ * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
1175
+ * @returns True if removed.
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
+
1203
+
1204
+ * @example
1205
+ * // Remove first occurrence
1206
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
1207
+ * list.delete(2);
1208
+ * console.log(list.toArray()); // [1, 3, 2];
1209
+ */
1210
+ delete(elementOrNode) {
1211
+ if (elementOrNode === void 0 || !this.head) return false;
1212
+ const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
1213
+ if (!node) return false;
1214
+ const prevNode = this._getPrevNode(node);
1215
+ if (!prevNode) {
1216
+ this._head = node.next;
1217
+ if (node === this.tail) this._tail = void 0;
1218
+ } else {
1219
+ prevNode.next = node.next;
1220
+ if (node === this.tail) this._tail = prevNode;
1221
+ }
1222
+ this._length--;
1223
+ return true;
1224
+ }
1225
+ /**
1226
+ * Insert a new element/node at an index, shifting following nodes.
1227
+ * @remarks Time O(N), Space O(1)
1228
+ * @param index - Zero-based index.
1229
+ * @param newElementOrNode - Element or node to insert.
1230
+ * @returns True if inserted.
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
+
1258
+
1259
+ * @example
1260
+ * // Insert at index
1261
+ * const list = new SinglyLinkedList<number>([1, 3]);
1262
+ * list.addAt(1, 2);
1263
+ * console.log(list.toArray()); // [1, 2, 3];
1264
+ */
1265
+ addAt(index, newElementOrNode) {
1266
+ if (index < 0 || index > this._length) return false;
1267
+ if (index === 0) return this.unshift(newElementOrNode);
1268
+ if (index === this._length) return this.push(newElementOrNode);
1269
+ const newNode = this._ensureNode(newElementOrNode);
1270
+ const prevNode = this.getNodeAt(index - 1);
1271
+ newNode.next = prevNode.next;
1272
+ prevNode.next = newNode;
1273
+ this._length++;
1274
+ return true;
1275
+ }
1276
+ /**
1277
+ * Set the element value at an index.
1278
+ * @remarks Time O(N), Space O(1)
1279
+ * @param index - Zero-based index.
1280
+ * @param value - New value.
1281
+ * @returns True if updated.
1282
+ */
1283
+ setAt(index, value) {
1284
+ const node = this.getNodeAt(index);
1285
+ if (!node) return false;
1286
+ node.value = value;
1287
+ return true;
1288
+ }
1289
+ /**
1290
+ * Check whether the list is empty.
1291
+ * @remarks Time O(1), Space O(1)
1292
+ * @returns True if length is 0.
1293
+
1294
+
1295
+
1296
+
1297
+
1298
+
1299
+
1300
+
1301
+
1302
+
1303
+
1304
+
1305
+
1306
+
1307
+
1308
+
1309
+
1310
+
1311
+
1312
+
1313
+
1314
+
1315
+
1316
+
1317
+
1318
+
1319
+
1320
+
1321
+
1322
+ * @example
1323
+ * // Check empty
1324
+ * console.log(new SinglyLinkedList().isEmpty()); // true;
1325
+ */
1326
+ isEmpty() {
1327
+ return this._length === 0;
1328
+ }
1329
+ /**
1330
+ * Remove all nodes and reset length.
1331
+ * @remarks Time O(N), Space O(1)
1332
+ * @returns void
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
+
1361
+
1362
+ * @example
1363
+ * // Remove all
1364
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1365
+ * list.clear();
1366
+ * console.log(list.isEmpty()); // true;
1367
+ */
1368
+ clear() {
1369
+ this._head = void 0;
1370
+ this._tail = void 0;
1371
+ this._length = 0;
1372
+ }
1373
+ /**
1374
+ * Reverse the list in place.
1375
+ * @remarks Time O(N), Space O(1)
1376
+ * @returns This list.
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
+
1407
+
1408
+ * @example
1409
+ * // Reverse the list in-place
1410
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
1411
+ * list.reverse();
1412
+ * console.log([...list]); // [4, 3, 2, 1];
1413
+ */
1414
+ reverse() {
1415
+ if (!this.head || this.head === this.tail) return this;
1416
+ let prev;
1417
+ let current = this.head;
1418
+ let next;
1419
+ while (current) {
1420
+ next = current.next;
1421
+ current.next = prev;
1422
+ prev = current;
1423
+ current = next;
1424
+ }
1425
+ [this._head, this._tail] = [this.tail, this.head];
1426
+ return this;
1427
+ }
1428
+ /**
1429
+ * Find a node by value, reference, or predicate.
1430
+ * @remarks Time O(N), Space O(1)
1431
+ * @param [elementNodeOrPredicate] - Element, node, or node predicate to match.
1432
+ * @returns Matching node or undefined.
1433
+ */
1434
+ getNode(elementNodeOrPredicate) {
1435
+ if (elementNodeOrPredicate === void 0) return;
1436
+ if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
1437
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1438
+ let current = this.head;
1439
+ while (current) {
1440
+ if (predicate(current)) return current;
1441
+ current = current.next;
1442
+ }
1443
+ return void 0;
1444
+ }
1445
+ /**
1446
+ * Insert a new element/node before an existing one.
1447
+ * @remarks Time O(N), Space O(1)
1448
+ * @param existingElementOrNode - Existing element or node.
1449
+ * @param newElementOrNode - Element or node to insert.
1450
+ * @returns True if inserted.
1451
+ */
1452
+ addBefore(existingElementOrNode, newElementOrNode) {
1453
+ const existingNode = this.getNode(existingElementOrNode);
1454
+ if (!existingNode) return false;
1455
+ const prevNode = this._getPrevNode(existingNode);
1456
+ const newNode = this._ensureNode(newElementOrNode);
1457
+ if (!prevNode) {
1458
+ newNode.next = this._head;
1459
+ this._head = newNode;
1460
+ if (!this._tail) this._tail = newNode;
1461
+ this._length++;
1462
+ } else {
1463
+ prevNode.next = newNode;
1464
+ newNode.next = existingNode;
1465
+ this._length++;
1466
+ }
1467
+ return true;
1468
+ }
1469
+ /**
1470
+ * Insert a new element/node after an existing one.
1471
+ * @remarks Time O(N), Space O(1)
1472
+ * @param existingElementOrNode - Existing element or node.
1473
+ * @param newElementOrNode - Element or node to insert.
1474
+ * @returns True if inserted.
1475
+ */
1476
+ addAfter(existingElementOrNode, newElementOrNode) {
1477
+ const existingNode = this.getNode(existingElementOrNode);
1478
+ if (!existingNode) return false;
1479
+ const newNode = this._ensureNode(newElementOrNode);
1480
+ newNode.next = existingNode.next;
1481
+ existingNode.next = newNode;
1482
+ if (existingNode === this.tail) this._tail = newNode;
1483
+ this._length++;
1484
+ return true;
1485
+ }
1486
+ /**
1487
+ * Remove and/or insert elements at a position (array-like behavior).
1488
+ * @remarks Time O(N + M), Space O(M)
1489
+ * @param start - Start index (clamped to [0, length]).
1490
+ * @param [deleteCount] - Number of elements to remove (default 0).
1491
+ * @param [items] - Elements to insert after `start`.
1492
+ * @returns A new list containing the removed elements (typed as `this`).
1493
+ */
1494
+ splice(start, deleteCount = 0, ...items) {
1495
+ start = Math.max(0, Math.min(start, this.length));
1496
+ deleteCount = Math.max(0, deleteCount);
1497
+ const removedList = this._createInstance();
1498
+ const prevNode = start === 0 ? void 0 : this.getNodeAt(start - 1);
1499
+ let cur = prevNode ? prevNode.next : this.head;
1500
+ let removedCount = 0;
1501
+ while (removedCount < deleteCount && cur) {
1502
+ removedList.push(cur.value);
1503
+ cur = cur.next;
1504
+ removedCount++;
1505
+ }
1506
+ const afterNode = cur;
1507
+ if (prevNode) {
1508
+ prevNode.next = afterNode;
1509
+ } else {
1510
+ this._head = afterNode;
1511
+ }
1512
+ if (!afterNode) this._tail = prevNode;
1513
+ if (items.length > 0) {
1514
+ let firstInserted;
1515
+ let lastInserted;
1516
+ for (const it of items) {
1517
+ const node = this._ensureNode(it);
1518
+ if (!firstInserted) firstInserted = node;
1519
+ if (lastInserted) lastInserted.next = node;
1520
+ lastInserted = node;
1521
+ }
1522
+ if (prevNode) prevNode.next = firstInserted;
1523
+ else this._head = firstInserted;
1524
+ lastInserted.next = afterNode;
1525
+ if (!afterNode) this._tail = lastInserted;
1526
+ }
1527
+ this._length += items.length - removedCount;
1528
+ if (this._length === 0) {
1529
+ this._head = void 0;
1530
+ this._tail = void 0;
1531
+ }
1532
+ return removedList;
1533
+ }
1534
+ /**
1535
+ * Count how many nodes match a value/node/predicate.
1536
+ * @remarks Time O(N), Space O(1)
1537
+ * @param elementOrNode - Element, node, or node predicate to match.
1538
+ * @returns Number of matches in the list.
1539
+ */
1540
+ countOccurrences(elementOrNode) {
1541
+ const predicate = elementOrPredicate(elementOrNode, this._equals);
1542
+ let count = 0;
1543
+ let current = this.head;
1544
+ while (current) {
1545
+ if (predicate(current)) count++;
1546
+ current = current.next;
1547
+ }
1548
+ return count;
1549
+ }
1550
+ /**
1551
+ * Set the equality comparator used to compare values.
1552
+ * @remarks Time O(1), Space O(1)
1553
+ * @param equals - Equality predicate (a, b) → boolean.
1554
+ * @returns This list.
1555
+ */
1556
+ setEquality(equals) {
1557
+ this._equals = equals;
1558
+ return this;
1559
+ }
1560
+ /**
1561
+ * Delete the first node whose value matches a predicate.
1562
+ * @remarks Time O(N), Space O(1)
1563
+ * @param predicate - Predicate (value, index, list) → boolean to decide deletion.
1564
+ * @returns True if a node was removed.
1565
+ */
1566
+ deleteWhere(predicate) {
1567
+ let prev;
1568
+ let current = this.head;
1569
+ let i = 0;
1570
+ while (current) {
1571
+ if (predicate(current.value, i++, this)) {
1572
+ if (!prev) {
1573
+ this._head = current.next;
1574
+ if (current === this._tail) this._tail = void 0;
1575
+ } else {
1576
+ prev.next = current.next;
1577
+ if (current === this._tail) this._tail = prev;
1578
+ }
1579
+ this._length--;
1580
+ return true;
1581
+ }
1582
+ prev = current;
1583
+ current = current.next;
1584
+ }
1585
+ return false;
1586
+ }
1587
+ /**
1588
+ * Deep clone this list (values are copied by reference).
1589
+ * @remarks Time O(N), Space O(N)
1590
+ * @returns A new list with the same element sequence.
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
+
1619
+
1620
+ * @example
1621
+ * // Deep copy
1622
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1623
+ * const copy = list.clone();
1624
+ * copy.pop();
1625
+ * console.log(list.length); // 3;
1626
+ * console.log(copy.length); // 2;
1627
+ */
1628
+ clone() {
1629
+ const out = this._createInstance();
1630
+ for (const v of this) out.push(v);
1631
+ return out;
1632
+ }
1633
+ /**
1634
+ * Filter values into a new list of the same class.
1635
+ * @remarks Time O(N), Space O(N)
1636
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
1637
+ * @param [thisArg] - Value for `this` inside the callback.
1638
+ * @returns A new list with kept values.
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
+
1669
+
1670
+ * @example
1671
+ * // SinglyLinkedList filter and map operations
1672
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
1673
+ *
1674
+ * // Filter even numbers
1675
+ * const filtered = list.filter(value => value % 2 === 0);
1676
+ * console.log(filtered.length); // 2;
1677
+ *
1678
+ * // Map to double values
1679
+ * const doubled = list.map(value => value * 2);
1680
+ * console.log(doubled.length); // 5;
1681
+ *
1682
+ * // Use reduce to sum
1683
+ * const sum = list.reduce((acc, value) => acc + value, 0);
1684
+ * console.log(sum); // 15;
1685
+ */
1686
+ filter(callback, thisArg) {
1687
+ const out = this._createInstance();
1688
+ let index = 0;
1689
+ for (const value of this) if (callback.call(thisArg, value, index++, this)) out.push(value);
1690
+ return out;
1691
+ }
1692
+ /**
1693
+ * Map values into a new list of the same class.
1694
+ * @remarks Time O(N), Space O(N)
1695
+ * @param callback - Mapping function (value, index, list) → newValue.
1696
+ * @param [thisArg] - Value for `this` inside the callback.
1697
+ * @returns A new list with mapped values.
1698
+ */
1699
+ mapSame(callback, thisArg) {
1700
+ const out = this._createInstance();
1701
+ let index = 0;
1702
+ for (const value of this) {
1703
+ const mv = thisArg === void 0 ? callback(value, index++, this) : callback.call(thisArg, value, index++, this);
1704
+ out.push(mv);
1705
+ }
1706
+ return out;
1707
+ }
1708
+ /**
1709
+ * Map values into a new list (possibly different element type).
1710
+ * @remarks Time O(N), Space O(N)
1711
+ * @template EM
1712
+ * @template RM
1713
+ * @param callback - Mapping function (value, index, list) → newElement.
1714
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1715
+ * @param [thisArg] - Value for `this` inside the callback.
1716
+ * @returns A new SinglyLinkedList with mapped values.
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
+
1747
+
1748
+ * @example
1749
+ * // Transform elements
1750
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1751
+ * const doubled = list.map(n => n * 2);
1752
+ * console.log([...doubled]); // [2, 4, 6];
1753
+ */
1754
+ map(callback, options, thisArg) {
1755
+ const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
1756
+ let index = 0;
1757
+ for (const value of this) out.push(callback.call(thisArg, value, index++, this));
1758
+ return out;
1759
+ }
1760
+ /**
1761
+ * (Protected) Create a node from a value.
1762
+ * @remarks Time O(1), Space O(1)
1763
+ * @param value - Value to wrap in a node.
1764
+ * @returns A new SinglyLinkedListNode instance.
1765
+ */
1766
+ createNode(value) {
1767
+ return new SinglyLinkedListNode(value);
1768
+ }
1769
+ /**
1770
+ * (Protected) Check if input is a node predicate function.
1771
+ * @remarks Time O(1), Space O(1)
1772
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
1773
+ * @returns True if input is a predicate function.
1774
+ */
1775
+ _isPredicate(elementNodeOrPredicate) {
1776
+ return typeof elementNodeOrPredicate === "function";
1777
+ }
1778
+ /**
1779
+ * (Protected) Normalize input into a node instance.
1780
+ * @remarks Time O(1), Space O(1)
1781
+ * @param elementOrNode - Element or node.
1782
+ * @returns A SinglyLinkedListNode for the provided input.
1783
+ */
1784
+ _ensureNode(elementOrNode) {
1785
+ if (this.isNode(elementOrNode)) return elementOrNode;
1786
+ return this.createNode(elementOrNode);
1787
+ }
1788
+ /**
1789
+ * (Protected) Normalize input into a node predicate.
1790
+ * @remarks Time O(1), Space O(1)
1791
+ * @param elementNodeOrPredicate - Element, node, or predicate.
1792
+ * @returns A predicate taking a node and returning true/false.
1793
+ */
1794
+ _ensurePredicate(elementNodeOrPredicate) {
1795
+ if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
1796
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
1797
+ const value = elementNodeOrPredicate;
1798
+ return (node) => this._equals(node.value, value);
1799
+ }
1800
+ /**
1801
+ * (Protected) Get the previous node of a given node.
1802
+ * @remarks Time O(N), Space O(1)
1803
+ * @param node - A node in the list.
1804
+ * @returns Previous node or undefined.
1805
+ */
1806
+ _getPrevNode(node) {
1807
+ if (!this.head || this.head === node) return void 0;
1808
+ let current = this.head;
1809
+ while (current.next && current.next !== node) current = current.next;
1810
+ return current.next === node ? current : void 0;
1811
+ }
1812
+ /**
1813
+ * (Protected) Iterate values from head to tail.
1814
+ * @remarks Time O(N), Space O(1)
1815
+ * @returns Iterator of values (E).
1816
+ */
1817
+ *_getIterator() {
1818
+ let current = this.head;
1819
+ while (current) {
1820
+ yield current.value;
1821
+ current = current.next;
1822
+ }
1823
+ }
1824
+ /**
1825
+ * (Protected) Iterate values from tail to head.
1826
+ * @remarks Time O(N), Space O(N)
1827
+ * @returns Iterator of values (E).
1828
+ */
1829
+ *_getReverseIterator() {
1830
+ const reversedArr = [...this].reverse();
1831
+ for (const item of reversedArr) yield item;
1832
+ }
1833
+ /**
1834
+ * (Protected) Iterate nodes from head to tail.
1835
+ * @remarks Time O(N), Space O(1)
1836
+ * @returns Iterator of nodes.
1837
+ */
1838
+ *_getNodeIterator() {
1839
+ let current = this.head;
1840
+ while (current) {
1841
+ yield current;
1842
+ current = current.next;
1843
+ }
1844
+ }
1845
+ /**
1846
+ * (Protected) Create an empty instance of the same concrete class.
1847
+ * @remarks Time O(1), Space O(1)
1848
+ * @param [options] - Options forwarded to the constructor.
1849
+ * @returns An empty like-kind list instance.
1850
+ */
1851
+ _createInstance(options) {
1852
+ const Ctor = this.constructor;
1853
+ return new Ctor([], options);
1854
+ }
1855
+ /**
1856
+ * (Protected) Create a like-kind instance and seed it from an iterable.
1857
+ * @remarks Time O(N), Space O(N)
1858
+ * @template EM
1859
+ * @template RM
1860
+ * @param [elements] - Iterable used to seed the new list.
1861
+ * @param [options] - Options forwarded to the constructor.
1862
+ * @returns A like-kind SinglyLinkedList instance.
1863
+ */
1864
+ _createLike(elements = [], options) {
1865
+ const Ctor = this.constructor;
1866
+ return new Ctor(elements, options);
1867
+ }
1868
+ /**
1869
+ * (Protected) Spawn an empty like-kind list instance.
1870
+ * @remarks Time O(1), Space O(1)
1871
+ * @template EM
1872
+ * @template RM
1873
+ * @param [options] - Options forwarded to the constructor.
1874
+ * @returns An empty like-kind SinglyLinkedList instance.
1875
+ */
1876
+ _spawnLike(options) {
1877
+ return this._createLike([], options);
1878
+ }
1879
+ };
1880
+ function elementOrPredicate(input, equals) {
1881
+ if (input instanceof SinglyLinkedListNode) return (node) => node === input;
1882
+ if (typeof input === "function") return input;
1883
+ const value = input;
1884
+ return (node) => equals(node.value, value);
1885
+ }
1886
+ __name(elementOrPredicate, "elementOrPredicate");
1887
+
1888
+ // src/data-structures/linked-list/doubly-linked-list.ts
1889
+ var DoublyLinkedListNode = class extends LinkedListNode {
1890
+ static {
1891
+ __name(this, "DoublyLinkedListNode");
1892
+ }
1893
+ /**
1894
+ * Create a node.
1895
+ * @remarks Time O(1), Space O(1)
1896
+ * @param value - Element value to store.
1897
+ * @returns New node instance.
1898
+ */
1899
+ constructor(value) {
1900
+ super(value);
1901
+ this._value = value;
1902
+ this._next = void 0;
1903
+ this._prev = void 0;
1904
+ }
1905
+ _next;
1906
+ /**
1907
+ * Get the next node link.
1908
+ * @remarks Time O(1), Space O(1)
1909
+ * @returns Next node or undefined.
1910
+ */
1911
+ get next() {
1912
+ return this._next;
1913
+ }
1914
+ /**
1915
+ * Set the next node link.
1916
+ * @remarks Time O(1), Space O(1)
1917
+ * @param value - Next node or undefined.
1918
+ * @returns void
1919
+ */
1920
+ set next(value) {
1921
+ this._next = value;
1922
+ }
1923
+ _prev;
1924
+ /**
1925
+ * Get the previous node link.
1926
+ * @remarks Time O(1), Space O(1)
1927
+ * @returns Previous node or undefined.
1928
+ */
1929
+ get prev() {
1930
+ return this._prev;
1931
+ }
1932
+ /**
1933
+ * Set the previous node link.
1934
+ * @remarks Time O(1), Space O(1)
1935
+ * @param value - Previous node or undefined.
1936
+ * @returns void
1937
+ */
1938
+ set prev(value) {
1939
+ this._prev = value;
1940
+ }
1941
+ };
1942
+ var DoublyLinkedList = class extends LinearLinkedBase {
1943
+ static {
1944
+ __name(this, "DoublyLinkedList");
1945
+ }
1946
+ _equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
1947
+ /**
1948
+ * Create a DoublyLinkedList and optionally bulk-insert elements.
1949
+ * @remarks Time O(N), Space O(N)
1950
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
1951
+ * @param [options] - Options such as maxLen and toElementFn.
1952
+ * @returns New DoublyLinkedList instance.
1953
+ */
1954
+ constructor(elements = [], options) {
1955
+ super(options);
1956
+ this._head = void 0;
1957
+ this._tail = void 0;
1958
+ this._length = 0;
1959
+ if (options?.maxLen && Number.isInteger(options.maxLen) && options.maxLen > 0) {
1960
+ this._maxLen = options.maxLen;
1961
+ }
1962
+ this.pushMany(elements);
1963
+ }
1964
+ _head;
1965
+ /**
1966
+ * Get the head node.
1967
+ * @remarks Time O(1), Space O(1)
1968
+ * @returns Head node or undefined.
1969
+ */
1970
+ get head() {
1971
+ return this._head;
1972
+ }
1973
+ _tail;
1974
+ /**
1975
+ * Get the tail node.
1976
+ * @remarks Time O(1), Space O(1)
1977
+ * @returns Tail node or undefined.
1978
+ */
1979
+ get tail() {
1980
+ return this._tail;
1981
+ }
1982
+ _length = 0;
1983
+ /**
1984
+ * Get the number of elements.
1985
+ * @remarks Time O(1), Space O(1)
1986
+ * @returns Current length.
1987
+ */
1988
+ get length() {
1989
+ return this._length;
1990
+ }
1991
+ /**
1992
+ * Get the first element value.
1993
+ * @remarks Time O(1), Space O(1)
1994
+ * @returns First element or undefined.
1995
+ */
1996
+ get first() {
1997
+ return this.head?.value;
1998
+ }
1999
+ /**
2000
+ * Get the last element value.
2001
+ * @remarks Time O(1), Space O(1)
2002
+ * @returns Last element or undefined.
2003
+ */
2004
+ get last() {
2005
+ return this.tail?.value;
2006
+ }
2007
+ /**
2008
+ * Create a new list from an array of elements.
2009
+ * @remarks Time O(N), Space O(N)
2010
+ * @template E
2011
+ * @template R
2012
+ * @param this - The constructor (subclass) to instantiate.
2013
+ * @param data - Array of elements to insert.
2014
+ * @returns A new list populated with the array's elements.
2015
+ */
2016
+ static fromArray(data) {
2017
+ return new this(data);
2018
+ }
2019
+ /**
2020
+ * Type guard: check whether the input is a DoublyLinkedListNode.
2021
+ * @remarks Time O(1), Space O(1)
2022
+ * @param elementNodeOrPredicate - Element, node, or predicate.
2023
+ * @returns True if the value is a DoublyLinkedListNode.
2024
+ */
2025
+ isNode(elementNodeOrPredicate) {
2026
+ return elementNodeOrPredicate instanceof DoublyLinkedListNode;
2027
+ }
2028
+ /**
2029
+ * Append an element/node to the tail.
2030
+ * @remarks Time O(1), Space O(1)
2031
+ * @param elementOrNode - Element or node to append.
2032
+ * @returns True when appended.
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
+
2063
+
2064
+ * @example
2065
+ * // basic DoublyLinkedList creation and push operation
2066
+ * // Create a simple DoublyLinkedList with initial values
2067
+ * const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
2068
+ *
2069
+ * // Verify the list maintains insertion order
2070
+ * console.log([...list]); // [1, 2, 3, 4, 5];
2071
+ *
2072
+ * // Check length
2073
+ * console.log(list.length); // 5;
2074
+ *
2075
+ * // Push a new element to the end
2076
+ * list.push(6);
2077
+ * console.log(list.length); // 6;
2078
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
2079
+ */
2080
+ push(elementOrNode) {
2081
+ const newNode = this._ensureNode(elementOrNode);
2082
+ if (!this.head) {
2083
+ this._head = newNode;
2084
+ this._tail = newNode;
2085
+ } else {
2086
+ newNode.prev = this.tail;
2087
+ this.tail.next = newNode;
2088
+ this._tail = newNode;
2089
+ }
2090
+ this._length++;
2091
+ if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
2092
+ return true;
2093
+ }
2094
+ /**
2095
+ * Remove and return the tail element.
2096
+ * @remarks Time O(1), Space O(1)
2097
+ * @returns Removed element or undefined.
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
+
2128
+
2129
+ * @example
2130
+ * // DoublyLinkedList pop and shift operations
2131
+ * const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
2132
+ *
2133
+ * // Pop removes from the end
2134
+ * const last = list.pop();
2135
+ * console.log(last); // 50;
2136
+ *
2137
+ * // Shift removes from the beginning
2138
+ * const first = list.shift();
2139
+ * console.log(first); // 10;
2140
+ *
2141
+ * // Verify remaining elements
2142
+ * console.log([...list]); // [20, 30, 40];
2143
+ * console.log(list.length); // 3;
2144
+ */
2145
+ pop() {
2146
+ if (!this.tail) return void 0;
2147
+ const removed = this.tail;
2148
+ if (this.head === this.tail) {
2149
+ this._head = void 0;
2150
+ this._tail = void 0;
2151
+ } else {
2152
+ this._tail = removed.prev;
2153
+ this.tail.next = void 0;
2154
+ }
2155
+ this._length--;
2156
+ return removed.value;
2157
+ }
2158
+ /**
2159
+ * Remove and return the head element.
2160
+ * @remarks Time O(1), Space O(1)
2161
+ * @returns Removed element or undefined.
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
+
2192
+
2193
+ * @example
2194
+ * // Remove from the front
2195
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
2196
+ * console.log(list.shift()); // 10;
2197
+ * console.log(list.first); // 20;
2198
+ */
2199
+ shift() {
2200
+ if (!this.head) return void 0;
2201
+ const removed = this.head;
2202
+ if (this.head === this.tail) {
2203
+ this._head = void 0;
2204
+ this._tail = void 0;
2205
+ } else {
2206
+ this._head = removed.next;
2207
+ this.head.prev = void 0;
2208
+ }
2209
+ this._length--;
2210
+ return removed.value;
2211
+ }
2212
+ /**
2213
+ * Prepend an element/node to the head.
2214
+ * @remarks Time O(1), Space O(1)
2215
+ * @param elementOrNode - Element or node to prepend.
2216
+ * @returns True when prepended.
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
+
2247
+
2248
+ * @example
2249
+ * // Add to the front
2250
+ * const list = new DoublyLinkedList<number>([2, 3]);
2251
+ * list.unshift(1);
2252
+ * console.log([...list]); // [1, 2, 3];
2253
+ */
2254
+ unshift(elementOrNode) {
2255
+ const newNode = this._ensureNode(elementOrNode);
2256
+ if (!this.head) {
2257
+ this._head = newNode;
2258
+ this._tail = newNode;
2259
+ } else {
2260
+ newNode.next = this.head;
2261
+ this.head.prev = newNode;
2262
+ this._head = newNode;
2263
+ }
2264
+ this._length++;
2265
+ if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
2266
+ return true;
2267
+ }
2268
+ /**
2269
+ * Append a sequence of elements/nodes.
2270
+ * @remarks Time O(N), Space O(1)
2271
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
2272
+ * @returns Array of per-element success flags.
2273
+ */
2274
+ pushMany(elements) {
2275
+ const ans = [];
2276
+ for (const el of elements) {
2277
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
2278
+ else ans.push(this.push(el));
2279
+ }
2280
+ return ans;
2281
+ }
2282
+ /**
2283
+ * Prepend a sequence of elements/nodes.
2284
+ * @remarks Time O(N), Space O(1)
2285
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
2286
+ * @returns Array of per-element success flags.
2287
+ */
2288
+ unshiftMany(elements) {
2289
+ const ans = [];
2290
+ for (const el of elements) {
2291
+ if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el)));
2292
+ else ans.push(this.unshift(el));
2293
+ }
2294
+ return ans;
2295
+ }
2296
+ /**
2297
+ * Get the element at a given index.
2298
+ * @remarks Time O(N), Space O(1)
2299
+ * @param index - Zero-based index.
2300
+ * @returns Element or undefined.
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
+
2331
+
2332
+ * @example
2333
+ * // Access by index
2334
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2335
+ * console.log(list.at(1)); // 'b';
2336
+ * console.log(list.at(2)); // 'c';
2337
+ */
2338
+ at(index) {
2339
+ if (index < 0 || index >= this._length) return void 0;
2340
+ let current = this.head;
2341
+ for (let i = 0; i < index && current; i++) current = current.next;
2342
+ return current?.value;
2343
+ }
2344
+ /**
2345
+ * Get the node reference at a given index.
2346
+ * @remarks Time O(N), Space O(1)
2347
+ * @param index - Zero-based index.
2348
+ * @returns Node or undefined.
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
+
2376
+
2377
+ * @example
2378
+ * // Get node at index
2379
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2380
+ * console.log(list.getNodeAt(1)?.value); // 'b';
2381
+ */
2382
+ getNodeAt(index) {
2383
+ if (index < 0 || index >= this._length) return void 0;
2384
+ let current = this.head;
2385
+ for (let i = 0; i < index && current; i++) current = current.next;
2386
+ return current;
2387
+ }
2388
+ /**
2389
+ * Find a node by value, reference, or predicate.
2390
+ * @remarks Time O(N), Space O(1)
2391
+ * @param [elementNodeOrPredicate] - Element, node, or predicate to match.
2392
+ * @returns Matching node or undefined.
2393
+ */
2394
+ getNode(elementNodeOrPredicate) {
2395
+ if (elementNodeOrPredicate === void 0) return;
2396
+ if (this.isNode(elementNodeOrPredicate)) {
2397
+ const target = elementNodeOrPredicate;
2398
+ let cur = this.head;
2399
+ while (cur) {
2400
+ if (cur === target) return target;
2401
+ cur = cur.next;
2402
+ }
2403
+ const isMatch = /* @__PURE__ */ __name((node) => this._equals(node.value, target.value), "isMatch");
2404
+ cur = this.head;
2405
+ while (cur) {
2406
+ if (isMatch(cur)) return cur;
2407
+ cur = cur.next;
2408
+ }
2409
+ return void 0;
2410
+ }
2411
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2412
+ let current = this.head;
2413
+ while (current) {
2414
+ if (predicate(current)) return current;
2415
+ current = current.next;
2416
+ }
2417
+ return void 0;
2418
+ }
2419
+ /**
2420
+ * Insert a new element/node at an index, shifting following nodes.
2421
+ * @remarks Time O(N), Space O(1)
2422
+ * @param index - Zero-based index.
2423
+ * @param newElementOrNode - Element or node to insert.
2424
+ * @returns True if inserted.
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
+
2452
+
2453
+ * @example
2454
+ * // Insert at position
2455
+ * const list = new DoublyLinkedList<number>([1, 3]);
2456
+ * list.addAt(1, 2);
2457
+ * console.log(list.toArray()); // [1, 2, 3];
2458
+ */
2459
+ addAt(index, newElementOrNode) {
2460
+ if (index < 0 || index > this._length) return false;
2461
+ if (index === 0) return this.unshift(newElementOrNode);
2462
+ if (index === this._length) return this.push(newElementOrNode);
2463
+ const newNode = this._ensureNode(newElementOrNode);
2464
+ const prevNode = this.getNodeAt(index - 1);
2465
+ const nextNode = prevNode.next;
2466
+ newNode.prev = prevNode;
2467
+ newNode.next = nextNode;
2468
+ prevNode.next = newNode;
2469
+ nextNode.prev = newNode;
2470
+ this._length++;
2471
+ return true;
2472
+ }
2473
+ /**
2474
+ * Insert a new element/node before an existing one.
2475
+ * @remarks Time O(N), Space O(1)
2476
+ * @param existingElementOrNode - Existing element or node.
2477
+ * @param newElementOrNode - Element or node to insert.
2478
+ * @returns True if inserted.
2479
+ */
2480
+ addBefore(existingElementOrNode, newElementOrNode) {
2481
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2482
+ if (!existingNode) return false;
2483
+ const newNode = this._ensureNode(newElementOrNode);
2484
+ newNode.prev = existingNode.prev;
2485
+ if (existingNode.prev) existingNode.prev.next = newNode;
2486
+ newNode.next = existingNode;
2487
+ existingNode.prev = newNode;
2488
+ if (existingNode === this.head) this._head = newNode;
2489
+ this._length++;
2490
+ return true;
2491
+ }
2492
+ /**
2493
+ * Insert a new element/node after an existing one.
2494
+ * @remarks Time O(N), Space O(1)
2495
+ * @param existingElementOrNode - Existing element or node.
2496
+ * @param newElementOrNode - Element or node to insert.
2497
+ * @returns True if inserted.
2498
+ */
2499
+ addAfter(existingElementOrNode, newElementOrNode) {
2500
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2501
+ if (!existingNode) return false;
2502
+ const newNode = this._ensureNode(newElementOrNode);
2503
+ newNode.next = existingNode.next;
2504
+ if (existingNode.next) existingNode.next.prev = newNode;
2505
+ newNode.prev = existingNode;
2506
+ existingNode.next = newNode;
2507
+ if (existingNode === this.tail) this._tail = newNode;
2508
+ this._length++;
2509
+ return true;
2510
+ }
2511
+ /**
2512
+ * Set the element value at an index.
2513
+ * @remarks Time O(N), Space O(1)
2514
+ * @param index - Zero-based index.
2515
+ * @param value - New value.
2516
+ * @returns True if updated.
2517
+ */
2518
+ setAt(index, value) {
2519
+ const node = this.getNodeAt(index);
2520
+ if (!node) return false;
2521
+ node.value = value;
2522
+ return true;
2523
+ }
2524
+ /**
2525
+ * Delete the element at an index.
2526
+ * @remarks Time O(N), Space O(1)
2527
+ * @param index - Zero-based index.
2528
+ * @returns Removed element or undefined.
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
+
2556
+
2557
+ * @example
2558
+ * // Remove by index
2559
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
2560
+ * list.deleteAt(1);
2561
+ * console.log(list.toArray()); // ['a', 'c'];
2562
+ */
2563
+ deleteAt(index) {
2564
+ if (index < 0 || index >= this._length) return;
2565
+ if (index === 0) return this.shift();
2566
+ if (index === this._length - 1) return this.pop();
2567
+ const removedNode = this.getNodeAt(index);
2568
+ const prevNode = removedNode.prev;
2569
+ const nextNode = removedNode.next;
2570
+ prevNode.next = nextNode;
2571
+ nextNode.prev = prevNode;
2572
+ this._length--;
2573
+ return removedNode.value;
2574
+ }
2575
+ /**
2576
+ * Delete the first match by value/node.
2577
+ * @remarks Time O(N), Space O(1)
2578
+ * @param [elementOrNode] - Element or node to remove.
2579
+ * @returns True if removed.
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
+
2607
+
2608
+ * @example
2609
+ * // Remove first occurrence
2610
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 2]);
2611
+ * list.delete(2);
2612
+ * console.log(list.toArray()); // [1, 3, 2];
2613
+ */
2614
+ delete(elementOrNode) {
2615
+ const node = this.getNode(elementOrNode);
2616
+ if (!node) return false;
2617
+ if (node === this.head) this.shift();
2618
+ else if (node === this.tail) this.pop();
2619
+ else {
2620
+ const prevNode = node.prev;
2621
+ const nextNode = node.next;
2622
+ prevNode.next = nextNode;
2623
+ nextNode.prev = prevNode;
2624
+ this._length--;
2625
+ }
2626
+ return true;
2627
+ }
2628
+ /**
2629
+ * Check whether the list is empty.
2630
+ * @remarks Time O(1), Space O(1)
2631
+ * @returns True if length is 0.
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
+
2660
+
2661
+ * @example
2662
+ * // Check empty
2663
+ * console.log(new DoublyLinkedList().isEmpty()); // true;
2664
+ */
2665
+ isEmpty() {
2666
+ return this._length === 0;
2667
+ }
2668
+ /**
2669
+ * Remove all nodes and reset length.
2670
+ * @remarks Time O(N), Space O(1)
2671
+ * @returns void
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
+
2700
+
2701
+ * @example
2702
+ * // Remove all
2703
+ * const list = new DoublyLinkedList<number>([1, 2]);
2704
+ * list.clear();
2705
+ * console.log(list.isEmpty()); // true;
2706
+ */
2707
+ clear() {
2708
+ this._head = void 0;
2709
+ this._tail = void 0;
2710
+ this._length = 0;
2711
+ }
2712
+ /**
2713
+ * Find the first value matching a predicate scanning forward.
2714
+ * @remarks Time O(N), Space O(1)
2715
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
2716
+ * @returns Matched value or undefined.
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
+
2744
+
2745
+ * @example
2746
+ * // Search with predicate
2747
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
2748
+ * const found = list.search(node => node.value > 15);
2749
+ * console.log(found); // 20;
2750
+ */
2751
+ search(elementNodeOrPredicate) {
2752
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2753
+ let current = this.head;
2754
+ while (current) {
2755
+ if (predicate(current)) return current.value;
2756
+ current = current.next;
2757
+ }
2758
+ return void 0;
2759
+ }
2760
+ /**
2761
+ * Find the first value matching a predicate scanning backward.
2762
+ * @remarks Time O(N), Space O(1)
2763
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
2764
+ * @returns Matched value or undefined.
2765
+
2766
+
2767
+
2768
+
2769
+
2770
+
2771
+
2772
+
2773
+
2774
+
2775
+
2776
+
2777
+
2778
+
2779
+
2780
+
2781
+
2782
+
2783
+
2784
+
2785
+
2786
+
2787
+
2788
+
2789
+
2790
+
2791
+
2792
+
2793
+ * @example
2794
+ * // Find value scanning from tail
2795
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
2796
+ * // getBackward scans from tail to head, returns first match
2797
+ * const found = list.getBackward(node => node.value < 4);
2798
+ * console.log(found); // 3;
2799
+ */
2800
+ getBackward(elementNodeOrPredicate) {
2801
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2802
+ let current = this.tail;
2803
+ while (current) {
2804
+ if (predicate(current)) return current.value;
2805
+ current = current.prev;
2806
+ }
2807
+ return void 0;
2808
+ }
2809
+ /**
2810
+ * Reverse the list in place.
2811
+ * @remarks Time O(N), Space O(1)
2812
+ * @returns This list.
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
+
2843
+
2844
+ * @example
2845
+ * // Reverse in-place
2846
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
2847
+ * list.reverse();
2848
+ * console.log([...list]); // [3, 2, 1];
2849
+ */
2850
+ reverse() {
2851
+ let current = this.head;
2852
+ [this._head, this._tail] = [this.tail, this.head];
2853
+ while (current) {
2854
+ const next = current.next;
2855
+ [current.prev, current.next] = [current.next, current.prev];
2856
+ current = next;
2857
+ }
2858
+ return this;
2859
+ }
2860
+ /**
2861
+ * Set the equality comparator used to compare values.
2862
+ * @remarks Time O(1), Space O(1)
2863
+ * @param equals - Equality predicate (a, b) → boolean.
2864
+ * @returns This list.
2865
+ */
2866
+ setEquality(equals) {
2867
+ this._equals = equals;
2868
+ return this;
2869
+ }
2870
+ /**
2871
+ * Deep clone this list (values are copied by reference).
2872
+ * @remarks Time O(N), Space O(N)
2873
+ * @returns A new list with the same element sequence.
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
+
2902
+
2903
+ * @example
2904
+ * // Deep copy
2905
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
2906
+ * const copy = list.clone();
2907
+ * copy.pop();
2908
+ * console.log(list.length); // 3;
2909
+ */
2910
+ clone() {
2911
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
2912
+ for (const v of this) out.push(v);
2913
+ return out;
2914
+ }
2915
+ /**
2916
+ * Filter values into a new list of the same class.
2917
+ * @remarks Time O(N), Space O(N)
2918
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
2919
+ * @param [thisArg] - Value for `this` inside the callback.
2920
+ * @returns A new list with kept values.
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
+
2951
+
2952
+ * @example
2953
+ * // Filter elements
2954
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
2955
+ * const evens = list.filter(n => n % 2 === 0);
2956
+ * console.log([...evens]); // [2, 4];
2957
+ */
2958
+ filter(callback, thisArg) {
2959
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
2960
+ let index = 0;
2961
+ for (const v of this) if (callback.call(thisArg, v, index++, this)) out.push(v);
2962
+ return out;
2963
+ }
2964
+ /**
2965
+ * Map values into a new list of the same class.
2966
+ * @remarks Time O(N), Space O(N)
2967
+ * @param callback - Mapping function (value, index, list) → newValue.
2968
+ * @param [thisArg] - Value for `this` inside the callback.
2969
+ * @returns A new list with mapped values.
2970
+ */
2971
+ mapSame(callback, thisArg) {
2972
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
2973
+ let index = 0;
2974
+ for (const v of this) {
2975
+ const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
2976
+ out.push(mv);
2977
+ }
2978
+ return out;
2979
+ }
2980
+ /**
2981
+ * Map values into a new list (possibly different element type).
2982
+ * @remarks Time O(N), Space O(N)
2983
+ * @template EM
2984
+ * @template RM
2985
+ * @param callback - Mapping function (value, index, list) → newElement.
2986
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
2987
+ * @param [thisArg] - Value for `this` inside the callback.
2988
+ * @returns A new DoublyLinkedList with mapped values.
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
+
3019
+
3020
+ * @example
3021
+ * // DoublyLinkedList for...of iteration and map operation
3022
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
3023
+ *
3024
+ * // Iterate through list
3025
+ * const doubled = list.map(value => value * 2);
3026
+ * console.log(doubled.length); // 5;
3027
+ *
3028
+ * // Use for...of loop
3029
+ * const result: number[] = [];
3030
+ * for (const item of list) {
3031
+ * result.push(item);
3032
+ * }
3033
+ * console.log(result); // [1, 2, 3, 4, 5];
3034
+ */
3035
+ map(callback, options, thisArg) {
3036
+ const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
3037
+ let index = 0;
3038
+ for (const v of this) out.push(callback.call(thisArg, v, index++, this));
3039
+ return out;
3040
+ }
3041
+ /**
3042
+ * (Protected) Create or return a node for the given input (node or raw element).
3043
+ * @remarks Time O(1), Space O(1)
3044
+ * @param elementOrNode - Element value or node to normalize.
3045
+ * @returns A DoublyLinkedListNode for the provided input.
3046
+ */
3047
+ _ensureNode(elementOrNode) {
3048
+ if (this.isNode(elementOrNode)) return elementOrNode;
3049
+ return new DoublyLinkedListNode(elementOrNode);
3050
+ }
3051
+ /**
3052
+ * (Protected) Normalize input into a predicate over nodes.
3053
+ * @remarks Time O(1), Space O(1)
3054
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
3055
+ * @returns A predicate function taking a node and returning true/false.
3056
+ */
3057
+ _ensurePredicate(elementNodeOrPredicate) {
3058
+ if (this.isNode(elementNodeOrPredicate)) {
3059
+ const target = elementNodeOrPredicate;
3060
+ return (node) => node === target;
3061
+ }
3062
+ if (typeof elementNodeOrPredicate === "function") {
3063
+ return elementNodeOrPredicate;
3064
+ }
3065
+ const value = elementNodeOrPredicate;
3066
+ return (node) => this._equals(node.value, value);
3067
+ }
3068
+ /**
3069
+ * (Protected) Get the previous node of a given node.
3070
+ * @remarks Time O(1), Space O(1)
3071
+ * @param node - A node in the list.
3072
+ * @returns Previous node or undefined.
3073
+ */
3074
+ _getPrevNode(node) {
3075
+ return node.prev;
3076
+ }
3077
+ /**
3078
+ * (Protected) Create an empty instance of the same concrete class.
3079
+ * @remarks Time O(1), Space O(1)
3080
+ * @param [options] - Options forwarded to the constructor.
3081
+ * @returns An empty like-kind list instance.
3082
+ */
3083
+ _createInstance(options) {
3084
+ const Ctor = this.constructor;
3085
+ return new Ctor([], options);
3086
+ }
3087
+ /**
3088
+ * (Protected) Create a like-kind instance and seed it from an iterable.
3089
+ * @remarks Time O(N), Space O(N)
3090
+ * @template EM
3091
+ * @template RM
3092
+ * @param [elements] - Iterable used to seed the new list.
3093
+ * @param [options] - Options forwarded to the constructor.
3094
+ * @returns A like-kind DoublyLinkedList instance.
3095
+ */
3096
+ _createLike(elements = [], options) {
3097
+ const Ctor = this.constructor;
3098
+ return new Ctor(elements, options);
3099
+ }
3100
+ *_getIterator() {
3101
+ let current = this.head;
3102
+ while (current) {
3103
+ yield current.value;
3104
+ current = current.next;
3105
+ }
3106
+ }
3107
+ *_getReverseIterator() {
3108
+ let current = this.tail;
3109
+ while (current) {
3110
+ yield current.value;
3111
+ current = current.prev;
3112
+ }
3113
+ }
3114
+ *_getNodeIterator() {
3115
+ let current = this.head;
3116
+ while (current) {
3117
+ yield current;
3118
+ current = current.next;
3119
+ }
3120
+ }
3121
+ };
3122
+
3123
+ // src/common/error.ts
3124
+ var ERR = {
3125
+ // Range / index
3126
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
3127
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
3128
+ // Type / argument
3129
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
3130
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
3131
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
3132
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
3133
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
3134
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
3135
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
3136
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
3137
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
3138
+ // State / operation
3139
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
3140
+ // Matrix
3141
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
3142
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
3143
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
3144
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
3145
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
3146
+ };
3147
+
3148
+ // src/data-structures/base/iterable-entry-base.ts
3149
+ var IterableEntryBase = class {
3150
+ static {
3151
+ __name(this, "IterableEntryBase");
3152
+ }
3153
+ /**
3154
+ * Default iterator yielding `[key, value]` entries.
3155
+ * @returns Iterator of `[K, V]`.
3156
+ * @remarks Time O(n) to iterate, Space O(1)
3157
+ */
3158
+ *[Symbol.iterator](...args) {
3159
+ yield* this._getIterator(...args);
3160
+ }
3161
+ /**
3162
+ * Iterate over `[key, value]` pairs (may yield `undefined` values).
3163
+ * @returns Iterator of `[K, V | undefined]`.
3164
+ * @remarks Time O(n), Space O(1)
3165
+ */
3166
+ *entries() {
3167
+ for (const item of this) {
3168
+ yield item;
3169
+ }
3170
+ }
3171
+ /**
3172
+ * Iterate over keys only.
3173
+ * @returns Iterator of keys.
3174
+ * @remarks Time O(n), Space O(1)
3175
+ */
3176
+ *keys() {
3177
+ for (const item of this) {
3178
+ yield item[0];
3179
+ }
3180
+ }
3181
+ /**
3182
+ * Iterate over values only.
3183
+ * @returns Iterator of values.
3184
+ * @remarks Time O(n), Space O(1)
3185
+ */
3186
+ *values() {
3187
+ for (const item of this) {
3188
+ yield item[1];
3189
+ }
3190
+ }
3191
+ /**
3192
+ * Test whether all entries satisfy the predicate.
3193
+ * @param predicate - `(key, value, index, self) => boolean`.
3194
+ * @param thisArg - Optional `this` for callback.
3195
+ * @returns `true` if all pass; otherwise `false`.
3196
+ * @remarks Time O(n), Space O(1)
3197
+ */
3198
+ every(predicate, thisArg) {
3199
+ let index = 0;
3200
+ for (const item of this) {
3201
+ if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
3202
+ return false;
3203
+ }
3204
+ }
3205
+ return true;
3206
+ }
3207
+ /**
3208
+ * Test whether any entry satisfies the predicate.
3209
+ * @param predicate - `(key, value, index, self) => boolean`.
3210
+ * @param thisArg - Optional `this` for callback.
3211
+ * @returns `true` if any passes; otherwise `false`.
3212
+ * @remarks Time O(n), Space O(1)
3213
+ */
3214
+ some(predicate, thisArg) {
3215
+ let index = 0;
3216
+ for (const item of this) {
3217
+ if (predicate.call(thisArg, item[1], item[0], index++, this)) {
3218
+ return true;
3219
+ }
3220
+ }
3221
+ return false;
3222
+ }
3223
+ /**
3224
+ * Visit each entry, left-to-right.
3225
+ * @param callbackfn - `(key, value, index, self) => void`.
3226
+ * @param thisArg - Optional `this` for callback.
3227
+ * @remarks Time O(n), Space O(1)
3228
+ */
3229
+ forEach(callbackfn, thisArg) {
3230
+ let index = 0;
3231
+ for (const item of this) {
3232
+ const [key, value] = item;
3233
+ callbackfn.call(thisArg, value, key, index++, this);
3234
+ }
3235
+ }
3236
+ /**
3237
+ * Find the first entry that matches a predicate.
3238
+ * @param callbackfn - `(key, value, index, self) => boolean`.
3239
+ * @param thisArg - Optional `this` for callback.
3240
+ * @returns Matching `[key, value]` or `undefined`.
3241
+ * @remarks Time O(n), Space O(1)
3242
+ */
3243
+ find(callbackfn, thisArg) {
3244
+ let index = 0;
3245
+ for (const item of this) {
3246
+ const [key, value] = item;
3247
+ if (callbackfn.call(thisArg, value, key, index++, this)) return item;
3248
+ }
3249
+ return;
3250
+ }
3251
+ /**
3252
+ * Whether the given key exists.
3253
+ * @param key - Key to test.
3254
+ * @returns `true` if found; otherwise `false`.
3255
+ * @remarks Time O(n) generic, Space O(1)
3256
+ */
3257
+ has(key) {
3258
+ for (const item of this) {
3259
+ const [itemKey] = item;
3260
+ if (itemKey === key) return true;
3261
+ }
3262
+ return false;
3263
+ }
3264
+ /**
3265
+ * Whether there exists an entry with the given value.
3266
+ * @param value - Value to test.
3267
+ * @returns `true` if found; otherwise `false`.
3268
+ * @remarks Time O(n), Space O(1)
3269
+ */
3270
+ hasValue(value) {
3271
+ for (const [, elementValue] of this) {
3272
+ if (elementValue === value) return true;
3273
+ }
3274
+ return false;
3275
+ }
3276
+ /**
3277
+ * Get the value under a key.
3278
+ * @param key - Key to look up.
3279
+ * @returns Value or `undefined`.
3280
+ * @remarks Time O(n) generic, Space O(1)
3281
+ */
3282
+ get(key) {
3283
+ for (const item of this) {
3284
+ const [itemKey, value] = item;
3285
+ if (itemKey === key) return value;
3286
+ }
3287
+ return;
3288
+ }
3289
+ /**
3290
+ * Reduce entries into a single accumulator.
3291
+ * @param callbackfn - `(acc, value, key, index, self) => acc`.
3292
+ * @param initialValue - Initial accumulator.
3293
+ * @returns Final accumulator.
3294
+ * @remarks Time O(n), Space O(1)
3295
+ */
3296
+ reduce(callbackfn, initialValue) {
3297
+ let accumulator = initialValue;
3298
+ let index = 0;
3299
+ for (const item of this) {
3300
+ const [key, value] = item;
3301
+ accumulator = callbackfn(accumulator, value, key, index++, this);
3302
+ }
3303
+ return accumulator;
3304
+ }
3305
+ /**
3306
+ * Converts data structure to `[key, value]` pairs.
3307
+ * @returns Array of entries.
3308
+ * @remarks Time O(n), Space O(n)
3309
+ */
3310
+ toArray() {
3311
+ return [...this];
3312
+ }
3313
+ /**
3314
+ * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
3315
+ * @returns Array of entries (default) or a string.
3316
+ * @remarks Time O(n), Space O(n)
3317
+ */
3318
+ toVisual() {
3319
+ return [...this];
3320
+ }
3321
+ /**
3322
+ * Print a human-friendly representation to the console.
3323
+ * @remarks Time O(n), Space O(n)
3324
+ */
3325
+ print() {
3326
+ console.log(this.toVisual());
3327
+ }
3328
+ };
3329
+
3330
+ // src/data-structures/linked-list/skip-linked-list.ts
3331
+ var SkipListNode = class {
3332
+ static {
3333
+ __name(this, "SkipListNode");
3334
+ }
3335
+ key;
3336
+ value;
3337
+ forward;
3338
+ constructor(key, value, level) {
3339
+ this.key = key;
3340
+ this.value = value;
3341
+ this.forward = new Array(level).fill(void 0);
3342
+ }
3343
+ };
3344
+ var SkipList = class _SkipList extends IterableEntryBase {
3345
+ static {
3346
+ __name(this, "SkipList");
3347
+ }
3348
+ #comparator;
3349
+ #isDefaultComparator;
3350
+ constructor(entries = [], options = {}) {
3351
+ super();
3352
+ const { comparator, toEntryFn, maxLevel, probability } = options;
3353
+ if (typeof maxLevel === "number" && maxLevel > 0) this._maxLevel = maxLevel;
3354
+ if (typeof probability === "number" && probability > 0 && probability < 1) this._probability = probability;
3355
+ this.#isDefaultComparator = comparator === void 0;
3356
+ this.#comparator = comparator ?? _SkipList.createDefaultComparator();
3357
+ this._head = new SkipListNode(void 0, void 0, this._maxLevel);
3358
+ for (const item of entries) {
3359
+ let k;
3360
+ let v;
3361
+ if (toEntryFn) {
3362
+ [k, v] = toEntryFn(item);
3363
+ } else {
3364
+ if (!Array.isArray(item) || item.length < 2) {
3365
+ throw new TypeError(ERR.invalidEntry("SkipList"));
3366
+ }
3367
+ [k, v] = item;
3368
+ }
3369
+ this.set(k, v);
3370
+ }
3371
+ }
3372
+ /**
3373
+ * Creates a default comparator supporting number, string, Date, and bigint.
3374
+ */
3375
+ static createDefaultComparator() {
3376
+ return (a, b) => {
3377
+ if (typeof a === "number" && typeof b === "number") {
3378
+ if (Number.isNaN(a) || Number.isNaN(b)) throw new TypeError(ERR.invalidNaN("SkipList"));
3379
+ return a - b;
3380
+ }
3381
+ if (typeof a === "string" && typeof b === "string") {
3382
+ return a < b ? -1 : a > b ? 1 : 0;
3383
+ }
3384
+ if (a instanceof Date && b instanceof Date) {
3385
+ const ta = a.getTime(), tb = b.getTime();
3386
+ if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("SkipList"));
3387
+ return ta - tb;
3388
+ }
3389
+ if (typeof a === "bigint" && typeof b === "bigint") {
3390
+ return a < b ? -1 : a > b ? 1 : 0;
3391
+ }
3392
+ throw new TypeError(ERR.comparatorRequired("SkipList"));
3393
+ };
3394
+ }
3395
+ // ─── Internal state ──────────────────────────────────────────
3396
+ _head;
3397
+ _level = 0;
3398
+ _size = 0;
3399
+ _maxLevel = 16;
3400
+ _probability = 0.5;
3401
+ // ─── Size & lifecycle ────────────────────────────────────────
3402
+ get size() {
3403
+ return this._size;
3404
+ }
3405
+ get maxLevel() {
3406
+ return this._maxLevel;
3407
+ }
3408
+ get probability() {
3409
+ return this._probability;
3410
+ }
3411
+ get comparator() {
3412
+ return this.#comparator;
3413
+ }
3414
+ /**
3415
+ * Check if empty
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
+
3443
+
3444
+ * @example
3445
+ * // Check if empty
3446
+ * const sl = new SkipList<number, string>();
3447
+ * console.log(sl.isEmpty()); // true;
3448
+ */
3449
+ isEmpty() {
3450
+ return this._size === 0;
3451
+ }
3452
+ /**
3453
+ * Remove all entries
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
+
3481
+
3482
+ * @example
3483
+ * // Remove all entries
3484
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
3485
+ * sl.clear();
3486
+ * console.log(sl.isEmpty()); // true;
3487
+ */
3488
+ clear() {
3489
+ this._head = new SkipListNode(void 0, void 0, this._maxLevel);
3490
+ this._level = 0;
3491
+ this._size = 0;
3492
+ }
3493
+ /**
3494
+ * Create independent copy
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
+
3522
+
3523
+ * @example
3524
+ * // Create independent copy
3525
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
3526
+ * const copy = sl.clone();
3527
+ * copy.delete(1);
3528
+ * console.log(sl.has(1)); // true;
3529
+ */
3530
+ clone() {
3531
+ return new _SkipList(this, {
3532
+ comparator: this.#isDefaultComparator ? void 0 : this.#comparator,
3533
+ maxLevel: this._maxLevel,
3534
+ probability: this._probability
3535
+ });
3536
+ }
3537
+ // ─── Core CRUD ───────────────────────────────────────────────
3538
+ /**
3539
+ * Insert or update a key-value pair. Returns `this` for chaining.
3540
+ * Unique keys only — if key exists, value is updated in place.
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
+
3571
+
3572
+ * @example
3573
+ * // In-memory sorted key-value store
3574
+ * const store = new SkipList<number, string>();
3575
+ *
3576
+ * store.set(3, 'three');
3577
+ * store.set(1, 'one');
3578
+ * store.set(5, 'five');
3579
+ * store.set(2, 'two');
3580
+ *
3581
+ * console.log(store.get(3)); // 'three';
3582
+ * console.log(store.get(1)); // 'one';
3583
+ * console.log(store.get(5)); // 'five';
3584
+ *
3585
+ * // Update existing key
3586
+ * store.set(3, 'THREE');
3587
+ * console.log(store.get(3)); // 'THREE';
3588
+ */
3589
+ set(key, value) {
3590
+ const cmp = this.#comparator;
3591
+ const update = this._findUpdate(key);
3592
+ const existing = update[0].forward[0];
3593
+ if (existing && cmp(existing.key, key) === 0) {
3594
+ existing.value = value;
3595
+ return this;
3596
+ }
3597
+ const newLevel = this._randomLevel();
3598
+ const newNode = new SkipListNode(key, value, newLevel);
3599
+ if (newLevel > this._level) {
3600
+ for (let i = this._level; i < newLevel; i++) {
3601
+ update[i] = this._head;
3602
+ }
3603
+ this._level = newLevel;
3604
+ }
3605
+ for (let i = 0; i < newLevel; i++) {
3606
+ newNode.forward[i] = update[i].forward[i];
3607
+ update[i].forward[i] = newNode;
3608
+ }
3609
+ this._size++;
3610
+ return this;
3611
+ }
3612
+ /**
3613
+ * Get the value for a key, or `undefined` if not found.
3614
+ * Overrides base O(n) with O(log n) skip-list search.
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
+
3645
+
3646
+ * @example
3647
+ * // Building a sorted index
3648
+ * type Product = { id: number; name: string; price: number };
3649
+ * const products: Product[] = [
3650
+ * { id: 1, name: 'Widget', price: 25 },
3651
+ * { id: 2, name: 'Gadget', price: 50 },
3652
+ * { id: 3, name: 'Doohickey', price: 15 }
3653
+ * ];
3654
+ *
3655
+ * const index = new SkipList<number, Product, Product>(products, {
3656
+ * toEntryFn: (p: Product) => [p.price, p]
3657
+ * });
3658
+ *
3659
+ * // Iterate in sorted order by price
3660
+ * const names = [...index.values()].map(p => p!.name);
3661
+ * console.log(names); // ['Doohickey', 'Widget', 'Gadget'];
3662
+ *
3663
+ * // Range search: products between $20 and $60
3664
+ * const range = index.rangeSearch([20, 60]);
3665
+ * console.log(range.map(([, p]) => p!.name)); // ['Widget', 'Gadget'];
3666
+ */
3667
+ get(key) {
3668
+ const node = this._findNode(key);
3669
+ return node ? node.value : void 0;
3670
+ }
3671
+ /**
3672
+ * Check if a key exists.
3673
+ * Overrides base O(n) with O(log n) skip-list search.
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
+
3704
+
3705
+ * @example
3706
+ * // Check key existence
3707
+ * const sl = new SkipList<number, string>([[1, 'a'], [3, 'c'], [5, 'e']]);
3708
+ * console.log(sl.has(3)); // true;
3709
+ * console.log(sl.has(4)); // false;
3710
+ */
3711
+ has(key) {
3712
+ return this._findNode(key) !== void 0;
3713
+ }
3714
+ /**
3715
+ * Delete a key. Returns `true` if the key was found and removed.
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
+
3746
+
3747
+ * @example
3748
+ * // Fast lookup with deletion
3749
+ * const cache = new SkipList<string, number>();
3750
+ *
3751
+ * cache.set('alpha', 1);
3752
+ * cache.set('beta', 2);
3753
+ * cache.set('gamma', 3);
3754
+ *
3755
+ * console.log(cache.has('beta')); // true;
3756
+ * cache.delete('beta');
3757
+ * console.log(cache.has('beta')); // false;
3758
+ * console.log(cache.size); // 2;
3759
+ */
3760
+ delete(key) {
3761
+ const cmp = this.#comparator;
3762
+ const update = this._findUpdate(key);
3763
+ const target = update[0].forward[0];
3764
+ if (!target || cmp(target.key, key) !== 0) return false;
3765
+ for (let i = 0; i < this._level; i++) {
3766
+ if (update[i].forward[i] !== target) break;
3767
+ update[i].forward[i] = target.forward[i];
3768
+ }
3769
+ while (this._level > 0 && !this._head.forward[this._level - 1]) {
3770
+ this._level--;
3771
+ }
3772
+ this._size--;
3773
+ return true;
3774
+ }
3775
+ // ─── Navigation ──────────────────────────────────────────────
3776
+ /**
3777
+ * Returns the first (smallest key) entry, or `undefined` if empty.
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
+
3808
+
3809
+ * @example
3810
+ * // Access the minimum entry
3811
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
3812
+ * console.log(sl.first()); // [1, 'a'];
3813
+ */
3814
+ first() {
3815
+ const node = this._head.forward[0];
3816
+ return node ? [node.key, node.value] : void 0;
3817
+ }
3818
+ /**
3819
+ * Returns the last (largest key) entry, or `undefined` if empty.
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
+
3850
+
3851
+ * @example
3852
+ * // Access the maximum entry
3853
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
3854
+ * console.log(sl.last()); // [5, 'e'];
3855
+ */
3856
+ last() {
3857
+ let current = this._head;
3858
+ for (let i = this._level - 1; i >= 0; i--) {
3859
+ while (current.forward[i]) {
3860
+ current = current.forward[i];
3861
+ }
3862
+ }
3863
+ return current === this._head ? void 0 : [current.key, current.value];
3864
+ }
3865
+ /**
3866
+ * Remove and return the first (smallest key) entry.
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
+
3894
+
3895
+ * @example
3896
+ * // Remove and return smallest
3897
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
3898
+ * console.log(sl.pollFirst()); // [1, 'a'];
3899
+ * console.log(sl.size); // 2;
3900
+ */
3901
+ pollFirst() {
3902
+ const entry = this.first();
3903
+ if (!entry) return void 0;
3904
+ this.delete(entry[0]);
3905
+ return entry;
3906
+ }
3907
+ /**
3908
+ * Remove and return the last (largest key) entry.
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
+
3936
+
3937
+ * @example
3938
+ * // Remove and return largest
3939
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
3940
+ * console.log(sl.pollLast()); // [3, 'c'];
3941
+ * console.log(sl.size); // 2;
3942
+ */
3943
+ pollLast() {
3944
+ const entry = this.last();
3945
+ if (!entry) return void 0;
3946
+ this.delete(entry[0]);
3947
+ return entry;
3948
+ }
3949
+ /**
3950
+ * Least entry ≥ key, or `undefined`.
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
+
3981
+
3982
+ * @example
3983
+ * // Least entry ≥ key
3984
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
3985
+ * console.log(sl.ceiling(15)); // [20, 'b'];
3986
+ * console.log(sl.ceiling(20)); // [20, 'b'];
3987
+ */
3988
+ ceiling(key) {
3989
+ const cmp = this.#comparator;
3990
+ let current = this._head;
3991
+ for (let i = this._level - 1; i >= 0; i--) {
3992
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
3993
+ current = current.forward[i];
3994
+ }
3995
+ }
3996
+ const node = current.forward[0];
3997
+ return node ? [node.key, node.value] : void 0;
3998
+ }
3999
+ /**
4000
+ * Greatest entry ≤ key, or `undefined`.
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
+
4031
+
4032
+ * @example
4033
+ * // Greatest entry ≤ key
4034
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4035
+ * console.log(sl.floor(25)); // [20, 'b'];
4036
+ * console.log(sl.floor(5)); // undefined;
4037
+ */
4038
+ floor(key) {
4039
+ const cmp = this.#comparator;
4040
+ let current = this._head;
4041
+ for (let i = this._level - 1; i >= 0; i--) {
4042
+ while (current.forward[i] && cmp(current.forward[i].key, key) <= 0) {
4043
+ current = current.forward[i];
4044
+ }
4045
+ }
4046
+ const result = current === this._head ? void 0 : current;
4047
+ if (result && cmp(result.key, key) <= 0) return [result.key, result.value];
4048
+ return void 0;
4049
+ }
4050
+ /**
4051
+ * Least entry strictly > key, or `undefined`.
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
+
4079
+
4080
+ * @example
4081
+ * // Strictly greater entry
4082
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4083
+ * console.log(sl.higher(15)); // [20, 'b'];
4084
+ * console.log(sl.higher(30)); // undefined;
4085
+ */
4086
+ higher(key) {
4087
+ const cmp = this.#comparator;
4088
+ let current = this._head;
4089
+ for (let i = this._level - 1; i >= 0; i--) {
4090
+ while (current.forward[i] && cmp(current.forward[i].key, key) <= 0) {
4091
+ current = current.forward[i];
4092
+ }
4093
+ }
4094
+ const node = current.forward[0];
4095
+ return node ? [node.key, node.value] : void 0;
4096
+ }
4097
+ /**
4098
+ * Greatest entry strictly < key, or `undefined`.
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
+
4126
+
4127
+ * @example
4128
+ * // Strictly less entry
4129
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
4130
+ * console.log(sl.lower(25)); // [20, 'b'];
4131
+ * console.log(sl.lower(10)); // undefined;
4132
+ */
4133
+ lower(key) {
4134
+ const cmp = this.#comparator;
4135
+ let current = this._head;
4136
+ let result;
4137
+ for (let i = this._level - 1; i >= 0; i--) {
4138
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4139
+ current = current.forward[i];
4140
+ }
4141
+ if (current !== this._head && cmp(current.key, key) < 0) {
4142
+ result = current;
4143
+ }
4144
+ }
4145
+ return result ? [result.key, result.value] : void 0;
4146
+ }
4147
+ /**
4148
+ * Returns entries within the given key range.
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
+
4179
+
4180
+ * @example
4181
+ * // Find entries in a range
4182
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e']]);
4183
+ * const result = sl.rangeSearch([2, 4]);
4184
+ * console.log(result); // [[2, 'b'], [3, 'c'], [4, 'd']];
4185
+ */
4186
+ rangeSearch(range, options = {}) {
4187
+ const { lowInclusive = true, highInclusive = true } = options;
4188
+ const [low, high] = range;
4189
+ const cmp = this.#comparator;
4190
+ const out = [];
4191
+ let current = this._head;
4192
+ for (let i = this._level - 1; i >= 0; i--) {
4193
+ while (current.forward[i] && cmp(current.forward[i].key, low) < 0) {
4194
+ current = current.forward[i];
4195
+ }
4196
+ }
4197
+ current = current.forward[0];
4198
+ while (current) {
4199
+ const cmpHigh = cmp(current.key, high);
4200
+ if (cmpHigh > 0) break;
4201
+ if (cmpHigh === 0 && !highInclusive) break;
4202
+ const cmpLow = cmp(current.key, low);
4203
+ if (cmpLow > 0 || cmpLow === 0 && lowInclusive) {
4204
+ out.push([current.key, current.value]);
4205
+ }
4206
+ current = current.forward[0];
4207
+ }
4208
+ return out;
4209
+ }
4210
+ // ─── Functional (overrides) ──────────────────────────────────
4211
+ /**
4212
+ * Creates a new SkipList with entries transformed by callback.
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
+
4240
+
4241
+ * @example
4242
+ * // Transform entries
4243
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
4244
+ * const mapped = sl.map((v, k) => [k, v?.toUpperCase()] as [number, string]);
4245
+ * console.log([...mapped.values()]); // ['A', 'B'];
4246
+ */
4247
+ map(callback, options) {
4248
+ const out = new _SkipList([], options ?? {});
4249
+ let i = 0;
4250
+ for (const [k, v] of this) {
4251
+ const [nk, nv] = callback(v, k, i++, this);
4252
+ out.set(nk, nv);
4253
+ }
4254
+ return out;
4255
+ }
4256
+ /**
4257
+ * Creates a new SkipList with entries that pass the predicate.
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
+
4285
+
4286
+ * @example
4287
+ * // Filter entries
4288
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
4289
+ * const result = sl.filter((v, k) => k > 1);
4290
+ * console.log(result.size); // 2;
4291
+ */
4292
+ filter(callbackfn, thisArg) {
4293
+ const out = new _SkipList([], {
4294
+ comparator: this.#isDefaultComparator ? void 0 : this.#comparator,
4295
+ maxLevel: this._maxLevel,
4296
+ probability: this._probability
4297
+ });
4298
+ let i = 0;
4299
+ for (const [k, v] of this) {
4300
+ const ok = callbackfn.call(thisArg, v, k, i++, this);
4301
+ if (ok) out.set(k, v);
4302
+ }
4303
+ return out;
4304
+ }
4305
+ // ─── Iterator (required by IterableEntryBase) ────────────────
4306
+ _getIterator() {
4307
+ const head = this._head;
4308
+ return (function* () {
4309
+ let node = head.forward[0];
4310
+ while (node) {
4311
+ yield [node.key, node.value];
4312
+ node = node.forward[0];
4313
+ }
4314
+ })();
4315
+ }
4316
+ // ─── Internal helpers ────────────────────────────────────────
4317
+ /**
4318
+ * Finds the update array (predecessors at each level) for a given key.
4319
+ */
4320
+ _findUpdate(key) {
4321
+ const cmp = this.#comparator;
4322
+ const update = new Array(this._maxLevel).fill(this._head);
4323
+ let current = this._head;
4324
+ for (let i = this._level - 1; i >= 0; i--) {
4325
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4326
+ current = current.forward[i];
4327
+ }
4328
+ update[i] = current;
4329
+ }
4330
+ return update;
4331
+ }
4332
+ /**
4333
+ * Finds the node for a given key, or undefined.
4334
+ */
4335
+ _findNode(key) {
4336
+ const cmp = this.#comparator;
4337
+ let current = this._head;
4338
+ for (let i = this._level - 1; i >= 0; i--) {
4339
+ while (current.forward[i] && cmp(current.forward[i].key, key) < 0) {
4340
+ current = current.forward[i];
4341
+ }
4342
+ }
4343
+ const candidate = current.forward[0];
4344
+ if (candidate && cmp(candidate.key, key) === 0) return candidate;
4345
+ return void 0;
4346
+ }
4347
+ _randomLevel() {
4348
+ let level = 1;
4349
+ while (Math.random() < this._probability && level < this._maxLevel) {
4350
+ level++;
4351
+ }
4352
+ return level;
4353
+ }
4354
+ };
4355
+ /**
4356
+ * data-structure-typed
4357
+ *
4358
+ * @author Pablo Zeng
4359
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
4360
+ * @license MIT License
4361
+ */
4362
+
4363
+ exports.DoublyLinkedList = DoublyLinkedList;
4364
+ exports.DoublyLinkedListNode = DoublyLinkedListNode;
4365
+ exports.SinglyLinkedList = SinglyLinkedList;
4366
+ exports.SinglyLinkedListNode = SinglyLinkedListNode;
4367
+ exports.SkipList = SkipList;
4368
+ exports.SkipListNode = SkipListNode;
4369
+ //# sourceMappingURL=linked-list.cjs.map
4370
+ //# sourceMappingURL=linked-list.cjs.map