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,194 @@
1
+ import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
2
+ import type { VertexKey } from '../../types';
3
+ import { IGraph } from '../../interfaces';
4
+ export declare class DirectedVertex<V = any> extends AbstractVertex<V> {
5
+ /**
6
+ * The constructor function initializes a vertex with an optional value.
7
+ * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
8
+ * used to uniquely identify the vertex within a graph or data structure.
9
+ * @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
10
+ * vertex. If no value is provided, the vertex will be initialized with a default value.
11
+ */
12
+ constructor(key: VertexKey, value?: V);
13
+ }
14
+ export declare class DirectedEdge<E = any> extends AbstractEdge<E> {
15
+ src: VertexKey;
16
+ dest: VertexKey;
17
+ /**
18
+ * The constructor function initializes the source and destination vertices of an edge, along with an optional weight
19
+ * and value.
20
+ * @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
21
+ * a graph.
22
+ * @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
23
+ * `VertexKey`, which is likely a unique identifier for a vertex in a graph.
24
+ * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
25
+ * @param {E} [value] - The `value` parameter is an optional parameter of type `E`. It represents the value associated with
26
+ * the edge.
27
+ */
28
+ constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E);
29
+ }
30
+ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V> = DirectedVertex<V>, EO extends DirectedEdge<E> = DirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
31
+ /**
32
+ * The constructor function initializes an instance of a class.
33
+ */
34
+ constructor();
35
+ protected _outEdgeMap: Map<VO, EO[]>;
36
+ get outEdgeMap(): Map<VO, EO[]>;
37
+ protected _inEdgeMap: Map<VO, EO[]>;
38
+ get inEdgeMap(): Map<VO, EO[]>;
39
+ /**
40
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
41
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
42
+ */
43
+ /**
44
+ * The function creates a new vertex with an optional value and returns it.
45
+ * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
46
+ * could be a number or a string depending on how you want to identify your vertices.
47
+ * @param [value] - The 'value' parameter is an optional value that can be assigned to the vertex. If a value is provided,
48
+ * it will be assigned to the 'value' property of the vertex. If no value is provided, the 'value' property will be
49
+ * assigned the same value as the 'key' parameter
50
+ * @returns a new instance of a DirectedVertex object, casted as type VO.
51
+ */
52
+ createVertex(key: VertexKey, value?: V): VO;
53
+ /**
54
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
55
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
56
+ */
57
+ /**
58
+ * The function creates a directed edge between two vertices with an optional weight and value.
59
+ * @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
60
+ * @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
61
+ * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
62
+ * weight is provided, it defaults to 1.
63
+ * @param [value] - The 'value' parameter is an optional value that can be assigned to the edge. It can be of any type and
64
+ * is used to store additional information or data associated with the edge.
65
+ * @returns a new instance of a DirectedEdge object, casted as type EO.
66
+ */
67
+ createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO;
68
+ /**
69
+ * The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
70
+ * @param {VO | VertexKey | null} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
71
+ * @param {VO | VertexKey | null} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
72
+ * destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `null` if the
73
+ * destination is not specified.
74
+ * @returns the first edge found between the source and destination vertices, or null if no such edge is found.
75
+ */
76
+ getEdge(srcOrKey: VO | VertexKey | null, destOrKey: VO | VertexKey | null): EO | null;
77
+ /**
78
+ * The function removes an edge between two vertices in a graph and returns the removed edge.
79
+ * @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
80
+ * @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
81
+ * @returns the removed edge (EO) if it exists, or null if either the source or destination vertex does not exist.
82
+ */
83
+ deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | null;
84
+ /**
85
+ * The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
86
+ * @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
87
+ * and `dest`, which represent the source and destination vertices of the edge, respectively.
88
+ * @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `null` if the edge does not exist.
89
+ */
90
+ deleteEdge(edge: EO): EO | null;
91
+ /**
92
+ * The function removes edges between two vertices and returns the removed edges.
93
+ * @param {VertexKey | VO} v1 - The parameter `v1` can be either a `VertexKey` or a `VO`. A `VertexKey` represents the
94
+ * unique identifier of a vertex in a graph, while `VO` represents the actual vertex object.
95
+ * @param {VertexKey | VO} v2 - The parameter `v2` represents either a `VertexKey` or a `VO` object. It is used to specify
96
+ * the second vertex in the edge that needs to be removed.
97
+ * @returns an array of removed edges (EO[]).
98
+ */
99
+ deleteEdgesBetween(v1: VertexKey | VO, v2: VertexKey | VO): EO[];
100
+ /**
101
+ * The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
102
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
103
+ * (`VertexKey`).
104
+ * @returns The method `incomingEdgesOf` returns an array of edges (`EO[]`).
105
+ */
106
+ incomingEdgesOf(vertexOrKey: VO | VertexKey): EO[];
107
+ /**
108
+ * The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
109
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`VO`) or a vertex ID
110
+ * (`VertexKey`).
111
+ * @returns The method `outgoingEdgesOf` returns an array of edges (`EO[]`).
112
+ */
113
+ outgoingEdgesOf(vertexOrKey: VO | VertexKey): EO[];
114
+ /**
115
+ * The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
116
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
117
+ * @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
118
+ */
119
+ degreeOf(vertexOrKey: VertexKey | VO): number;
120
+ /**
121
+ * The function "inDegreeOf" returns the number of incoming edges for a given vertex.
122
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
123
+ * @returns The number of incoming edges of the specified vertex or vertex ID.
124
+ */
125
+ inDegreeOf(vertexOrKey: VertexKey | VO): number;
126
+ /**
127
+ * The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
128
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
129
+ * @returns The number of outgoing edges from the specified vertex or vertex ID.
130
+ */
131
+ outDegreeOf(vertexOrKey: VertexKey | VO): number;
132
+ /**
133
+ * The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
134
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
135
+ * @returns The function `edgesOf` returns an array of edges.
136
+ */
137
+ edgesOf(vertexOrKey: VertexKey | VO): EO[];
138
+ /**
139
+ * The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
140
+ * @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
141
+ * @returns either a vertex object (VO) or null.
142
+ */
143
+ getEdgeSrc(e: EO): VO | null;
144
+ /**
145
+ * The function "getEdgeDest" returns the destination vertex of an edge.
146
+ * @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
147
+ * @returns either a vertex object of type VO or null.
148
+ */
149
+ getEdgeDest(e: EO): VO | null;
150
+ /**
151
+ * The function `getDestinations` returns an array of destination vertices connected to a given vertex.
152
+ * @param {VO | VertexKey | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
153
+ * find the destinations. It can be either a `VO` object, a `VertexKey` value, or `null`.
154
+ * @returns an array of vertices (VO[]).
155
+ */
156
+ getDestinations(vertex: VO | VertexKey | null): VO[];
157
+ /**
158
+ * The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
159
+ * in the sorted order, or null if the graph contains a cycle.
160
+ * @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
161
+ * property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
162
+ * specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
163
+ * @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
164
+ */
165
+ topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | null;
166
+ /**
167
+ * The `edgeSet` function returns an array of all the edges in the graph.
168
+ * @returns The `edgeSet()` method returns an array of edges (`EO[]`).
169
+ */
170
+ edgeSet(): EO[];
171
+ /**
172
+ * The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
173
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
174
+ * (`VertexKey`).
175
+ * @returns an array of vertices (VO[]).
176
+ */
177
+ getNeighbors(vertexOrKey: VO | VertexKey): VO[];
178
+ /**
179
+ * The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
180
+ * otherwise it returns null.
181
+ * @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
182
+ * @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
183
+ * graph. If the edge does not exist, it returns `null`.
184
+ */
185
+ getEndsOfEdge(edge: EO): [VO, VO] | null;
186
+ /**
187
+ * The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertices exist.
188
+ * @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
189
+ * needs to be added to the graph.
190
+ * @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
191
+ * source or destination vertex does not exist in the graph.
192
+ */
193
+ protected _addEdgeOnly(edge: EO): boolean;
194
+ }
@@ -0,0 +1,404 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = 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
+ const abstract_graph_1 = require("./abstract-graph");
13
+ class DirectedVertex extends abstract_graph_1.AbstractVertex {
14
+ /**
15
+ * The constructor function initializes a vertex with an optional value.
16
+ * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
17
+ * used to uniquely identify the vertex within a graph or data structure.
18
+ * @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
19
+ * vertex. If no value is provided, the vertex will be initialized with a default value.
20
+ */
21
+ constructor(key, value) {
22
+ super(key, value);
23
+ }
24
+ }
25
+ exports.DirectedVertex = DirectedVertex;
26
+ class DirectedEdge extends abstract_graph_1.AbstractEdge {
27
+ /**
28
+ * The constructor function initializes the source and destination vertices of an edge, along with an optional weight
29
+ * and value.
30
+ * @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
31
+ * a graph.
32
+ * @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
33
+ * `VertexKey`, which is likely a unique identifier for a vertex in a graph.
34
+ * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
35
+ * @param {E} [value] - The `value` parameter is an optional parameter of type `E`. It represents the value associated with
36
+ * the edge.
37
+ */
38
+ constructor(src, dest, weight, value) {
39
+ super(weight, value);
40
+ this.src = src;
41
+ this.dest = dest;
42
+ }
43
+ }
44
+ exports.DirectedEdge = DirectedEdge;
45
+ class DirectedGraph extends abstract_graph_1.AbstractGraph {
46
+ /**
47
+ * The constructor function initializes an instance of a class.
48
+ */
49
+ constructor() {
50
+ super();
51
+ this._outEdgeMap = new Map();
52
+ this._inEdgeMap = new Map();
53
+ }
54
+ get outEdgeMap() {
55
+ return this._outEdgeMap;
56
+ }
57
+ get inEdgeMap() {
58
+ return this._inEdgeMap;
59
+ }
60
+ /**
61
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
62
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
63
+ */
64
+ /**
65
+ * The function creates a new vertex with an optional value and returns it.
66
+ * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
67
+ * could be a number or a string depending on how you want to identify your vertices.
68
+ * @param [value] - The 'value' parameter is an optional value that can be assigned to the vertex. If a value is provided,
69
+ * it will be assigned to the 'value' property of the vertex. If no value is provided, the 'value' property will be
70
+ * assigned the same value as the 'key' parameter
71
+ * @returns a new instance of a DirectedVertex object, casted as type VO.
72
+ */
73
+ createVertex(key, value) {
74
+ return new DirectedVertex(key, value !== null && value !== void 0 ? value : key);
75
+ }
76
+ /**
77
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
78
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
79
+ */
80
+ /**
81
+ * The function creates a directed edge between two vertices with an optional weight and value.
82
+ * @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
83
+ * @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
84
+ * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
85
+ * weight is provided, it defaults to 1.
86
+ * @param [value] - The 'value' parameter is an optional value that can be assigned to the edge. It can be of any type and
87
+ * is used to store additional information or data associated with the edge.
88
+ * @returns a new instance of a DirectedEdge object, casted as type EO.
89
+ */
90
+ createEdge(src, dest, weight, value) {
91
+ return new DirectedEdge(src, dest, weight !== null && weight !== void 0 ? weight : 1, value);
92
+ }
93
+ /**
94
+ * The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
95
+ * @param {VO | VertexKey | null} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
96
+ * @param {VO | VertexKey | null} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
97
+ * destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `null` if the
98
+ * destination is not specified.
99
+ * @returns the first edge found between the source and destination vertices, or null if no such edge is found.
100
+ */
101
+ getEdge(srcOrKey, destOrKey) {
102
+ let edges = [];
103
+ if (srcOrKey !== null && destOrKey !== null) {
104
+ const src = this._getVertex(srcOrKey);
105
+ const dest = this._getVertex(destOrKey);
106
+ if (src && dest) {
107
+ const srcOutEdges = this._outEdgeMap.get(src);
108
+ if (srcOutEdges) {
109
+ edges = srcOutEdges.filter(edge => edge.dest === dest.key);
110
+ }
111
+ }
112
+ }
113
+ return edges[0] || null;
114
+ }
115
+ /**
116
+ * The function removes an edge between two vertices in a graph and returns the removed edge.
117
+ * @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
118
+ * @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
119
+ * @returns the removed edge (EO) if it exists, or null if either the source or destination vertex does not exist.
120
+ */
121
+ deleteEdgeSrcToDest(srcOrKey, destOrKey) {
122
+ const src = this._getVertex(srcOrKey);
123
+ const dest = this._getVertex(destOrKey);
124
+ let removed = null;
125
+ if (!src || !dest) {
126
+ return null;
127
+ }
128
+ const srcOutEdges = this._outEdgeMap.get(src);
129
+ if (srcOutEdges) {
130
+ (0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.dest === dest.key);
131
+ }
132
+ const destInEdges = this._inEdgeMap.get(dest);
133
+ if (destInEdges) {
134
+ removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.src === src.key)[0] || null;
135
+ }
136
+ return removed;
137
+ }
138
+ /**
139
+ * The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
140
+ * @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
141
+ * and `dest`, which represent the source and destination vertices of the edge, respectively.
142
+ * @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `null` if the edge does not exist.
143
+ */
144
+ deleteEdge(edge) {
145
+ let removed = null;
146
+ const src = this._getVertex(edge.src);
147
+ const dest = this._getVertex(edge.dest);
148
+ if (src && dest) {
149
+ const srcOutEdges = this._outEdgeMap.get(src);
150
+ if (srcOutEdges && srcOutEdges.length > 0) {
151
+ (0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.src === src.key);
152
+ }
153
+ const destInEdges = this._inEdgeMap.get(dest);
154
+ if (destInEdges && destInEdges.length > 0) {
155
+ removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.dest === dest.key)[0];
156
+ }
157
+ }
158
+ return removed;
159
+ }
160
+ /**
161
+ * The function removes edges between two vertices and returns the removed edges.
162
+ * @param {VertexKey | VO} v1 - The parameter `v1` can be either a `VertexKey` or a `VO`. A `VertexKey` represents the
163
+ * unique identifier of a vertex in a graph, while `VO` represents the actual vertex object.
164
+ * @param {VertexKey | VO} v2 - The parameter `v2` represents either a `VertexKey` or a `VO` object. It is used to specify
165
+ * the second vertex in the edge that needs to be removed.
166
+ * @returns an array of removed edges (EO[]).
167
+ */
168
+ deleteEdgesBetween(v1, v2) {
169
+ const removed = [];
170
+ if (v1 && v2) {
171
+ const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
172
+ const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
173
+ v1ToV2 && removed.push(v1ToV2);
174
+ v2ToV1 && removed.push(v2ToV1);
175
+ }
176
+ return removed;
177
+ }
178
+ /**
179
+ * The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
180
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
181
+ * (`VertexKey`).
182
+ * @returns The method `incomingEdgesOf` returns an array of edges (`EO[]`).
183
+ */
184
+ incomingEdgesOf(vertexOrKey) {
185
+ const target = this._getVertex(vertexOrKey);
186
+ if (target) {
187
+ return this.inEdgeMap.get(target) || [];
188
+ }
189
+ return [];
190
+ }
191
+ /**
192
+ * The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
193
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`VO`) or a vertex ID
194
+ * (`VertexKey`).
195
+ * @returns The method `outgoingEdgesOf` returns an array of edges (`EO[]`).
196
+ */
197
+ outgoingEdgesOf(vertexOrKey) {
198
+ const target = this._getVertex(vertexOrKey);
199
+ if (target) {
200
+ return this._outEdgeMap.get(target) || [];
201
+ }
202
+ return [];
203
+ }
204
+ /**
205
+ * The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
206
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
207
+ * @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
208
+ */
209
+ degreeOf(vertexOrKey) {
210
+ return this.outDegreeOf(vertexOrKey) + this.inDegreeOf(vertexOrKey);
211
+ }
212
+ /**
213
+ * The function "inDegreeOf" returns the number of incoming edges for a given vertex.
214
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
215
+ * @returns The number of incoming edges of the specified vertex or vertex ID.
216
+ */
217
+ inDegreeOf(vertexOrKey) {
218
+ return this.incomingEdgesOf(vertexOrKey).length;
219
+ }
220
+ /**
221
+ * The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
222
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
223
+ * @returns The number of outgoing edges from the specified vertex or vertex ID.
224
+ */
225
+ outDegreeOf(vertexOrKey) {
226
+ return this.outgoingEdgesOf(vertexOrKey).length;
227
+ }
228
+ /**
229
+ * The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
230
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
231
+ * @returns The function `edgesOf` returns an array of edges.
232
+ */
233
+ edgesOf(vertexOrKey) {
234
+ return [...this.outgoingEdgesOf(vertexOrKey), ...this.incomingEdgesOf(vertexOrKey)];
235
+ }
236
+ /**
237
+ * The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
238
+ * @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
239
+ * @returns either a vertex object (VO) or null.
240
+ */
241
+ getEdgeSrc(e) {
242
+ return this._getVertex(e.src);
243
+ }
244
+ /**
245
+ * The function "getEdgeDest" returns the destination vertex of an edge.
246
+ * @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
247
+ * @returns either a vertex object of type VO or null.
248
+ */
249
+ getEdgeDest(e) {
250
+ return this._getVertex(e.dest);
251
+ }
252
+ /**
253
+ * The function `getDestinations` returns an array of destination vertices connected to a given vertex.
254
+ * @param {VO | VertexKey | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
255
+ * find the destinations. It can be either a `VO` object, a `VertexKey` value, or `null`.
256
+ * @returns an array of vertices (VO[]).
257
+ */
258
+ getDestinations(vertex) {
259
+ if (vertex === null) {
260
+ return [];
261
+ }
262
+ const destinations = [];
263
+ const outgoingEdges = this.outgoingEdgesOf(vertex);
264
+ for (const outEdge of outgoingEdges) {
265
+ const child = this.getEdgeDest(outEdge);
266
+ if (child) {
267
+ destinations.push(child);
268
+ }
269
+ }
270
+ return destinations;
271
+ }
272
+ /**
273
+ * The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
274
+ * in the sorted order, or null if the graph contains a cycle.
275
+ * @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
276
+ * property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
277
+ * specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
278
+ * @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
279
+ */
280
+ topologicalSort(propertyName) {
281
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
282
+ // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
283
+ // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
284
+ const statusMap = new Map();
285
+ for (const entry of this.vertices) {
286
+ statusMap.set(entry[1], 0);
287
+ }
288
+ let sorted = [];
289
+ let hasCycle = false;
290
+ const dfs = (cur) => {
291
+ statusMap.set(cur, 1);
292
+ const children = this.getDestinations(cur);
293
+ for (const child of children) {
294
+ const childStatus = statusMap.get(child);
295
+ if (childStatus === 0) {
296
+ dfs(child);
297
+ }
298
+ else if (childStatus === 1) {
299
+ hasCycle = true;
300
+ }
301
+ }
302
+ statusMap.set(cur, 2);
303
+ sorted.push(cur);
304
+ };
305
+ for (const entry of this.vertices) {
306
+ if (statusMap.get(entry[1]) === 0) {
307
+ dfs(entry[1]);
308
+ }
309
+ }
310
+ if (hasCycle)
311
+ return null;
312
+ if (propertyName === 'key')
313
+ sorted = sorted.map(vertex => (vertex instanceof DirectedVertex ? vertex.key : vertex));
314
+ return sorted.reverse();
315
+ }
316
+ /**
317
+ * The `edgeSet` function returns an array of all the edges in the graph.
318
+ * @returns The `edgeSet()` method returns an array of edges (`EO[]`).
319
+ */
320
+ edgeSet() {
321
+ let edges = [];
322
+ this._outEdgeMap.forEach(outEdges => {
323
+ edges = [...edges, ...outEdges];
324
+ });
325
+ return edges;
326
+ }
327
+ /**
328
+ * The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
329
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
330
+ * (`VertexKey`).
331
+ * @returns an array of vertices (VO[]).
332
+ */
333
+ getNeighbors(vertexOrKey) {
334
+ const neighbors = [];
335
+ const vertex = this._getVertex(vertexOrKey);
336
+ if (vertex) {
337
+ const outEdges = this.outgoingEdgesOf(vertex);
338
+ for (const outEdge of outEdges) {
339
+ const neighbor = this._getVertex(outEdge.dest);
340
+ // TODO after no-non-null-assertion not ensure the logic
341
+ if (neighbor) {
342
+ neighbors.push(neighbor);
343
+ }
344
+ }
345
+ }
346
+ return neighbors;
347
+ }
348
+ /**
349
+ * The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
350
+ * otherwise it returns null.
351
+ * @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
352
+ * @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
353
+ * graph. If the edge does not exist, it returns `null`.
354
+ */
355
+ getEndsOfEdge(edge) {
356
+ if (!this.hasEdge(edge.src, edge.dest)) {
357
+ return null;
358
+ }
359
+ const v1 = this._getVertex(edge.src);
360
+ const v2 = this._getVertex(edge.dest);
361
+ if (v1 && v2) {
362
+ return [v1, v2];
363
+ }
364
+ else {
365
+ return null;
366
+ }
367
+ }
368
+ /**
369
+ * The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertices exist.
370
+ * @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
371
+ * needs to be added to the graph.
372
+ * @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
373
+ * source or destination vertex does not exist in the graph.
374
+ */
375
+ _addEdgeOnly(edge) {
376
+ if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
377
+ return false;
378
+ }
379
+ const srcVertex = this._getVertex(edge.src);
380
+ const destVertex = this._getVertex(edge.dest);
381
+ // TODO after no-non-null-assertion not ensure the logic
382
+ if (srcVertex && destVertex) {
383
+ const srcOutEdges = this._outEdgeMap.get(srcVertex);
384
+ if (srcOutEdges) {
385
+ srcOutEdges.push(edge);
386
+ }
387
+ else {
388
+ this._outEdgeMap.set(srcVertex, [edge]);
389
+ }
390
+ const destInEdges = this._inEdgeMap.get(destVertex);
391
+ if (destInEdges) {
392
+ destInEdges.push(edge);
393
+ }
394
+ else {
395
+ this._inEdgeMap.set(destVertex, [edge]);
396
+ }
397
+ return true;
398
+ }
399
+ else {
400
+ return false;
401
+ }
402
+ }
403
+ }
404
+ exports.DirectedGraph = DirectedGraph;
@@ -0,0 +1,4 @@
1
+ export * from './abstract-graph';
2
+ export * from './directed-graph';
3
+ export * from './undirected-graph';
4
+ export * from './map-graph';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./abstract-graph"), exports);
18
+ __exportStar(require("./directed-graph"), exports);
19
+ __exportStar(require("./undirected-graph"), exports);
20
+ __exportStar(require("./map-graph"), exports);