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,1001 @@
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 {
10
+ DequeOptions,
11
+ ElementCallback,
12
+ IterableElementBaseOptions,
13
+ IterableWithSizeOrLength,
14
+ LinearBaseOptions
15
+ } from '../../types';
16
+ import { calcMinUnitsRequired, rangeCheck } from '../../utils';
17
+ import { LinearBase } from '../base/linear-base';
18
+
19
+ /**
20
+ * Deque implemented with circular buckets allowing O(1) amortized push/pop at both ends.
21
+ * @remarks Time O(1), Space O(1)
22
+ * @template E
23
+ * @template R
24
+ * 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
25
+ * 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
26
+ * 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
27
+ * 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
28
+ * 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
29
+ * @example
30
+ * // basic Deque creation and push/pop operations
31
+ * // Create a simple Deque with initial values
32
+ * const deque = new Deque([1, 2, 3, 4, 5]);
33
+ *
34
+ * // Verify the deque maintains insertion order
35
+ * console.log([...deque]); // [1, 2, 3, 4, 5];
36
+ *
37
+ * // Check length
38
+ * console.log(deque.length); // 5;
39
+ *
40
+ * // Push to the end
41
+ * deque.push(6);
42
+ * console.log(deque.length); // 6;
43
+ *
44
+ * // Pop from the end
45
+ * const last = deque.pop();
46
+ * console.log(last); // 6;
47
+ * @example
48
+ * // Deque shift and unshift operations
49
+ * const deque = new Deque<number>([20, 30, 40]);
50
+ *
51
+ * // Unshift adds to the front
52
+ * deque.unshift(10);
53
+ * console.log([...deque]); // [10, 20, 30, 40];
54
+ *
55
+ * // Shift removes from the front (O(1) complexity!)
56
+ * const first = deque.shift();
57
+ * console.log(first); // 10;
58
+ *
59
+ * // Verify remaining elements
60
+ * console.log([...deque]); // [20, 30, 40];
61
+ * console.log(deque.length); // 3;
62
+ * @example
63
+ * // Deque peek at both ends
64
+ * const deque = new Deque<number>([10, 20, 30, 40, 50]);
65
+ *
66
+ * // Get first element without removing
67
+ * const first = deque.at(0);
68
+ * console.log(first); // 10;
69
+ *
70
+ * // Get last element without removing
71
+ * const last = deque.at(deque.length - 1);
72
+ * console.log(last); // 50;
73
+ *
74
+ * // Length unchanged
75
+ * console.log(deque.length); // 5;
76
+ * @example
77
+ * // Deque for...of iteration and reverse
78
+ * const deque = new Deque<string>(['A', 'B', 'C', 'D']);
79
+ *
80
+ * // Iterate forward
81
+ * const forward: string[] = [];
82
+ * for (const item of deque) {
83
+ * forward.push(item);
84
+ * }
85
+ * console.log(forward); // ['A', 'B', 'C', 'D'];
86
+ *
87
+ * // Reverse the deque
88
+ * deque.reverse();
89
+ * const backward: string[] = [];
90
+ * for (const item of deque) {
91
+ * backward.push(item);
92
+ * }
93
+ * console.log(backward); // ['D', 'C', 'B', 'A'];
94
+ * @example
95
+ * // Deque as sliding window for stream processing
96
+ * interface DataPoint {
97
+ * timestamp: number;
98
+ * value: number;
99
+ * sensor: string;
100
+ * }
101
+ *
102
+ * // Create a deque-based sliding window for real-time data aggregation
103
+ * const windowSize = 3;
104
+ * const dataWindow = new Deque<DataPoint>();
105
+ *
106
+ * // Simulate incoming sensor data stream
107
+ * const incomingData: DataPoint[] = [
108
+ * { timestamp: 1000, value: 25.5, sensor: 'temp-01' },
109
+ * { timestamp: 1100, value: 26.2, sensor: 'temp-01' },
110
+ * { timestamp: 1200, value: 25.8, sensor: 'temp-01' },
111
+ * { timestamp: 1300, value: 27.1, sensor: 'temp-01' },
112
+ * { timestamp: 1400, value: 26.9, sensor: 'temp-01' }
113
+ * ];
114
+ *
115
+ * const windowResults: Array<{ avgValue: number; windowSize: number }> = [];
116
+ *
117
+ * for (const dataPoint of incomingData) {
118
+ * // Add new data to the end
119
+ * dataWindow.push(dataPoint);
120
+ *
121
+ * // Remove oldest data when window exceeds size (O(1) from front)
122
+ * if (dataWindow.length > windowSize) {
123
+ * dataWindow.shift();
124
+ * }
125
+ *
126
+ * // Calculate average of current window
127
+ * let sum = 0;
128
+ * for (const point of dataWindow) {
129
+ * sum += point.value;
130
+ * }
131
+ * const avg = sum / dataWindow.length;
132
+ *
133
+ * windowResults.push({
134
+ * avgValue: Math.round(avg * 10) / 10,
135
+ * windowSize: dataWindow.length
136
+ * });
137
+ * }
138
+ *
139
+ * // Verify sliding window behavior
140
+ * console.log(windowResults.length); // 5;
141
+ * console.log(windowResults[0].windowSize); // 1; // First window has 1 element
142
+ * console.log(windowResults[2].windowSize); // 3; // Windows are at max size from 3rd onwards
143
+ * console.log(windowResults[4].windowSize); // 3; // Last window still has 3 elements
144
+ * console.log(dataWindow.length); // 3;
145
+ */
146
+ export class Deque<E = any, R = any> extends LinearBase<E, R> {
147
+ protected _equals: (a: E, b: E) => boolean = Object.is as unknown as (a: E, b: E) => boolean;
148
+
149
+ /**
150
+ * Create a Deque and optionally bulk-insert elements.
151
+ * @remarks Time O(N), Space O(N)
152
+ * @param [elements] - Iterable (or iterable-like) of elements/records to insert.
153
+ * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
154
+ * @returns New Deque instance.
155
+ */
156
+
157
+ constructor(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R> = [], options?: DequeOptions<E, R>) {
158
+ super(options);
159
+
160
+ if (options) {
161
+ const { bucketSize } = options;
162
+ if (typeof bucketSize === 'number') this._bucketSize = bucketSize;
163
+ }
164
+
165
+ let _size: number;
166
+ if ('length' in elements) {
167
+ _size = typeof elements.length === 'function' ? elements.length() : elements.length;
168
+ } else {
169
+ _size = typeof elements.size === 'function' ? elements.size() : elements.size;
170
+ }
171
+
172
+ this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
173
+ for (let i = 0; i < this._bucketCount; ++i) {
174
+ this._buckets.push(new Array(this._bucketSize));
175
+ }
176
+ const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
177
+ this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
178
+ this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
179
+ this.pushMany(elements);
180
+ }
181
+
182
+ protected _bucketSize: number = 1 << 12;
183
+
184
+ /**
185
+ * Get the current bucket size.
186
+ * @remarks Time O(1), Space O(1)
187
+ * @returns Bucket capacity per bucket.
188
+ */
189
+
190
+ get bucketSize() {
191
+ return this._bucketSize;
192
+ }
193
+
194
+ protected _bucketFirst = 0;
195
+
196
+ /**
197
+ * Get the index of the first bucket in use.
198
+ * @remarks Time O(1), Space O(1)
199
+ * @returns Zero-based bucket index.
200
+ */
201
+
202
+ get bucketFirst(): number {
203
+ return this._bucketFirst;
204
+ }
205
+
206
+ protected _firstInBucket = 0;
207
+
208
+ /**
209
+ * Get the index inside the first bucket.
210
+ * @remarks Time O(1), Space O(1)
211
+ * @returns Zero-based index within the first bucket.
212
+ */
213
+
214
+ get firstInBucket(): number {
215
+ return this._firstInBucket;
216
+ }
217
+
218
+ protected _bucketLast = 0;
219
+
220
+ /**
221
+ * Get the index of the last bucket in use.
222
+ * @remarks Time O(1), Space O(1)
223
+ * @returns Zero-based bucket index.
224
+ */
225
+
226
+ get bucketLast(): number {
227
+ return this._bucketLast;
228
+ }
229
+
230
+ protected _lastInBucket = 0;
231
+
232
+ /**
233
+ * Get the index inside the last bucket.
234
+ * @remarks Time O(1), Space O(1)
235
+ * @returns Zero-based index within the last bucket.
236
+ */
237
+
238
+ get lastInBucket(): number {
239
+ return this._lastInBucket;
240
+ }
241
+
242
+ protected _bucketCount = 0;
243
+
244
+ /**
245
+ * Get the number of buckets allocated.
246
+ * @remarks Time O(1), Space O(1)
247
+ * @returns Bucket count.
248
+ */
249
+
250
+ get bucketCount(): number {
251
+ return this._bucketCount;
252
+ }
253
+
254
+ protected _buckets: E[][] = [];
255
+
256
+ /**
257
+ * Get the internal buckets array.
258
+ * @remarks Time O(1), Space O(1)
259
+ * @returns Array of buckets storing values.
260
+ */
261
+
262
+ get buckets() {
263
+ return this._buckets;
264
+ }
265
+
266
+ protected _length = 0;
267
+
268
+ /**
269
+ * Get the number of elements in the deque.
270
+ * @remarks Time O(1), Space O(1)
271
+ * @returns Current length.
272
+ */
273
+
274
+ get length() {
275
+ return this._length;
276
+ }
277
+
278
+ /**
279
+ * Get the first element without removing it.
280
+ * @remarks Time O(1), Space O(1)
281
+ * @returns First element or undefined.
282
+ */
283
+
284
+ get first(): E | undefined {
285
+ if (this._length === 0) return;
286
+ return this._buckets[this._bucketFirst][this._firstInBucket];
287
+ }
288
+
289
+ /**
290
+ * Get the last element without removing it.
291
+ * @remarks Time O(1), Space O(1)
292
+ * @returns Last element or undefined.
293
+ */
294
+
295
+ get last(): E | undefined {
296
+ if (this._length === 0) return;
297
+ return this._buckets[this._bucketLast][this._lastInBucket];
298
+ }
299
+
300
+ /**
301
+ * Create a Deque from an array of elements.
302
+ * @remarks Time O(N), Space O(N)
303
+ * @template E
304
+ * @template R
305
+ * @param this - Constructor (subclass) to instantiate.
306
+ * @param data - Array of elements to insert in order.
307
+ * @param [options] - Options forwarded to the constructor.
308
+ * @returns A new Deque populated from the array.
309
+ */
310
+
311
+ static fromArray<E, R = any>(
312
+ this: new (
313
+ elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>,
314
+ options?: DequeOptions<E, R>
315
+ ) => any,
316
+ data: E[],
317
+ options?: DequeOptions<E, R>
318
+ ) {
319
+ return new this(data, options);
320
+ }
321
+
322
+ /**
323
+ * Append one element at the back.
324
+ * @remarks Time O(1) amortized, Space O(1)
325
+ * @param element - Element to append.
326
+ * @returns True when appended.
327
+ */
328
+
329
+ push(element: E): boolean {
330
+ if (this._length) {
331
+ if (this._lastInBucket < this._bucketSize - 1) {
332
+ this._lastInBucket += 1;
333
+ } else if (this._bucketLast < this._bucketCount - 1) {
334
+ this._bucketLast += 1;
335
+ this._lastInBucket = 0;
336
+ } else {
337
+ this._bucketLast = 0;
338
+ this._lastInBucket = 0;
339
+ }
340
+ if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket) this._reallocate();
341
+ }
342
+ this._length += 1;
343
+ this._buckets[this._bucketLast][this._lastInBucket] = element;
344
+ if (this._maxLen > 0 && this._length > this._maxLen) this.shift();
345
+ return true;
346
+ }
347
+
348
+ /**
349
+ * Remove and return the last element.
350
+ * @remarks Time O(1), Space O(1)
351
+ * @returns Removed element or undefined.
352
+ */
353
+
354
+ pop(): E | undefined {
355
+ if (this._length === 0) return;
356
+ const element = this._buckets[this._bucketLast][this._lastInBucket];
357
+ if (this._length !== 1) {
358
+ if (this._lastInBucket > 0) {
359
+ this._lastInBucket -= 1;
360
+ } else if (this._bucketLast > 0) {
361
+ this._bucketLast -= 1;
362
+ this._lastInBucket = this._bucketSize - 1;
363
+ } else {
364
+ this._bucketLast = this._bucketCount - 1;
365
+ this._lastInBucket = this._bucketSize - 1;
366
+ }
367
+ }
368
+ this._length -= 1;
369
+ return element;
370
+ }
371
+
372
+ /**
373
+ * Remove and return the first element.
374
+ * @remarks Time O(1) amortized, Space O(1)
375
+ * @returns Removed element or undefined.
376
+ */
377
+
378
+ shift(): E | undefined {
379
+ if (this._length === 0) return;
380
+ const element = this._buckets[this._bucketFirst][this._firstInBucket];
381
+ if (this._length !== 1) {
382
+ if (this._firstInBucket < this._bucketSize - 1) {
383
+ this._firstInBucket += 1;
384
+ } else if (this._bucketFirst < this._bucketCount - 1) {
385
+ this._bucketFirst += 1;
386
+ this._firstInBucket = 0;
387
+ } else {
388
+ this._bucketFirst = 0;
389
+ this._firstInBucket = 0;
390
+ }
391
+ }
392
+ this._length -= 1;
393
+ return element;
394
+ }
395
+
396
+ /**
397
+ * Prepend one element at the front.
398
+ * @remarks Time O(1) amortized, Space O(1)
399
+ * @param element - Element to prepend.
400
+ * @returns True when prepended.
401
+ */
402
+
403
+ unshift(element: E): boolean {
404
+ if (this._length) {
405
+ if (this._firstInBucket > 0) {
406
+ this._firstInBucket -= 1;
407
+ } else if (this._bucketFirst > 0) {
408
+ this._bucketFirst -= 1;
409
+ this._firstInBucket = this._bucketSize - 1;
410
+ } else {
411
+ this._bucketFirst = this._bucketCount - 1;
412
+ this._firstInBucket = this._bucketSize - 1;
413
+ }
414
+ if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket) this._reallocate();
415
+ }
416
+ this._length += 1;
417
+ this._buckets[this._bucketFirst][this._firstInBucket] = element;
418
+ if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
419
+ return true;
420
+ }
421
+
422
+ /**
423
+ * Append a sequence of elements.
424
+ * @remarks Time O(N), Space O(1)
425
+ * @param elements - Iterable (or iterable-like) of elements/records.
426
+ * @returns Array of per-element success flags.
427
+ */
428
+
429
+ pushMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>) {
430
+ const ans: boolean[] = [];
431
+ for (const el of elements) {
432
+ if (this.toElementFn) {
433
+ ans.push(this.push(this.toElementFn(el as R)));
434
+ } else {
435
+ ans.push(this.push(el as E));
436
+ }
437
+ }
438
+ return ans;
439
+ }
440
+
441
+ /**
442
+ * Prepend a sequence of elements.
443
+ * @remarks Time O(N), Space O(1)
444
+ * @param [elements] - Iterable (or iterable-like) of elements/records.
445
+ * @returns Array of per-element success flags.
446
+ */
447
+
448
+ unshiftMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R> = []) {
449
+ const ans: boolean[] = [];
450
+ for (const el of elements) {
451
+ if (this.toElementFn) {
452
+ ans.push(this.unshift(this.toElementFn(el as R)));
453
+ } else {
454
+ ans.push(this.unshift(el as E));
455
+ }
456
+ }
457
+ return ans;
458
+ }
459
+
460
+ /**
461
+ * Check whether the deque is empty.
462
+ * @remarks Time O(1), Space O(1)
463
+ * @returns True if length is 0.
464
+ */
465
+
466
+ isEmpty(): boolean {
467
+ return this._length === 0;
468
+ }
469
+
470
+ /**
471
+ * Remove all elements and reset structure.
472
+ * @remarks Time O(1), Space O(1)
473
+ * @returns void
474
+ */
475
+
476
+ clear(): void {
477
+ this._buckets = [new Array(this._bucketSize)];
478
+ this._bucketCount = 1;
479
+ this._bucketFirst = this._bucketLast = this._length = 0;
480
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
481
+ }
482
+
483
+ /**
484
+ * Get the element at a given position.
485
+ * @remarks Time O(1), Space O(1)
486
+ * @param pos - Zero-based position from the front.
487
+ * @returns Element or undefined.
488
+ */
489
+
490
+ at(pos: number): E | undefined {
491
+ if (pos < 0 || pos >= this._length) return undefined;
492
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
493
+ return this._buckets[bucketIndex][indexInBucket];
494
+ }
495
+
496
+ /**
497
+ * Replace the element at a given position.
498
+ * @remarks Time O(1), Space O(1)
499
+ * @param pos - Zero-based position from the front.
500
+ * @param element - New element value.
501
+ * @returns True if updated.
502
+ */
503
+
504
+ setAt(pos: number, element: E): boolean {
505
+ rangeCheck(pos, 0, this._length - 1);
506
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
507
+ this._buckets[bucketIndex][indexInBucket] = element;
508
+ return true;
509
+ }
510
+
511
+ /**
512
+ * Insert repeated copies of an element at a position.
513
+ * @remarks Time O(N), Space O(1)
514
+ * @param pos - Zero-based position from the front.
515
+ * @param element - Element to insert.
516
+ * @param [num] - Number of times to insert (default 1).
517
+ * @returns True if inserted.
518
+ */
519
+
520
+ addAt(pos: number, element: E, num = 1): boolean {
521
+ const length = this._length;
522
+ rangeCheck(pos, 0, length);
523
+ if (pos === 0) {
524
+ while (num--) this.unshift(element);
525
+ } else if (pos === this._length) {
526
+ while (num--) this.push(element);
527
+ } else {
528
+ const arr: E[] = [];
529
+ for (let i = pos; i < this._length; ++i) {
530
+ const v = this.at(i);
531
+ if (v !== undefined) arr.push(v);
532
+ }
533
+ this.cut(pos - 1, true);
534
+ for (let i = 0; i < num; ++i) this.push(element);
535
+ for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
536
+ }
537
+ return true;
538
+ }
539
+
540
+ /**
541
+ * Cut the deque to keep items up to index; optionally mutate in-place.
542
+ * @remarks Time O(N), Space O(1)
543
+ * @param pos - Last index to keep.
544
+ * @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
545
+ * @returns This deque if in-place; otherwise a new deque of the prefix.
546
+ */
547
+
548
+ cut(pos: number, isCutSelf = false): Deque<E> {
549
+ if (isCutSelf) {
550
+ if (pos < 0) {
551
+ this.clear();
552
+ return this;
553
+ }
554
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
555
+ this._bucketLast = bucketIndex;
556
+ this._lastInBucket = indexInBucket;
557
+ this._length = pos + 1;
558
+ return this;
559
+ } else {
560
+ const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
561
+ newDeque._setBucketSize(this._bucketSize);
562
+ for (let i = 0; i <= pos; i++) {
563
+ const v = this.at(i);
564
+ if (v !== undefined) newDeque.push(v);
565
+ }
566
+
567
+ return newDeque;
568
+ }
569
+ }
570
+
571
+ /**
572
+ * Remove and/or insert elements at a position (array-like behavior).
573
+ * @remarks Time O(N + M), Space O(M)
574
+ * @param start - Start index (clamped to [0, length]).
575
+ * @param [deleteCount] - Number of elements to remove (default: length - start).
576
+ * @param [items] - Elements to insert after `start`.
577
+ * @returns A new deque containing the removed elements (typed as `this`).
578
+ */
579
+
580
+ override splice(start: number, deleteCount: number = this._length - start, ...items: E[]): this {
581
+ rangeCheck(start, 0, this._length);
582
+ if (deleteCount < 0) deleteCount = 0;
583
+ if (start + deleteCount > this._length) deleteCount = this._length - start;
584
+
585
+ const removed = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
586
+ removed._setBucketSize(this._bucketSize);
587
+ for (let i = 0; i < deleteCount; i++) {
588
+ const v = this.at(start + i);
589
+ if (v !== undefined) removed.push(v);
590
+ }
591
+
592
+ const tail: E[] = [];
593
+ for (let i = start + deleteCount; i < this._length; i++) {
594
+ const v = this.at(i);
595
+ if (v !== undefined) tail.push(v);
596
+ }
597
+
598
+ this.cut(start - 1, true);
599
+
600
+ for (const it of items) this.push(it);
601
+
602
+ for (const v of tail) this.push(v);
603
+
604
+ return removed as unknown as this;
605
+ }
606
+
607
+ /**
608
+ * Cut the deque to keep items from index onward; optionally mutate in-place.
609
+ * @remarks Time O(N), Space O(1)
610
+ * @param pos - First index to keep.
611
+ * @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
612
+ * @returns This deque if in-place; otherwise a new deque of the suffix.
613
+ */
614
+
615
+ cutRest(pos: number, isCutSelf = false): Deque<E> {
616
+ if (isCutSelf) {
617
+ if (pos < 0) {
618
+ return this;
619
+ }
620
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
621
+ this._bucketFirst = bucketIndex;
622
+ this._firstInBucket = indexInBucket;
623
+ this._length = this._length - pos;
624
+ return this;
625
+ } else {
626
+ const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
627
+ newDeque._setBucketSize(this._bucketSize);
628
+ if (pos < 0) pos = 0;
629
+ for (let i = pos; i < this._length; i++) {
630
+ const v = this.at(i);
631
+ if (v !== undefined) newDeque.push(v);
632
+ }
633
+ return newDeque;
634
+ }
635
+ }
636
+
637
+ /**
638
+ * Delete the element at a given position.
639
+ * @remarks Time O(N), Space O(1)
640
+ * @param pos - Zero-based position from the front.
641
+ * @returns Removed element or undefined.
642
+ */
643
+
644
+ deleteAt(pos: number): E | undefined {
645
+ rangeCheck(pos, 0, this._length - 1);
646
+
647
+ let deleted: E | undefined;
648
+ if (pos === 0) {
649
+ return this.shift();
650
+ } else if (pos === this._length - 1) {
651
+ deleted = this.last;
652
+ this.pop();
653
+ return deleted;
654
+ } else {
655
+ const length = this._length - 1;
656
+ const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
657
+ deleted = this._buckets[targetBucket][targetPointer];
658
+
659
+ for (let i = pos; i < length; i++) {
660
+ const { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(i);
661
+ const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
662
+ this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
663
+ }
664
+
665
+ this.pop();
666
+ return deleted;
667
+ }
668
+ }
669
+
670
+ /**
671
+ * Delete the first occurrence of a value.
672
+ * @remarks Time O(N), Space O(1)
673
+ * @param element - Element to remove (using the configured equality).
674
+ * @returns True if an element was removed.
675
+ */
676
+
677
+ delete(element: E): boolean {
678
+ const size = this._length;
679
+ if (size === 0) return false;
680
+ let i = 0;
681
+ let index = 0;
682
+ while (i < size) {
683
+ const oldElement = this.at(i);
684
+ if (!this._equals(oldElement as E, element)) {
685
+ this.setAt(index, oldElement!);
686
+ index += 1;
687
+ }
688
+ i += 1;
689
+ }
690
+ this.cut(index - 1, true);
691
+ return true;
692
+ }
693
+
694
+ /**
695
+ * Delete the first element matching a predicate.
696
+ * @remarks Time O(N), Space O(1)
697
+ * @param predicate - Function (value, index, deque) → boolean.
698
+ * @returns True if a match was removed.
699
+ */
700
+
701
+ deleteWhere(predicate: (value: E, index: number, deque: this) => boolean): boolean {
702
+ for (let i = 0; i < this._length; i++) {
703
+ const v = this.at(i);
704
+ if (predicate(v as E, i, this)) {
705
+ this.deleteAt(i);
706
+ return true;
707
+ }
708
+ }
709
+ return false;
710
+ }
711
+
712
+ /**
713
+ * Set the equality comparator used by delete operations.
714
+ * @remarks Time O(1), Space O(1)
715
+ * @param equals - Equality predicate (a, b) → boolean.
716
+ * @returns This deque.
717
+ */
718
+
719
+ setEquality(equals: (a: E, b: E) => boolean): this {
720
+ this._equals = equals;
721
+ return this;
722
+ }
723
+
724
+ /**
725
+ * Reverse the deque by reversing buckets and pointers.
726
+ * @remarks Time O(N), Space O(N)
727
+ * @returns This deque.
728
+ */
729
+
730
+ reverse(): this {
731
+ this._buckets.reverse().forEach(function (bucket) {
732
+ bucket.reverse();
733
+ });
734
+ const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
735
+ this._bucketFirst = this._bucketCount - _bucketLast - 1;
736
+ this._bucketLast = this._bucketCount - _bucketFirst - 1;
737
+ this._firstInBucket = this._bucketSize - _lastInBucket - 1;
738
+ this._lastInBucket = this._bucketSize - _firstInBucket - 1;
739
+ return this;
740
+ }
741
+
742
+ /**
743
+ * Deduplicate consecutive equal elements in-place.
744
+ * @remarks Time O(N), Space O(1)
745
+ * @returns This deque.
746
+ */
747
+
748
+ unique(): this {
749
+ if (this._length <= 1) {
750
+ return this;
751
+ }
752
+ let index = 1;
753
+ let prev = this.at(0);
754
+ for (let i = 1; i < this._length; ++i) {
755
+ const cur = this.at(i);
756
+ if (!this._equals(cur as E, prev as E)) {
757
+ prev = cur;
758
+ this.setAt(index++, cur as E);
759
+ }
760
+ }
761
+ this.cut(index - 1, true);
762
+ return this;
763
+ }
764
+
765
+ /**
766
+ * Trim unused buckets to fit exactly the active range.
767
+ * @remarks Time O(N), Space O(1)
768
+ * @returns void
769
+ */
770
+
771
+ shrinkToFit(): void {
772
+ if (this._length === 0) return;
773
+ const newBuckets = [] as E[][];
774
+ if (this._bucketFirst === this._bucketLast) return;
775
+ else if (this._bucketFirst < this._bucketLast) {
776
+ for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
777
+ newBuckets.push(this._buckets[i]);
778
+ }
779
+ } else {
780
+ for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
781
+ newBuckets.push(this._buckets[i]);
782
+ }
783
+ for (let i = 0; i <= this._bucketLast; ++i) {
784
+ newBuckets.push(this._buckets[i]);
785
+ }
786
+ }
787
+ this._bucketFirst = 0;
788
+ this._bucketLast = newBuckets.length - 1;
789
+ this._buckets = newBuckets;
790
+ }
791
+
792
+ /**
793
+ * Deep clone this deque, preserving options.
794
+ * @remarks Time O(N), Space O(N)
795
+ * @returns A new deque with the same content and options.
796
+ */
797
+
798
+ clone(): this {
799
+ return this._createLike<E, R>(this, {
800
+ bucketSize: this.bucketSize,
801
+ toElementFn: this.toElementFn,
802
+ maxLen: this._maxLen
803
+ }) as this;
804
+ }
805
+
806
+ /**
807
+ * Filter elements into a new deque of the same class.
808
+ * @remarks Time O(N), Space O(N)
809
+ * @param predicate - Predicate (value, index, deque) → boolean to keep element.
810
+ * @param [thisArg] - Value for `this` inside the predicate.
811
+ * @returns A new deque with kept elements.
812
+ */
813
+
814
+ filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): this {
815
+ const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
816
+ out._setBucketSize(this._bucketSize);
817
+ let index = 0;
818
+ for (const el of this) {
819
+ if (predicate.call(thisArg, el, index, this)) out.push(el);
820
+ index++;
821
+ }
822
+ return out;
823
+ }
824
+
825
+ /**
826
+ * Map elements into a new deque of the same element type.
827
+ * @remarks Time O(N), Space O(N)
828
+ * @param callback - Mapping function (value, index, deque) → newValue.
829
+ * @param [thisArg] - Value for `this` inside the callback.
830
+ * @returns A new deque with mapped values.
831
+ */
832
+
833
+ mapSame(callback: ElementCallback<E, R, E>, thisArg?: any): this {
834
+ const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
835
+ out._setBucketSize(this._bucketSize);
836
+ let index = 0;
837
+ for (const v of this) {
838
+ const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
839
+ out.push(mv);
840
+ }
841
+ return out;
842
+ }
843
+
844
+ /**
845
+ * Map elements into a new deque (possibly different element type).
846
+ * @remarks Time O(N), Space O(N)
847
+ * @template EM
848
+ * @template RM
849
+ * @param callback - Mapping function (value, index, deque) → newElement.
850
+ * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
851
+ * @param [thisArg] - Value for `this` inside the callback.
852
+ * @returns A new Deque with mapped elements.
853
+ */
854
+
855
+ map<EM, RM>(
856
+ callback: ElementCallback<E, R, EM>,
857
+ options?: IterableElementBaseOptions<EM, RM>,
858
+ thisArg?: any
859
+ ): Deque<EM, RM> {
860
+ const out = this._createLike<EM, RM>([], {
861
+ ...(options ?? {}),
862
+ bucketSize: this._bucketSize,
863
+ maxLen: this._maxLen
864
+ }) as Deque<EM, RM>;
865
+ let index = 0;
866
+ for (const el of this) {
867
+ const mv = thisArg === undefined ? callback(el, index, this) : callback.call(thisArg, el, index, this);
868
+ out.push(mv);
869
+ index++;
870
+ }
871
+ return out;
872
+ }
873
+
874
+ /**
875
+ * (Protected) Set the internal bucket size.
876
+ * @remarks Time O(1), Space O(1)
877
+ * @param size - Bucket capacity to assign.
878
+ * @returns void
879
+ */
880
+
881
+ protected _setBucketSize(size: number): void {
882
+ this._bucketSize = size;
883
+ }
884
+
885
+ /**
886
+ * (Protected) Iterate elements from front to back.
887
+ * @remarks Time O(N), Space O(1)
888
+ * @returns Iterator of elements.
889
+ */
890
+
891
+ protected *_getIterator(): IterableIterator<E> {
892
+ for (let i = 0; i < this._length; ++i) {
893
+ const v = this.at(i);
894
+ if (v !== undefined) yield v;
895
+ }
896
+ }
897
+
898
+ /**
899
+ * (Protected) Reallocate buckets to make room near the ends.
900
+ * @remarks Time O(N), Space O(N)
901
+ * @param [needBucketNum] - How many extra buckets to add; defaults to half of current.
902
+ * @returns void
903
+ */
904
+
905
+ protected _reallocate(needBucketNum?: number) {
906
+ const newBuckets = [] as E[][];
907
+ const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
908
+ for (let i = 0; i < addBucketNum; ++i) {
909
+ newBuckets[i] = new Array(this._bucketSize);
910
+ }
911
+ for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
912
+ newBuckets[newBuckets.length] = this._buckets[i];
913
+ }
914
+ for (let i = 0; i < this._bucketLast; ++i) {
915
+ newBuckets[newBuckets.length] = this._buckets[i];
916
+ }
917
+ newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
918
+ this._bucketFirst = addBucketNum;
919
+ this._bucketLast = newBuckets.length - 1;
920
+ for (let i = 0; i < addBucketNum; ++i) {
921
+ newBuckets[newBuckets.length] = new Array(this._bucketSize);
922
+ }
923
+ this._buckets = newBuckets;
924
+ this._bucketCount = newBuckets.length;
925
+ }
926
+
927
+ /**
928
+ * (Protected) Translate a logical position to bucket/offset.
929
+ * @remarks Time O(1), Space O(1)
930
+ * @param pos - Zero-based position.
931
+ * @returns An object containing bucketIndex and indexInBucket.
932
+ */
933
+
934
+ protected _getBucketAndPosition(pos: number) {
935
+ let bucketIndex: number;
936
+ let indexInBucket: number;
937
+
938
+ const overallIndex = this._firstInBucket + pos;
939
+ bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
940
+
941
+ if (bucketIndex >= this._bucketCount) {
942
+ bucketIndex -= this._bucketCount;
943
+ }
944
+
945
+ indexInBucket = ((overallIndex + 1) % this._bucketSize) - 1;
946
+ if (indexInBucket < 0) {
947
+ indexInBucket = this._bucketSize - 1;
948
+ }
949
+
950
+ return { bucketIndex, indexInBucket };
951
+ }
952
+
953
+ /**
954
+ * (Protected) Create an empty instance of the same concrete class.
955
+ * @remarks Time O(1), Space O(1)
956
+ * @param [options] - Options forwarded to the constructor.
957
+ * @returns An empty like-kind deque instance.
958
+ */
959
+
960
+ protected override _createInstance(options?: LinearBaseOptions<E, R>): this {
961
+ const Ctor = this.constructor as new (
962
+ elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>,
963
+ options?: DequeOptions<E, R>
964
+ ) => this;
965
+ return new Ctor([], options as DequeOptions<E, R> | undefined);
966
+ }
967
+
968
+ /**
969
+ * (Protected) Create a like-kind deque seeded by elements.
970
+ * @remarks Time O(N), Space O(N)
971
+ * @template T
972
+ * @template RR
973
+ * @param [elements] - Iterable used to seed the new deque.
974
+ * @param [options] - Options forwarded to the constructor.
975
+ * @returns A like-kind Deque instance.
976
+ */
977
+
978
+ protected _createLike<T = E, RR = R>(
979
+ elements: IterableWithSizeOrLength<T> | IterableWithSizeOrLength<RR> = [],
980
+ options?: DequeOptions<T, RR>
981
+ ): any {
982
+ const Ctor = this.constructor as new (
983
+ elements?: IterableWithSizeOrLength<T> | IterableWithSizeOrLength<RR>,
984
+ options?: DequeOptions<T, RR>
985
+ ) => any;
986
+ return new Ctor(elements, options);
987
+ }
988
+
989
+ /**
990
+ * (Protected) Iterate elements from back to front.
991
+ * @remarks Time O(N), Space O(1)
992
+ * @returns Iterator of elements.
993
+ */
994
+
995
+ protected *_getReverseIterator(): IterableIterator<E> {
996
+ for (let i = this._length - 1; i > -1; i--) {
997
+ const v = this.at(i);
998
+ if (v !== undefined) yield v;
999
+ }
1000
+ }
1001
+ }