min-heap-typed 1.40.0-rc → 1.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (347) hide show
  1. package/.dependency-cruiser.js +422 -422
  2. package/.eslintrc.js +59 -59
  3. package/.prettierrc.js +14 -14
  4. package/README.md +20 -3
  5. package/coverage/clover.xml +11 -7
  6. package/coverage/coverage-final.json +95 -1
  7. package/coverage/coverage-summary.json +59 -2
  8. package/coverage/lcov-report/base.css +278 -99
  9. package/coverage/lcov-report/index.html +69 -65
  10. package/coverage/lcov-report/index.ts.html +36 -35
  11. package/coverage/lcov-report/sorter.js +15 -5
  12. package/dist/data-structures/binary-tree/avl-tree.d.ts +106 -0
  13. package/dist/data-structures/binary-tree/avl-tree.js +347 -0
  14. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
  15. package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
  16. package/dist/data-structures/binary-tree/binary-tree.d.ts +355 -0
  17. package/dist/data-structures/binary-tree/binary-tree.js +1115 -0
  18. package/dist/data-structures/binary-tree/bst.d.ts +167 -0
  19. package/dist/data-structures/binary-tree/bst.js +512 -0
  20. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  21. package/dist/data-structures/binary-tree/index.js +23 -0
  22. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -0
  23. package/dist/data-structures/binary-tree/rb-tree.js +21 -0
  24. package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
  25. package/dist/data-structures/binary-tree/segment-tree.js +180 -0
  26. package/dist/data-structures/binary-tree/tree-multiset.d.ts +126 -0
  27. package/dist/data-structures/binary-tree/tree-multiset.js +355 -0
  28. package/dist/data-structures/graph/abstract-graph.d.ts +313 -0
  29. package/dist/data-structures/graph/abstract-graph.js +884 -0
  30. package/dist/data-structures/graph/directed-graph.d.ts +194 -0
  31. package/dist/data-structures/graph/directed-graph.js +404 -0
  32. package/dist/data-structures/graph/index.d.ts +4 -0
  33. package/dist/data-structures/graph/index.js +20 -0
  34. package/dist/data-structures/graph/map-graph.d.ts +73 -0
  35. package/dist/data-structures/graph/map-graph.js +93 -0
  36. package/dist/data-structures/graph/undirected-graph.d.ts +120 -0
  37. package/dist/data-structures/graph/undirected-graph.js +239 -0
  38. package/dist/data-structures/hash/coordinate-map.d.ts +44 -0
  39. package/dist/data-structures/hash/coordinate-map.js +62 -0
  40. package/dist/data-structures/hash/coordinate-set.d.ts +36 -0
  41. package/dist/data-structures/hash/coordinate-set.js +52 -0
  42. package/dist/data-structures/hash/hash-map.d.ts +50 -0
  43. package/dist/data-structures/hash/hash-map.js +153 -0
  44. package/dist/data-structures/hash/hash-table.d.ts +103 -0
  45. package/dist/data-structures/hash/hash-table.js +236 -0
  46. package/dist/data-structures/hash/index.d.ts +6 -0
  47. package/dist/data-structures/hash/index.js +22 -0
  48. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  49. package/dist/data-structures/hash/tree-map.js +6 -0
  50. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  51. package/dist/data-structures/hash/tree-set.js +6 -0
  52. package/dist/data-structures/heap/heap.d.ts +235 -0
  53. package/dist/data-structures/heap/heap.js +515 -0
  54. package/dist/data-structures/heap/index.d.ts +3 -0
  55. package/dist/data-structures/heap/index.js +19 -0
  56. package/dist/data-structures/heap/max-heap.d.ts +15 -0
  57. package/dist/data-structures/heap/max-heap.js +26 -0
  58. package/dist/data-structures/heap/min-heap.d.ts +15 -0
  59. package/dist/data-structures/heap/min-heap.js +26 -0
  60. package/dist/data-structures/index.d.ts +11 -0
  61. package/dist/data-structures/index.js +27 -0
  62. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +253 -0
  63. package/dist/data-structures/linked-list/doubly-linked-list.js +569 -0
  64. package/dist/data-structures/linked-list/index.d.ts +3 -0
  65. package/dist/data-structures/linked-list/index.js +19 -0
  66. package/dist/data-structures/linked-list/singly-linked-list.d.ts +232 -0
  67. package/dist/data-structures/linked-list/singly-linked-list.js +533 -0
  68. package/dist/data-structures/linked-list/skip-linked-list.d.ts +80 -0
  69. package/dist/data-structures/linked-list/skip-linked-list.js +187 -0
  70. package/dist/data-structures/matrix/index.d.ts +4 -0
  71. package/dist/data-structures/matrix/index.js +20 -0
  72. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  73. package/dist/data-structures/matrix/matrix.js +28 -0
  74. package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
  75. package/dist/data-structures/matrix/matrix2d.js +199 -0
  76. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  77. package/dist/data-structures/matrix/navigator.js +106 -0
  78. package/dist/data-structures/matrix/vector2d.d.ts +200 -0
  79. package/dist/data-structures/matrix/vector2d.js +290 -0
  80. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  81. package/dist/data-structures/priority-queue/index.js +19 -0
  82. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
  83. package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
  84. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
  85. package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
  86. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
  87. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  88. package/dist/data-structures/queue/deque.d.ts +161 -0
  89. package/dist/data-structures/queue/deque.js +264 -0
  90. package/dist/data-structures/queue/index.d.ts +2 -0
  91. package/dist/data-structures/queue/index.js +18 -0
  92. package/dist/data-structures/queue/queue.d.ts +122 -0
  93. package/dist/data-structures/queue/queue.js +187 -0
  94. package/dist/data-structures/stack/index.d.ts +1 -0
  95. package/dist/data-structures/stack/index.js +17 -0
  96. package/dist/data-structures/stack/stack.d.ts +64 -0
  97. package/dist/data-structures/stack/stack.js +94 -0
  98. package/dist/data-structures/tree/index.d.ts +1 -0
  99. package/dist/data-structures/tree/index.js +17 -0
  100. package/dist/data-structures/tree/tree.d.ts +8 -0
  101. package/dist/data-structures/tree/tree.js +40 -0
  102. package/dist/data-structures/trie/index.d.ts +1 -0
  103. package/dist/data-structures/trie/index.js +17 -0
  104. package/dist/data-structures/trie/trie.d.ts +79 -0
  105. package/dist/data-structures/trie/trie.js +251 -0
  106. package/dist/index.d.ts +3 -1
  107. package/dist/index.js +18 -4
  108. package/dist/interfaces/binary-tree.d.ts +7 -0
  109. package/dist/interfaces/binary-tree.js +2 -0
  110. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  111. package/dist/interfaces/doubly-linked-list.js +2 -0
  112. package/dist/interfaces/graph.d.ts +5 -0
  113. package/dist/interfaces/graph.js +2 -0
  114. package/dist/interfaces/heap.d.ts +1 -0
  115. package/dist/interfaces/heap.js +2 -0
  116. package/dist/interfaces/index.d.ts +8 -0
  117. package/dist/interfaces/index.js +24 -0
  118. package/dist/interfaces/navigator.d.ts +1 -0
  119. package/dist/interfaces/navigator.js +2 -0
  120. package/dist/interfaces/priority-queue.d.ts +1 -0
  121. package/dist/interfaces/priority-queue.js +2 -0
  122. package/dist/interfaces/segment-tree.d.ts +1 -0
  123. package/dist/interfaces/segment-tree.js +2 -0
  124. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  125. package/dist/interfaces/singly-linked-list.js +2 -0
  126. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
  127. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  128. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  129. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  130. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  131. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  132. package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
  133. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  134. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  135. package/dist/types/data-structures/binary-tree/index.js +22 -0
  136. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +8 -0
  137. package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
  138. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  139. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  140. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  141. package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
  142. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  143. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  144. package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
  145. package/dist/types/data-structures/graph/directed-graph.js +9 -0
  146. package/dist/types/data-structures/graph/index.d.ts +3 -0
  147. package/dist/types/data-structures/graph/index.js +19 -0
  148. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  149. package/dist/types/data-structures/graph/map-graph.js +2 -0
  150. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  151. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  152. package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
  153. package/dist/types/data-structures/hash/coordinate-map.js +2 -0
  154. package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
  155. package/dist/types/data-structures/hash/coordinate-set.js +2 -0
  156. package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
  157. package/dist/types/data-structures/hash/hash-map.js +2 -0
  158. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  159. package/dist/types/data-structures/hash/hash-table.js +2 -0
  160. package/dist/types/data-structures/hash/index.d.ts +1 -0
  161. package/dist/types/data-structures/hash/index.js +2 -0
  162. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  163. package/dist/types/data-structures/hash/tree-map.js +2 -0
  164. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  165. package/dist/types/data-structures/hash/tree-set.js +2 -0
  166. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  167. package/dist/types/data-structures/heap/heap.js +2 -0
  168. package/dist/types/data-structures/heap/index.d.ts +1 -0
  169. package/dist/types/data-structures/heap/index.js +17 -0
  170. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  171. package/dist/types/data-structures/heap/max-heap.js +2 -0
  172. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  173. package/dist/types/data-structures/heap/min-heap.js +2 -0
  174. package/dist/types/data-structures/index.d.ts +11 -0
  175. package/dist/types/data-structures/index.js +27 -0
  176. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  177. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  178. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  179. package/dist/types/data-structures/linked-list/index.js +18 -0
  180. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  181. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  182. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  183. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  184. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  185. package/dist/types/data-structures/matrix/index.js +17 -0
  186. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  187. package/dist/types/data-structures/matrix/matrix.js +2 -0
  188. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  189. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  190. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  191. package/dist/types/data-structures/matrix/navigator.js +2 -0
  192. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  193. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  194. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  195. package/dist/types/data-structures/priority-queue/index.js +19 -0
  196. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  197. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  198. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  199. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  200. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  201. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  202. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  203. package/dist/types/data-structures/queue/deque.js +2 -0
  204. package/dist/types/data-structures/queue/index.d.ts +2 -0
  205. package/dist/types/data-structures/queue/index.js +18 -0
  206. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  207. package/dist/types/data-structures/queue/queue.js +2 -0
  208. package/dist/types/data-structures/stack/index.d.ts +1 -0
  209. package/dist/types/data-structures/stack/index.js +17 -0
  210. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  211. package/dist/types/data-structures/stack/stack.js +2 -0
  212. package/dist/types/data-structures/tree/index.d.ts +1 -0
  213. package/dist/types/data-structures/tree/index.js +17 -0
  214. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  215. package/dist/types/data-structures/tree/tree.js +2 -0
  216. package/dist/types/data-structures/trie/index.d.ts +1 -0
  217. package/dist/types/data-structures/trie/index.js +17 -0
  218. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  219. package/dist/types/data-structures/trie/trie.js +2 -0
  220. package/dist/types/helpers.d.ts +8 -0
  221. package/dist/types/helpers.js +9 -0
  222. package/dist/types/index.d.ts +3 -0
  223. package/dist/types/index.js +19 -0
  224. package/dist/types/utils/index.d.ts +2 -0
  225. package/dist/types/utils/index.js +18 -0
  226. package/dist/types/utils/utils.d.ts +7 -0
  227. package/dist/types/utils/utils.js +2 -0
  228. package/dist/types/utils/validate-type.d.ts +19 -0
  229. package/dist/types/utils/validate-type.js +2 -0
  230. package/dist/utils/index.d.ts +1 -0
  231. package/dist/utils/index.js +17 -0
  232. package/dist/utils/utils.d.ts +20 -0
  233. package/dist/utils/utils.js +73 -0
  234. package/jest.config.js +6 -6
  235. package/package.json +2 -2
  236. package/src/data-structures/binary-tree/avl-tree.ts +350 -0
  237. package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
  238. package/src/data-structures/binary-tree/binary-tree.ts +1262 -0
  239. package/src/data-structures/binary-tree/bst.ts +522 -0
  240. package/src/data-structures/binary-tree/index.ts +7 -0
  241. package/src/data-structures/binary-tree/rb-tree.ts +358 -0
  242. package/src/data-structures/binary-tree/segment-tree.ts +190 -0
  243. package/src/data-structures/binary-tree/tree-multiset.ts +379 -0
  244. package/src/data-structures/graph/abstract-graph.ts +1000 -0
  245. package/src/data-structures/graph/directed-graph.ts +449 -0
  246. package/src/data-structures/graph/index.ts +4 -0
  247. package/src/data-structures/graph/map-graph.ts +106 -0
  248. package/src/data-structures/graph/undirected-graph.ts +259 -0
  249. package/src/data-structures/hash/coordinate-map.ts +63 -0
  250. package/src/data-structures/hash/coordinate-set.ts +52 -0
  251. package/src/data-structures/hash/hash-map.ts +185 -0
  252. package/src/data-structures/hash/hash-table.ts +268 -0
  253. package/src/data-structures/hash/index.ts +6 -0
  254. package/src/data-structures/hash/tree-map.ts +2 -0
  255. package/src/data-structures/hash/tree-set.ts +2 -0
  256. package/src/data-structures/heap/heap.ts +589 -0
  257. package/src/data-structures/heap/index.ts +3 -0
  258. package/src/data-structures/heap/max-heap.ts +26 -0
  259. package/src/data-structures/heap/min-heap.ts +26 -0
  260. package/src/data-structures/index.ts +11 -0
  261. package/src/data-structures/linked-list/doubly-linked-list.ts +605 -0
  262. package/src/data-structures/linked-list/index.ts +3 -0
  263. package/src/data-structures/linked-list/singly-linked-list.ts +576 -0
  264. package/src/data-structures/linked-list/skip-linked-list.ts +221 -0
  265. package/src/data-structures/matrix/index.ts +4 -0
  266. package/src/data-structures/matrix/matrix.ts +27 -0
  267. package/src/data-structures/matrix/matrix2d.ts +211 -0
  268. package/src/data-structures/matrix/navigator.ts +121 -0
  269. package/src/data-structures/matrix/vector2d.ts +315 -0
  270. package/src/data-structures/priority-queue/index.ts +3 -0
  271. package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
  272. package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
  273. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  274. package/src/data-structures/queue/deque.ts +282 -0
  275. package/src/data-structures/queue/index.ts +2 -0
  276. package/src/data-structures/queue/queue.ts +209 -0
  277. package/src/data-structures/stack/index.ts +1 -0
  278. package/src/data-structures/stack/stack.ts +102 -0
  279. package/src/data-structures/tree/index.ts +1 -0
  280. package/src/data-structures/tree/tree.ts +41 -0
  281. package/src/data-structures/trie/index.ts +1 -0
  282. package/src/data-structures/trie/trie.ts +262 -0
  283. package/src/index.ts +4 -1
  284. package/src/interfaces/binary-tree.ts +10 -0
  285. package/src/interfaces/doubly-linked-list.ts +1 -0
  286. package/src/interfaces/graph.ts +7 -0
  287. package/src/interfaces/heap.ts +1 -0
  288. package/src/interfaces/index.ts +8 -0
  289. package/src/interfaces/navigator.ts +1 -0
  290. package/src/interfaces/priority-queue.ts +1 -0
  291. package/src/interfaces/segment-tree.ts +1 -0
  292. package/src/interfaces/singly-linked-list.ts +1 -0
  293. package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
  294. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  295. package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
  296. package/src/types/data-structures/binary-tree/bst.ts +11 -0
  297. package/src/types/data-structures/binary-tree/index.ts +6 -0
  298. package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
  299. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  300. package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
  301. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  302. package/src/types/data-structures/graph/directed-graph.ts +8 -0
  303. package/src/types/data-structures/graph/index.ts +3 -0
  304. package/src/types/data-structures/graph/map-graph.ts +1 -0
  305. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  306. package/src/types/data-structures/hash/coordinate-map.ts +1 -0
  307. package/src/types/data-structures/hash/coordinate-set.ts +1 -0
  308. package/src/types/data-structures/hash/hash-map.ts +1 -0
  309. package/src/types/data-structures/hash/hash-table.ts +1 -0
  310. package/src/types/data-structures/hash/index.ts +1 -0
  311. package/src/types/data-structures/hash/tree-map.ts +1 -0
  312. package/src/types/data-structures/hash/tree-set.ts +1 -0
  313. package/src/types/data-structures/heap/heap.ts +1 -0
  314. package/src/types/data-structures/heap/index.ts +1 -0
  315. package/src/types/data-structures/heap/max-heap.ts +1 -0
  316. package/src/types/data-structures/heap/min-heap.ts +1 -0
  317. package/src/types/data-structures/index.ts +11 -0
  318. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  319. package/src/types/data-structures/linked-list/index.ts +2 -0
  320. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  321. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  322. package/src/types/data-structures/matrix/index.ts +1 -0
  323. package/src/types/data-structures/matrix/matrix.ts +1 -0
  324. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  325. package/src/types/data-structures/matrix/navigator.ts +14 -0
  326. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  327. package/src/types/data-structures/priority-queue/index.ts +3 -0
  328. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  329. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  330. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  331. package/src/types/data-structures/queue/deque.ts +1 -0
  332. package/src/types/data-structures/queue/index.ts +2 -0
  333. package/src/types/data-structures/queue/queue.ts +1 -0
  334. package/src/types/data-structures/stack/index.ts +1 -0
  335. package/src/types/data-structures/stack/stack.ts +1 -0
  336. package/src/types/data-structures/tree/index.ts +1 -0
  337. package/src/types/data-structures/tree/tree.ts +1 -0
  338. package/src/types/data-structures/trie/index.ts +1 -0
  339. package/src/types/data-structures/trie/trie.ts +1 -0
  340. package/src/types/helpers.ts +11 -0
  341. package/src/types/index.ts +3 -0
  342. package/src/types/utils/index.ts +2 -0
  343. package/src/types/utils/utils.ts +6 -0
  344. package/src/types/utils/validate-type.ts +35 -0
  345. package/src/utils/index.ts +1 -0
  346. package/src/utils/utils.ts +86 -0
  347. package/tsconfig.json +1 -2
