red-black-tree-typed 1.47.3

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 (341) 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 +713 -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/data-structures/binary-tree/avl-tree.d.ts +173 -0
  20. package/dist/data-structures/binary-tree/avl-tree.js +429 -0
  21. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
  22. package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
  23. package/dist/data-structures/binary-tree/binary-tree.d.ts +515 -0
  24. package/dist/data-structures/binary-tree/binary-tree.js +1665 -0
  25. package/dist/data-structures/binary-tree/bst.d.ts +253 -0
  26. package/dist/data-structures/binary-tree/bst.js +651 -0
  27. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  28. package/dist/data-structures/binary-tree/index.js +23 -0
  29. package/dist/data-structures/binary-tree/rb-tree.d.ts +169 -0
  30. package/dist/data-structures/binary-tree/rb-tree.js +524 -0
  31. package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
  32. package/dist/data-structures/binary-tree/segment-tree.js +180 -0
  33. package/dist/data-structures/binary-tree/tree-multimap.d.ts +162 -0
  34. package/dist/data-structures/binary-tree/tree-multimap.js +407 -0
  35. package/dist/data-structures/graph/abstract-graph.d.ts +450 -0
  36. package/dist/data-structures/graph/abstract-graph.js +1047 -0
  37. package/dist/data-structures/graph/directed-graph.d.ts +320 -0
  38. package/dist/data-structures/graph/directed-graph.js +530 -0
  39. package/dist/data-structures/graph/index.d.ts +4 -0
  40. package/dist/data-structures/graph/index.js +20 -0
  41. package/dist/data-structures/graph/map-graph.d.ts +73 -0
  42. package/dist/data-structures/graph/map-graph.js +93 -0
  43. package/dist/data-structures/graph/undirected-graph.d.ts +183 -0
  44. package/dist/data-structures/graph/undirected-graph.js +302 -0
  45. package/dist/data-structures/hash/hash-map.d.ts +186 -0
  46. package/dist/data-structures/hash/hash-map.js +367 -0
  47. package/dist/data-structures/hash/hash-table.d.ts +103 -0
  48. package/dist/data-structures/hash/hash-table.js +236 -0
  49. package/dist/data-structures/hash/index.d.ts +2 -0
  50. package/dist/data-structures/hash/index.js +18 -0
  51. package/dist/data-structures/heap/heap.d.ts +410 -0
  52. package/dist/data-structures/heap/heap.js +697 -0
  53. package/dist/data-structures/heap/index.d.ts +3 -0
  54. package/dist/data-structures/heap/index.js +19 -0
  55. package/dist/data-structures/heap/max-heap.d.ts +15 -0
  56. package/dist/data-structures/heap/max-heap.js +26 -0
  57. package/dist/data-structures/heap/min-heap.d.ts +15 -0
  58. package/dist/data-structures/heap/min-heap.js +26 -0
  59. package/dist/data-structures/index.d.ts +11 -0
  60. package/dist/data-structures/index.js +27 -0
  61. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +456 -0
  62. package/dist/data-structures/linked-list/doubly-linked-list.js +772 -0
  63. package/dist/data-structures/linked-list/index.d.ts +3 -0
  64. package/dist/data-structures/linked-list/index.js +19 -0
  65. package/dist/data-structures/linked-list/singly-linked-list.d.ts +414 -0
  66. package/dist/data-structures/linked-list/singly-linked-list.js +715 -0
  67. package/dist/data-structures/linked-list/skip-linked-list.d.ts +144 -0
  68. package/dist/data-structures/linked-list/skip-linked-list.js +251 -0
  69. package/dist/data-structures/matrix/index.d.ts +4 -0
  70. package/dist/data-structures/matrix/index.js +20 -0
  71. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  72. package/dist/data-structures/matrix/matrix.js +28 -0
  73. package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
  74. package/dist/data-structures/matrix/matrix2d.js +199 -0
  75. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  76. package/dist/data-structures/matrix/navigator.js +106 -0
  77. package/dist/data-structures/matrix/vector2d.d.ts +200 -0
  78. package/dist/data-structures/matrix/vector2d.js +290 -0
  79. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  80. package/dist/data-structures/priority-queue/index.js +19 -0
  81. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
  82. package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
  83. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
  84. package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
  85. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
  86. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  87. package/dist/data-structures/queue/deque.d.ts +572 -0
  88. package/dist/data-structures/queue/deque.js +990 -0
  89. package/dist/data-structures/queue/index.d.ts +2 -0
  90. package/dist/data-structures/queue/index.js +18 -0
  91. package/dist/data-structures/queue/queue.d.ts +209 -0
  92. package/dist/data-structures/queue/queue.js +274 -0
  93. package/dist/data-structures/stack/index.d.ts +1 -0
  94. package/dist/data-structures/stack/index.js +17 -0
  95. package/dist/data-structures/stack/stack.d.ts +106 -0
  96. package/dist/data-structures/stack/stack.js +136 -0
  97. package/dist/data-structures/tree/index.d.ts +1 -0
  98. package/dist/data-structures/tree/index.js +17 -0
  99. package/dist/data-structures/tree/tree.d.ts +8 -0
  100. package/dist/data-structures/tree/tree.js +40 -0
  101. package/dist/data-structures/trie/index.d.ts +1 -0
  102. package/dist/data-structures/trie/index.js +17 -0
  103. package/dist/data-structures/trie/trie.d.ts +155 -0
  104. package/dist/data-structures/trie/trie.js +326 -0
  105. package/dist/index.d.ts +10 -0
  106. package/dist/index.js +27 -0
  107. package/dist/interfaces/binary-tree.d.ts +7 -0
  108. package/dist/interfaces/binary-tree.js +2 -0
  109. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  110. package/dist/interfaces/doubly-linked-list.js +2 -0
  111. package/dist/interfaces/graph.d.ts +5 -0
  112. package/dist/interfaces/graph.js +2 -0
  113. package/dist/interfaces/heap.d.ts +1 -0
  114. package/dist/interfaces/heap.js +2 -0
  115. package/dist/interfaces/index.d.ts +8 -0
  116. package/dist/interfaces/index.js +24 -0
  117. package/dist/interfaces/navigator.d.ts +1 -0
  118. package/dist/interfaces/navigator.js +2 -0
  119. package/dist/interfaces/priority-queue.d.ts +1 -0
  120. package/dist/interfaces/priority-queue.js +2 -0
  121. package/dist/interfaces/segment-tree.d.ts +1 -0
  122. package/dist/interfaces/segment-tree.js +2 -0
  123. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  124. package/dist/interfaces/singly-linked-list.js +2 -0
  125. package/dist/types/common.d.ts +20 -0
  126. package/dist/types/common.js +9 -0
  127. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +5 -0
  128. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  129. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  130. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  131. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
  132. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  133. package/dist/types/data-structures/binary-tree/bst.d.ts +8 -0
  134. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  135. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  136. package/dist/types/data-structures/binary-tree/index.js +22 -0
  137. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +9 -0
  138. package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
  139. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  140. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  141. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +5 -0
  142. package/dist/types/data-structures/binary-tree/tree-multimap.js +2 -0
  143. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  144. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  145. package/dist/types/data-structures/graph/directed-graph.d.ts +1 -0
  146. package/dist/types/data-structures/graph/directed-graph.js +2 -0
  147. package/dist/types/data-structures/graph/index.d.ts +3 -0
  148. package/dist/types/data-structures/graph/index.js +19 -0
  149. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  150. package/dist/types/data-structures/graph/map-graph.js +2 -0
  151. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  152. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  153. package/dist/types/data-structures/hash/hash-map.d.ts +11 -0
  154. package/dist/types/data-structures/hash/hash-map.js +2 -0
  155. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  156. package/dist/types/data-structures/hash/hash-table.js +2 -0
  157. package/dist/types/data-structures/hash/index.d.ts +3 -0
  158. package/dist/types/data-structures/hash/index.js +18 -0
  159. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  160. package/dist/types/data-structures/heap/heap.js +2 -0
  161. package/dist/types/data-structures/heap/index.d.ts +1 -0
  162. package/dist/types/data-structures/heap/index.js +17 -0
  163. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  164. package/dist/types/data-structures/heap/max-heap.js +2 -0
  165. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  166. package/dist/types/data-structures/heap/min-heap.js +2 -0
  167. package/dist/types/data-structures/index.d.ts +11 -0
  168. package/dist/types/data-structures/index.js +27 -0
  169. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  170. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  171. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  172. package/dist/types/data-structures/linked-list/index.js +18 -0
  173. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  174. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  175. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  176. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  177. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  178. package/dist/types/data-structures/matrix/index.js +17 -0
  179. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  180. package/dist/types/data-structures/matrix/matrix.js +2 -0
  181. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  182. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  183. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  184. package/dist/types/data-structures/matrix/navigator.js +2 -0
  185. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  186. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  187. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  188. package/dist/types/data-structures/priority-queue/index.js +19 -0
  189. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  190. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  191. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  192. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  193. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  194. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  195. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  196. package/dist/types/data-structures/queue/deque.js +2 -0
  197. package/dist/types/data-structures/queue/index.d.ts +2 -0
  198. package/dist/types/data-structures/queue/index.js +18 -0
  199. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  200. package/dist/types/data-structures/queue/queue.js +2 -0
  201. package/dist/types/data-structures/stack/index.d.ts +1 -0
  202. package/dist/types/data-structures/stack/index.js +17 -0
  203. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  204. package/dist/types/data-structures/stack/stack.js +2 -0
  205. package/dist/types/data-structures/tree/index.d.ts +1 -0
  206. package/dist/types/data-structures/tree/index.js +17 -0
  207. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  208. package/dist/types/data-structures/tree/tree.js +2 -0
  209. package/dist/types/data-structures/trie/index.d.ts +1 -0
  210. package/dist/types/data-structures/trie/index.js +17 -0
  211. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  212. package/dist/types/data-structures/trie/trie.js +2 -0
  213. package/dist/types/index.d.ts +3 -0
  214. package/dist/types/index.js +19 -0
  215. package/dist/types/utils/index.d.ts +2 -0
  216. package/dist/types/utils/index.js +18 -0
  217. package/dist/types/utils/utils.d.ts +7 -0
  218. package/dist/types/utils/utils.js +2 -0
  219. package/dist/types/utils/validate-type.d.ts +19 -0
  220. package/dist/types/utils/validate-type.js +2 -0
  221. package/dist/utils/index.d.ts +1 -0
  222. package/dist/utils/index.js +17 -0
  223. package/dist/utils/utils.d.ts +24 -0
  224. package/dist/utils/utils.js +89 -0
  225. package/docs/.nojekyll +1 -0
  226. package/docs/assets/highlight.css +92 -0
  227. package/docs/assets/main.js +59 -0
  228. package/docs/assets/navigation.js +1 -0
  229. package/docs/assets/search.js +1 -0
  230. package/docs/assets/style.css +1383 -0
  231. package/docs/classes/AVLTree.html +2046 -0
  232. package/docs/classes/AVLTreeNode.html +263 -0
  233. package/docs/index.html +523 -0
  234. package/docs/modules.html +45 -0
  235. package/jest.config.js +8 -0
  236. package/package.json +147 -0
  237. package/src/data-structures/binary-tree/avl-tree.ts +443 -0
  238. package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
  239. package/src/data-structures/binary-tree/binary-tree.ts +1974 -0
  240. package/src/data-structures/binary-tree/bst.ts +676 -0
  241. package/src/data-structures/binary-tree/index.ts +7 -0
  242. package/src/data-structures/binary-tree/rb-tree.ts +585 -0
  243. package/src/data-structures/binary-tree/segment-tree.ts +190 -0
  244. package/src/data-structures/binary-tree/tree-multimap.ts +435 -0
  245. package/src/data-structures/graph/abstract-graph.ts +1181 -0
  246. package/src/data-structures/graph/directed-graph.ts +593 -0
  247. package/src/data-structures/graph/index.ts +4 -0
  248. package/src/data-structures/graph/map-graph.ts +106 -0
  249. package/src/data-structures/graph/undirected-graph.ts +331 -0
  250. package/src/data-structures/hash/hash-map.ts +401 -0
  251. package/src/data-structures/hash/hash-table.ts +268 -0
  252. package/src/data-structures/hash/index.ts +2 -0
  253. package/src/data-structures/heap/heap.ts +790 -0
  254. package/src/data-structures/heap/index.ts +3 -0
  255. package/src/data-structures/heap/max-heap.ts +26 -0
  256. package/src/data-structures/heap/min-heap.ts +26 -0
  257. package/src/data-structures/index.ts +11 -0
  258. package/src/data-structures/linked-list/doubly-linked-list.ts +837 -0
  259. package/src/data-structures/linked-list/index.ts +3 -0
  260. package/src/data-structures/linked-list/singly-linked-list.ts +784 -0
  261. package/src/data-structures/linked-list/skip-linked-list.ts +295 -0
  262. package/src/data-structures/matrix/index.ts +4 -0
  263. package/src/data-structures/matrix/matrix.ts +27 -0
  264. package/src/data-structures/matrix/matrix2d.ts +211 -0
  265. package/src/data-structures/matrix/navigator.ts +121 -0
  266. package/src/data-structures/matrix/vector2d.ts +315 -0
  267. package/src/data-structures/priority-queue/index.ts +3 -0
  268. package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
  269. package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
  270. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  271. package/src/data-structures/queue/deque.ts +1073 -0
  272. package/src/data-structures/queue/index.ts +2 -0
  273. package/src/data-structures/queue/queue.ts +308 -0
  274. package/src/data-structures/stack/index.ts +1 -0
  275. package/src/data-structures/stack/stack.ts +150 -0
  276. package/src/data-structures/tree/index.ts +1 -0
  277. package/src/data-structures/tree/tree.ts +41 -0
  278. package/src/data-structures/trie/index.ts +1 -0
  279. package/src/data-structures/trie/trie.ts +345 -0
  280. package/src/index.ts +11 -0
  281. package/src/interfaces/binary-tree.ts +10 -0
  282. package/src/interfaces/doubly-linked-list.ts +1 -0
  283. package/src/interfaces/graph.ts +7 -0
  284. package/src/interfaces/heap.ts +1 -0
  285. package/src/interfaces/index.ts +8 -0
  286. package/src/interfaces/navigator.ts +1 -0
  287. package/src/interfaces/priority-queue.ts +1 -0
  288. package/src/interfaces/segment-tree.ts +1 -0
  289. package/src/interfaces/singly-linked-list.ts +1 -0
  290. package/src/types/common.ts +23 -0
  291. package/src/types/data-structures/binary-tree/avl-tree.ts +9 -0
  292. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  293. package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
  294. package/src/types/data-structures/binary-tree/bst.ts +13 -0
  295. package/src/types/data-structures/binary-tree/index.ts +6 -0
  296. package/src/types/data-structures/binary-tree/rb-tree.ts +10 -0
  297. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  298. package/src/types/data-structures/binary-tree/tree-multimap.ts +8 -0
  299. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  300. package/src/types/data-structures/graph/directed-graph.ts +2 -0
  301. package/src/types/data-structures/graph/index.ts +3 -0
  302. package/src/types/data-structures/graph/map-graph.ts +1 -0
  303. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  304. package/src/types/data-structures/hash/hash-map.ts +12 -0
  305. package/src/types/data-structures/hash/hash-table.ts +1 -0
  306. package/src/types/data-structures/hash/index.ts +4 -0
  307. package/src/types/data-structures/heap/heap.ts +1 -0
  308. package/src/types/data-structures/heap/index.ts +1 -0
  309. package/src/types/data-structures/heap/max-heap.ts +1 -0
  310. package/src/types/data-structures/heap/min-heap.ts +1 -0
  311. package/src/types/data-structures/index.ts +11 -0
  312. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  313. package/src/types/data-structures/linked-list/index.ts +2 -0
  314. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  315. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  316. package/src/types/data-structures/matrix/index.ts +1 -0
  317. package/src/types/data-structures/matrix/matrix.ts +1 -0
  318. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  319. package/src/types/data-structures/matrix/navigator.ts +14 -0
  320. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  321. package/src/types/data-structures/priority-queue/index.ts +3 -0
  322. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  323. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  324. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  325. package/src/types/data-structures/queue/deque.ts +1 -0
  326. package/src/types/data-structures/queue/index.ts +2 -0
  327. package/src/types/data-structures/queue/queue.ts +1 -0
  328. package/src/types/data-structures/stack/index.ts +1 -0
  329. package/src/types/data-structures/stack/stack.ts +1 -0
  330. package/src/types/data-structures/tree/index.ts +1 -0
  331. package/src/types/data-structures/tree/tree.ts +1 -0
  332. package/src/types/data-structures/trie/index.ts +1 -0
  333. package/src/types/data-structures/trie/trie.ts +1 -0
  334. package/src/types/index.ts +3 -0
  335. package/src/types/utils/index.ts +2 -0
  336. package/src/types/utils/utils.ts +6 -0
  337. package/src/types/utils/validate-type.ts +35 -0
  338. package/src/utils/index.ts +1 -0
  339. package/src/utils/utils.ts +101 -0
  340. package/test/index.test.ts +111 -0
  341. package/tsconfig.json +38 -0
