avl-tree-typed 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (281) hide show
  1. package/dist/cjs/index.cjs +3859 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/esm/index.mjs +3852 -0
  4. package/dist/esm/index.mjs.map +1 -0
  5. package/dist/types/data-structures/base/index.d.ts +2 -1
  6. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
  7. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
  8. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
  9. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
  12. package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
  14. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
  17. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
  19. package/dist/types/data-structures/graph/index.d.ts +2 -1
  20. package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
  21. package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
  22. package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
  23. package/dist/types/data-structures/hash/index.d.ts +0 -1
  24. package/dist/types/data-structures/heap/heap.d.ts +503 -5
  25. package/dist/types/data-structures/heap/index.d.ts +2 -0
  26. package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
  27. package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
  28. package/dist/types/data-structures/index.d.ts +7 -7
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
  32. package/dist/types/data-structures/matrix/index.d.ts +1 -1
  33. package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
  34. package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
  38. package/dist/types/data-structures/queue/deque.d.ts +431 -4
  39. package/dist/types/data-structures/queue/queue.d.ts +308 -4
  40. package/dist/types/data-structures/stack/stack.d.ts +306 -2
  41. package/dist/types/data-structures/tree/tree.d.ts +62 -1
  42. package/dist/types/data-structures/trie/trie.d.ts +350 -4
  43. package/dist/types/index.d.ts +15 -3
  44. package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
  45. package/dist/types/types/data-structures/base/index.d.ts +1 -0
  46. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  47. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  48. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  49. package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  50. package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  51. package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
  52. package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
  53. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  54. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  55. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  56. package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  57. package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
  58. package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
  59. package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
  60. package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
  61. package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
  62. package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
  63. package/dist/types/types/data-structures/hash/index.d.ts +2 -0
  64. package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
  65. package/dist/types/types/data-structures/heap/index.d.ts +1 -0
  66. package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
  67. package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
  68. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
  69. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
  70. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
  71. package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
  72. package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
  73. package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  74. package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  75. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
  76. package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
  77. package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
  78. package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
  79. package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
  80. package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
  81. package/dist/types/types/index.d.ts +3 -0
  82. package/dist/types/types/utils/index.d.ts +2 -0
  83. package/dist/types/types/utils/utils.d.ts +22 -0
  84. package/dist/types/utils/index.d.ts +1 -1
  85. package/dist/types/utils/utils.d.ts +209 -22
  86. package/dist/umd/avl-tree-typed.js +3852 -0
  87. package/dist/umd/avl-tree-typed.js.map +1 -0
  88. package/dist/umd/avl-tree-typed.min.js +13 -0
  89. package/dist/umd/avl-tree-typed.min.js.map +1 -0
  90. package/package.json +25 -5
  91. package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
  92. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  93. package/src/data-structures/binary-tree/avl-tree.ts +2 -2
  94. package/src/data-structures/binary-tree/binary-tree.ts +4 -4
  95. package/src/data-structures/binary-tree/bst.ts +1 -1
  96. package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
  97. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  98. package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
  99. package/src/data-structures/heap/heap.ts +5 -5
  100. package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
  101. package/src/interfaces/binary-tree.ts +1 -1
  102. package/tsconfig.base.json +23 -0
  103. package/tsconfig.json +8 -34
  104. package/tsconfig.test.json +8 -0
  105. package/tsconfig.types.json +15 -0
  106. package/tsup.config.js +28 -0
  107. package/tsup.node.config.js +37 -0
  108. package/dist/common/index.js +0 -28
  109. package/dist/constants/index.js +0 -8
  110. package/dist/data-structures/base/index.d.ts +0 -2
  111. package/dist/data-structures/base/index.js +0 -18
  112. package/dist/data-structures/base/iterable-element-base.js +0 -243
  113. package/dist/data-structures/base/iterable-entry-base.js +0 -183
  114. package/dist/data-structures/base/linear-base.js +0 -415
  115. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
  116. package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
  117. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
  118. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
  119. package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
  120. package/dist/data-structures/binary-tree/avl-tree.js +0 -611
  121. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
  122. package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
  123. package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
  124. package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
  125. package/dist/data-structures/binary-tree/bst.d.ts +0 -413
  126. package/dist/data-structures/binary-tree/bst.js +0 -903
  127. package/dist/data-structures/binary-tree/index.js +0 -26
  128. package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
  129. package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
  130. package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
  131. package/dist/data-structures/binary-tree/segment-tree.js +0 -297
  132. package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
  133. package/dist/data-structures/binary-tree/tree-counter.js +0 -413
  134. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
  135. package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
  136. package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
  137. package/dist/data-structures/graph/abstract-graph.js +0 -896
  138. package/dist/data-structures/graph/directed-graph.d.ts +0 -207
  139. package/dist/data-structures/graph/directed-graph.js +0 -525
  140. package/dist/data-structures/graph/index.js +0 -20
  141. package/dist/data-structures/graph/map-graph.d.ts +0 -78
  142. package/dist/data-structures/graph/map-graph.js +0 -107
  143. package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
  144. package/dist/data-structures/graph/undirected-graph.js +0 -424
  145. package/dist/data-structures/hash/hash-map.d.ts +0 -345
  146. package/dist/data-structures/hash/hash-map.js +0 -692
  147. package/dist/data-structures/hash/index.d.ts +0 -1
  148. package/dist/data-structures/hash/index.js +0 -17
  149. package/dist/data-structures/heap/heap.d.ts +0 -503
  150. package/dist/data-structures/heap/heap.js +0 -901
  151. package/dist/data-structures/heap/index.d.ts +0 -3
  152. package/dist/data-structures/heap/index.js +0 -19
  153. package/dist/data-structures/heap/max-heap.d.ts +0 -32
  154. package/dist/data-structures/heap/max-heap.js +0 -40
  155. package/dist/data-structures/heap/min-heap.d.ts +0 -33
  156. package/dist/data-structures/heap/min-heap.js +0 -31
  157. package/dist/data-structures/index.js +0 -28
  158. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
  159. package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
  160. package/dist/data-structures/linked-list/index.js +0 -19
  161. package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
  162. package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
  163. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
  164. package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
  165. package/dist/data-structures/matrix/index.js +0 -18
  166. package/dist/data-structures/matrix/matrix.d.ts +0 -168
  167. package/dist/data-structures/matrix/matrix.js +0 -448
  168. package/dist/data-structures/matrix/navigator.d.ts +0 -55
  169. package/dist/data-structures/matrix/navigator.js +0 -111
  170. package/dist/data-structures/priority-queue/index.js +0 -19
  171. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
  172. package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
  173. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
  174. package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
  175. package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
  176. package/dist/data-structures/priority-queue/priority-queue.js +0 -20
  177. package/dist/data-structures/queue/deque.d.ts +0 -431
  178. package/dist/data-structures/queue/deque.js +0 -879
  179. package/dist/data-structures/queue/index.js +0 -18
  180. package/dist/data-structures/queue/queue.d.ts +0 -308
  181. package/dist/data-structures/queue/queue.js +0 -473
  182. package/dist/data-structures/stack/index.js +0 -17
  183. package/dist/data-structures/stack/stack.d.ts +0 -306
  184. package/dist/data-structures/stack/stack.js +0 -401
  185. package/dist/data-structures/tree/index.js +0 -17
  186. package/dist/data-structures/tree/tree.d.ts +0 -62
  187. package/dist/data-structures/tree/tree.js +0 -107
  188. package/dist/data-structures/trie/index.js +0 -17
  189. package/dist/data-structures/trie/trie.d.ts +0 -350
  190. package/dist/data-structures/trie/trie.js +0 -610
  191. package/dist/index.d.ts +0 -15
  192. package/dist/index.js +0 -31
  193. package/dist/interfaces/binary-tree.js +0 -2
  194. package/dist/interfaces/doubly-linked-list.js +0 -2
  195. package/dist/interfaces/graph.js +0 -2
  196. package/dist/interfaces/heap.js +0 -2
  197. package/dist/interfaces/index.js +0 -24
  198. package/dist/interfaces/navigator.js +0 -2
  199. package/dist/interfaces/priority-queue.js +0 -2
  200. package/dist/interfaces/segment-tree.js +0 -2
  201. package/dist/interfaces/singly-linked-list.js +0 -2
  202. package/dist/types/common.js +0 -2
  203. package/dist/types/data-structures/base/base.js +0 -2
  204. package/dist/types/data-structures/base/index.js +0 -17
  205. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
  206. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
  207. package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
  208. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
  209. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
  210. package/dist/types/data-structures/binary-tree/bst.js +0 -2
  211. package/dist/types/data-structures/binary-tree/index.js +0 -25
  212. package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
  213. package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
  214. package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
  215. package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
  216. package/dist/types/data-structures/graph/abstract-graph.js +0 -2
  217. package/dist/types/data-structures/graph/directed-graph.js +0 -2
  218. package/dist/types/data-structures/graph/index.js +0 -19
  219. package/dist/types/data-structures/graph/map-graph.js +0 -2
  220. package/dist/types/data-structures/graph/undirected-graph.js +0 -2
  221. package/dist/types/data-structures/hash/hash-map.js +0 -2
  222. package/dist/types/data-structures/hash/index.js +0 -17
  223. package/dist/types/data-structures/heap/heap.js +0 -2
  224. package/dist/types/data-structures/heap/index.js +0 -17
  225. package/dist/types/data-structures/heap/max-heap.js +0 -2
  226. package/dist/types/data-structures/heap/min-heap.js +0 -2
  227. package/dist/types/data-structures/index.js +0 -28
  228. package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
  229. package/dist/types/data-structures/linked-list/index.js +0 -19
  230. package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
  231. package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
  232. package/dist/types/data-structures/matrix/index.js +0 -18
  233. package/dist/types/data-structures/matrix/matrix.js +0 -2
  234. package/dist/types/data-structures/matrix/navigator.js +0 -2
  235. package/dist/types/data-structures/priority-queue/index.js +0 -19
  236. package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
  237. package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
  238. package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
  239. package/dist/types/data-structures/queue/deque.js +0 -2
  240. package/dist/types/data-structures/queue/index.js +0 -18
  241. package/dist/types/data-structures/queue/queue.js +0 -2
  242. package/dist/types/data-structures/stack/index.js +0 -17
  243. package/dist/types/data-structures/stack/stack.js +0 -2
  244. package/dist/types/data-structures/tree/index.js +0 -17
  245. package/dist/types/data-structures/tree/tree.js +0 -2
  246. package/dist/types/data-structures/trie/index.js +0 -17
  247. package/dist/types/data-structures/trie/trie.js +0 -2
  248. package/dist/types/index.js +0 -19
  249. package/dist/types/utils/index.js +0 -18
  250. package/dist/types/utils/utils.js +0 -2
  251. package/dist/types/utils/validate-type.js +0 -2
  252. package/dist/utils/index.d.ts +0 -2
  253. package/dist/utils/index.js +0 -18
  254. package/dist/utils/number.js +0 -23
  255. package/dist/utils/utils.d.ts +0 -209
  256. package/dist/utils/utils.js +0 -353
  257. package/dist/{common → types/common}/index.d.ts +0 -0
  258. package/dist/{constants → types/constants}/index.d.ts +0 -0
  259. package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
  260. package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
  261. package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
  262. package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
  263. package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
  264. package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
  265. package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
  266. package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
  267. package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
  268. package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
  269. package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
  270. package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
  271. package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
  272. package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
  273. package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
  274. package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
  275. /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
  276. /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
  277. /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
  278. /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
  279. /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
  280. /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
  281. /package/dist/{utils → types/utils}/number.d.ts +0 -0
