tree-set-typed 2.3.0

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 (273) hide show
  1. package/.eslintrc.js +61 -0
  2. package/.prettierignore +6 -0
  3. package/.prettierrc.js +16 -0
  4. package/LICENSE +21 -0
  5. package/README.md +482 -0
  6. package/coverage/clover.xml +13 -0
  7. package/coverage/coverage-final.json +96 -0
  8. package/coverage/coverage-summary.json +60 -0
  9. package/coverage/lcov-report/base.css +403 -0
  10. package/coverage/lcov-report/block-navigation.js +87 -0
  11. package/coverage/lcov-report/favicon.png +0 -0
  12. package/coverage/lcov-report/index.html +119 -0
  13. package/coverage/lcov-report/index.ts.html +109 -0
  14. package/coverage/lcov-report/prettify.css +1 -0
  15. package/coverage/lcov-report/prettify.js +2 -0
  16. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  17. package/coverage/lcov-report/sorter.js +206 -0
  18. package/coverage/lcov.info +14 -0
  19. package/dist/cjs/index.cjs +12 -0
  20. package/dist/cjs/index.cjs.map +1 -0
  21. package/dist/cjs-legacy/index.cjs +12 -0
  22. package/dist/cjs-legacy/index.cjs.map +1 -0
  23. package/dist/esm/index.mjs +3 -0
  24. package/dist/esm/index.mjs.map +1 -0
  25. package/dist/esm-legacy/index.mjs +3 -0
  26. package/dist/esm-legacy/index.mjs.map +1 -0
  27. package/dist/types/common/index.d.ts +12 -0
  28. package/dist/types/constants/index.d.ts +4 -0
  29. package/dist/types/data-structures/base/index.d.ts +2 -0
  30. package/dist/types/data-structures/base/iterable-element-base.d.ts +219 -0
  31. package/dist/types/data-structures/base/iterable-entry-base.d.ts +150 -0
  32. package/dist/types/data-structures/base/linear-base.d.ts +335 -0
  33. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +236 -0
  34. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +197 -0
  35. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +440 -0
  36. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -0
  37. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +807 -0
  38. package/dist/types/data-structures/binary-tree/bst.d.ts +645 -0
  39. package/dist/types/data-structures/binary-tree/index.d.ts +10 -0
  40. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +312 -0
  41. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -0
  42. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +243 -0
  43. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +333 -0
  44. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -0
  45. package/dist/types/data-structures/graph/directed-graph.d.ts +332 -0
  46. package/dist/types/data-structures/graph/index.d.ts +4 -0
  47. package/dist/types/data-structures/graph/map-graph.d.ts +78 -0
  48. package/dist/types/data-structures/graph/undirected-graph.d.ts +347 -0
  49. package/dist/types/data-structures/hash/hash-map.d.ts +428 -0
  50. package/dist/types/data-structures/hash/index.d.ts +1 -0
  51. package/dist/types/data-structures/heap/heap.d.ts +552 -0
  52. package/dist/types/data-structures/heap/index.d.ts +3 -0
  53. package/dist/types/data-structures/heap/max-heap.d.ts +32 -0
  54. package/dist/types/data-structures/heap/min-heap.d.ts +33 -0
  55. package/dist/types/data-structures/index.d.ts +12 -0
  56. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +437 -0
  57. package/dist/types/data-structures/linked-list/index.d.ts +3 -0
  58. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +567 -0
  59. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -0
  60. package/dist/types/data-structures/matrix/index.d.ts +2 -0
  61. package/dist/types/data-structures/matrix/matrix.d.ts +168 -0
  62. package/dist/types/data-structures/matrix/navigator.d.ts +55 -0
  63. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  64. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -0
  65. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -0
  66. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -0
  67. package/dist/types/data-structures/queue/deque.d.ts +459 -0
  68. package/dist/types/data-structures/queue/index.d.ts +2 -0
  69. package/dist/types/data-structures/queue/queue.d.ts +364 -0
  70. package/dist/types/data-structures/stack/index.d.ts +1 -0
  71. package/dist/types/data-structures/stack/stack.d.ts +324 -0
  72. package/dist/types/data-structures/tree/index.d.ts +1 -0
  73. package/dist/types/data-structures/tree/tree.d.ts +62 -0
  74. package/dist/types/data-structures/trie/index.d.ts +1 -0
  75. package/dist/types/data-structures/trie/trie.d.ts +412 -0
  76. package/dist/types/index.d.ts +23 -0
  77. package/dist/types/interfaces/binary-tree.d.ts +60 -0
  78. package/dist/types/interfaces/doubly-linked-list.d.ts +1 -0
  79. package/dist/types/interfaces/graph.d.ts +21 -0
  80. package/dist/types/interfaces/heap.d.ts +1 -0
  81. package/dist/types/interfaces/index.d.ts +8 -0
  82. package/dist/types/interfaces/navigator.d.ts +1 -0
  83. package/dist/types/interfaces/priority-queue.d.ts +1 -0
  84. package/dist/types/interfaces/segment-tree.d.ts +1 -0
  85. package/dist/types/interfaces/singly-linked-list.d.ts +1 -0
  86. package/dist/types/types/common.d.ts +15 -0
  87. package/dist/types/types/data-structures/base/base.d.ts +13 -0
  88. package/dist/types/types/data-structures/base/index.d.ts +1 -0
  89. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  90. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  91. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  92. package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  93. package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  94. package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
  95. package/dist/types/types/data-structures/binary-tree/index.d.ts +9 -0
  96. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  97. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  98. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  99. package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  100. package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
  101. package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
  102. package/dist/types/types/data-structures/graph/index.d.ts +3 -0
  103. package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
  104. package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
  105. package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
  106. package/dist/types/types/data-structures/hash/index.d.ts +2 -0
  107. package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
  108. package/dist/types/types/data-structures/heap/index.d.ts +1 -0
  109. package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
  110. package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
  111. package/dist/types/types/data-structures/index.d.ts +12 -0
  112. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
  113. package/dist/types/types/data-structures/linked-list/index.d.ts +3 -0
  114. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
  115. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
  116. package/dist/types/types/data-structures/matrix/index.d.ts +2 -0
  117. package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
  118. package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
  119. package/dist/types/types/data-structures/priority-queue/index.d.ts +3 -0
  120. package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  121. package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  122. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
  123. package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
  124. package/dist/types/types/data-structures/queue/index.d.ts +2 -0
  125. package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
  126. package/dist/types/types/data-structures/stack/index.d.ts +1 -0
  127. package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
  128. package/dist/types/types/data-structures/tree/index.d.ts +1 -0
  129. package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
  130. package/dist/types/types/data-structures/trie/index.d.ts +1 -0
  131. package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
  132. package/dist/types/types/index.d.ts +3 -0
  133. package/dist/types/types/utils/index.d.ts +2 -0
  134. package/dist/types/types/utils/utils.d.ts +22 -0
  135. package/dist/types/types/utils/validate-type.d.ts +19 -0
  136. package/dist/types/utils/index.d.ts +2 -0
  137. package/dist/types/utils/number.d.ts +14 -0
  138. package/dist/types/utils/utils.d.ts +209 -0
  139. package/dist/umd/red-black-tree-typed.js +14578 -0
  140. package/dist/umd/red-black-tree-typed.js.map +1 -0
  141. package/dist/umd/red-black-tree-typed.min.js +44 -0
  142. package/dist/umd/red-black-tree-typed.min.js.map +1 -0
  143. package/docs/.nojekyll +1 -0
  144. package/docs/assets/highlight.css +92 -0
  145. package/docs/assets/main.js +59 -0
  146. package/docs/assets/navigation.js +1 -0
  147. package/docs/assets/search.js +1 -0
  148. package/docs/assets/style.css +1383 -0
  149. package/docs/classes/AVLTree.html +2046 -0
  150. package/docs/classes/AVLTreeNode.html +263 -0
  151. package/docs/index.html +523 -0
  152. package/docs/modules.html +45 -0
  153. package/jest.config.js +8 -0
  154. package/package.json +113 -0
  155. package/src/common/index.ts +23 -0
  156. package/src/constants/index.ts +4 -0
  157. package/src/data-structures/base/index.ts +2 -0
  158. package/src/data-structures/base/iterable-element-base.ts +352 -0
  159. package/src/data-structures/base/iterable-entry-base.ts +246 -0
  160. package/src/data-structures/base/linear-base.ts +643 -0
  161. package/src/data-structures/binary-tree/avl-tree-counter.ts +539 -0
  162. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +438 -0
  163. package/src/data-structures/binary-tree/avl-tree.ts +840 -0
  164. package/src/data-structures/binary-tree/binary-indexed-tree.ts +331 -0
  165. package/src/data-structures/binary-tree/binary-tree.ts +2492 -0
  166. package/src/data-structures/binary-tree/bst.ts +2024 -0
  167. package/src/data-structures/binary-tree/index.ts +10 -0
  168. package/src/data-structures/binary-tree/red-black-tree.ts +767 -0
  169. package/src/data-structures/binary-tree/segment-tree.ts +324 -0
  170. package/src/data-structures/binary-tree/tree-counter.ts +575 -0
  171. package/src/data-structures/binary-tree/tree-multi-map.ts +549 -0
  172. package/src/data-structures/graph/abstract-graph.ts +1081 -0
  173. package/src/data-structures/graph/directed-graph.ts +715 -0
  174. package/src/data-structures/graph/index.ts +4 -0
  175. package/src/data-structures/graph/map-graph.ts +132 -0
  176. package/src/data-structures/graph/undirected-graph.ts +626 -0
  177. package/src/data-structures/hash/hash-map.ts +813 -0
  178. package/src/data-structures/hash/index.ts +1 -0
  179. package/src/data-structures/heap/heap.ts +1020 -0
  180. package/src/data-structures/heap/index.ts +3 -0
  181. package/src/data-structures/heap/max-heap.ts +47 -0
  182. package/src/data-structures/heap/min-heap.ts +36 -0
  183. package/src/data-structures/index.ts +12 -0
  184. package/src/data-structures/linked-list/doubly-linked-list.ts +876 -0
  185. package/src/data-structures/linked-list/index.ts +3 -0
  186. package/src/data-structures/linked-list/singly-linked-list.ts +1050 -0
  187. package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
  188. package/src/data-structures/matrix/index.ts +2 -0
  189. package/src/data-structures/matrix/matrix.ts +491 -0
  190. package/src/data-structures/matrix/navigator.ts +124 -0
  191. package/src/data-structures/priority-queue/index.ts +3 -0
  192. package/src/data-structures/priority-queue/max-priority-queue.ts +42 -0
  193. package/src/data-structures/priority-queue/min-priority-queue.ts +29 -0
  194. package/src/data-structures/priority-queue/priority-queue.ts +19 -0
  195. package/src/data-structures/queue/deque.ts +1001 -0
  196. package/src/data-structures/queue/index.ts +2 -0
  197. package/src/data-structures/queue/queue.ts +592 -0
  198. package/src/data-structures/stack/index.ts +1 -0
  199. package/src/data-structures/stack/stack.ts +469 -0
  200. package/src/data-structures/tree/index.ts +1 -0
  201. package/src/data-structures/tree/tree.ts +115 -0
  202. package/src/data-structures/trie/index.ts +1 -0
  203. package/src/data-structures/trie/trie.ts +756 -0
  204. package/src/index.ts +24 -0
  205. package/src/interfaces/binary-tree.ts +252 -0
  206. package/src/interfaces/doubly-linked-list.ts +1 -0
  207. package/src/interfaces/graph.ts +44 -0
  208. package/src/interfaces/heap.ts +1 -0
  209. package/src/interfaces/index.ts +8 -0
  210. package/src/interfaces/navigator.ts +1 -0
  211. package/src/interfaces/priority-queue.ts +1 -0
  212. package/src/interfaces/segment-tree.ts +1 -0
  213. package/src/interfaces/singly-linked-list.ts +1 -0
  214. package/src/types/common.ts +25 -0
  215. package/src/types/data-structures/base/base.ts +34 -0
  216. package/src/types/data-structures/base/index.ts +1 -0
  217. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  218. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -0
  219. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -0
  220. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  221. package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
  222. package/src/types/data-structures/binary-tree/bst.ts +19 -0
  223. package/src/types/data-structures/binary-tree/index.ts +9 -0
  224. package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
  225. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  226. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  227. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -0
  228. package/src/types/data-structures/graph/abstract-graph.ts +18 -0
  229. package/src/types/data-structures/graph/directed-graph.ts +2 -0
  230. package/src/types/data-structures/graph/index.ts +3 -0
  231. package/src/types/data-structures/graph/map-graph.ts +1 -0
  232. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  233. package/src/types/data-structures/hash/hash-map.ts +19 -0
  234. package/src/types/data-structures/hash/index.ts +3 -0
  235. package/src/types/data-structures/heap/heap.ts +6 -0
  236. package/src/types/data-structures/heap/index.ts +1 -0
  237. package/src/types/data-structures/heap/max-heap.ts +1 -0
  238. package/src/types/data-structures/heap/min-heap.ts +1 -0
  239. package/src/types/data-structures/index.ts +12 -0
  240. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -0
  241. package/src/types/data-structures/linked-list/index.ts +3 -0
  242. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -0
  243. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  244. package/src/types/data-structures/matrix/index.ts +2 -0
  245. package/src/types/data-structures/matrix/matrix.ts +7 -0
  246. package/src/types/data-structures/matrix/navigator.ts +14 -0
  247. package/src/types/data-structures/priority-queue/index.ts +3 -0
  248. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  249. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  250. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -0
  251. package/src/types/data-structures/queue/deque.ts +5 -0
  252. package/src/types/data-structures/queue/index.ts +2 -0
  253. package/src/types/data-structures/queue/queue.ts +5 -0
  254. package/src/types/data-structures/stack/index.ts +1 -0
  255. package/src/types/data-structures/stack/stack.ts +3 -0
  256. package/src/types/data-structures/tree/index.ts +1 -0
  257. package/src/types/data-structures/tree/tree.ts +1 -0
  258. package/src/types/data-structures/trie/index.ts +1 -0
  259. package/src/types/data-structures/trie/trie.ts +3 -0
  260. package/src/types/index.ts +3 -0
  261. package/src/types/utils/index.ts +2 -0
  262. package/src/types/utils/utils.ts +33 -0
  263. package/src/types/utils/validate-type.ts +35 -0
  264. package/src/utils/index.ts +2 -0
  265. package/src/utils/number.ts +22 -0
  266. package/src/utils/utils.ts +350 -0
  267. package/test/index.test.ts +111 -0
  268. package/tsconfig.base.json +23 -0
  269. package/tsconfig.json +12 -0
  270. package/tsconfig.test.json +8 -0
  271. package/tsconfig.types.json +15 -0
  272. package/tsup.config.js +28 -0
  273. package/tsup.node.config.js +71 -0
