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,576 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ export class SinglyLinkedListNode<E = any> {
9
+ value: E;
10
+ next: SinglyLinkedListNode<E> | null;
11
+
12
+ /**
13
+ * The constructor function initializes an instance of a class with a given value and sets the next property to null.
14
+ * @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
15
+ * will be stored in the node of a linked list.
16
+ */
17
+ constructor(value: E) {
18
+ this.value = value;
19
+ this.next = null;
20
+ }
21
+ }
22
+
23
+ export class SinglyLinkedList<E = any> {
24
+ /**
25
+ * The constructor initializes the linked list with an empty head, tail, and length.
26
+ */
27
+ constructor() {
28
+ this._head = null;
29
+ this._tail = null;
30
+ this._length = 0;
31
+ }
32
+
33
+ protected _head: SinglyLinkedListNode<E> | null;
34
+
35
+ get head(): SinglyLinkedListNode<E> | null {
36
+ return this._head;
37
+ }
38
+
39
+ protected _tail: SinglyLinkedListNode<E> | null;
40
+
41
+ get tail(): SinglyLinkedListNode<E> | null {
42
+ return this._tail;
43
+ }
44
+
45
+ protected _length: number;
46
+
47
+ get length(): number {
48
+ return this._length;
49
+ }
50
+
51
+ /**
52
+ * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
53
+ * array.
54
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
55
+ * @returns The `fromArray` function returns a `SinglyLinkedList` object.
56
+ */
57
+ static fromArray<E>(data: E[]) {
58
+ const singlyLinkedList = new SinglyLinkedList<E>();
59
+ for (const item of data) {
60
+ singlyLinkedList.push(item);
61
+ }
62
+ return singlyLinkedList;
63
+ }
64
+
65
+ /**
66
+ * The `push` function adds a new node with the given value to the end of a singly linked list.
67
+ * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
68
+ * any type (E) as specified in the generic type declaration of the class or function.
69
+ */
70
+ push(value: E): void {
71
+ const newNode = new SinglyLinkedListNode(value);
72
+ if (!this.head) {
73
+ this._head = newNode;
74
+ this._tail = newNode;
75
+ } else {
76
+ this.tail!.next = newNode;
77
+ this._tail = newNode;
78
+ }
79
+ this._length++;
80
+ }
81
+
82
+ /**
83
+ * The `push` function adds a new node with the given value to the end of a singly linked list.
84
+ * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
85
+ * any type (E) as specified in the generic type declaration of the class or function.
86
+ */
87
+ addLast(value: E): void {
88
+ this.push(value);
89
+ }
90
+
91
+ /**
92
+ * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
93
+ * pointers accordingly.
94
+ * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
95
+ * the linked list is empty, it returns `null`.
96
+ */
97
+ pop(): E | undefined {
98
+ if (!this.head) return undefined;
99
+ if (this.head === this.tail) {
100
+ const value = this.head.value;
101
+ this._head = null;
102
+ this._tail = null;
103
+ this._length--;
104
+ return value;
105
+ }
106
+
107
+ let current = this.head;
108
+ while (current.next !== this.tail) {
109
+ current = current.next!;
110
+ }
111
+ const value = this.tail!.value;
112
+ current.next = null;
113
+ this._tail = current;
114
+ this._length--;
115
+ return value;
116
+ }
117
+
118
+ /**
119
+ * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
120
+ * pointers accordingly.
121
+ * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
122
+ * the linked list is empty, it returns `null`.
123
+ */
124
+ popLast(): E | undefined {
125
+ return this.pop();
126
+ }
127
+
128
+ /**
129
+ * The `shift()` function removes and returns the value of the first node in a linked list.
130
+ * @returns The value of the node that is being removed from the beginning of the linked list.
131
+ */
132
+ shift(): E | undefined {
133
+ if (!this.head) return undefined;
134
+ const removedNode = this.head;
135
+ this._head = this.head.next;
136
+ this._length--;
137
+ return removedNode.value;
138
+ }
139
+
140
+ /**
141
+ * The `popFirst()` function removes and returns the value of the first node in a linked list.
142
+ * @returns The value of the node that is being removed from the beginning of the linked list.
143
+ */
144
+ popFirst(): E | undefined {
145
+ return this.shift();
146
+ }
147
+
148
+ /**
149
+ * The unshift function adds a new node with the given value to the beginning of a singly linked list.
150
+ * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
151
+ * linked list.
152
+ */
153
+ unshift(value: E): void {
154
+ const newNode = new SinglyLinkedListNode(value);
155
+ if (!this.head) {
156
+ this._head = newNode;
157
+ this._tail = newNode;
158
+ } else {
159
+ newNode.next = this.head;
160
+ this._head = newNode;
161
+ }
162
+ this._length++;
163
+ }
164
+
165
+ /**
166
+ * The addFirst function adds a new node with the given value to the beginning of a singly linked list.
167
+ * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
168
+ * linked list.
169
+ */
170
+ addFirst(value: E): void {
171
+ this.unshift(value);
172
+ }
173
+
174
+ /**
175
+ * The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
176
+ * @param {number} index - The index parameter is a number that represents the position of the element we want to
177
+ * retrieve from the list.
178
+ * @returns The method `getAt(index: number): E | null` returns the value at the specified index in the linked list, or
179
+ * `null` if the index is out of bounds.
180
+ */
181
+ getAt(index: number): E | undefined {
182
+ if (index < 0 || index >= this.length) return undefined;
183
+ let current = this.head;
184
+ for (let i = 0; i < index; i++) {
185
+ current = current!.next;
186
+ }
187
+ return current!.value;
188
+ }
189
+
190
+ /**
191
+ * The function `getNodeAt` returns the node at a given index in a singly linked list.
192
+ * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
193
+ * retrieve from the linked list. It indicates the zero-based index of the node we want to access.
194
+ * @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<E>` object if the node at the
195
+ * specified index exists, or `null` if the index is out of bounds.
196
+ */
197
+ getNodeAt(index: number): SinglyLinkedListNode<E> | null {
198
+ let current = this.head;
199
+ for (let i = 0; i < index; i++) {
200
+ current = current!.next;
201
+ }
202
+ return current;
203
+ }
204
+
205
+ /**
206
+ * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
207
+ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
208
+ * data structure. It is of type number.
209
+ * @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
210
+ * bounds.
211
+ */
212
+ deleteAt(index: number): E | undefined {
213
+ if (index < 0 || index >= this.length) return undefined;
214
+ if (index === 0) return this.shift();
215
+ if (index === this.length - 1) return this.pop();
216
+
217
+ const prevNode = this.getNodeAt(index - 1);
218
+ const removedNode = prevNode!.next;
219
+ prevNode!.next = removedNode!.next;
220
+ this._length--;
221
+ return removedNode!.value;
222
+ }
223
+
224
+ /**
225
+ * The delete function removes a node with a specific value from a singly linked list.
226
+ * @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
227
+ * or a `SinglyLinkedListNode<E>` object.
228
+ * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
229
+ * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
230
+ */
231
+ delete(valueOrNode: E | SinglyLinkedListNode<E> | null | undefined): boolean {
232
+ if (!valueOrNode) return false;
233
+ let value: E;
234
+ if (valueOrNode instanceof SinglyLinkedListNode) {
235
+ value = valueOrNode.value;
236
+ } else {
237
+ value = valueOrNode;
238
+ }
239
+ let current = this.head,
240
+ prev = null;
241
+
242
+ while (current) {
243
+ if (current.value === value) {
244
+ if (prev === null) {
245
+ this._head = current.next;
246
+ if (current === this.tail) {
247
+ this._tail = null;
248
+ }
249
+ } else {
250
+ prev.next = current.next;
251
+ if (current === this.tail) {
252
+ this._tail = prev;
253
+ }
254
+ }
255
+ this._length--;
256
+ return true;
257
+ }
258
+ prev = current;
259
+ current = current.next;
260
+ }
261
+
262
+ return false;
263
+ }
264
+
265
+ /**
266
+ * The `insertAt` function inserts a value at a specified index in a singly linked list.
267
+ * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
268
+ * linked list. It is of type number.
269
+ * @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
270
+ * specified index.
271
+ * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
272
+ * if the index is out of bounds.
273
+ */
274
+ insertAt(index: number, value: E): boolean {
275
+ if (index < 0 || index > this.length) return false;
276
+ if (index === 0) {
277
+ this.unshift(value);
278
+ return true;
279
+ }
280
+ if (index === this.length) {
281
+ this.push(value);
282
+ return true;
283
+ }
284
+
285
+ const newNode = new SinglyLinkedListNode(value);
286
+ const prevNode = this.getNodeAt(index - 1);
287
+ newNode.next = prevNode!.next;
288
+ prevNode!.next = newNode;
289
+ this._length++;
290
+ return true;
291
+ }
292
+
293
+ /**
294
+ * The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
295
+ * whether it is empty or not.
296
+ * @returns A boolean value indicating whether the length of the object is equal to 0.
297
+ */
298
+ isEmpty(): boolean {
299
+ return this.length === 0;
300
+ }
301
+
302
+ /**
303
+ * The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
304
+ */
305
+ clear(): void {
306
+ this._head = null;
307
+ this._tail = null;
308
+ this._length = 0;
309
+ }
310
+
311
+ /**
312
+ * The `toArray` function converts a linked list into an array.
313
+ * @returns The `toArray()` method is returning an array of type `E[]`.
314
+ */
315
+ toArray(): E[] {
316
+ const array: E[] = [];
317
+ let current = this.head;
318
+ while (current) {
319
+ array.push(current.value);
320
+ current = current.next;
321
+ }
322
+ return array;
323
+ }
324
+
325
+ /**
326
+ * The `reverse` function reverses the order of the nodes in a singly linked list.
327
+ * @returns The reverse() method does not return anything. It has a return type of void.
328
+ */
329
+ reverse(): void {
330
+ if (!this.head || this.head === this.tail) return;
331
+
332
+ let prev: SinglyLinkedListNode<E> | null = null;
333
+ let current: SinglyLinkedListNode<E> | null = this.head;
334
+ let next: SinglyLinkedListNode<E> | null = null;
335
+
336
+ while (current) {
337
+ next = current.next;
338
+ current.next = prev;
339
+ prev = current;
340
+ current = next;
341
+ }
342
+
343
+ [this._head, this._tail] = [this.tail!, this.head!];
344
+ }
345
+
346
+ /**
347
+ * The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
348
+ * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
349
+ * function is used to determine whether a particular value in the linked list satisfies a certain condition.
350
+ * @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
351
+ * the callback function. If no element satisfies the condition, it returns `null`.
352
+ */
353
+ find(callback: (value: E) => boolean): E | null {
354
+ let current = this.head;
355
+ while (current) {
356
+ if (callback(current.value)) {
357
+ return current.value;
358
+ }
359
+ current = current.next;
360
+ }
361
+ return null;
362
+ }
363
+
364
+ /**
365
+ * The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
366
+ * @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
367
+ * @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
368
+ * value is not found, it returns -1.
369
+ */
370
+ indexOf(value: E): number {
371
+ let index = 0;
372
+ let current = this.head;
373
+
374
+ while (current) {
375
+ if (current.value === value) {
376
+ return index;
377
+ }
378
+ index++;
379
+ current = current.next;
380
+ }
381
+
382
+ return -1;
383
+ }
384
+
385
+ /**
386
+ * The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
387
+ * null.
388
+ * @param {E} value - The value parameter is the value that we want to search for in the linked list.
389
+ * @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
390
+ * the specified value is found, the function returns `null`.
391
+ */
392
+ getNode(value: E): SinglyLinkedListNode<E> | null {
393
+ let current = this.head;
394
+
395
+ while (current) {
396
+ if (current.value === value) {
397
+ return current;
398
+ }
399
+ current = current.next;
400
+ }
401
+
402
+ return null;
403
+ }
404
+
405
+ /**
406
+ * The `insertBefore` function inserts a new value before an existing value in a singly linked list.
407
+ * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
408
+ * new value before. It can be either the value itself or a node containing the value in the linked list.
409
+ * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
410
+ * @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
411
+ * inserted before the existing value, and `false` otherwise.
412
+ */
413
+ insertBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean {
414
+ if (!this.head) return false;
415
+
416
+ let existingValue: E;
417
+ if (existingValueOrNode instanceof SinglyLinkedListNode) {
418
+ existingValue = existingValueOrNode.value;
419
+ } else {
420
+ existingValue = existingValueOrNode;
421
+ }
422
+ if (this.head.value === existingValue) {
423
+ this.unshift(newValue);
424
+ return true;
425
+ }
426
+
427
+ let current = this.head;
428
+ while (current.next) {
429
+ if (current.next.value === existingValue) {
430
+ const newNode = new SinglyLinkedListNode(newValue);
431
+ newNode.next = current.next;
432
+ current.next = newNode;
433
+ this._length++;
434
+ return true;
435
+ }
436
+ current = current.next;
437
+ }
438
+
439
+ return false;
440
+ }
441
+
442
+ /**
443
+ * The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
444
+ * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
445
+ * the new value will be inserted. It can be either the value of the existing node or the existing node itself.
446
+ * @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
447
+ * @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
448
+ * existing value or node, and false if the existing value or node was not found in the linked list.
449
+ */
450
+ insertAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean {
451
+ let existingNode: E | SinglyLinkedListNode<E> | null;
452
+
453
+ if (existingValueOrNode instanceof SinglyLinkedListNode) {
454
+ existingNode = existingValueOrNode;
455
+ } else {
456
+ existingNode = this.getNode(existingValueOrNode);
457
+ }
458
+
459
+ if (existingNode) {
460
+ const newNode = new SinglyLinkedListNode(newValue);
461
+ newNode.next = existingNode.next;
462
+ existingNode.next = newNode;
463
+ if (existingNode === this.tail) {
464
+ this._tail = newNode;
465
+ }
466
+ this._length++;
467
+ return true;
468
+ }
469
+
470
+ return false;
471
+ }
472
+
473
+ /**
474
+ * The function counts the number of occurrences of a given value in a linked list.
475
+ * @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
476
+ * @returns The count of occurrences of the given value in the linked list.
477
+ */
478
+ countOccurrences(value: E): number {
479
+ let count = 0;
480
+ let current = this.head;
481
+
482
+ while (current) {
483
+ if (current.value === value) {
484
+ count++;
485
+ }
486
+ current = current.next;
487
+ }
488
+
489
+ return count;
490
+ }
491
+
492
+ /**
493
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
494
+ * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
495
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
496
+ * current node in the linked list.
497
+ */
498
+ forEach(callback: (value: E, index: number) => void): void {
499
+ let current = this.head;
500
+ let index = 0;
501
+ while (current) {
502
+ callback(current.value, index);
503
+ current = current.next;
504
+ index++;
505
+ }
506
+ }
507
+
508
+ /**
509
+ * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
510
+ * SinglyLinkedList with the transformed values.
511
+ * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
512
+ * the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
513
+ * SinglyLinkedList).
514
+ * @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
515
+ */
516
+ map<U>(callback: (value: E) => U): SinglyLinkedList<U> {
517
+ const mappedList = new SinglyLinkedList<U>();
518
+ let current = this.head;
519
+ while (current) {
520
+ mappedList.push(callback(current.value));
521
+ current = current.next;
522
+ }
523
+ return mappedList;
524
+ }
525
+
526
+ /**
527
+ * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
528
+ * elements that satisfy the given callback function.
529
+ * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
530
+ * It is used to determine whether a value should be included in the filtered list or not.
531
+ * @returns The filtered list, which is an instance of the SinglyLinkedList class.
532
+ */
533
+ filter(callback: (value: E) => boolean): SinglyLinkedList<E> {
534
+ const filteredList = new SinglyLinkedList<E>();
535
+ let current = this.head;
536
+ while (current) {
537
+ if (callback(current.value)) {
538
+ filteredList.push(current.value);
539
+ }
540
+ current = current.next;
541
+ }
542
+ return filteredList;
543
+ }
544
+
545
+ /**
546
+ * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
547
+ * single value.
548
+ * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
549
+ * used to perform a specific operation on each element of the linked list.
550
+ * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
551
+ * point for the reduction operation.
552
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
553
+ * elements in the linked list.
554
+ */
555
+ reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U {
556
+ let accumulator = initialValue;
557
+ let current = this.head;
558
+ while (current) {
559
+ accumulator = callback(accumulator, current.value);
560
+ current = current.next;
561
+ }
562
+ return accumulator;
563
+ }
564
+
565
+ /**
566
+ * The function returns an iterator that iterates over the values of a linked list.
567
+ */
568
+ * [Symbol.iterator]() {
569
+ let current = this.head;
570
+
571
+ while (current) {
572
+ yield current.value;
573
+ current = current.next;
574
+ }
575
+ }
576
+ }