@@ -1,879 +0,0 @@
1
- "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Pablo Zeng
6
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.Deque = void 0;
11
- const utils_1 = require("../../utils");
12
- const linear_base_1 = require("../base/linear-base");
13
- /**
14
- * Deque implemented with circular buckets allowing O(1) amortized push/pop at both ends.
15
- * @remarks Time O(1), Space O(1)
16
- * @template E
17
- * @template R
18
- * 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).
19
- * 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
20
- * 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.
21
- * 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).
22
- * 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
23
- * @example
24
- * // prize roulette
25
- * class PrizeRoulette {
26
- * private deque: Deque<string>;
27
- *
28
- * constructor(prizes: string[]) {
29
- * // Initialize the deque with prizes
30
- * this.deque = new Deque<string>(prizes);
31
- * }
32
- *
33
- * // Rotate clockwise to the right (forward)
34
- * rotateClockwise(steps: number): void {
35
- * const n = this.deque.length;
36
- * if (n === 0) return;
37
- *
38
- * for (let i = 0; i < steps; i++) {
39
- * const last = this.deque.pop(); // Remove the last element
40
- * this.deque.unshift(last!); // Add it to the front
41
- * }
42
- * }
43
- *
44
- * // Rotate counterclockwise to the left (backward)
45
- * rotateCounterClockwise(steps: number): void {
46
- * const n = this.deque.length;
47
- * if (n === 0) return;
48
- *
49
- * for (let i = 0; i < steps; i++) {
50
- * const first = this.deque.shift(); // Remove the first element
51
- * this.deque.push(first!); // Add it to the back
52
- * }
53
- * }
54
- *
55
- * // Display the current prize at the head
56
- * display() {
57
- * return this.deque.first;
58
- * }
59
- * }
60
- *
61
- * // Example usage
62
- * const prizes = ['Car', 'Bike', 'Laptop', 'Phone', 'Watch', 'Headphones']; // Initialize the prize list
63
- * const roulette = new PrizeRoulette(prizes);
64
- *
65
- * // Display the initial state
66
- * console.log(roulette.display()); // 'Car' // Car
67
- *
68
- * // Rotate clockwise by 3 steps
69
- * roulette.rotateClockwise(3);
70
- * console.log(roulette.display()); // 'Phone' // Phone
71
- *
72
- * // Rotate counterclockwise by 2 steps
73
- * roulette.rotateCounterClockwise(2);
74
- * console.log(roulette.display()); // 'Headphones'
75
- * @example
76
- * // sliding window
77
- * // Maximum function of sliding window
78
- * function maxSlidingWindow(nums: number[], k: number): number[] {
79
- * const n = nums.length;
80
- * if (n * k === 0) return [];
81
- *
82
- * const deq = new Deque<number>();
83
- * const result: number[] = [];
84
- *
85
- * for (let i = 0; i < n; i++) {
86
- * // Delete indexes in the queue that are not within the window range
87
- * if (deq.length > 0 && deq.first! === i - k) {
88
- * deq.shift();
89
- * }
90
- *
91
- * // Remove all indices less than the current value from the tail of the queue
92
- * while (deq.length > 0 && nums[deq.last!] < nums[i]) {
93
- * deq.pop();
94
- * }
95
- *
96
- * // Add the current index to the end of the queue
97
- * deq.push(i);
98
- *
99
- * // Add the maximum value of the window to the results
100
- * if (i >= k - 1) {
101
- * result.push(nums[deq.first!]);
102
- * }
103
- * }
104
- *
105
- * return result;
106
- * }
107
- *
108
- * const nums = [1, 3, -1, -3, 5, 3, 6, 7];
109
- * const k = 3;
110
- * console.log(maxSlidingWindow(nums, k)); // [3, 3, 5, 5, 6, 7]
111
- */
112
- class Deque extends linear_base_1.LinearBase {
113
- /**
114
- * Create a Deque and optionally bulk-insert elements.
115
- * @remarks Time O(N), Space O(N)
116
- * @param [elements] - Iterable (or iterable-like) of elements/records to insert.
117
- * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
118
- * @returns New Deque instance.
119
- */
120
- constructor(elements = [], options) {
121
- super(options);
122
- this._equals = Object.is;
123
- this._bucketSize = 1 << 12;
124
- this._bucketFirst = 0;
125
- this._firstInBucket = 0;
126
- this._bucketLast = 0;
127
- this._lastInBucket = 0;
128
- this._bucketCount = 0;
129
- this._buckets = [];
130
- this._length = 0;
131
- if (options) {
132
- const { bucketSize } = options;
133
- if (typeof bucketSize === 'number')
134
- this._bucketSize = bucketSize;
135
- }
136
- let _size;
137
- if ('length' in elements) {
138
- _size = typeof elements.length === 'function' ? elements.length() : elements.length;
139
- }
140
- else {
141
- _size = typeof elements.size === 'function' ? elements.size() : elements.size;
142
- }
143
- this._bucketCount = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize) || 1;
144
- for (let i = 0; i < this._bucketCount; ++i) {
145
- this._buckets.push(new Array(this._bucketSize));
146
- }
147
- const needBucketNum = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize);
148
- this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
149
- this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
150
- this.pushMany(elements);
151
- }
152
- /**
153
- * Get the current bucket size.
154
- * @remarks Time O(1), Space O(1)
155
- * @returns Bucket capacity per bucket.
156
- */
157
- get bucketSize() {
158
- return this._bucketSize;
159
- }
160
- /**
161
- * Get the index of the first bucket in use.
162
- * @remarks Time O(1), Space O(1)
163
- * @returns Zero-based bucket index.
164
- */
165
- get bucketFirst() {
166
- return this._bucketFirst;
167
- }
168
- /**
169
- * Get the index inside the first bucket.
170
- * @remarks Time O(1), Space O(1)
171
- * @returns Zero-based index within the first bucket.
172
- */
173
- get firstInBucket() {
174
- return this._firstInBucket;
175
- }
176
- /**
177
- * Get the index of the last bucket in use.
178
- * @remarks Time O(1), Space O(1)
179
- * @returns Zero-based bucket index.
180
- */
181
- get bucketLast() {
182
- return this._bucketLast;
183
- }
184
- /**
185
- * Get the index inside the last bucket.
186
- * @remarks Time O(1), Space O(1)
187
- * @returns Zero-based index within the last bucket.
188
- */
189
- get lastInBucket() {
190
- return this._lastInBucket;
191
- }
192
- /**
193
- * Get the number of buckets allocated.
194
- * @remarks Time O(1), Space O(1)
195
- * @returns Bucket count.
196
- */
197
- get bucketCount() {
198
- return this._bucketCount;
199
- }
200
- /**
201
- * Get the internal buckets array.
202
- * @remarks Time O(1), Space O(1)
203
- * @returns Array of buckets storing values.
204
- */
205
- get buckets() {
206
- return this._buckets;
207
- }
208
- /**
209
- * Get the number of elements in the deque.
210
- * @remarks Time O(1), Space O(1)
211
- * @returns Current length.
212
- */
213
- get length() {
214
- return this._length;
215
- }
216
- /**
217
- * Get the first element without removing it.
218
- * @remarks Time O(1), Space O(1)
219
- * @returns First element or undefined.
220
- */
221
- get first() {
222
- if (this._length === 0)
223
- return;
224
- return this._buckets[this._bucketFirst][this._firstInBucket];
225
- }
226
- /**
227
- * Get the last element without removing it.
228
- * @remarks Time O(1), Space O(1)
229
- * @returns Last element or undefined.
230
- */
231
- get last() {
232
- if (this._length === 0)
233
- return;
234
- return this._buckets[this._bucketLast][this._lastInBucket];
235
- }
236
- /**
237
- * Create a Deque from an array of elements.
238
- * @remarks Time O(N), Space O(N)
239
- * @template E
240
- * @template R
241
- * @param this - Constructor (subclass) to instantiate.
242
- * @param data - Array of elements to insert in order.
243
- * @param [options] - Options forwarded to the constructor.
244
- * @returns A new Deque populated from the array.
245
- */
246
- static fromArray(data, options) {
247
- return new this(data, options);
248
- }
249
- /**
250
- * Append one element at the back.
251
- * @remarks Time O(1) amortized, Space O(1)
252
- * @param element - Element to append.
253
- * @returns True when appended.
254
- */
255
- push(element) {
256
- if (this._length) {
257
- if (this._lastInBucket < this._bucketSize - 1) {
258
- this._lastInBucket += 1;
259
- }
260
- else if (this._bucketLast < this._bucketCount - 1) {
261
- this._bucketLast += 1;
262
- this._lastInBucket = 0;
263
- }
264
- else {
265
- this._bucketLast = 0;
266
- this._lastInBucket = 0;
267
- }
268
- if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket)
269
- this._reallocate();
270
- }
271
- this._length += 1;
272
- this._buckets[this._bucketLast][this._lastInBucket] = element;
273
- if (this._maxLen > 0 && this._length > this._maxLen)
274
- this.shift();
275
- return true;
276
- }
277
- /**
278
- * Remove and return the last element.
279
- * @remarks Time O(1), Space O(1)
280
- * @returns Removed element or undefined.
281
- */
282
- pop() {
283
- if (this._length === 0)
284
- return;
285
- const element = this._buckets[this._bucketLast][this._lastInBucket];
286
- if (this._length !== 1) {
287
- if (this._lastInBucket > 0) {
288
- this._lastInBucket -= 1;
289
- }
290
- else if (this._bucketLast > 0) {
291
- this._bucketLast -= 1;
292
- this._lastInBucket = this._bucketSize - 1;
293
- }
294
- else {
295
- this._bucketLast = this._bucketCount - 1;
296
- this._lastInBucket = this._bucketSize - 1;
297
- }
298
- }
299
- this._length -= 1;
300
- return element;
301
- }
302
- /**
303
- * Remove and return the first element.
304
- * @remarks Time O(1) amortized, Space O(1)
305
- * @returns Removed element or undefined.
306
- */
307
- shift() {
308
- if (this._length === 0)
309
- return;
310
- const element = this._buckets[this._bucketFirst][this._firstInBucket];
311
- if (this._length !== 1) {
312
- if (this._firstInBucket < this._bucketSize - 1) {
313
- this._firstInBucket += 1;
314
- }
315
- else if (this._bucketFirst < this._bucketCount - 1) {
316
- this._bucketFirst += 1;
317
- this._firstInBucket = 0;
318
- }
319
- else {
320
- this._bucketFirst = 0;
321
- this._firstInBucket = 0;
322
- }
323
- }
324
- this._length -= 1;
325
- return element;
326
- }
327
- /**
328
- * Prepend one element at the front.
329
- * @remarks Time O(1) amortized, Space O(1)
330
- * @param element - Element to prepend.
331
- * @returns True when prepended.
332
- */
333
- unshift(element) {
334
- if (this._length) {
335
- if (this._firstInBucket > 0) {
336
- this._firstInBucket -= 1;
337
- }
338
- else if (this._bucketFirst > 0) {
339
- this._bucketFirst -= 1;
340
- this._firstInBucket = this._bucketSize - 1;
341
- }
342
- else {
343
- this._bucketFirst = this._bucketCount - 1;
344
- this._firstInBucket = this._bucketSize - 1;
345
- }
346
- if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket)
347
- this._reallocate();
348
- }
349
- this._length += 1;
350
- this._buckets[this._bucketFirst][this._firstInBucket] = element;
351
- if (this._maxLen > 0 && this._length > this._maxLen)
352
- this.pop();
353
- return true;
354
- }
355
- /**
356
- * Append a sequence of elements.
357
- * @remarks Time O(N), Space O(1)
358
- * @param elements - Iterable (or iterable-like) of elements/records.
359
- * @returns Array of per-element success flags.
360
- */
361
- pushMany(elements) {
362
- const ans = [];
363
- for (const el of elements) {
364
- if (this.toElementFn) {
365
- ans.push(this.push(this.toElementFn(el)));
366
- }
367
- else {
368
- ans.push(this.push(el));
369
- }
370
- }
371
- return ans;
372
- }
373
- /**
374
- * Prepend a sequence of elements.
375
- * @remarks Time O(N), Space O(1)
376
- * @param [elements] - Iterable (or iterable-like) of elements/records.
377
- * @returns Array of per-element success flags.
378
- */
379
- unshiftMany(elements = []) {
380
- const ans = [];
381
- for (const el of elements) {
382
- if (this.toElementFn) {
383
- ans.push(this.unshift(this.toElementFn(el)));
384
- }
385
- else {
386
- ans.push(this.unshift(el));
387
- }
388
- }
389
- return ans;
390
- }
391
- /**
392
- * Check whether the deque is empty.
393
- * @remarks Time O(1), Space O(1)
394
- * @returns True if length is 0.
395
- */
396
- isEmpty() {
397
- return this._length === 0;
398
- }
399
- /**
400
- * Remove all elements and reset structure.
401
- * @remarks Time O(1), Space O(1)
402
- * @returns void
403
- */
404
- clear() {
405
- this._buckets = [new Array(this._bucketSize)];
406
- this._bucketCount = 1;
407
- this._bucketFirst = this._bucketLast = this._length = 0;
408
- this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
409
- }
410
- /**
411
- * Get the element at a given position.
412
- * @remarks Time O(1), Space O(1)
413
- * @param pos - Zero-based position from the front.
414
- * @returns Element or undefined.
415
- */
416
- at(pos) {
417
- if (pos < 0 || pos >= this._length)
418
- return undefined;
419
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
420
- return this._buckets[bucketIndex][indexInBucket];
421
- }
422
- /**
423
- * Replace the element at a given position.
424
- * @remarks Time O(1), Space O(1)
425
- * @param pos - Zero-based position from the front.
426
- * @param element - New element value.
427
- * @returns True if updated.
428
- */
429
- setAt(pos, element) {
430
- (0, utils_1.rangeCheck)(pos, 0, this._length - 1);
431
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
432
- this._buckets[bucketIndex][indexInBucket] = element;
433
- return true;
434
- }
435
- /**
436
- * Insert repeated copies of an element at a position.
437
- * @remarks Time O(N), Space O(1)
438
- * @param pos - Zero-based position from the front.
439
- * @param element - Element to insert.
440
- * @param [num] - Number of times to insert (default 1).
441
- * @returns True if inserted.
442
- */
443
- addAt(pos, element, num = 1) {
444
- const length = this._length;
445
- (0, utils_1.rangeCheck)(pos, 0, length);
446
- if (pos === 0) {
447
- while (num--)
448
- this.unshift(element);
449
- }
450
- else if (pos === this._length) {
451
- while (num--)
452
- this.push(element);
453
- }
454
- else {
455
- const arr = [];
456
- for (let i = pos; i < this._length; ++i) {
457
- const v = this.at(i);
458
- if (v !== undefined)
459
- arr.push(v);
460
- }
461
- this.cut(pos - 1, true);
462
- for (let i = 0; i < num; ++i)
463
- this.push(element);
464
- for (let i = 0; i < arr.length; ++i)
465
- this.push(arr[i]);
466
- }
467
- return true;
468
- }
469
- /**
470
- * Cut the deque to keep items up to index; optionally mutate in-place.
471
- * @remarks Time O(N), Space O(1)
472
- * @param pos - Last index to keep.
473
- * @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
474
- * @returns This deque if in-place; otherwise a new deque of the prefix.
475
- */
476
- cut(pos, isCutSelf = false) {
477
- if (isCutSelf) {
478
- if (pos < 0) {
479
- this.clear();
480
- return this;
481
- }
482
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
483
- this._bucketLast = bucketIndex;
484
- this._lastInBucket = indexInBucket;
485
- this._length = pos + 1;
486
- return this;
487
- }
488
- else {
489
- const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
490
- newDeque._setBucketSize(this._bucketSize);
491
- for (let i = 0; i <= pos; i++) {
492
- const v = this.at(i);
493
- if (v !== undefined)
494
- newDeque.push(v);
495
- }
496
- return newDeque;
497
- }
498
- }
499
- /**
500
- * Remove and/or insert elements at a position (array-like behavior).
501
- * @remarks Time O(N + M), Space O(M)
502
- * @param start - Start index (clamped to [0, length]).
503
- * @param [deleteCount] - Number of elements to remove (default: length - start).
504
- * @param [items] - Elements to insert after `start`.
505
- * @returns A new deque containing the removed elements (typed as `this`).
506
- */
507
- splice(start, deleteCount = this._length - start, ...items) {
508
- (0, utils_1.rangeCheck)(start, 0, this._length);
509
- if (deleteCount < 0)
510
- deleteCount = 0;
511
- if (start + deleteCount > this._length)
512
- deleteCount = this._length - start;
513
- const removed = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
514
- removed._setBucketSize(this._bucketSize);
515
- for (let i = 0; i < deleteCount; i++) {
516
- const v = this.at(start + i);
517
- if (v !== undefined)
518
- removed.push(v);
519
- }
520
- const tail = [];
521
- for (let i = start + deleteCount; i < this._length; i++) {
522
- const v = this.at(i);
523
- if (v !== undefined)
524
- tail.push(v);
525
- }
526
- this.cut(start - 1, true);
527
- for (const it of items)
528
- this.push(it);
529
- for (const v of tail)
530
- this.push(v);
531
- return removed;
532
- }
533
- /**
534
- * Cut the deque to keep items from index onward; optionally mutate in-place.
535
- * @remarks Time O(N), Space O(1)
536
- * @param pos - First index to keep.
537
- * @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
538
- * @returns This deque if in-place; otherwise a new deque of the suffix.
539
- */
540
- cutRest(pos, isCutSelf = false) {
541
- if (isCutSelf) {
542
- if (pos < 0) {
543
- return this;
544
- }
545
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
546
- this._bucketFirst = bucketIndex;
547
- this._firstInBucket = indexInBucket;
548
- this._length = this._length - pos;
549
- return this;
550
- }
551
- else {
552
- const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
553
- newDeque._setBucketSize(this._bucketSize);
554
- if (pos < 0)
555
- pos = 0;
556
- for (let i = pos; i < this._length; i++) {
557
- const v = this.at(i);
558
- if (v !== undefined)
559
- newDeque.push(v);
560
- }
561
- return newDeque;
562
- }
563
- }
564
- /**
565
- * Delete the element at a given position.
566
- * @remarks Time O(N), Space O(1)
567
- * @param pos - Zero-based position from the front.
568
- * @returns Removed element or undefined.
569
- */
570
- deleteAt(pos) {
571
- (0, utils_1.rangeCheck)(pos, 0, this._length - 1);
572
- let deleted;
573
- if (pos === 0) {
574
- return this.shift();
575
- }
576
- else if (pos === this._length - 1) {
577
- deleted = this.last;
578
- this.pop();
579
- return deleted;
580
- }
581
- else {
582
- const length = this._length - 1;
583
- const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
584
- deleted = this._buckets[targetBucket][targetPointer];
585
- for (let i = pos; i < length; i++) {
586
- const { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(i);
587
- const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
588
- this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
589
- }
590
- this.pop();
591
- return deleted;
592
- }
593
- }
594
- /**
595
- * Delete the first occurrence of a value.
596
- * @remarks Time O(N), Space O(1)
597
- * @param element - Element to remove (using the configured equality).
598
- * @returns True if an element was removed.
599
- */
600
- delete(element) {
601
- const size = this._length;
602
- if (size === 0)
603
- return false;
604
- let i = 0;
605
- let index = 0;
606
- while (i < size) {
607
- const oldElement = this.at(i);
608
- if (!this._equals(oldElement, element)) {
609
- this.setAt(index, oldElement);
610
- index += 1;
611
- }
612
- i += 1;
613
- }
614
- this.cut(index - 1, true);
615
- return true;
616
- }
617
- /**
618
- * Delete the first element matching a predicate.
619
- * @remarks Time O(N), Space O(1)
620
- * @param predicate - Function (value, index, deque) → boolean.
621
- * @returns True if a match was removed.
622
- */
623
- deleteWhere(predicate) {
624
- for (let i = 0; i < this._length; i++) {
625
- const v = this.at(i);
626
- if (predicate(v, i, this)) {
627
- this.deleteAt(i);
628
- return true;
629
- }
630
- }
631
- return false;
632
- }
633
- /**
634
- * Set the equality comparator used by delete operations.
635
- * @remarks Time O(1), Space O(1)
636
- * @param equals - Equality predicate (a, b) → boolean.
637
- * @returns This deque.
638
- */
639
- setEquality(equals) {
640
- this._equals = equals;
641
- return this;
642
- }
643
- /**
644
- * Reverse the deque by reversing buckets and pointers.
645
- * @remarks Time O(N), Space O(N)
646
- * @returns This deque.
647
- */
648
- reverse() {
649
- this._buckets.reverse().forEach(function (bucket) {
650
- bucket.reverse();
651
- });
652
- const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
653
- this._bucketFirst = this._bucketCount - _bucketLast - 1;
654
- this._bucketLast = this._bucketCount - _bucketFirst - 1;
655
- this._firstInBucket = this._bucketSize - _lastInBucket - 1;
656
- this._lastInBucket = this._bucketSize - _firstInBucket - 1;
657
- return this;
658
- }
659
- /**
660
- * Deduplicate consecutive equal elements in-place.
661
- * @remarks Time O(N), Space O(1)
662
- * @returns This deque.
663
- */
664
- unique() {
665
- if (this._length <= 1) {
666
- return this;
667
- }
668
- let index = 1;
669
- let prev = this.at(0);
670
- for (let i = 1; i < this._length; ++i) {
671
- const cur = this.at(i);
672
- if (!this._equals(cur, prev)) {
673
- prev = cur;
674
- this.setAt(index++, cur);
675
- }
676
- }
677
- this.cut(index - 1, true);
678
- return this;
679
- }
680
- /**
681
- * Trim unused buckets to fit exactly the active range.
682
- * @remarks Time O(N), Space O(1)
683
- * @returns void
684
- */
685
- shrinkToFit() {
686
- if (this._length === 0)
687
- return;
688
- const newBuckets = [];
689
- if (this._bucketFirst === this._bucketLast)
690
- return;
691
- else if (this._bucketFirst < this._bucketLast) {
692
- for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
693
- newBuckets.push(this._buckets[i]);
694
- }
695
- }
696
- else {
697
- for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
698
- newBuckets.push(this._buckets[i]);
699
- }
700
- for (let i = 0; i <= this._bucketLast; ++i) {
701
- newBuckets.push(this._buckets[i]);
702
- }
703
- }
704
- this._bucketFirst = 0;
705
- this._bucketLast = newBuckets.length - 1;
706
- this._buckets = newBuckets;
707
- }
708
- /**
709
- * Deep clone this deque, preserving options.
710
- * @remarks Time O(N), Space O(N)
711
- * @returns A new deque with the same content and options.
712
- */
713
- clone() {
714
- return this._createLike(this, {
715
- bucketSize: this.bucketSize,
716
- toElementFn: this.toElementFn,
717
- maxLen: this._maxLen
718
- });
719
- }
720
- /**
721
- * Filter elements into a new deque of the same class.
722
- * @remarks Time O(N), Space O(N)
723
- * @param predicate - Predicate (value, index, deque) → boolean to keep element.
724
- * @param [thisArg] - Value for `this` inside the predicate.
725
- * @returns A new deque with kept elements.
726
- */
727
- filter(predicate, thisArg) {
728
- const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
729
- out._setBucketSize(this._bucketSize);
730
- let index = 0;
731
- for (const el of this) {
732
- if (predicate.call(thisArg, el, index, this))
733
- out.push(el);
734
- index++;
735
- }
736
- return out;
737
- }
738
- /**
739
- * Map elements into a new deque of the same element type.
740
- * @remarks Time O(N), Space O(N)
741
- * @param callback - Mapping function (value, index, deque) → newValue.
742
- * @param [thisArg] - Value for `this` inside the callback.
743
- * @returns A new deque with mapped values.
744
- */
745
- mapSame(callback, thisArg) {
746
- const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
747
- out._setBucketSize(this._bucketSize);
748
- let index = 0;
749
- for (const v of this) {
750
- const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
751
- out.push(mv);
752
- }
753
- return out;
754
- }
755
- /**
756
- * Map elements into a new deque (possibly different element type).
757
- * @remarks Time O(N), Space O(N)
758
- * @template EM
759
- * @template RM
760
- * @param callback - Mapping function (value, index, deque) → newElement.
761
- * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
762
- * @param [thisArg] - Value for `this` inside the callback.
763
- * @returns A new Deque with mapped elements.
764
- */
765
- map(callback, options, thisArg) {
766
- const out = this._createLike([], Object.assign(Object.assign({}, (options !== null && options !== void 0 ? options : {})), { bucketSize: this._bucketSize, maxLen: this._maxLen }));
767
- let index = 0;
768
- for (const el of this) {
769
- const mv = thisArg === undefined ? callback(el, index, this) : callback.call(thisArg, el, index, this);
770
- out.push(mv);
771
- index++;
772
- }
773
- return out;
774
- }
775
- /**
776
- * (Protected) Set the internal bucket size.
777
- * @remarks Time O(1), Space O(1)
778
- * @param size - Bucket capacity to assign.
779
- * @returns void
780
- */
781
- _setBucketSize(size) {
782
- this._bucketSize = size;
783
- }
784
- /**
785
- * (Protected) Iterate elements from front to back.
786
- * @remarks Time O(N), Space O(1)
787
- * @returns Iterator of elements.
788
- */
789
- *_getIterator() {
790
- for (let i = 0; i < this._length; ++i) {
791
- const v = this.at(i);
792
- if (v !== undefined)
793
- yield v;
794
- }
795
- }
796
- /**
797
- * (Protected) Reallocate buckets to make room near the ends.
798
- * @remarks Time O(N), Space O(N)
799
- * @param [needBucketNum] - How many extra buckets to add; defaults to half of current.
800
- * @returns void
801
- */
802
- _reallocate(needBucketNum) {
803
- const newBuckets = [];
804
- const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
805
- for (let i = 0; i < addBucketNum; ++i) {
806
- newBuckets[i] = new Array(this._bucketSize);
807
- }
808
- for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
809
- newBuckets[newBuckets.length] = this._buckets[i];
810
- }
811
- for (let i = 0; i < this._bucketLast; ++i) {
812
- newBuckets[newBuckets.length] = this._buckets[i];
813
- }
814
- newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
815
- this._bucketFirst = addBucketNum;
816
- this._bucketLast = newBuckets.length - 1;
817
- for (let i = 0; i < addBucketNum; ++i) {
818
- newBuckets[newBuckets.length] = new Array(this._bucketSize);
819
- }
820
- this._buckets = newBuckets;
821
- this._bucketCount = newBuckets.length;
822
- }
823
- /**
824
- * (Protected) Translate a logical position to bucket/offset.
825
- * @remarks Time O(1), Space O(1)
826
- * @param pos - Zero-based position.
827
- * @returns An object containing bucketIndex and indexInBucket.
828
- */
829
- _getBucketAndPosition(pos) {
830
- let bucketIndex;
831
- let indexInBucket;
832
- const overallIndex = this._firstInBucket + pos;
833
- bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
834
- if (bucketIndex >= this._bucketCount) {
835
- bucketIndex -= this._bucketCount;
836
- }
837
- indexInBucket = ((overallIndex + 1) % this._bucketSize) - 1;
838
- if (indexInBucket < 0) {
839
- indexInBucket = this._bucketSize - 1;
840
- }
841
- return { bucketIndex, indexInBucket };
842
- }
843
- /**
844
- * (Protected) Create an empty instance of the same concrete class.
845
- * @remarks Time O(1), Space O(1)
846
- * @param [options] - Options forwarded to the constructor.
847
- * @returns An empty like-kind deque instance.
848
- */
849
- _createInstance(options) {
850
- const Ctor = this.constructor;
851
- return new Ctor([], options);
852
- }
853
- /**
854
- * (Protected) Create a like-kind deque seeded by elements.
855
- * @remarks Time O(N), Space O(N)
856
- * @template T
857
- * @template RR
858
- * @param [elements] - Iterable used to seed the new deque.
859
- * @param [options] - Options forwarded to the constructor.
860
- * @returns A like-kind Deque instance.
861
- */
862
- _createLike(elements = [], options) {
863
- const Ctor = this.constructor;
864
- return new Ctor(elements, options);
865
- }
866
- /**
867
- * (Protected) Iterate elements from back to front.
868
- * @remarks Time O(N), Space O(1)
869
- * @returns Iterator of elements.
870
- */
871
- *_getReverseIterator() {
872
- for (let i = this._length - 1; i > -1; i--) {
873
- const v = this.at(i);
874
- if (v !== undefined)
875
- yield v;
876
- }
877
- }
878
- }
879
- exports.Deque = Deque;