@@ -0,0 +1,1050 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Pablo Zeng
5
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+
9
+ import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
10
+ import { LinearLinkedBase, LinkedListNode } from '../base/linear-base';
11
+
12
+ /**
13
+ * Node of a singly linked list; stores value and the next link.
14
+ * @remarks Time O(1), Space O(1)
15
+ * @template E
16
+ */
17
+ export class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
18
+ /**
19
+ * Create a list node.
20
+ * @remarks Time O(1), Space O(1)
21
+ * @param value - Element value to store.
22
+ * @returns New node instance.
23
+ */
24
+
25
+ constructor(value: E) {
26
+ super(value);
27
+ this._value = value;
28
+ this._next = undefined;
29
+ }
30
+
31
+ protected override _next: SinglyLinkedListNode<E> | undefined;
32
+
33
+ /**
34
+ * Get the next node.
35
+ * @remarks Time O(1), Space O(1)
36
+ * @returns Next node or undefined.
37
+ */
38
+
39
+ override get next(): SinglyLinkedListNode<E> | undefined {
40
+ return this._next;
41
+ }
42
+
43
+ /**
44
+ * Set the next node.
45
+ * @remarks Time O(1), Space O(1)
46
+ * @param value - Next node or undefined.
47
+ * @returns void
48
+ */
49
+
50
+ override set next(value: SinglyLinkedListNode<E> | undefined) {
51
+ this._next = value;
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Singly linked list with O(1) push/pop-like ends operations and linear scans.
57
+ * @remarks Time O(1), Space O(1)
58
+ * @template E
59
+ * @template R
60
+ * 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
61
+ * 2. Bidirectional Traversal: Unlike doubly linked lists, singly linked lists can be easily traversed forwards but not backwards.
62
+ * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
63
+ * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
64
+ * Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
65
+ * @example
66
+ * // basic SinglyLinkedList creation and push operation
67
+ * // Create a simple SinglyLinkedList with initial values
68
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
69
+ *
70
+ * // Verify the list maintains insertion order
71
+ * console.log([...list]); // [1, 2, 3, 4, 5];
72
+ *
73
+ * // Check length
74
+ * console.log(list.length); // 5;
75
+ *
76
+ * // Push a new element to the end
77
+ * list.push(6);
78
+ * console.log(list.length); // 6;
79
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
80
+ * @example
81
+ * // SinglyLinkedList pop and shift operations
82
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
83
+ *
84
+ * // Pop removes from the end
85
+ * const last = list.pop();
86
+ * console.log(last); // 50;
87
+ *
88
+ * // Shift removes from the beginning
89
+ * const first = list.shift();
90
+ * console.log(first); // 10;
91
+ *
92
+ * // Verify remaining elements
93
+ * console.log([...list]); // [20, 30, 40];
94
+ * console.log(list.length); // 3;
95
+ * @example
96
+ * // SinglyLinkedList unshift and forward traversal
97
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
98
+ *
99
+ * // Unshift adds to the beginning
100
+ * list.unshift(10);
101
+ * console.log([...list]); // [10, 20, 30, 40];
102
+ *
103
+ * // Access elements (forward traversal only for singly linked)
104
+ * const second = list.at(1);
105
+ * console.log(second); // 20;
106
+ *
107
+ * // SinglyLinkedList allows forward iteration only
108
+ * const elements: number[] = [];
109
+ * for (const item of list) {
110
+ * elements.push(item);
111
+ * }
112
+ * console.log(elements); // [10, 20, 30, 40];
113
+ *
114
+ * console.log(list.length); // 4;
115
+ * @example
116
+ * // SinglyLinkedList filter and map operations
117
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
118
+ *
119
+ * // Filter even numbers
120
+ * const filtered = list.filter(value => value % 2 === 0);
121
+ * console.log(filtered.length); // 2;
122
+ *
123
+ * // Map to double values
124
+ * const doubled = list.map(value => value * 2);
125
+ * console.log(doubled.length); // 5;
126
+ *
127
+ * // Use reduce to sum
128
+ * const sum = list.reduce((acc, value) => acc + value, 0);
129
+ * console.log(sum); // 15;
130
+ * @example
131
+ * // SinglyLinkedList for sequentially processed data stream
132
+ * interface LogEntry {
133
+ * timestamp: number;
134
+ * level: 'INFO' | 'WARN' | 'ERROR';
135
+ * message: string;
136
+ * }
137
+ *
138
+ * // SinglyLinkedList is ideal for sequential processing where you only need forward iteration
139
+ * // O(1) insertion/deletion at head, O(n) for tail operations
140
+ * const logStream = new SinglyLinkedList<LogEntry>();
141
+ *
142
+ * // Simulate incoming log entries
143
+ * const entries: LogEntry[] = [
144
+ * { timestamp: 1000, level: 'INFO', message: 'Server started' },
145
+ * { timestamp: 1100, level: 'WARN', message: 'Memory usage high' },
146
+ * { timestamp: 1200, level: 'ERROR', message: 'Connection failed' },
147
+ * { timestamp: 1300, level: 'INFO', message: 'Connection restored' }
148
+ * ];
149
+ *
150
+ * // Add entries to the stream
151
+ * for (const entry of entries) {
152
+ * logStream.push(entry);
153
+ * }
154
+ *
155
+ * console.log(logStream.length); // 4;
156
+ *
157
+ * // Process logs sequentially (only forward iteration needed)
158
+ * const processedLogs: string[] = [];
159
+ * for (const log of logStream) {
160
+ * processedLogs.push(`[${log.level}] ${log.message}`);
161
+ * }
162
+ *
163
+ * console.log(processedLogs); // [
164
+ * // '[INFO] Server started',
165
+ * // '[WARN] Memory usage high',
166
+ * // '[ERROR] Connection failed',
167
+ * // '[INFO] Connection restored'
168
+ * // ];
169
+ *
170
+ * // Get first log (O(1) - direct head access)
171
+ * const firstLog = logStream.at(0);
172
+ * console.log(firstLog?.message); // 'Server started';
173
+ *
174
+ * // Remove oldest log (O(1) operation at head)
175
+ * const removed = logStream.shift();
176
+ * console.log(removed?.message); // 'Server started';
177
+ * console.log(logStream.length); // 3;
178
+ *
179
+ * // Remaining logs still maintain order for sequential processing
180
+ * console.log(logStream.length); // 3;
181
+ * @example
182
+ * // implementation of a basic text editor
183
+ * class TextEditor {
184
+ * private content: SinglyLinkedList<string>;
185
+ * private cursorIndex: number;
186
+ * private undoStack: Stack<{ operation: string; data?: any }>;
187
+ *
188
+ * constructor() {
189
+ * this.content = new SinglyLinkedList<string>();
190
+ * this.cursorIndex = 0; // Cursor starts at the beginning
191
+ * this.undoStack = new Stack<{ operation: string; data?: any }>(); // Stack to keep track of operations for undo
192
+ * }
193
+ *
194
+ * insert(char: string) {
195
+ * this.content.addAt(this.cursorIndex, char);
196
+ * this.cursorIndex++;
197
+ * this.undoStack.push({ operation: 'insert', data: { index: this.cursorIndex - 1 } });
198
+ * }
199
+ *
200
+ * delete() {
201
+ * if (this.cursorIndex === 0) return; // Nothing to delete
202
+ * const deleted = this.content.deleteAt(this.cursorIndex - 1);
203
+ * this.cursorIndex--;
204
+ * this.undoStack.push({ operation: 'delete', data: { index: this.cursorIndex, char: deleted } });
205
+ * }
206
+ *
207
+ * moveCursor(index: number) {
208
+ * this.cursorIndex = Math.max(0, Math.min(index, this.content.length));
209
+ * }
210
+ *
211
+ * undo() {
212
+ * if (this.undoStack.size === 0) return; // No operations to undo
213
+ * const lastAction = this.undoStack.pop();
214
+ *
215
+ * if (lastAction!.operation === 'insert') {
216
+ * this.content.deleteAt(lastAction!.data.index);
217
+ * this.cursorIndex = lastAction!.data.index;
218
+ * } else if (lastAction!.operation === 'delete') {
219
+ * this.content.addAt(lastAction!.data.index, lastAction!.data.char);
220
+ * this.cursorIndex = lastAction!.data.index + 1;
221
+ * }
222
+ * }
223
+ *
224
+ * getText(): string {
225
+ * return [...this.content].join('');
226
+ * }
227
+ * }
228
+ *
229
+ * // Example Usage
230
+ * const editor = new TextEditor();
231
+ * editor.insert('H');
232
+ * editor.insert('e');
233
+ * editor.insert('l');
234
+ * editor.insert('l');
235
+ * editor.insert('o');
236
+ * console.log(editor.getText()); // 'Hello'; // Output: "Hello"
237
+ *
238
+ * editor.delete();
239
+ * console.log(editor.getText()); // 'Hell'; // Output: "Hell"
240
+ *
241
+ * editor.undo();
242
+ * console.log(editor.getText()); // 'Hello'; // Output: "Hello"
243
+ *
244
+ * editor.moveCursor(1);
245
+ * editor.insert('a');
246
+ * console.log(editor.getText()); // 'Haello';
247
+ */
248
+ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, SinglyLinkedListNode<E>> {
249
+ protected _equals: (a: E, b: E) => boolean = Object.is as unknown as (a: E, b: E) => boolean;
250
+
251
+ /**
252
+ * Create a SinglyLinkedList and optionally bulk-insert elements.
253
+ * @remarks Time O(N), Space O(N)
254
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
255
+ * @param [options] - Options such as maxLen and toElementFn.
256
+ * @returns New SinglyLinkedList instance.
257
+ */
258
+
259
+ constructor(
260
+ elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>> = [],
261
+ options?: SinglyLinkedListOptions<E, R>
262
+ ) {
263
+ super(options);
264
+ this.pushMany(elements);
265
+ }
266
+
267
+ protected _head: SinglyLinkedListNode<E> | undefined;
268
+
269
+ /**
270
+ * Get the head node.
271
+ * @remarks Time O(1), Space O(1)
272
+ * @returns Head node or undefined.
273
+ */
274
+
275
+ get head(): SinglyLinkedListNode<E> | undefined {
276
+ return this._head;
277
+ }
278
+
279
+ protected _tail: SinglyLinkedListNode<E> | undefined;
280
+
281
+ /**
282
+ * Get the tail node.
283
+ * @remarks Time O(1), Space O(1)
284
+ * @returns Tail node or undefined.
285
+ */
286
+
287
+ get tail(): SinglyLinkedListNode<E> | undefined {
288
+ return this._tail;
289
+ }
290
+
291
+ protected _length = 0;
292
+
293
+ /**
294
+ * Get the number of elements.
295
+ * @remarks Time O(1), Space O(1)
296
+ * @returns Current length.
297
+ */
298
+
299
+ get length(): number {
300
+ return this._length;
301
+ }
302
+
303
+ /**
304
+ * Get the first element value.
305
+ * @remarks Time O(1), Space O(1)
306
+ * @returns First element or undefined.
307
+ */
308
+
309
+ get first(): E | undefined {
310
+ return this.head?.value;
311
+ }
312
+
313
+ /**
314
+ * Get the last element value.
315
+ * @remarks Time O(1), Space O(1)
316
+ * @returns Last element or undefined.
317
+ */
318
+
319
+ get last(): E | undefined {
320
+ return this.tail?.value;
321
+ }
322
+
323
+ /**
324
+ * Create a new list from an iterable of elements.
325
+ * @remarks Time O(N), Space O(N)
326
+ * @template E
327
+ * @template R
328
+ * @template S
329
+ * @param this - The constructor (subclass) to instantiate.
330
+ * @param data - Iterable of elements to insert.
331
+ * @param [options] - Options forwarded to the constructor.
332
+ * @returns A new list populated with the iterable's elements.
333
+ */
334
+
335
+ static from<E, R = any, S extends SinglyLinkedList<E, R> = SinglyLinkedList<E, R>>(
336
+ this: new (
337
+ elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>,
338
+ options?: SinglyLinkedListOptions<E, R>
339
+ ) => S,
340
+ data: Iterable<E>,
341
+ options?: SinglyLinkedListOptions<E, R>
342
+ ): S {
343
+ const list = new this([], options);
344
+ for (const x of data) list.push(x);
345
+ return list;
346
+ }
347
+
348
+ /**
349
+ * Append an element/node to the tail.
350
+ * @remarks Time O(1), Space O(1)
351
+ * @param elementOrNode - Element or node to append.
352
+ * @returns True when appended.
353
+ */
354
+
355
+ push(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
356
+ const newNode = this._ensureNode(elementOrNode);
357
+ if (!this.head) {
358
+ this._head = this._tail = newNode;
359
+ } else {
360
+ this.tail!.next = newNode;
361
+ this._tail = newNode;
362
+ }
363
+ this._length++;
364
+ if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
365
+ return true;
366
+ }
367
+
368
+ /**
369
+ * Remove and return the tail element.
370
+ * @remarks Time O(N), Space O(1)
371
+ * @returns Removed element or undefined.
372
+ */
373
+
374
+ pop(): E | undefined {
375
+ if (!this.head) return undefined;
376
+ if (this.head === this.tail) {
377
+ const value = this.head.value;
378
+ this._head = undefined;
379
+ this._tail = undefined;
380
+ this._length--;
381
+ return value;
382
+ }
383
+ let current = this.head;
384
+ while (current.next !== this.tail) current = current.next!;
385
+ const value = this.tail!.value;
386
+ current.next = undefined;
387
+ this._tail = current;
388
+ this._length--;
389
+ return value;
390
+ }
391
+
392
+ /**
393
+ * Remove and return the head element.
394
+ * @remarks Time O(1), Space O(1)
395
+ * @returns Removed element or undefined.
396
+ */
397
+
398
+ shift(): E | undefined {
399
+ if (!this.head) return undefined;
400
+ const removed = this.head;
401
+ this._head = this.head.next;
402
+ if (!this._head) this._tail = undefined;
403
+ this._length--;
404
+ return removed.value;
405
+ }
406
+
407
+ /**
408
+ * Prepend an element/node to the head.
409
+ * @remarks Time O(1), Space O(1)
410
+ * @param elementOrNode - Element or node to prepend.
411
+ * @returns True when prepended.
412
+ */
413
+
414
+ unshift(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
415
+ const newNode = this._ensureNode(elementOrNode);
416
+ if (!this.head) {
417
+ this._head = this._tail = newNode;
418
+ } else {
419
+ newNode.next = this.head;
420
+ this._head = newNode;
421
+ }
422
+ this._length++;
423
+ return true;
424
+ }
425
+
426
+ /**
427
+ * Append a sequence of elements/nodes.
428
+ * @remarks Time O(N), Space O(1)
429
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
430
+ * @returns Array of per-element success flags.
431
+ */
432
+
433
+ pushMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[] {
434
+ const ans: boolean[] = [];
435
+ for (const el of elements) {
436
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el as R)));
437
+ else ans.push(this.push(el as E | SinglyLinkedListNode<E>));
438
+ }
439
+ return ans;
440
+ }
441
+
442
+ /**
443
+ * Prepend a sequence of elements/nodes.
444
+ * @remarks Time O(N), Space O(1)
445
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
446
+ * @returns Array of per-element success flags.
447
+ */
448
+
449
+ unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[] {
450
+ const ans: boolean[] = [];
451
+ for (const el of elements) {
452
+ if (this.toElementFn) ans.push(this.unshift(this.toElementFn(el as R)));
453
+ else ans.push(this.unshift(el as E | SinglyLinkedListNode<E>));
454
+ }
455
+ return ans;
456
+ }
457
+
458
+ /**
459
+ * Find the first value matching a predicate (by node).
460
+ * @remarks Time O(N), Space O(1)
461
+ * @param elementNodeOrPredicate - Element, node, or node predicate to match.
462
+ * @returns Matched value or undefined.
463
+ */
464
+
465
+ search(
466
+ elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
467
+ ): E | undefined {
468
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
469
+ let current = this.head;
470
+ while (current) {
471
+ if (predicate(current)) return current.value;
472
+ current = current.next;
473
+ }
474
+ return undefined;
475
+ }
476
+
477
+ /**
478
+ * Get the element at a given index.
479
+ * @remarks Time O(N), Space O(1)
480
+ * @param index - Zero-based index.
481
+ * @returns Element or undefined.
482
+ */
483
+
484
+ at(index: number): E | undefined {
485
+ if (index < 0 || index >= this._length) return undefined;
486
+ let current = this.head;
487
+ for (let i = 0; i < index; i++) current = current!.next;
488
+ return current!.value;
489
+ }
490
+
491
+ /**
492
+ * Type guard: check whether the input is a SinglyLinkedListNode.
493
+ * @remarks Time O(1), Space O(1)
494
+ * @param elementNodeOrPredicate - Element, node, or predicate.
495
+ * @returns True if the value is a SinglyLinkedListNode.
496
+ */
497
+
498
+ isNode(
499
+ elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
500
+ ): elementNodeOrPredicate is SinglyLinkedListNode<E> {
501
+ return elementNodeOrPredicate instanceof SinglyLinkedListNode;
502
+ }
503
+
504
+ /**
505
+ * Get the node reference at a given index.
506
+ * @remarks Time O(N), Space O(1)
507
+ * @param index - Zero-based index.
508
+ * @returns Node or undefined.
509
+ */
510
+
511
+ getNodeAt(index: number): SinglyLinkedListNode<E> | undefined {
512
+ if (index < 0 || index >= this._length) return undefined;
513
+ let current = this.head;
514
+ for (let i = 0; i < index; i++) current = current!.next;
515
+ return current;
516
+ }
517
+
518
+ /**
519
+ * Delete the element at an index.
520
+ * @remarks Time O(N), Space O(1)
521
+ * @param index - Zero-based index.
522
+ * @returns Removed element or undefined.
523
+ */
524
+
525
+ deleteAt(index: number): E | undefined {
526
+ if (index < 0 || index >= this._length) return undefined;
527
+ if (index === 0) return this.shift();
528
+ const targetNode = this.getNodeAt(index)!;
529
+ const prevNode = this._getPrevNode(targetNode)!;
530
+ const value = targetNode.value;
531
+ prevNode.next = targetNode.next;
532
+ if (targetNode === this.tail) this._tail = prevNode;
533
+ this._length--;
534
+ return value;
535
+ }
536
+
537
+ /**
538
+ * Delete the first match by value/node.
539
+ * @remarks Time O(N), Space O(1)
540
+ * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
541
+ * @returns True if removed.
542
+ */
543
+
544
+ delete(elementOrNode: E | SinglyLinkedListNode<E> | undefined): boolean {
545
+ if (elementOrNode === undefined || !this.head) return false;
546
+ const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
547
+ if (!node) return false;
548
+ const prevNode = this._getPrevNode(node);
549
+
550
+ if (!prevNode) {
551
+ this._head = node.next;
552
+ if (node === this.tail) this._tail = undefined;
553
+ } else {
554
+ prevNode.next = node.next;
555
+ if (node === this.tail) this._tail = prevNode;
556
+ }
557
+ this._length--;
558
+ return true;
559
+ }
560
+
561
+ /**
562
+ * Insert a new element/node at an index, shifting following nodes.
563
+ * @remarks Time O(N), Space O(1)
564
+ * @param index - Zero-based index.
565
+ * @param newElementOrNode - Element or node to insert.
566
+ * @returns True if inserted.
567
+ */
568
+
569
+ addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean {
570
+ if (index < 0 || index > this._length) return false;
571
+ if (index === 0) return this.unshift(newElementOrNode);
572
+ if (index === this._length) return this.push(newElementOrNode);
573
+ const newNode = this._ensureNode(newElementOrNode);
574
+ const prevNode = this.getNodeAt(index - 1)!;
575
+ newNode.next = prevNode.next;
576
+ prevNode.next = newNode;
577
+ this._length++;
578
+ return true;
579
+ }
580
+
581
+ /**
582
+ * Set the element value at an index.
583
+ * @remarks Time O(N), Space O(1)
584
+ * @param index - Zero-based index.
585
+ * @param value - New value.
586
+ * @returns True if updated.
587
+ */
588
+
589
+ setAt(index: number, value: E): boolean {
590
+ const node = this.getNodeAt(index);
591
+ if (!node) return false;
592
+ node.value = value;
593
+ return true;
594
+ }
595
+
596
+ /**
597
+ * Check whether the list is empty.
598
+ * @remarks Time O(1), Space O(1)
599
+ * @returns True if length is 0.
600
+ */
601
+
602
+ isEmpty(): boolean {
603
+ return this._length === 0;
604
+ }
605
+
606
+ /**
607
+ * Remove all nodes and reset length.
608
+ * @remarks Time O(N), Space O(1)
609
+ * @returns void
610
+ */
611
+
612
+ clear(): void {
613
+ this._head = undefined;
614
+ this._tail = undefined;
615
+ this._length = 0;
616
+ }
617
+
618
+ /**
619
+ * Reverse the list in place.
620
+ * @remarks Time O(N), Space O(1)
621
+ * @returns This list.
622
+ */
623
+
624
+ reverse(): this {
625
+ if (!this.head || this.head === this.tail) return this;
626
+ let prev: SinglyLinkedListNode<E> | undefined;
627
+ let current: SinglyLinkedListNode<E> | undefined = this.head;
628
+ let next: SinglyLinkedListNode<E> | undefined;
629
+ while (current) {
630
+ next = current.next;
631
+ current.next = prev;
632
+ prev = current;
633
+ current = next;
634
+ }
635
+ [this._head, this._tail] = [this.tail!, this.head!];
636
+ return this;
637
+ }
638
+
639
+ /**
640
+ * Find a node by value, reference, or predicate.
641
+ * @remarks Time O(N), Space O(1)
642
+ * @param [elementNodeOrPredicate] - Element, node, or node predicate to match.
643
+ * @returns Matching node or undefined.
644
+ */
645
+
646
+ getNode(
647
+ elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined
648
+ ): SinglyLinkedListNode<E> | undefined {
649
+ if (elementNodeOrPredicate === undefined) return;
650
+ if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
651
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
652
+ let current = this.head;
653
+ while (current) {
654
+ if (predicate(current)) return current;
655
+ current = current.next;
656
+ }
657
+ return undefined;
658
+ }
659
+
660
+ /**
661
+ * Insert a new element/node before an existing one.
662
+ * @remarks Time O(N), Space O(1)
663
+ * @param existingElementOrNode - Existing element or node.
664
+ * @param newElementOrNode - Element or node to insert.
665
+ * @returns True if inserted.
666
+ */
667
+
668
+ addBefore(
669
+ existingElementOrNode: E | SinglyLinkedListNode<E>,
670
+ newElementOrNode: E | SinglyLinkedListNode<E>
671
+ ): boolean {
672
+ const existingNode = this.getNode(existingElementOrNode);
673
+ if (!existingNode) return false;
674
+ const prevNode = this._getPrevNode(existingNode);
675
+ const newNode = this._ensureNode(newElementOrNode);
676
+
677
+ if (!prevNode) {
678
+ newNode.next = this._head;
679
+ this._head = newNode;
680
+ if (!this._tail) this._tail = newNode;
681
+ this._length++;
682
+ } else {
683
+ prevNode.next = newNode;
684
+ newNode.next = existingNode;
685
+ this._length++;
686
+ }
687
+ return true;
688
+ }
689
+
690
+ /**
691
+ * Insert a new element/node after an existing one.
692
+ * @remarks Time O(N), Space O(1)
693
+ * @param existingElementOrNode - Existing element or node.
694
+ * @param newElementOrNode - Element or node to insert.
695
+ * @returns True if inserted.
696
+ */
697
+
698
+ addAfter(existingElementOrNode: E | SinglyLinkedListNode<E>, newElementOrNode: E | SinglyLinkedListNode<E>): boolean {
699
+ const existingNode = this.getNode(existingElementOrNode);
700
+ if (!existingNode) return false;
701
+ const newNode = this._ensureNode(newElementOrNode);
702
+ newNode.next = existingNode.next;
703
+ existingNode.next = newNode;
704
+ if (existingNode === this.tail) this._tail = newNode;
705
+ this._length++;
706
+ return true;
707
+ }
708
+
709
+ /**
710
+ * Remove and/or insert elements at a position (array-like behavior).
711
+ * @remarks Time O(N + M), Space O(M)
712
+ * @param start - Start index (clamped to [0, length]).
713
+ * @param [deleteCount] - Number of elements to remove (default 0).
714
+ * @param [items] - Elements to insert after `start`.
715
+ * @returns A new list containing the removed elements (typed as `this`).
716
+ */
717
+
718
+ override splice(start: number, deleteCount = 0, ...items: E[]): this {
719
+ start = Math.max(0, Math.min(start, this.length));
720
+ deleteCount = Math.max(0, deleteCount);
721
+
722
+ const removedList = this._createInstance();
723
+
724
+ const prevNode = start === 0 ? undefined : this.getNodeAt(start - 1);
725
+ let cur = prevNode ? prevNode.next : this.head;
726
+
727
+ let removedCount = 0;
728
+ while (removedCount < deleteCount && cur) {
729
+ removedList.push(cur.value);
730
+ cur = cur.next;
731
+ removedCount++;
732
+ }
733
+ const afterNode = cur;
734
+
735
+ if (prevNode) {
736
+ prevNode.next = afterNode;
737
+ } else {
738
+ this._head = afterNode;
739
+ }
740
+ if (!afterNode) this._tail = prevNode;
741
+
742
+ if (items.length > 0) {
743
+ let firstInserted: SinglyLinkedListNode<E> | undefined;
744
+ let lastInserted: SinglyLinkedListNode<E> | undefined;
745
+ for (const it of items) {
746
+ const node = this._ensureNode(it);
747
+ if (!firstInserted) firstInserted = node;
748
+ if (lastInserted) lastInserted.next = node;
749
+ lastInserted = node;
750
+ }
751
+ if (prevNode) prevNode.next = firstInserted!;
752
+ else this._head = firstInserted!;
753
+
754
+ lastInserted!.next = afterNode;
755
+ if (!afterNode) this._tail = lastInserted!;
756
+ }
757
+
758
+ this._length += items.length - removedCount;
759
+ if (this._length === 0) {
760
+ this._head = undefined;
761
+ this._tail = undefined;
762
+ }
763
+
764
+ return removedList as unknown as this;
765
+ }
766
+
767
+ /**
768
+ * Count how many nodes match a value/node/predicate.
769
+ * @remarks Time O(N), Space O(1)
770
+ * @param elementOrNode - Element, node, or node predicate to match.
771
+ * @returns Number of matches in the list.
772
+ */
773
+
774
+ countOccurrences(elementOrNode: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): number {
775
+ const predicate = elementOrPredicate(elementOrNode, this._equals);
776
+ let count = 0;
777
+ let current = this.head;
778
+ while (current) {
779
+ if (predicate(current)) count++;
780
+ current = current.next;
781
+ }
782
+ return count;
783
+ }
784
+
785
+ /**
786
+ * Set the equality comparator used to compare values.
787
+ * @remarks Time O(1), Space O(1)
788
+ * @param equals - Equality predicate (a, b) → boolean.
789
+ * @returns This list.
790
+ */
791
+
792
+ setEquality(equals: (a: E, b: E) => boolean): this {
793
+ this._equals = equals;
794
+ return this;
795
+ }
796
+
797
+ /**
798
+ * Delete the first node whose value matches a predicate.
799
+ * @remarks Time O(N), Space O(1)
800
+ * @param predicate - Predicate (value, index, list) → boolean to decide deletion.
801
+ * @returns True if a node was removed.
802
+ */
803
+
804
+ deleteWhere(predicate: (value: E, index: number, list: this) => boolean): boolean {
805
+ let prev: SinglyLinkedListNode<E> | undefined;
806
+ let current = this.head;
807
+ let i = 0;
808
+ while (current) {
809
+ if (predicate(current.value, i++, this)) {
810
+ if (!prev) {
811
+ this._head = current.next;
812
+ if (current === this._tail) this._tail = undefined;
813
+ } else {
814
+ prev.next = current.next;
815
+ if (current === this._tail) this._tail = prev;
816
+ }
817
+ this._length--;
818
+ return true;
819
+ }
820
+ prev = current;
821
+ current = current.next;
822
+ }
823
+ return false;
824
+ }
825
+
826
+ /**
827
+ * Deep clone this list (values are copied by reference).
828
+ * @remarks Time O(N), Space O(N)
829
+ * @returns A new list with the same element sequence.
830
+ */
831
+
832
+ clone(): this {
833
+ const out = this._createInstance();
834
+ for (const v of this) out.push(v);
835
+ return out;
836
+ }
837
+
838
+ /**
839
+ * Filter values into a new list of the same class.
840
+ * @remarks Time O(N), Space O(N)
841
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
842
+ * @param [thisArg] - Value for `this` inside the callback.
843
+ * @returns A new list with kept values.
844
+ */
845
+
846
+ filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): this {
847
+ const out = this._createInstance();
848
+ let index = 0;
849
+ for (const value of this) if (callback.call(thisArg, value, index++, this)) out.push(value);
850
+ return out;
851
+ }
852
+
853
+ /**
854
+ * Map values into a new list of the same class.
855
+ * @remarks Time O(N), Space O(N)
856
+ * @param callback - Mapping function (value, index, list) → newValue.
857
+ * @param [thisArg] - Value for `this` inside the callback.
858
+ * @returns A new list with mapped values.
859
+ */
860
+
861
+ mapSame(callback: ElementCallback<E, R, E>, thisArg?: any): this {
862
+ const out = this._createInstance();
863
+ let index = 0;
864
+ for (const value of this) {
865
+ const mv = thisArg === undefined ? callback(value, index++, this) : callback.call(thisArg, value, index++, this);
866
+ out.push(mv);
867
+ }
868
+ return out;
869
+ }
870
+
871
+ /**
872
+ * Map values into a new list (possibly different element type).
873
+ * @remarks Time O(N), Space O(N)
874
+ * @template EM
875
+ * @template RM
876
+ * @param callback - Mapping function (value, index, list) → newElement.
877
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
878
+ * @param [thisArg] - Value for `this` inside the callback.
879
+ * @returns A new SinglyLinkedList with mapped values.
880
+ */
881
+
882
+ map<EM, RM = any>(
883
+ callback: ElementCallback<E, R, EM>,
884
+ options?: SinglyLinkedListOptions<EM, RM>,
885
+ thisArg?: any
886
+ ): SinglyLinkedList<EM, RM> {
887
+ const out = this._createLike<EM, RM>([], { ...(options ?? {}), maxLen: this._maxLen as number });
888
+ let index = 0;
889
+ for (const value of this) out.push(callback.call(thisArg, value, index++, this));
890
+ return out;
891
+ }
892
+
893
+ /**
894
+ * (Protected) Create a node from a value.
895
+ * @remarks Time O(1), Space O(1)
896
+ * @param value - Value to wrap in a node.
897
+ * @returns A new SinglyLinkedListNode instance.
898
+ */
899
+
900
+ protected createNode(value: E): SinglyLinkedListNode<E> {
901
+ return new SinglyLinkedListNode<E>(value);
902
+ }
903
+
904
+ /**
905
+ * (Protected) Check if input is a node predicate function.
906
+ * @remarks Time O(1), Space O(1)
907
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
908
+ * @returns True if input is a predicate function.
909
+ */
910
+
911
+ protected _isPredicate(
912
+ elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
913
+ ): elementNodeOrPredicate is (node: SinglyLinkedListNode<E>) => boolean {
914
+ return typeof elementNodeOrPredicate === 'function';
915
+ }
916
+
917
+ /**
918
+ * (Protected) Normalize input into a node instance.
919
+ * @remarks Time O(1), Space O(1)
920
+ * @param elementOrNode - Element or node.
921
+ * @returns A SinglyLinkedListNode for the provided input.
922
+ */
923
+
924
+ protected _ensureNode(elementOrNode: E | SinglyLinkedListNode<E>) {
925
+ if (this.isNode(elementOrNode)) return elementOrNode;
926
+ return this.createNode(elementOrNode);
927
+ }
928
+
929
+ /**
930
+ * (Protected) Normalize input into a node predicate.
931
+ * @remarks Time O(1), Space O(1)
932
+ * @param elementNodeOrPredicate - Element, node, or predicate.
933
+ * @returns A predicate taking a node and returning true/false.
934
+ */
935
+
936
+ protected _ensurePredicate(
937
+ elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
938
+ ) {
939
+ if (this.isNode(elementNodeOrPredicate)) return (node: SinglyLinkedListNode<E>) => node === elementNodeOrPredicate;
940
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
941
+ const value = elementNodeOrPredicate as E;
942
+ return (node: SinglyLinkedListNode<E>) => this._equals(node.value, value);
943
+ }
944
+
945
+ /**
946
+ * (Protected) Get the previous node of a given node.
947
+ * @remarks Time O(N), Space O(1)
948
+ * @param node - A node in the list.
949
+ * @returns Previous node or undefined.
950
+ */
951
+
952
+ protected _getPrevNode(node: SinglyLinkedListNode<E>): SinglyLinkedListNode<E> | undefined {
953
+ if (!this.head || this.head === node) return undefined;
954
+ let current = this.head;
955
+ while (current.next && current.next !== node) current = current.next;
956
+ return current.next === node ? current : undefined;
957
+ }
958
+
959
+ /**
960
+ * (Protected) Iterate values from head to tail.
961
+ * @remarks Time O(N), Space O(1)
962
+ * @returns Iterator of values (E).
963
+ */
964
+
965
+ protected *_getIterator(): IterableIterator<E> {
966
+ let current = this.head;
967
+ while (current) {
968
+ yield current.value;
969
+ current = current.next;
970
+ }
971
+ }
972
+
973
+ /**
974
+ * (Protected) Iterate values from tail to head.
975
+ * @remarks Time O(N), Space O(N)
976
+ * @returns Iterator of values (E).
977
+ */
978
+
979
+ protected *_getReverseIterator(): IterableIterator<E> {
980
+ const reversedArr = [...this].reverse();
981
+ for (const item of reversedArr) yield item;
982
+ }
983
+
984
+ /**
985
+ * (Protected) Iterate nodes from head to tail.
986
+ * @remarks Time O(N), Space O(1)
987
+ * @returns Iterator of nodes.
988
+ */
989
+
990
+ protected *_getNodeIterator(): IterableIterator<SinglyLinkedListNode<E>> {
991
+ let current = this.head;
992
+ while (current) {
993
+ yield current;
994
+ current = current.next;
995
+ }
996
+ }
997
+
998
+ /**
999
+ * (Protected) Create an empty instance of the same concrete class.
1000
+ * @remarks Time O(1), Space O(1)
1001
+ * @param [options] - Options forwarded to the constructor.
1002
+ * @returns An empty like-kind list instance.
1003
+ */
1004
+
1005
+ protected _createInstance(options?: SinglyLinkedListOptions<E, R>): this {
1006
+ const Ctor: any = this.constructor;
1007
+ return new Ctor([], options);
1008
+ }
1009
+
1010
+ /**
1011
+ * (Protected) Create a like-kind instance and seed it from an iterable.
1012
+ * @remarks Time O(N), Space O(N)
1013
+ * @template EM
1014
+ * @template RM
1015
+ * @param [elements] - Iterable used to seed the new list.
1016
+ * @param [options] - Options forwarded to the constructor.
1017
+ * @returns A like-kind SinglyLinkedList instance.
1018
+ */
1019
+
1020
+ protected _createLike<EM, RM>(
1021
+ elements: Iterable<EM> | Iterable<RM> | Iterable<SinglyLinkedListNode<EM>> = [],
1022
+ options?: SinglyLinkedListOptions<EM, RM>
1023
+ ): SinglyLinkedList<EM, RM> {
1024
+ const Ctor: any = this.constructor;
1025
+ return new Ctor(elements, options) as SinglyLinkedList<EM, RM>;
1026
+ }
1027
+
1028
+ /**
1029
+ * (Protected) Spawn an empty like-kind list instance.
1030
+ * @remarks Time O(1), Space O(1)
1031
+ * @template EM
1032
+ * @template RM
1033
+ * @param [options] - Options forwarded to the constructor.
1034
+ * @returns An empty like-kind SinglyLinkedList instance.
1035
+ */
1036
+
1037
+ protected _spawnLike<EM, RM>(options?: SinglyLinkedListOptions<EM, RM>): SinglyLinkedList<EM, RM> {
1038
+ return this._createLike<EM, RM>([], options);
1039
+ }
1040
+ }
1041
+
1042
+ function elementOrPredicate<E>(
1043
+ input: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean),
1044
+ equals: (a: E, b: E) => boolean
1045
+ ) {
1046
+ if (input instanceof SinglyLinkedListNode) return (node: SinglyLinkedListNode<E>) => node === input;
1047
+ if (typeof input === 'function') return input as (node: SinglyLinkedListNode<E>) => boolean;
1048
+ const value = input as E;
1049
+ return (node: SinglyLinkedListNode<E>) => equals(node.value, value);
1050
+ }