@@ -0,0 +1,149 @@
1
+ export declare class BinaryIndexedTree {
2
+ protected readonly _freq: number;
3
+ protected readonly _max: number;
4
+ /**
5
+ * The constructor initializes the properties of an object, including default frequency, maximum
6
+ * value, a freqMap data structure, the most significant bit, and the count of negative frequencies.
7
+ * @param - - `frequency`: The default frequency value. It is optional and has a default
8
+ * value of 0.
9
+ */
10
+ constructor({ frequency, max }: {
11
+ frequency?: number;
12
+ max: number;
13
+ });
14
+ protected _freqMap: Record<number, number>;
15
+ get freqMap(): Record<number, number>;
16
+ protected _msb: number;
17
+ get msb(): number;
18
+ protected _negativeCount: number;
19
+ get negativeCount(): number;
20
+ get freq(): number;
21
+ get max(): number;
22
+ /**
23
+ * The function "readSingle" reads a single number from a specified index.
24
+ * @param {number} index - The `index` parameter is a number that represents the index of an element in a
25
+ * collection or array.
26
+ * @returns a number.
27
+ */
28
+ readSingle(index: number): number;
29
+ /**
30
+ * The "update" function updates the value at a given index by adding a delta and triggers a callback
31
+ * to notify of the change.
32
+ * @param {number} position - The `index` parameter represents the index of the element that needs to be
33
+ * updated in the data structure.
34
+ * @param {number} change - The "delta" parameter represents the change in value that needs to be
35
+ * applied to the frequency at the specified index.
36
+ */
37
+ update(position: number, change: number): void;
38
+ /**
39
+ * The function "writeSingle" checks the index and writes a single value with a given frequency.
40
+ * @param {number} index - The `index` parameter is a number that represents the index of an element. It
41
+ * is used to identify the specific element that needs to be written.
42
+ * @param {number} freq - The `freq` parameter represents the frequency value that needs to be
43
+ * written.
44
+ */
45
+ writeSingle(index: number, freq: number): void;
46
+ /**
47
+ * The read function takes a count parameter, checks if it is an integer, and returns the result of
48
+ * calling the _read function with the count parameter clamped between 0 and the maximum value.
49
+ * @param {number} count - The `count` parameter is a number that represents the number of items to
50
+ * read.
51
+ * @returns a number.
52
+ */
53
+ read(count: number): number;
54
+ /**
55
+ * The function returns the lower bound of a non-descending sequence that sums up to a given number.
56
+ * @param {number} sum - The `sum` parameter is a number that represents the target sum that we want
57
+ * to find in the sequence.
58
+ * @returns The lowerBound function is returning a number.
59
+ */
60
+ lowerBound(sum: number): number;
61
+ /**
62
+ * The upperBound function returns the index of the first element in a sequence that is greater than
63
+ * or equal to a given sum.
64
+ * @param {number} sum - The "sum" parameter is a number that represents the target sum that we want
65
+ * to find in the sequence.
66
+ * @returns The upperBound function is returning a number.
67
+ */
68
+ upperBound(sum: number): number;
69
+ /**
70
+ * The function calculates the prefix sum of an array using a binary indexed tree.
71
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
72
+ * array for which we want to calculate the prefix sum.
73
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
74
+ * `i`.
75
+ */
76
+ getPrefixSum(i: number): number;
77
+ /**
78
+ * The function returns the value of a specific index in a freqMap data structure, or a default value if
79
+ * the index is not found.
80
+ * @param {number} index - The `index` parameter is a number that represents the index of a node in a
81
+ * freqMap data structure.
82
+ * @returns a number.
83
+ */
84
+ protected _getFrequency(index: number): number;
85
+ /**
86
+ * The function _updateFrequency adds a delta value to the element at the specified index in the freqMap array.
87
+ * @param {number} index - The index parameter is a number that represents the index of the freqMap
88
+ * element that needs to be updated.
89
+ * @param {number} delta - The `delta` parameter represents the change in value that needs to be
90
+ * added to the freqMap at the specified `index`.
91
+ */
92
+ protected _updateFrequency(index: number, delta: number): void;
93
+ /**
94
+ * The function checks if the given index is valid and within the range.
95
+ * @param {number} index - The parameter "index" is of type number and represents the index value
96
+ * that needs to be checked.
97
+ */
98
+ protected _checkIndex(index: number): void;
99
+ /**
100
+ * The function calculates the sum of elements in an array up to a given index using a binary indexed
101
+ * freqMap.
102
+ * @param {number} index - The `index` parameter is a number that represents the index of an element in a
103
+ * data structure.
104
+ * @returns a number.
105
+ */
106
+ protected _readSingle(index: number): number;
107
+ /**
108
+ * The function `_updateNegativeCount` updates a counter based on changes in frequency values.
109
+ * @param {number} freqCur - The current frequency value.
110
+ * @param {number} freqNew - The freqNew parameter represents the new frequency value.
111
+ */
112
+ protected _updateNegativeCount(freqCur: number, freqNew: number): void;
113
+ /**
114
+ * The `_update` function updates the values in a binary indexed freqMap starting from a given index and
115
+ * propagating the changes to its parent nodes.
116
+ * @param {number} index - The `index` parameter is a number that represents the index of the element in
117
+ * the data structure that needs to be updated.
118
+ * @param {number} delta - The `delta` parameter represents the change in value that needs to be
119
+ * applied to the elements in the data structure.
120
+ */
121
+ protected _update(index: number, delta: number): void;
122
+ /**
123
+ * The `_writeSingle` function updates the frequency at a specific index and triggers a callback if
124
+ * the frequency has changed.
125
+ * @param {number} index - The `index` parameter is a number that represents the index of the element
126
+ * being modified or accessed.
127
+ * @param {number} freq - The `freq` parameter represents the new frequency value that needs to be
128
+ * written to the specified index `index`.
129
+ */
130
+ protected _writeSingle(index: number, freq: number): void;
131
+ /**
132
+ * The `_read` function calculates the sum of values in a binary freqMap up to a given count.
133
+ * @param {number} count - The `count` parameter is a number that represents the number of elements
134
+ * to read from the freqMap.
135
+ * @returns the sum of the values obtained from calling the `_getFrequency` method for each index in the
136
+ * range from `count` to 1.
137
+ */
138
+ protected _read(count: number): number;
139
+ /**
140
+ * The function `_binarySearch` performs a binary search to find the largest number that satisfies a given
141
+ * condition.
142
+ * @param {number} sum - The sum parameter is a number that represents the target sum value.
143
+ * @param before - The `before` parameter is a function that takes two numbers `x` and `y` as
144
+ * arguments and returns a boolean value. It is used to determine if `x` is less than or equal to
145
+ * `y`. The purpose of this function is to compare two numbers and determine their order.
146
+ * @returns the value of the variable "left".
147
+ */
148
+ protected _binarySearch(sum: number, before: (x: number, y: number) => boolean): number;
149
+ }
@@ -0,0 +1,269 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BinaryIndexedTree = void 0;
4
+ /**
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
10
+ */
11
+ const utils_1 = require("../../utils");
12
+ class BinaryIndexedTree {
13
+ /**
14
+ * The constructor initializes the properties of an object, including default frequency, maximum
15
+ * value, a freqMap data structure, the most significant bit, and the count of negative frequencies.
16
+ * @param - - `frequency`: The default frequency value. It is optional and has a default
17
+ * value of 0.
18
+ */
19
+ constructor({ frequency = 0, max }) {
20
+ this._freq = frequency;
21
+ this._max = max;
22
+ this._freqMap = { 0: 0 };
23
+ this._msb = (0, utils_1.getMSB)(max);
24
+ this._negativeCount = frequency < 0 ? max : 0;
25
+ }
26
+ get freqMap() {
27
+ return this._freqMap;
28
+ }
29
+ get msb() {
30
+ return this._msb;
31
+ }
32
+ get negativeCount() {
33
+ return this._negativeCount;
34
+ }
35
+ get freq() {
36
+ return this._freq;
37
+ }
38
+ get max() {
39
+ return this._max;
40
+ }
41
+ /**
42
+ * The function "readSingle" reads a single number from a specified index.
43
+ * @param {number} index - The `index` parameter is a number that represents the index of an element in a
44
+ * collection or array.
45
+ * @returns a number.
46
+ */
47
+ readSingle(index) {
48
+ this._checkIndex(index);
49
+ return this._readSingle(index);
50
+ }
51
+ /**
52
+ * The "update" function updates the value at a given index by adding a delta and triggers a callback
53
+ * to notify of the change.
54
+ * @param {number} position - The `index` parameter represents the index of the element that needs to be
55
+ * updated in the data structure.
56
+ * @param {number} change - The "delta" parameter represents the change in value that needs to be
57
+ * applied to the frequency at the specified index.
58
+ */
59
+ update(position, change) {
60
+ this._checkIndex(position);
61
+ const freqCur = this._readSingle(position);
62
+ this._update(position, change);
63
+ this._updateNegativeCount(freqCur, freqCur + change);
64
+ }
65
+ /**
66
+ * The function "writeSingle" checks the index and writes a single value with a given frequency.
67
+ * @param {number} index - The `index` parameter is a number that represents the index of an element. It
68
+ * is used to identify the specific element that needs to be written.
69
+ * @param {number} freq - The `freq` parameter represents the frequency value that needs to be
70
+ * written.
71
+ */
72
+ writeSingle(index, freq) {
73
+ this._checkIndex(index);
74
+ this._writeSingle(index, freq);
75
+ }
76
+ /**
77
+ * The read function takes a count parameter, checks if it is an integer, and returns the result of
78
+ * calling the _read function with the count parameter clamped between 0 and the maximum value.
79
+ * @param {number} count - The `count` parameter is a number that represents the number of items to
80
+ * read.
81
+ * @returns a number.
82
+ */
83
+ read(count) {
84
+ if (!Number.isInteger(count)) {
85
+ throw new Error('Invalid count');
86
+ }
87
+ return this._read(Math.max(Math.min(count, this.max), 0));
88
+ }
89
+ /**
90
+ * The function returns the lower bound of a non-descending sequence that sums up to a given number.
91
+ * @param {number} sum - The `sum` parameter is a number that represents the target sum that we want
92
+ * to find in the sequence.
93
+ * @returns The lowerBound function is returning a number.
94
+ */
95
+ lowerBound(sum) {
96
+ if (this.negativeCount > 0) {
97
+ throw new Error('Sequence is not non-descending');
98
+ }
99
+ return this._binarySearch(sum, (x, y) => x < y);
100
+ }
101
+ /**
102
+ * The upperBound function returns the index of the first element in a sequence that is greater than
103
+ * or equal to a given sum.
104
+ * @param {number} sum - The "sum" parameter is a number that represents the target sum that we want
105
+ * to find in the sequence.
106
+ * @returns The upperBound function is returning a number.
107
+ */
108
+ upperBound(sum) {
109
+ if (this.negativeCount > 0) {
110
+ throw new Error('Must not be descending');
111
+ }
112
+ return this._binarySearch(sum, (x, y) => x <= y);
113
+ }
114
+ /**
115
+ * The function calculates the prefix sum of an array using a binary indexed tree.
116
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
117
+ * array for which we want to calculate the prefix sum.
118
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
119
+ * `i`.
120
+ */
121
+ getPrefixSum(i) {
122
+ this._checkIndex(i);
123
+ i++; // Convert to 1-based index
124
+ let sum = 0;
125
+ while (i > 0) {
126
+ sum += this._getFrequency(i);
127
+ i -= i & -i;
128
+ }
129
+ return sum;
130
+ }
131
+ /**
132
+ * The function returns the value of a specific index in a freqMap data structure, or a default value if
133
+ * the index is not found.
134
+ * @param {number} index - The `index` parameter is a number that represents the index of a node in a
135
+ * freqMap data structure.
136
+ * @returns a number.
137
+ */
138
+ _getFrequency(index) {
139
+ if (index in this.freqMap) {
140
+ return this.freqMap[index];
141
+ }
142
+ return this.freq * (index & -index);
143
+ }
144
+ /**
145
+ * The function _updateFrequency adds a delta value to the element at the specified index in the freqMap array.
146
+ * @param {number} index - The index parameter is a number that represents the index of the freqMap
147
+ * element that needs to be updated.
148
+ * @param {number} delta - The `delta` parameter represents the change in value that needs to be
149
+ * added to the freqMap at the specified `index`.
150
+ */
151
+ _updateFrequency(index, delta) {
152
+ this.freqMap[index] = this._getFrequency(index) + delta;
153
+ }
154
+ /**
155
+ * The function checks if the given index is valid and within the range.
156
+ * @param {number} index - The parameter "index" is of type number and represents the index value
157
+ * that needs to be checked.
158
+ */
159
+ _checkIndex(index) {
160
+ if (!Number.isInteger(index)) {
161
+ throw new Error('Invalid index: Index must be an integer.');
162
+ }
163
+ if (index < 0 || index >= this.max) {
164
+ throw new Error('Index out of range: Index must be within the range [0, this.max).');
165
+ }
166
+ }
167
+ /**
168
+ * The function calculates the sum of elements in an array up to a given index using a binary indexed
169
+ * freqMap.
170
+ * @param {number} index - The `index` parameter is a number that represents the index of an element in a
171
+ * data structure.
172
+ * @returns a number.
173
+ */
174
+ _readSingle(index) {
175
+ index = index + 1;
176
+ let sum = this._getFrequency(index);
177
+ const z = index - (index & -index);
178
+ index--;
179
+ while (index !== z) {
180
+ sum -= this._getFrequency(index);
181
+ index -= index & -index;
182
+ }
183
+ return sum;
184
+ }
185
+ /**
186
+ * The function `_updateNegativeCount` updates a counter based on changes in frequency values.
187
+ * @param {number} freqCur - The current frequency value.
188
+ * @param {number} freqNew - The freqNew parameter represents the new frequency value.
189
+ */
190
+ _updateNegativeCount(freqCur, freqNew) {
191
+ if (freqCur < 0 && freqNew >= 0) {
192
+ this._negativeCount--;
193
+ }
194
+ else if (freqCur >= 0 && freqNew < 0) {
195
+ this._negativeCount++;
196
+ }
197
+ }
198
+ /**
199
+ * The `_update` function updates the values in a binary indexed freqMap starting from a given index and
200
+ * propagating the changes to its parent nodes.
201
+ * @param {number} index - The `index` parameter is a number that represents the index of the element in
202
+ * the data structure that needs to be updated.
203
+ * @param {number} delta - The `delta` parameter represents the change in value that needs to be
204
+ * applied to the elements in the data structure.
205
+ */
206
+ _update(index, delta) {
207
+ index = index + 1;
208
+ while (index <= this.max) {
209
+ this._updateFrequency(index, delta);
210
+ index += index & -index;
211
+ }
212
+ }
213
+ /**
214
+ * The `_writeSingle` function updates the frequency at a specific index and triggers a callback if
215
+ * the frequency has changed.
216
+ * @param {number} index - The `index` parameter is a number that represents the index of the element
217
+ * being modified or accessed.
218
+ * @param {number} freq - The `freq` parameter represents the new frequency value that needs to be
219
+ * written to the specified index `index`.
220
+ */
221
+ _writeSingle(index, freq) {
222
+ const freqCur = this._readSingle(index);
223
+ this._update(index, freq - freqCur);
224
+ this._updateNegativeCount(freqCur, freq);
225
+ }
226
+ /**
227
+ * The `_read` function calculates the sum of values in a binary freqMap up to a given count.
228
+ * @param {number} count - The `count` parameter is a number that represents the number of elements
229
+ * to read from the freqMap.
230
+ * @returns the sum of the values obtained from calling the `_getFrequency` method for each index in the
231
+ * range from `count` to 1.
232
+ */
233
+ _read(count) {
234
+ let index = count;
235
+ let sum = 0;
236
+ while (index) {
237
+ sum += this._getFrequency(index);
238
+ index -= index & -index;
239
+ }
240
+ return sum;
241
+ }
242
+ /**
243
+ * The function `_binarySearch` performs a binary search to find the largest number that satisfies a given
244
+ * condition.
245
+ * @param {number} sum - The sum parameter is a number that represents the target sum value.
246
+ * @param before - The `before` parameter is a function that takes two numbers `x` and `y` as
247
+ * arguments and returns a boolean value. It is used to determine if `x` is less than or equal to
248
+ * `y`. The purpose of this function is to compare two numbers and determine their order.
249
+ * @returns the value of the variable "left".
250
+ */
251
+ _binarySearch(sum, before) {
252
+ let left = 0;
253
+ let right = this.msb << 1;
254
+ let sumT = sum;
255
+ while (right > left + 1) {
256
+ const middle = (left + right) >> 1;
257
+ const sumM = this._getFrequency(middle);
258
+ if (middle <= this.max && before(sumM, sumT)) {
259
+ sumT -= sumM;
260
+ left = middle;
261
+ }
262
+ else {
263
+ right = middle;
264
+ }
265
+ }
266
+ return left;
267
+ }
268
+ }
269
+ exports.BinaryIndexedTree = BinaryIndexedTree;