@@ -0,0 +1,990 @@
1
+ "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Tyler Zeng
6
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.ObjectDeque = exports.Deque = void 0;
11
+ const utils_1 = require("../../utils");
12
+ /**
13
+ * Deque can provide random access with O(1) time complexity
14
+ * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
15
+ * Deque may experience performance jitter, but DoublyLinkedList will not
16
+ * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
17
+ */
18
+ class Deque {
19
+ /**
20
+ * The constructor initializes a data structure with a specified bucket size and populates it with
21
+ * elements from an iterable.
22
+ * @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that
23
+ * contains the initial elements to be stored in the data structure. It can also be an object with a
24
+ * `length` property or a `size` property, which represents the number of elements in the iterable.
25
+ * @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
26
+ * stored in each bucket. It determines the size of each bucket in the data structure.
27
+ */
28
+ constructor(elements = [], bucketSize = (1 << 12)) {
29
+ this._bucketFirst = 0;
30
+ this._firstInBucket = 0;
31
+ this._bucketLast = 0;
32
+ this._lastInBucket = 0;
33
+ this._bucketCount = 0;
34
+ this._buckets = [];
35
+ this._size = 0;
36
+ let _size;
37
+ if ('length' in elements) {
38
+ if (elements.length instanceof Function)
39
+ _size = elements.length();
40
+ else
41
+ _size = elements.length;
42
+ }
43
+ else {
44
+ if (elements.size instanceof Function)
45
+ _size = elements.size();
46
+ else
47
+ _size = elements.size;
48
+ }
49
+ this._bucketSize = bucketSize;
50
+ this._bucketCount = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize) || 1;
51
+ for (let i = 0; i < this._bucketCount; ++i) {
52
+ this._buckets.push(new Array(this._bucketSize));
53
+ }
54
+ const needBucketNum = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize);
55
+ this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
56
+ this._firstInBucket = this._lastInBucket = (this._bucketSize - _size % this._bucketSize) >> 1;
57
+ for (const element of elements) {
58
+ this.push(element);
59
+ }
60
+ }
61
+ get buckets() {
62
+ return this._buckets;
63
+ }
64
+ get size() {
65
+ return this._size;
66
+ }
67
+ /**
68
+ * The function returns the first element in a collection if it exists, otherwise it returns
69
+ * undefined.
70
+ * @returns The first element of the collection, of type E, is being returned.
71
+ */
72
+ get first() {
73
+ if (this.size === 0)
74
+ return;
75
+ return this._buckets[this._bucketFirst][this._firstInBucket];
76
+ }
77
+ get last() {
78
+ if (this.size === 0)
79
+ return;
80
+ return this._buckets[this._bucketLast][this._lastInBucket];
81
+ }
82
+ /**
83
+ * Time Complexity: O(1) - Removes the last element.
84
+ * Space Complexity: O(1) - Operates in-place.
85
+ */
86
+ isEmpty() {
87
+ return this.size === 0;
88
+ }
89
+ /**
90
+ * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
91
+ * Space Complexity: O(n) - Due to potential resizing.
92
+ */
93
+ /**
94
+ * Time Complexity: O(1)
95
+ * Space Complexity: O(n) - In worst case, resizing doubles the array size.
96
+ *
97
+ * The addLast function adds an element to the end of an array.
98
+ * @param {E} element - The element parameter represents the element that you want to add to the end of the
99
+ * data structure.
100
+ */
101
+ addLast(element) {
102
+ this.push(element);
103
+ }
104
+ /**
105
+ * Time Complexity: O(1) - Removes the first element.
106
+ * Space Complexity: O(1) - In-place operation.
107
+ */
108
+ /**
109
+ * Time Complexity: O(1) - Removes the last element.
110
+ * Space Complexity: O(1) - Operates in-place.
111
+ *
112
+ * The function "popLast" removes and returns the last element of an array.
113
+ * @returns The last element of the array is being returned.
114
+ */
115
+ popLast() {
116
+ return this.pop();
117
+ }
118
+ /**
119
+ * Time Complexity: O(1).
120
+ * Space Complexity: O(n) - Due to potential resizing.
121
+ *
122
+ * The "addFirst" function adds an element to the beginning of an array.
123
+ * @param {E} element - The parameter "element" represents the element that you want to add to the
124
+ * beginning of the data structure.
125
+ */
126
+ addFirst(element) {
127
+ this.unshift(element);
128
+ }
129
+ /**
130
+ * Time Complexity: O(1) - Removes the first element.
131
+ * Space Complexity: O(1) - In-place operation.
132
+ *
133
+ * The function "popFirst" removes and returns the first element of an array.
134
+ * @returns The method `popFirst()` is returning the first element of the array after removing it
135
+ * from the beginning. If the array is empty, it will return `undefined`.
136
+ */
137
+ popFirst() {
138
+ return this.shift();
139
+ }
140
+ /**
141
+ * The clear() function resets the state of the object by initializing all variables to their default
142
+ * values.
143
+ */
144
+ clear() {
145
+ this._buckets = [new Array(this._bucketSize)];
146
+ this._bucketCount = 1;
147
+ this._bucketFirst = this._bucketLast = this._size = 0;
148
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
149
+ }
150
+ /**
151
+ * The below function is a generator that yields elements from a collection one by one.
152
+ */
153
+ *begin() {
154
+ let index = 0;
155
+ while (index < this.size) {
156
+ yield this.getAt(index);
157
+ index++;
158
+ }
159
+ }
160
+ /**
161
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
162
+ * the last element.
163
+ */
164
+ *reverseBegin() {
165
+ let index = this.size - 1;
166
+ while (index >= 0) {
167
+ yield this.getAt(index);
168
+ index--;
169
+ }
170
+ }
171
+ /**
172
+ * Time Complexity - Amortized O(1) (possible reallocation)
173
+ * Space Complexity - O(n) (due to potential resizing).
174
+ */
175
+ /**
176
+ * Time Complexity - Amortized O(1) (possible reallocation),
177
+ * Space Complexity - O(n) (due to potential resizing).
178
+ *
179
+ * The push function adds an element to a data structure and reallocates memory if necessary.
180
+ * @param {E} element - The `element` parameter represents the value that you want to add to the data
181
+ * structure.
182
+ * @returns The size of the data structure after the element has been pushed.
183
+ */
184
+ push(element) {
185
+ if (this.size) {
186
+ if (this._lastInBucket < this._bucketSize - 1) {
187
+ this._lastInBucket += 1;
188
+ }
189
+ else if (this._bucketLast < this._bucketCount - 1) {
190
+ this._bucketLast += 1;
191
+ this._lastInBucket = 0;
192
+ }
193
+ else {
194
+ this._bucketLast = 0;
195
+ this._lastInBucket = 0;
196
+ }
197
+ if (this._bucketLast === this._bucketFirst &&
198
+ this._lastInBucket === this._firstInBucket)
199
+ this._reallocate();
200
+ }
201
+ this._size += 1;
202
+ this._buckets[this._bucketLast][this._lastInBucket] = element;
203
+ return this.size;
204
+ }
205
+ /**
206
+ * Time Complexity: O(1)
207
+ * Space Complexity: O(1)
208
+ */
209
+ /**
210
+ * Time Complexity: O(1)
211
+ * Space Complexity: O(1)
212
+ *
213
+ * The `pop()` function removes and returns the last element from a data structure, updating the
214
+ * internal state variables accordingly.
215
+ * @returns The element that was removed from the data structure is being returned.
216
+ */
217
+ pop() {
218
+ if (this.size === 0)
219
+ return;
220
+ const element = this._buckets[this._bucketLast][this._lastInBucket];
221
+ if (this.size !== 1) {
222
+ if (this._lastInBucket > 0) {
223
+ this._lastInBucket -= 1;
224
+ }
225
+ else if (this._bucketLast > 0) {
226
+ this._bucketLast -= 1;
227
+ this._lastInBucket = this._bucketSize - 1;
228
+ }
229
+ else {
230
+ this._bucketLast = this._bucketCount - 1;
231
+ this._lastInBucket = this._bucketSize - 1;
232
+ }
233
+ }
234
+ this._size -= 1;
235
+ return element;
236
+ }
237
+ /**
238
+ * Time Complexity: Amortized O(1)
239
+ * Space Complexity: O(n)
240
+ */
241
+ /**
242
+ * Time Complexity: Amortized O(1)
243
+ * Space Complexity: O(n)
244
+ *
245
+ * The `unshift` function adds an element to the beginning of an array-like data structure and
246
+ * returns the new size of the structure.
247
+ * @param {E} element - The `element` parameter represents the element that you want to add to the
248
+ * beginning of the data structure.
249
+ * @returns The size of the data structure after the element has been added.
250
+ */
251
+ unshift(element) {
252
+ if (this.size) {
253
+ if (this._firstInBucket > 0) {
254
+ this._firstInBucket -= 1;
255
+ }
256
+ else if (this._bucketFirst > 0) {
257
+ this._bucketFirst -= 1;
258
+ this._firstInBucket = this._bucketSize - 1;
259
+ }
260
+ else {
261
+ this._bucketFirst = this._bucketCount - 1;
262
+ this._firstInBucket = this._bucketSize - 1;
263
+ }
264
+ if (this._bucketFirst === this._bucketLast &&
265
+ this._firstInBucket === this._lastInBucket)
266
+ this._reallocate();
267
+ }
268
+ this._size += 1;
269
+ this._buckets[this._bucketFirst][this._firstInBucket] = element;
270
+ return this.size;
271
+ }
272
+ /**
273
+ * Time Complexity: O(1)
274
+ * Space Complexity: O(1)
275
+ */
276
+ /**
277
+ * Time Complexity: O(1)
278
+ * Space Complexity: O(1)
279
+ *
280
+ * The `shift()` function removes and returns the first element from a data structure, updating the
281
+ * internal state variables accordingly.
282
+ * @returns The element that is being removed from the beginning of the data structure is being
283
+ * returned.
284
+ */
285
+ shift() {
286
+ if (this.size === 0)
287
+ return;
288
+ const element = this._buckets[this._bucketFirst][this._firstInBucket];
289
+ if (this.size !== 1) {
290
+ if (this._firstInBucket < this._bucketSize - 1) {
291
+ this._firstInBucket += 1;
292
+ }
293
+ else if (this._bucketFirst < this._bucketCount - 1) {
294
+ this._bucketFirst += 1;
295
+ this._firstInBucket = 0;
296
+ }
297
+ else {
298
+ this._bucketFirst = 0;
299
+ this._firstInBucket = 0;
300
+ }
301
+ }
302
+ this._size -= 1;
303
+ return element;
304
+ }
305
+ /**
306
+ * Time Complexity: O(1)
307
+ * Space Complexity: O(1)
308
+ */
309
+ /**
310
+ * Time Complexity: O(1)
311
+ * Space Complexity: O(1)
312
+ *
313
+ * The `getAt` function retrieves an element at a specified position in an array-like data structure.
314
+ * @param {number} pos - The `pos` parameter represents the position of the element that you want to
315
+ * retrieve from the data structure. It is of type `number` and should be a valid index within the
316
+ * range of the data structure.
317
+ * @returns The element at the specified position in the data structure is being returned.
318
+ */
319
+ getAt(pos) {
320
+ (0, utils_1.rangeCheck)(pos, 0, this.size - 1);
321
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
322
+ return this._buckets[bucketIndex][indexInBucket];
323
+ }
324
+ /**
325
+ * Time Complexity: O(1)
326
+ * Space Complexity: O(1)
327
+ */
328
+ /**
329
+ * Time Complexity: O(1)
330
+ * Space Complexity: O(1)
331
+ *
332
+ * The `setAt` function sets an element at a specific position in an array-like data structure.
333
+ * @param {number} pos - The `pos` parameter represents the position at which the element needs to be
334
+ * set. It is of type `number`.
335
+ * @param {E} element - The `element` parameter is the value that you want to set at the specified
336
+ * position in the data structure.
337
+ */
338
+ setAt(pos, element) {
339
+ (0, utils_1.rangeCheck)(pos, 0, this.size - 1);
340
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
341
+ this._buckets[bucketIndex][indexInBucket] = element;
342
+ }
343
+ /**
344
+ * Time Complexity: O(n)
345
+ * Space Complexity: O(n)
346
+ */
347
+ /**
348
+ * Time Complexity: O(n)
349
+ * Space Complexity: O(n)
350
+ *
351
+ * The `insertAt` function inserts one or more elements at a specified position in an array-like data
352
+ * structure.
353
+ * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
354
+ * be inserted. It is of type `number`.
355
+ * @param {E} element - The `element` parameter represents the element that you want to insert into
356
+ * the array at the specified position.
357
+ * @param [num=1] - The `num` parameter represents the number of times the `element` should be
358
+ * inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
359
+ * will be inserted once. However, you can provide a different value for `num` if you want
360
+ * @returns The size of the array after the insertion is being returned.
361
+ */
362
+ insertAt(pos, element, num = 1) {
363
+ const length = this.size;
364
+ (0, utils_1.rangeCheck)(pos, 0, length);
365
+ if (pos === 0) {
366
+ while (num--)
367
+ this.unshift(element);
368
+ }
369
+ else if (pos === this.size) {
370
+ while (num--)
371
+ this.push(element);
372
+ }
373
+ else {
374
+ const arr = [];
375
+ for (let i = pos; i < this.size; ++i) {
376
+ arr.push(this.getAt(i));
377
+ }
378
+ this.cut(pos - 1);
379
+ for (let i = 0; i < num; ++i)
380
+ this.push(element);
381
+ for (let i = 0; i < arr.length; ++i)
382
+ this.push(arr[i]);
383
+ }
384
+ return this.size;
385
+ }
386
+ /**
387
+ * Time Complexity: O(1)
388
+ * Space Complexity: O(1)
389
+ */
390
+ /**
391
+ * Time Complexity: O(1)
392
+ * Space Complexity: O(1)
393
+ *
394
+ * The `cut` function updates the state of the object based on the given position and returns the
395
+ * updated size.
396
+ * @param {number} pos - The `pos` parameter represents the position at which the string should be
397
+ * cut. It is a number that indicates the index of the character where the cut should be made.
398
+ * @returns The method is returning the updated size of the data structure.
399
+ */
400
+ cut(pos) {
401
+ if (pos < 0) {
402
+ this.clear();
403
+ return 0;
404
+ }
405
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
406
+ this._bucketLast = bucketIndex;
407
+ this._lastInBucket = indexInBucket;
408
+ this._size = pos + 1;
409
+ return this.size;
410
+ }
411
+ /**
412
+ * Time Complexity: O(n)
413
+ * Space Complexity: O(1)
414
+ */
415
+ /**
416
+ * Time Complexity: O(n)
417
+ * Space Complexity: O(1)
418
+ *
419
+ * The `deleteAt` function removes an element at a specified position in an array-like data
420
+ * structure.
421
+ * @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
422
+ * which an element needs to be deleted from the data structure. It is of type `number` and indicates
423
+ * the index of the element to be deleted.
424
+ * @returns The size of the data structure after the deletion operation is performed.
425
+ */
426
+ deleteAt(pos) {
427
+ (0, utils_1.rangeCheck)(pos, 0, this.size - 1);
428
+ if (pos === 0)
429
+ this.shift();
430
+ else if (pos === this.size - 1)
431
+ this.pop();
432
+ else {
433
+ const length = this.size - 1;
434
+ let { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(pos);
435
+ for (let i = pos; i < length; ++i) {
436
+ const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(pos + 1);
437
+ this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
438
+ curBucket = nextBucket;
439
+ curPointer = nextPointer;
440
+ }
441
+ this.pop();
442
+ }
443
+ return this.size;
444
+ }
445
+ /**
446
+ * Time Complexity: O(n)
447
+ * Space Complexity: O(1)
448
+ */
449
+ /**
450
+ * Time Complexity: O(n)
451
+ * Space Complexity: O(1)
452
+ *
453
+ * The `delete` function removes all occurrences of a specified element from an array-like data
454
+ * structure.
455
+ * @param {E} element - The `element` parameter represents the element that you want to delete from
456
+ * the data structure.
457
+ * @returns The size of the data structure after the element has been deleted.
458
+ */
459
+ delete(element) {
460
+ const size = this.size;
461
+ if (size === 0)
462
+ return 0;
463
+ let i = 0;
464
+ let index = 0;
465
+ while (i < size) {
466
+ const oldElement = this.getAt(i);
467
+ if (oldElement !== element) {
468
+ this.setAt(index, oldElement);
469
+ index += 1;
470
+ }
471
+ i += 1;
472
+ }
473
+ this.cut(index - 1);
474
+ return this.size;
475
+ }
476
+ /**
477
+ * Time Complexity: O(n)
478
+ * Space Complexity: O(1)
479
+ */
480
+ /**
481
+ * Time Complexity: O(n)
482
+ * Space Complexity: O(1)
483
+ *
484
+ * The reverse() function reverses the order of the buckets and the elements within each bucket in a
485
+ * data structure.
486
+ * @returns The reverse() method is returning the object itself (this) after performing the reverse
487
+ * operation on the buckets and updating the relevant properties.
488
+ */
489
+ reverse() {
490
+ this._buckets.reverse().forEach(function (bucket) {
491
+ bucket.reverse();
492
+ });
493
+ const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
494
+ this._bucketFirst = this._bucketCount - _bucketLast - 1;
495
+ this._bucketLast = this._bucketCount - _bucketFirst - 1;
496
+ this._firstInBucket = this._bucketSize - _lastInBucket - 1;
497
+ this._lastInBucket = this._bucketSize - _firstInBucket - 1;
498
+ return this;
499
+ }
500
+ /**
501
+ * Time Complexity: O(n)
502
+ * Space Complexity: O(1)
503
+ */
504
+ /**
505
+ * Time Complexity: O(n)
506
+ * Space Complexity: O(1)
507
+ *
508
+ * The `unique()` function removes duplicate elements from an array-like data structure and returns
509
+ * the number of unique elements.
510
+ * @returns The size of the modified array is being returned.
511
+ */
512
+ unique() {
513
+ if (this.size <= 1) {
514
+ return this.size;
515
+ }
516
+ let index = 1;
517
+ let prev = this.getAt(0);
518
+ for (let i = 1; i < this.size; ++i) {
519
+ const cur = this.getAt(i);
520
+ if (cur !== prev) {
521
+ prev = cur;
522
+ this.setAt(index++, cur);
523
+ }
524
+ }
525
+ this.cut(index - 1);
526
+ return this.size;
527
+ }
528
+ /**
529
+ * Time Complexity: O(n log n)
530
+ * Space Complexity: O(n)
531
+ */
532
+ /**
533
+ * Time Complexity: O(n log n)
534
+ * Space Complexity: O(n)
535
+ *
536
+ * The `sort` function sorts the elements in a data structure using a provided comparator function.
537
+ * @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
538
+ * `y` of type `E` and returns a number. The comparator function is used to determine the order of
539
+ * the elements in the sorted array.
540
+ * @returns The method is returning the sorted instance of the object on which the method is called.
541
+ */
542
+ sort(comparator) {
543
+ const arr = [];
544
+ for (let i = 0; i < this.size; ++i) {
545
+ arr.push(this.getAt(i));
546
+ }
547
+ arr.sort(comparator);
548
+ for (let i = 0; i < this.size; ++i) {
549
+ this.setAt(i, arr[i]);
550
+ }
551
+ return this;
552
+ }
553
+ /**
554
+ * Time Complexity: O(n)
555
+ * Space Complexity: O(n)
556
+ */
557
+ /**
558
+ * Time Complexity: O(n)
559
+ * Space Complexity: O(n)
560
+ *
561
+ * The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
562
+ * memory usage.
563
+ * @returns Nothing is being returned. The function is using the `return` statement to exit early if
564
+ * `this.size` is 0, but it does not return any value.
565
+ */
566
+ shrinkToFit() {
567
+ if (this.size === 0)
568
+ return;
569
+ const newBuckets = [];
570
+ if (this._bucketFirst === this._bucketLast)
571
+ return;
572
+ else if (this._bucketFirst < this._bucketLast) {
573
+ for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
574
+ newBuckets.push(this._buckets[i]);
575
+ }
576
+ }
577
+ else {
578
+ for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
579
+ newBuckets.push(this._buckets[i]);
580
+ }
581
+ for (let i = 0; i <= this._bucketLast; ++i) {
582
+ newBuckets.push(this._buckets[i]);
583
+ }
584
+ }
585
+ this._bucketFirst = 0;
586
+ this._bucketLast = newBuckets.length - 1;
587
+ this._buckets = newBuckets;
588
+ }
589
+ /**
590
+ * Time Complexity: O(n)
591
+ * Space Complexity: O(1)
592
+ */
593
+ /**
594
+ * Time Complexity: O(n)
595
+ * Space Complexity: O(1)
596
+ *
597
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
598
+ * each element.
599
+ * @param callback - The callback parameter is a function that will be called for each element in the
600
+ * deque. It takes three parameters:
601
+ */
602
+ forEach(callback) {
603
+ for (let i = 0; i < this.size; ++i) {
604
+ callback(this.getAt(i), i, this);
605
+ }
606
+ }
607
+ /**
608
+ * Time Complexity: O(n)
609
+ * Space Complexity: O(1)
610
+ */
611
+ /**
612
+ * Time Complexity: O(n)
613
+ * Space Complexity: O(1)
614
+ *
615
+ * The `find` function iterates over the elements in a deque and returns the first element for which
616
+ * the callback function returns true, or undefined if no such element is found.
617
+ * @param callback - A function that takes three parameters: element, index, and deque. It should
618
+ * return a boolean value indicating whether the element satisfies a certain condition.
619
+ * @returns The method `find` returns the first element in the deque that satisfies the condition
620
+ * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
621
+ */
622
+ find(callback) {
623
+ for (let i = 0; i < this.size; ++i) {
624
+ const element = this.getAt(i);
625
+ if (callback(element, i, this)) {
626
+ return element;
627
+ }
628
+ }
629
+ return undefined;
630
+ }
631
+ /**
632
+ * Time Complexity: O(n)
633
+ * Space Complexity: O(n)
634
+ */
635
+ /**
636
+ * Time Complexity: O(n)
637
+ * Space Complexity: O(n)
638
+ *
639
+ * The `toArray` function converts the elements of a data structure into an array.
640
+ * @returns The `toArray()` method is returning an array of elements of type `E`.
641
+ */
642
+ toArray() {
643
+ const arr = [];
644
+ for (let i = 0; i < this.size; ++i) {
645
+ arr.push(this.getAt(i));
646
+ }
647
+ return arr;
648
+ }
649
+ /**
650
+ * Time Complexity: O(n)
651
+ * Space Complexity: O(n)
652
+ */
653
+ /**
654
+ * Time Complexity: O(n)
655
+ * Space Complexity: O(n)
656
+ *
657
+ * The `map` function takes a callback function and applies it to each element in the deque,
658
+ * returning a new deque with the results.
659
+ * @param callback - The `callback` parameter is a function that takes three arguments:
660
+ * @returns The `map` method is returning a new `Deque` object with the transformed elements.
661
+ */
662
+ map(callback) {
663
+ const newDeque = new Deque([], this._bucketSize);
664
+ for (let i = 0; i < this.size; ++i) {
665
+ newDeque.push(callback(this.getAt(i), i, this));
666
+ }
667
+ return newDeque;
668
+ }
669
+ /**
670
+ * Time Complexity: O(n)
671
+ * Space Complexity: O(n)
672
+ */
673
+ /**
674
+ * Time Complexity: O(n)
675
+ * Space Complexity: O(n)
676
+ *
677
+ * The `filter` function creates a new deque containing only the elements that satisfy the given
678
+ * predicate function.
679
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
680
+ * `index`, and `deque`.
681
+ * @returns The `filter` method is returning a new `Deque` object that contains only the elements
682
+ * that satisfy the given `predicate` function.
683
+ */
684
+ filter(predicate) {
685
+ const newDeque = new Deque([], this._bucketSize);
686
+ for (let i = 0; i < this.size; ++i) {
687
+ const element = this.getAt(i);
688
+ if (predicate(element, i, this)) {
689
+ newDeque.push(element);
690
+ }
691
+ }
692
+ return newDeque;
693
+ }
694
+ /**
695
+ * Time Complexity: O(n)
696
+ * Space Complexity: O(1)
697
+ */
698
+ /**
699
+ * Time Complexity: O(n)
700
+ * Space Complexity: O(1)
701
+ *
702
+ * The `reduce` function iterates over the elements of a deque and applies a callback function to
703
+ * each element, accumulating a single value.
704
+ * @param callback - The `callback` parameter is a function that takes four arguments:
705
+ * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
706
+ * is the value that will be passed as the first argument to the `callback` function when reducing
707
+ * the elements of the deque.
708
+ * @returns the final value of the accumulator after iterating over all elements in the deque and
709
+ * applying the callback function to each element.
710
+ */
711
+ reduce(callback, initialValue) {
712
+ let accumulator = initialValue;
713
+ for (let i = 0; i < this.size; ++i) {
714
+ accumulator = callback(accumulator, this.getAt(i), i, this);
715
+ }
716
+ return accumulator;
717
+ }
718
+ /**
719
+ * Time Complexity: O(n)
720
+ * Space Complexity: O(1)
721
+ */
722
+ /**
723
+ * Time Complexity: O(n)
724
+ * Space Complexity: O(1)
725
+ *
726
+ * The function "indexOf" returns the index of the first occurrence of a given element in an array,
727
+ * or -1 if the element is not found.
728
+ * @param {E} element - The "element" parameter represents the element that you want to find the
729
+ * index of in the data structure.
730
+ * @returns The indexOf function returns the index of the first occurrence of the specified element
731
+ * in the data structure. If the element is not found, it returns -1.
732
+ */
733
+ indexOf(element) {
734
+ for (let i = 0; i < this.size; ++i) {
735
+ if (this.getAt(i) === element) {
736
+ return i;
737
+ }
738
+ }
739
+ return -1;
740
+ }
741
+ /**
742
+ * Time Complexity: O(n)
743
+ * Space Complexity: O(1)
744
+ */
745
+ /**
746
+ * Time Complexity: O(n)
747
+ * Space Complexity: O(1)
748
+ *
749
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
750
+ * object to be iterated over using a for...of loop.
751
+ */
752
+ *[Symbol.iterator]() {
753
+ for (let i = 0; i < this.size; ++i) {
754
+ yield this.getAt(i);
755
+ }
756
+ }
757
+ /**
758
+ * Time Complexity: O(n)
759
+ * Space Complexity: O(n)
760
+ */
761
+ /**
762
+ * Time Complexity: O(n)
763
+ * Space Complexity: O(n)
764
+ *
765
+ * The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
766
+ * @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
767
+ * specifies the number of new buckets needed. If not provided, it will default to half of the
768
+ * current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
769
+ */
770
+ _reallocate(needBucketNum) {
771
+ const newBuckets = [];
772
+ const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
773
+ for (let i = 0; i < addBucketNum; ++i) {
774
+ newBuckets[i] = new Array(this._bucketSize);
775
+ }
776
+ for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
777
+ newBuckets[newBuckets.length] = this._buckets[i];
778
+ }
779
+ for (let i = 0; i < this._bucketLast; ++i) {
780
+ newBuckets[newBuckets.length] = this._buckets[i];
781
+ }
782
+ newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
783
+ this._bucketFirst = addBucketNum;
784
+ this._bucketLast = newBuckets.length - 1;
785
+ for (let i = 0; i < addBucketNum; ++i) {
786
+ newBuckets[newBuckets.length] = new Array(this._bucketSize);
787
+ }
788
+ this._buckets = newBuckets;
789
+ this._bucketCount = newBuckets.length;
790
+ }
791
+ /**
792
+ * Time Complexity: O(1)
793
+ * Space Complexity: O(1)
794
+ */
795
+ /**
796
+ * Time Complexity: O(1)
797
+ * Space Complexity: O(1)
798
+ *
799
+ * The function calculates the bucket index and index within the bucket based on the given position.
800
+ * @param {number} pos - The `pos` parameter represents the position within the data structure. It is
801
+ * a number that indicates the index or position of an element within the structure.
802
+ * @returns an object with two properties: "bucketIndex" and "indexInBucket".
803
+ */
804
+ _getBucketAndPosition(pos) {
805
+ let bucketIndex;
806
+ let indexInBucket;
807
+ const overallIndex = this._firstInBucket + pos;
808
+ bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
809
+ if (bucketIndex >= this._bucketCount) {
810
+ bucketIndex -= this._bucketCount;
811
+ }
812
+ indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
813
+ if (indexInBucket < 0) {
814
+ indexInBucket = this._bucketSize - 1;
815
+ }
816
+ return { bucketIndex, indexInBucket };
817
+ }
818
+ }
819
+ exports.Deque = Deque;
820
+ // O(1) time complexity of obtaining the element
821
+ // O(n) time complexity of adding at the beginning and the end
822
+ // todo tested slowest one
823
+ class ObjectDeque {
824
+ constructor(capacity) {
825
+ this._nodes = {};
826
+ this._capacity = Number.MAX_SAFE_INTEGER;
827
+ this._first = -1;
828
+ this._last = -1;
829
+ this._size = 0;
830
+ if (capacity !== undefined)
831
+ this._capacity = capacity;
832
+ }
833
+ get nodes() {
834
+ return this._nodes;
835
+ }
836
+ get capacity() {
837
+ return this._capacity;
838
+ }
839
+ get first() {
840
+ return this._first;
841
+ }
842
+ get last() {
843
+ return this._last;
844
+ }
845
+ get size() {
846
+ return this._size;
847
+ }
848
+ /**
849
+ * Time Complexity: O(1)
850
+ * Space Complexity: O(1)
851
+ */
852
+ /**
853
+ * Time Complexity: O(1)
854
+ * Space Complexity: O(1)
855
+ *
856
+ * The "addFirst" function adds an element to the beginning of an array-like data structure.
857
+ * @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
858
+ * structure.
859
+ */
860
+ addFirst(element) {
861
+ if (this.size === 0) {
862
+ const mid = Math.floor(this.capacity / 2);
863
+ this._first = mid;
864
+ this._last = mid;
865
+ }
866
+ else {
867
+ this._first--;
868
+ }
869
+ this.nodes[this.first] = element;
870
+ this._size++;
871
+ }
872
+ /**
873
+ * Time Complexity: O(1)
874
+ * Space Complexity: O(1)
875
+ */
876
+ /**
877
+ * Time Complexity: O(1)
878
+ * Space Complexity: O(1)
879
+ *
880
+ * The addLast function adds an element to the end of an array-like data structure.
881
+ * @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
882
+ */
883
+ addLast(element) {
884
+ if (this.size === 0) {
885
+ const mid = Math.floor(this.capacity / 2);
886
+ this._first = mid;
887
+ this._last = mid;
888
+ }
889
+ else {
890
+ this._last++;
891
+ }
892
+ this.nodes[this.last] = element;
893
+ this._size++;
894
+ }
895
+ /**
896
+ * Time Complexity: O(1)
897
+ * Space Complexity: O(1)
898
+ */
899
+ /**
900
+ * Time Complexity: O(1)
901
+ * Space Complexity: O(1)
902
+ *
903
+ * The function `popFirst()` removes and returns the first element in a data structure.
904
+ * @returns The element of the first element in the data structure.
905
+ */
906
+ popFirst() {
907
+ if (!this.size)
908
+ return;
909
+ const element = this.getFirst();
910
+ delete this.nodes[this.first];
911
+ this._first++;
912
+ this._size--;
913
+ return element;
914
+ }
915
+ /**
916
+ * Time Complexity: O(1)
917
+ * Space Complexity: O(1)
918
+ */
919
+ /**
920
+ * Time Complexity: O(1)
921
+ * Space Complexity: O(1)
922
+ *
923
+ * The `getFirst` function returns the first element in an array-like data structure if it exists.
924
+ * @returns The element at the first position of the `_nodes` array.
925
+ */
926
+ getFirst() {
927
+ if (this.size)
928
+ return this.nodes[this.first];
929
+ }
930
+ /**
931
+ * Time Complexity: O(1)
932
+ * Space Complexity: O(1)
933
+ */
934
+ /**
935
+ * Time Complexity: O(1)
936
+ * Space Complexity: O(1)
937
+ *
938
+ * The `popLast()` function removes and returns the last element in a data structure.
939
+ * @returns The element that was removed from the data structure.
940
+ */
941
+ popLast() {
942
+ if (!this.size)
943
+ return;
944
+ const element = this.getLast();
945
+ delete this.nodes[this.last];
946
+ this._last--;
947
+ this._size--;
948
+ return element;
949
+ }
950
+ /**
951
+ * Time Complexity: O(1)
952
+ * Space Complexity: O(1)
953
+ */
954
+ /**
955
+ * Time Complexity: O(1)
956
+ * Space Complexity: O(1)
957
+ *
958
+ * The `getLast()` function returns the last element in an array-like data structure.
959
+ * @returns The last element in the array "_nodes" is being returned.
960
+ */
961
+ getLast() {
962
+ if (this.size)
963
+ return this.nodes[this.last];
964
+ }
965
+ /**
966
+ * Time Complexity: O(1)
967
+ * Space Complexity: O(1)
968
+ */
969
+ /**
970
+ * Time Complexity: O(1)
971
+ * Space Complexity: O(1)
972
+ *
973
+ * The get function returns the element at the specified index in an array-like data structure.
974
+ * @param {number} index - The index parameter is a number that represents the position of the element you want to
975
+ * retrieve from the array.
976
+ * @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
977
+ * index, `undefined` is returned.
978
+ */
979
+ get(index) {
980
+ return this.nodes[this.first + index] || undefined;
981
+ }
982
+ /**
983
+ * The function checks if the size of a data structure is less than or equal to zero.
984
+ * @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
985
+ */
986
+ isEmpty() {
987
+ return this.size <= 0;
988
+ }
989
+ }
990
+ exports.ObjectDeque = ObjectDeque;