data-structure-typed 0.9.16 → 1.3.1

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 (264) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +665 -172
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
  9. package/dist/data-structures/binary-tree/avl-tree.js +142 -116
  10. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
  14. package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
  15. package/dist/data-structures/binary-tree/bst.d.ts +117 -23
  16. package/dist/data-structures/binary-tree/bst.js +233 -240
  17. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  18. package/dist/data-structures/binary-tree/index.js +1 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  20. package/dist/data-structures/binary-tree/rb-tree.js +40 -5
  21. package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
  22. package/dist/data-structures/binary-tree/segment-tree.js +126 -93
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
  25. package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
  26. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  27. package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
  28. package/dist/data-structures/graph/abstract-graph.js +610 -572
  29. package/dist/data-structures/graph/directed-graph.d.ts +173 -16
  30. package/dist/data-structures/graph/directed-graph.js +345 -313
  31. package/dist/data-structures/graph/index.d.ts +1 -0
  32. package/dist/data-structures/graph/index.js +1 -0
  33. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  34. package/dist/data-structures/graph/map-graph.js +111 -0
  35. package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
  36. package/dist/data-structures/graph/undirected-graph.js +203 -178
  37. package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
  38. package/dist/data-structures/hash/coordinate-map.js +59 -36
  39. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  40. package/dist/data-structures/hash/coordinate-set.js +49 -33
  41. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  42. package/dist/data-structures/hash/hash-table.js +4 -0
  43. package/dist/data-structures/hash/pair.d.ts +2 -1
  44. package/dist/data-structures/hash/pair.js +4 -0
  45. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-map.js +4 -0
  47. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  48. package/dist/data-structures/hash/tree-set.js +4 -0
  49. package/dist/data-structures/heap/heap.d.ts +62 -45
  50. package/dist/data-structures/heap/heap.js +124 -86
  51. package/dist/data-structures/heap/max-heap.d.ts +13 -5
  52. package/dist/data-structures/heap/max-heap.js +18 -28
  53. package/dist/data-structures/heap/min-heap.d.ts +14 -5
  54. package/dist/data-structures/heap/min-heap.js +19 -28
  55. package/dist/data-structures/index.d.ts +1 -1
  56. package/dist/data-structures/index.js +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
  58. package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
  59. package/dist/data-structures/linked-list/index.d.ts +1 -0
  60. package/dist/data-structures/linked-list/index.js +1 -0
  61. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
  62. package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
  63. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  64. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  65. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  66. package/dist/data-structures/matrix/matrix.js +21 -8
  67. package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
  68. package/dist/data-structures/matrix/matrix2d.js +146 -80
  69. package/dist/data-structures/matrix/navigator.d.ts +36 -1
  70. package/dist/data-structures/matrix/navigator.js +46 -37
  71. package/dist/data-structures/matrix/vector2d.d.ts +142 -15
  72. package/dist/data-structures/matrix/vector2d.js +215 -109
  73. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
  74. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
  75. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
  76. package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
  77. package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
  78. package/dist/data-structures/priority-queue/priority-queue.js +244 -143
  79. package/dist/data-structures/queue/deque.d.ts +141 -13
  80. package/dist/data-structures/queue/deque.js +200 -82
  81. package/dist/data-structures/queue/queue.d.ts +65 -38
  82. package/dist/data-structures/queue/queue.js +110 -66
  83. package/dist/data-structures/stack/stack.d.ts +27 -32
  84. package/dist/data-structures/stack/stack.js +47 -53
  85. package/dist/data-structures/tree/index.d.ts +1 -0
  86. package/dist/data-structures/tree/index.js +17 -0
  87. package/dist/data-structures/tree/tree.d.ts +14 -0
  88. package/dist/data-structures/tree/tree.js +60 -0
  89. package/dist/data-structures/trie/trie.d.ts +33 -10
  90. package/dist/data-structures/trie/trie.js +123 -208
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.js +3 -0
  93. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  94. package/dist/interfaces/abstract-graph.d.ts +17 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/binary-tree.d.ts +6 -0
  97. package/dist/interfaces/bst.d.ts +17 -0
  98. package/dist/interfaces/directed-graph.d.ts +12 -0
  99. package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
  100. package/dist/interfaces/index.js +31 -0
  101. package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
  102. package/dist/interfaces/rb-tree.d.ts +8 -0
  103. package/dist/interfaces/segment-tree.d.ts +1 -0
  104. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  105. package/dist/interfaces/singly-linked-list.js +2 -0
  106. package/dist/interfaces/tree-multiset.d.ts +7 -0
  107. package/dist/interfaces/tree-multiset.js +2 -0
  108. package/dist/interfaces/undirected-graph.d.ts +5 -0
  109. package/dist/interfaces/undirected-graph.js +2 -0
  110. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  111. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  112. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  113. package/dist/types/data-structures/abstract-graph.js +2 -0
  114. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  115. package/dist/types/data-structures/avl-tree.js +2 -0
  116. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  117. package/dist/types/data-structures/binary-tree.js +2 -0
  118. package/dist/types/data-structures/bst.d.ts +13 -0
  119. package/dist/types/data-structures/bst.js +9 -0
  120. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  121. package/dist/types/data-structures/directed-graph.js +9 -0
  122. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  123. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  124. package/dist/types/data-structures/heap.d.ts +3 -0
  125. package/dist/types/data-structures/heap.js +2 -0
  126. package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
  127. package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
  128. package/dist/types/data-structures/map-graph.d.ts +1 -0
  129. package/dist/types/data-structures/map-graph.js +2 -0
  130. package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
  131. package/dist/types/data-structures/navigator.js +2 -0
  132. package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
  133. package/dist/types/data-structures/priority-queue.js +2 -0
  134. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  135. package/dist/types/data-structures/rb-tree.js +8 -0
  136. package/dist/types/data-structures/segment-tree.js +2 -0
  137. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  138. package/dist/types/data-structures/singly-linked-list.js +2 -0
  139. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  140. package/dist/types/data-structures/tree-multiset.js +2 -0
  141. package/dist/types/helpers.d.ts +1 -0
  142. package/dist/types/helpers.js +2 -0
  143. package/dist/types/index.d.ts +3 -0
  144. package/dist/types/index.js +19 -0
  145. package/dist/types/utils/index.d.ts +2 -0
  146. package/dist/types/utils/index.js +18 -0
  147. package/dist/types/utils/utils.d.ts +7 -0
  148. package/dist/types/utils/utils.js +2 -0
  149. package/dist/types/utils/validate-type.d.ts +19 -0
  150. package/dist/types/utils/validate-type.js +2 -0
  151. package/dist/utils/utils.d.ts +17 -103
  152. package/dist/utils/utils.js +40 -625
  153. package/package.json +134 -23
  154. package/.idea/data-structure-typed.iml +0 -12
  155. package/.idea/modules.xml +0 -8
  156. package/.idea/vcs.xml +0 -6
  157. package/dist/data-structures/trampoline.d.ts +0 -16
  158. package/dist/data-structures/trampoline.js +0 -130
  159. package/dist/data-structures/types/abstract-graph.d.ts +0 -29
  160. package/dist/data-structures/types/avl-tree.d.ts +0 -5
  161. package/dist/data-structures/types/binary-tree.d.ts +0 -16
  162. package/dist/data-structures/types/bst.d.ts +0 -7
  163. package/dist/data-structures/types/directed-graph.d.ts +0 -10
  164. package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
  165. package/dist/data-structures/types/heap.d.ts +0 -7
  166. package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
  167. package/dist/data-structures/types/tree-multiset.d.ts +0 -5
  168. package/dist/data-structures/types/utils.d.ts +0 -52
  169. package/dist/data-structures/types/utils.js +0 -54
  170. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +0 -227
  172. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  173. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  174. package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
  175. package/src/data-structures/binary-tree/bst.ts +0 -395
  176. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  177. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  178. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  179. package/src/data-structures/binary-tree/index.ts +0 -11
  180. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  181. package/src/data-structures/binary-tree/segment-tree.ts +0 -172
  182. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  183. package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
  184. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  185. package/src/data-structures/diagrams/README.md +0 -7
  186. package/src/data-structures/graph/abstract-graph.ts +0 -753
  187. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  188. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  189. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  191. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  192. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  194. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  195. package/src/data-structures/graph/diagrams/mst.png +0 -0
  196. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  197. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  198. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  199. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  200. package/src/data-structures/graph/directed-graph.ts +0 -306
  201. package/src/data-structures/graph/index.ts +0 -3
  202. package/src/data-structures/graph/undirected-graph.ts +0 -155
  203. package/src/data-structures/hash/coordinate-map.ts +0 -24
  204. package/src/data-structures/hash/coordinate-set.ts +0 -20
  205. package/src/data-structures/hash/index.ts +0 -6
  206. package/src/data-structures/heap/heap.ts +0 -127
  207. package/src/data-structures/heap/index.ts +0 -3
  208. package/src/data-structures/heap/max-heap.ts +0 -23
  209. package/src/data-structures/heap/min-heap.ts +0 -25
  210. package/src/data-structures/index.ts +0 -12
  211. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
  212. package/src/data-structures/linked-list/index.ts +0 -2
  213. package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
  214. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  215. package/src/data-structures/matrix/index.ts +0 -4
  216. package/src/data-structures/matrix/matrix.ts +0 -13
  217. package/src/data-structures/matrix/matrix2d.ts +0 -125
  218. package/src/data-structures/matrix/navigator.ts +0 -87
  219. package/src/data-structures/matrix/vector2d.ts +0 -189
  220. package/src/data-structures/priority-queue/index.ts +0 -3
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
  223. package/src/data-structures/priority-queue/priority-queue.ts +0 -200
  224. package/src/data-structures/queue/deque.ts +0 -139
  225. package/src/data-structures/queue/index.ts +0 -2
  226. package/src/data-structures/queue/queue.ts +0 -122
  227. package/src/data-structures/stack/index.ts +0 -1
  228. package/src/data-structures/stack/stack.ts +0 -103
  229. package/src/data-structures/trampoline.ts +0 -51
  230. package/src/data-structures/trie/index.ts +0 -1
  231. package/src/data-structures/trie/trie.ts +0 -203
  232. package/src/data-structures/types/abstract-graph.ts +0 -51
  233. package/src/data-structures/types/avl-tree.ts +0 -6
  234. package/src/data-structures/types/binary-tree.ts +0 -15
  235. package/src/data-structures/types/bst.ts +0 -5
  236. package/src/data-structures/types/directed-graph.ts +0 -18
  237. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  238. package/src/data-structures/types/heap.ts +0 -8
  239. package/src/data-structures/types/navigator.ts +0 -12
  240. package/src/data-structures/types/priority-queue.ts +0 -9
  241. package/src/data-structures/types/segment-tree.ts +0 -1
  242. package/src/data-structures/types/singly-linked-list.ts +0 -15
  243. package/src/data-structures/types/tree-multiset.ts +0 -3
  244. package/src/data-structures/types/utils.ts +0 -173
  245. package/src/index.ts +0 -1
  246. package/src/utils/index.ts +0 -1
  247. package/src/utils/utils.ts +0 -505
  248. package/tsconfig.json +0 -56
  249. /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
  250. /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
  251. /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
  252. /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
  253. /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
  254. /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
  255. /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
  256. /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
  257. /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
  258. /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
  259. /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
  260. /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
  261. /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
  262. /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
  263. /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
  264. /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
@@ -1,390 +1,422 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
- if (ar || !(i in from)) {
36
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
- ar[i] = from[i];
38
- }
39
- }
40
- return to.concat(ar || Array.prototype.slice.call(from));
41
- };
42
- var __values = (this && this.__values) || function(o) {
43
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
44
- if (m) return m.call(o);
45
- if (o && typeof o.length === "number") return {
46
- next: function () {
47
- if (o && i >= o.length) o = void 0;
48
- return { value: o && o[i++], done: !o };
49
- }
50
- };
51
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
52
- };
53
2
  Object.defineProperty(exports, "__esModule", { value: true });
54
3
  exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = void 0;
55
- var utils_1 = require("../../utils");
56
- var abstract_graph_1 = require("./abstract-graph");
57
- var DirectedVertex = /** @class */ (function (_super) {
58
- __extends(DirectedVertex, _super);
59
- function DirectedVertex(id) {
60
- return _super.call(this, id) || this;
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 {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
17
+ * used to uniquely identify the vertex within a graph or data structure.
18
+ * @param {T} [val] - The "val" parameter is an optional parameter of type T. 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(id, val) {
22
+ super(id, val);
61
23
  }
62
- return DirectedVertex;
63
- }(abstract_graph_1.AbstractVertex));
24
+ }
64
25
  exports.DirectedVertex = DirectedVertex;
65
- var DirectedEdge = /** @class */ (function (_super) {
66
- __extends(DirectedEdge, _super);
67
- function DirectedEdge(src, dest, weight) {
68
- var _this = _super.call(this, weight) || this;
69
- _this._src = src;
70
- _this._dest = dest;
71
- return _this;
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 {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
31
+ * a graph.
32
+ * @param {VertexId} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
33
+ * `VertexId`, 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 {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value associated with
36
+ * the edge.
37
+ */
38
+ constructor(src, dest, weight, val) {
39
+ super(weight, val);
40
+ this._src = src;
41
+ this._dest = dest;
42
+ }
43
+ get src() {
44
+ return this._src;
45
+ }
46
+ set src(v) {
47
+ this._src = v;
48
+ }
49
+ get dest() {
50
+ return this._dest;
51
+ }
52
+ set dest(v) {
53
+ this._dest = v;
72
54
  }
73
- Object.defineProperty(DirectedEdge.prototype, "src", {
74
- get: function () {
75
- return this._src;
76
- },
77
- set: function (v) {
78
- this._src = v;
79
- },
80
- enumerable: false,
81
- configurable: true
82
- });
83
- Object.defineProperty(DirectedEdge.prototype, "dest", {
84
- get: function () {
85
- return this._dest;
86
- },
87
- set: function (v) {
88
- this._dest = v;
89
- },
90
- enumerable: false,
91
- configurable: true
92
- });
93
- return DirectedEdge;
94
- }(abstract_graph_1.AbstractEdge));
55
+ }
95
56
  exports.DirectedEdge = DirectedEdge;
96
- // Strongly connected, One direction connected, Weakly connected
97
- var DirectedGraph = /** @class */ (function (_super) {
98
- __extends(DirectedGraph, _super);
99
- function DirectedGraph() {
100
- var _this = _super.call(this) || this;
101
- _this._outEdgeMap = new Map();
102
- _this._inEdgeMap = new Map();
103
- return _this;
57
+ class DirectedGraph extends abstract_graph_1.AbstractGraph {
58
+ /**
59
+ * The constructor function initializes an instance of a class.
60
+ */
61
+ constructor() {
62
+ super();
63
+ this._outEdgeMap = new Map();
64
+ this._inEdgeMap = new Map();
65
+ }
66
+ get outEdgeMap() {
67
+ return this._outEdgeMap;
68
+ }
69
+ get inEdgeMap() {
70
+ return this._inEdgeMap;
104
71
  }
105
- DirectedGraph.prototype.getEdge = function (srcOrId, destOrId) {
106
- var edges = [];
72
+ /**
73
+ * 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.
74
+ * 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.
75
+ */
76
+ /**
77
+ * The function creates a new vertex with an optional value and returns it.
78
+ * @param {VertexId} id - The `id` parameter is the unique identifier for the vertex. It is of type `VertexId`, which
79
+ * could be a number or a string depending on how you want to identify your vertices.
80
+ * @param [val] - The 'val' parameter is an optional value that can be assigned to the vertex. If a value is provided,
81
+ * it will be assigned to the 'val' property of the vertex. If no value is provided, the 'val' property will be
82
+ * assigned the same value as the 'id' parameter
83
+ * @returns a new instance of a DirectedVertex object, casted as type V.
84
+ */
85
+ createVertex(id, val) {
86
+ return new DirectedVertex(id, val !== null && val !== void 0 ? val : id);
87
+ }
88
+ /**
89
+ * 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.
90
+ * 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.
91
+ */
92
+ /**
93
+ * The function creates a directed edge between two vertices with an optional weight and value.
94
+ * @param {VertexId} src - The source vertex ID of the edge. It represents the starting point of the edge.
95
+ * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
96
+ * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
97
+ * weight is provided, it defaults to 1.
98
+ * @param [val] - The 'val' parameter is an optional value that can be assigned to the edge. It can be of any type and
99
+ * is used to store additional information or data associated with the edge.
100
+ * @returns a new instance of a DirectedEdge object, casted as type E.
101
+ */
102
+ createEdge(src, dest, weight, val) {
103
+ return new DirectedEdge(src, dest, weight !== null && weight !== void 0 ? weight : 1, val);
104
+ }
105
+ /**
106
+ * The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
107
+ * @param {V | null | VertexId} srcOrId - The source vertex or its ID. It can be either a vertex object or a vertex ID.
108
+ * @param {V | null | VertexId} destOrId - The `destOrId` parameter in the `getEdge` function represents the
109
+ * destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`VertexId`), or `null` if the
110
+ * destination is not specified.
111
+ * @returns the first edge found between the source and destination vertices, or null if no such edge is found.
112
+ */
113
+ getEdge(srcOrId, destOrId) {
114
+ let edges = [];
107
115
  if (srcOrId !== null && destOrId !== null) {
108
- var src = this.getVertex(srcOrId);
109
- var dest_1 = this.getVertex(destOrId);
110
- if (src && dest_1) {
111
- var srcOutEdges = this._outEdgeMap.get(src);
116
+ const src = this._getVertex(srcOrId);
117
+ const dest = this._getVertex(destOrId);
118
+ if (src && dest) {
119
+ const srcOutEdges = this._outEdgeMap.get(src);
112
120
  if (srcOutEdges) {
113
- edges = srcOutEdges.filter(function (edge) { return edge.dest === dest_1.id; });
121
+ edges = srcOutEdges.filter(edge => edge.dest === dest.id);
114
122
  }
115
123
  }
116
124
  }
117
125
  return edges[0] || null;
118
- };
119
- DirectedGraph.prototype.addEdge = function (edge) {
120
- if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
121
- return false;
122
- }
123
- var srcVertex = this.getVertex(edge.src);
124
- var destVertex = this.getVertex(edge.dest);
125
- // TODO after no-non-null-assertion not ensure the logic
126
- if (srcVertex && destVertex) {
127
- var srcOutEdges = this._outEdgeMap.get(srcVertex);
128
- if (srcOutEdges) {
129
- srcOutEdges.push(edge);
130
- }
131
- else {
132
- this._outEdgeMap.set(srcVertex, [edge]);
133
- }
134
- var destInEdges = this._inEdgeMap.get(destVertex);
135
- if (destInEdges) {
136
- destInEdges.push(edge);
137
- }
138
- else {
139
- this._inEdgeMap.set(destVertex, [edge]);
140
- }
141
- return true;
142
- }
143
- else {
144
- return false;
145
- }
146
- };
147
- DirectedGraph.prototype.removeEdgeBetween = function (srcOrId, destOrId) {
148
- var src = this.getVertex(srcOrId);
149
- var dest = this.getVertex(destOrId);
150
- var removed = null;
126
+ }
127
+ /**
128
+ * The function removes an edge between two vertices in a graph and returns the removed edge.
129
+ * @param {V | VertexId} srcOrId - The source vertex or its ID.
130
+ * @param {V | VertexId} destOrId - The `destOrId` parameter represents the destination vertex or its ID.
131
+ * @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
132
+ */
133
+ removeEdgeSrcToDest(srcOrId, destOrId) {
134
+ const src = this._getVertex(srcOrId);
135
+ const dest = this._getVertex(destOrId);
136
+ let removed = null;
151
137
  if (!src || !dest) {
152
138
  return null;
153
139
  }
154
- var srcOutEdges = this._outEdgeMap.get(src);
140
+ const srcOutEdges = this._outEdgeMap.get(src);
155
141
  if (srcOutEdges) {
156
- (0, utils_1.arrayRemove)(srcOutEdges, function (edge) { return edge.dest === dest.id; });
142
+ (0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.dest === dest.id);
157
143
  }
158
- var destInEdges = this._inEdgeMap.get(dest);
144
+ const destInEdges = this._inEdgeMap.get(dest);
159
145
  if (destInEdges) {
160
- removed = (0, utils_1.arrayRemove)(destInEdges, function (edge) { return edge.src === src.id; })[0] || null;
146
+ removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.src === src.id)[0] || null;
161
147
  }
162
148
  return removed;
163
- };
164
- DirectedGraph.prototype.removeEdge = function (edge) {
165
- var removed = null;
166
- var src = this.getVertex(edge.src);
167
- var dest = this.getVertex(edge.dest);
149
+ }
150
+ /**
151
+ * The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
152
+ * @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
153
+ * and `dest`, which represent the source and destination vertices of the edge, respectively.
154
+ * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
155
+ */
156
+ removeEdge(edge) {
157
+ let removed = null;
158
+ const src = this._getVertex(edge.src);
159
+ const dest = this._getVertex(edge.dest);
168
160
  if (src && dest) {
169
- var srcOutEdges = this._outEdgeMap.get(src);
161
+ const srcOutEdges = this._outEdgeMap.get(src);
170
162
  if (srcOutEdges && srcOutEdges.length > 0) {
171
- (0, utils_1.arrayRemove)(srcOutEdges, function (edge) { return edge.src === src.id; });
163
+ (0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.src === src.id);
172
164
  }
173
- var destInEdges = this._inEdgeMap.get(dest);
165
+ const destInEdges = this._inEdgeMap.get(dest);
174
166
  if (destInEdges && destInEdges.length > 0) {
175
- removed = (0, utils_1.arrayRemove)(destInEdges, function (edge) { return edge.dest === dest.id; })[0];
167
+ removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.dest === dest.id)[0];
176
168
  }
177
169
  }
178
170
  return removed;
179
- };
180
- DirectedGraph.prototype.removeAllEdges = function (src, dest) {
181
- return [];
182
- };
183
- DirectedGraph.prototype.incomingEdgesOf = function (vertexOrId) {
184
- var target = this.getVertex(vertexOrId);
171
+ }
172
+ /**
173
+ * The function removes edges between two vertices and returns the removed edges.
174
+ * @param {VertexId | V} v1 - The parameter `v1` can be either a `VertexId` or a `V`. A `VertexId` represents the
175
+ * unique identifier of a vertex in a graph, while `V` represents the actual vertex object.
176
+ * @param {VertexId | V} v2 - The parameter `v2` represents either a `VertexId` or a `V` object. It is used to specify
177
+ * the second vertex in the edge that needs to be removed.
178
+ * @returns an array of removed edges (E[]).
179
+ */
180
+ removeEdgesBetween(v1, v2) {
181
+ const removed = [];
182
+ if (v1 && v2) {
183
+ const v1ToV2 = this.removeEdgeSrcToDest(v1, v2);
184
+ const v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
185
+ v1ToV2 && removed.push(v1ToV2);
186
+ v2ToV1 && removed.push(v2ToV1);
187
+ }
188
+ return removed;
189
+ }
190
+ /**
191
+ * The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
192
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
193
+ * (`VertexId`).
194
+ * @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
195
+ */
196
+ incomingEdgesOf(vertexOrId) {
197
+ const target = this._getVertex(vertexOrId);
185
198
  if (target) {
186
- return this._inEdgeMap.get(target) || [];
199
+ return this.inEdgeMap.get(target) || [];
187
200
  }
188
201
  return [];
189
- };
190
- DirectedGraph.prototype.outgoingEdgesOf = function (vertexOrId) {
191
- var target = this.getVertex(vertexOrId);
202
+ }
203
+ /**
204
+ * The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
205
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
206
+ * (`VertexId`).
207
+ * @returns The method `outgoingEdgesOf` returns an array of edges (`E[]`).
208
+ */
209
+ outgoingEdgesOf(vertexOrId) {
210
+ const target = this._getVertex(vertexOrId);
192
211
  if (target) {
193
212
  return this._outEdgeMap.get(target) || [];
194
213
  }
195
214
  return [];
196
- };
197
- DirectedGraph.prototype.degreeOf = function (vertexOrId) {
215
+ }
216
+ /**
217
+ * The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
218
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
219
+ * @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
220
+ */
221
+ degreeOf(vertexOrId) {
198
222
  return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
199
- };
200
- DirectedGraph.prototype.inDegreeOf = function (vertexOrId) {
223
+ }
224
+ /**
225
+ * The function "inDegreeOf" returns the number of incoming edges for a given vertex.
226
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
227
+ * @returns The number of incoming edges of the specified vertex or vertex ID.
228
+ */
229
+ inDegreeOf(vertexOrId) {
201
230
  return this.incomingEdgesOf(vertexOrId).length;
202
- };
203
- DirectedGraph.prototype.outDegreeOf = function (vertexOrId) {
231
+ }
232
+ /**
233
+ * The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
234
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
235
+ * @returns The number of outgoing edges from the specified vertex or vertex ID.
236
+ */
237
+ outDegreeOf(vertexOrId) {
204
238
  return this.outgoingEdgesOf(vertexOrId).length;
205
- };
206
- DirectedGraph.prototype.edgesOf = function (vertexOrId) {
207
- return __spreadArray(__spreadArray([], __read(this.outgoingEdgesOf(vertexOrId)), false), __read(this.incomingEdgesOf(vertexOrId)), false);
208
- };
209
- DirectedGraph.prototype.getEdgeSrc = function (e) {
210
- return this.getVertex(e.src);
211
- };
212
- DirectedGraph.prototype.getEdgeDest = function (e) {
213
- return this.getVertex(e.dest);
214
- };
215
- DirectedGraph.prototype.getDestinations = function (vertex) {
216
- var e_1, _a;
239
+ }
240
+ /**
241
+ * The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
242
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
243
+ * @returns The function `edgesOf` returns an array of edges.
244
+ */
245
+ edgesOf(vertexOrId) {
246
+ return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
247
+ }
248
+ /**
249
+ * The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
250
+ * @param {E} e - The parameter "e" is of type E, which represents an edge in a graph.
251
+ * @returns either a vertex object (V) or null.
252
+ */
253
+ getEdgeSrc(e) {
254
+ return this._getVertex(e.src);
255
+ }
256
+ /**
257
+ * The function "getEdgeDest" returns the destination vertex of an edge.
258
+ * @param {E} e - The parameter "e" is of type "E", which represents an edge in a graph.
259
+ * @returns either a vertex object of type V or null.
260
+ */
261
+ getEdgeDest(e) {
262
+ return this._getVertex(e.dest);
263
+ }
264
+ /**
265
+ * The function `getDestinations` returns an array of destination vertices connected to a given vertex.
266
+ * @param {V | VertexId | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
267
+ * find the destinations. It can be either a `V` object, a `VertexId` value, or `null`.
268
+ * @returns an array of vertices (V[]).
269
+ */
270
+ getDestinations(vertex) {
217
271
  if (vertex === null) {
218
272
  return [];
219
273
  }
220
- var destinations = [];
221
- var outgoingEdges = this.outgoingEdgesOf(vertex);
222
- try {
223
- for (var outgoingEdges_1 = __values(outgoingEdges), outgoingEdges_1_1 = outgoingEdges_1.next(); !outgoingEdges_1_1.done; outgoingEdges_1_1 = outgoingEdges_1.next()) {
224
- var outEdge = outgoingEdges_1_1.value;
225
- var child = this.getEdgeDest(outEdge);
226
- if (child) {
227
- destinations.push(child);
228
- }
229
- }
230
- }
231
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
232
- finally {
233
- try {
234
- if (outgoingEdges_1_1 && !outgoingEdges_1_1.done && (_a = outgoingEdges_1.return)) _a.call(outgoingEdges_1);
274
+ const destinations = [];
275
+ const outgoingEdges = this.outgoingEdgesOf(vertex);
276
+ for (const outEdge of outgoingEdges) {
277
+ const child = this.getEdgeDest(outEdge);
278
+ if (child) {
279
+ destinations.push(child);
235
280
  }
236
- finally { if (e_1) throw e_1.error; }
237
281
  }
238
282
  return destinations;
239
- };
240
- /**--- start find cycles --- */
283
+ }
241
284
  /**
242
- * when stored with adjacency list time: O(V+E)
243
- * when stored with adjacency matrix time: O(V^2)
285
+ * The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
286
+ * in the sorted order, or null if the graph contains a cycle.
287
+ * @param {'vertex' | 'id'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
288
+ * property to use for sorting the vertices. It can have two possible values: 'vertex' or 'id'. If 'vertex' is
289
+ * specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
290
+ * @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
244
291
  */
245
- DirectedGraph.prototype.topologicalSort = function () {
246
- var e_2, _a, e_3, _b;
247
- var _this = this;
248
- // vector<vector<int>> g;
249
- // vector<int> color;
250
- // int last;
251
- // bool hasCycle;
252
- //
253
- // bool topo_sort() {
254
- // int n = g.size();
255
- // vector<int> degree(n, 0);
256
- // queue<int> q;
257
- // for (int i = 0; i < n; i++) {
258
- // degree[i] = g[i].size();
259
- // if (degree[i] <= 1) {
260
- // q.push(i);
261
- // }
262
- // }
263
- // int cnt = 0;
264
- // while (!q.empty()) {
265
- // cnt++;
266
- // int root = q.front();
267
- // q.pop();
268
- // for (auto child : g[root]) {
269
- // degree[child]--;
270
- // if (degree[child] == 1) {
271
- // q.push(child);
272
- // }
273
- // }
274
- // }
275
- // return (cnt != n);
276
- // }
292
+ topologicalSort(propertyName) {
293
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
277
294
  // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
278
295
  // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
279
- var statusMap = new Map();
280
- try {
281
- for (var _c = __values(this._vertices), _d = _c.next(); !_d.done; _d = _c.next()) {
282
- var entry = _d.value;
283
- statusMap.set(entry[1], 0);
284
- }
285
- }
286
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
287
- finally {
288
- try {
289
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
290
- }
291
- finally { if (e_2) throw e_2.error; }
296
+ const statusMap = new Map();
297
+ for (const entry of this.vertices) {
298
+ statusMap.set(entry[1], 0);
292
299
  }
293
- var sorted = [];
294
- var hasCycle = false;
295
- var dfs = function (cur) {
296
- var e_4, _a;
300
+ let sorted = [];
301
+ let hasCycle = false;
302
+ const dfs = (cur) => {
297
303
  statusMap.set(cur, 1);
298
- var children = _this.getDestinations(cur);
299
- try {
300
- for (var children_1 = __values(children), children_1_1 = children_1.next(); !children_1_1.done; children_1_1 = children_1.next()) {
301
- var child = children_1_1.value;
302
- var childStatus = statusMap.get(child);
303
- if (childStatus === 0) {
304
- dfs(child);
305
- }
306
- else if (childStatus === 1) {
307
- hasCycle = true;
308
- }
304
+ const children = this.getDestinations(cur);
305
+ for (const child of children) {
306
+ const childStatus = statusMap.get(child);
307
+ if (childStatus === 0) {
308
+ dfs(child);
309
309
  }
310
- }
311
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
312
- finally {
313
- try {
314
- if (children_1_1 && !children_1_1.done && (_a = children_1.return)) _a.call(children_1);
310
+ else if (childStatus === 1) {
311
+ hasCycle = true;
315
312
  }
316
- finally { if (e_4) throw e_4.error; }
317
313
  }
318
314
  statusMap.set(cur, 2);
319
315
  sorted.push(cur);
320
316
  };
321
- try {
322
- for (var _e = __values(this._vertices), _f = _e.next(); !_f.done; _f = _e.next()) {
323
- var entry = _f.value;
324
- if (statusMap.get(entry[1]) === 0) {
325
- dfs(entry[1]);
326
- }
327
- }
328
- }
329
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
330
- finally {
331
- try {
332
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
317
+ for (const entry of this.vertices) {
318
+ if (statusMap.get(entry[1]) === 0) {
319
+ dfs(entry[1]);
333
320
  }
334
- finally { if (e_3) throw e_3.error; }
335
321
  }
336
- if (hasCycle) {
322
+ if (hasCycle)
337
323
  return null;
338
- }
324
+ if (propertyName === 'id')
325
+ sorted = sorted.map(vertex => vertex instanceof DirectedVertex ? vertex.id : vertex);
339
326
  return sorted.reverse();
340
- };
341
- /**--- end find cycles --- */
342
- DirectedGraph.prototype.edgeSet = function () {
343
- var edges = [];
344
- this._outEdgeMap.forEach(function (outEdges) {
345
- edges = __spreadArray(__spreadArray([], __read(edges), false), __read(outEdges), false);
327
+ }
328
+ /**
329
+ * The `edgeSet` function returns an array of all the edges in the graph.
330
+ * @returns The `edgeSet()` method returns an array of edges (`E[]`).
331
+ */
332
+ edgeSet() {
333
+ let edges = [];
334
+ this._outEdgeMap.forEach(outEdges => {
335
+ edges = [...edges, ...outEdges];
346
336
  });
347
337
  return edges;
348
- };
349
- DirectedGraph.prototype.getNeighbors = function (vertexOrId) {
350
- var e_5, _a;
351
- var neighbors = [];
352
- var vertex = this.getVertex(vertexOrId);
338
+ }
339
+ /**
340
+ * The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
341
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
342
+ * (`VertexId`).
343
+ * @returns an array of vertices (V[]).
344
+ */
345
+ getNeighbors(vertexOrId) {
346
+ const neighbors = [];
347
+ const vertex = this._getVertex(vertexOrId);
353
348
  if (vertex) {
354
- var outEdges = this.outgoingEdgesOf(vertex);
355
- try {
356
- for (var outEdges_1 = __values(outEdges), outEdges_1_1 = outEdges_1.next(); !outEdges_1_1.done; outEdges_1_1 = outEdges_1.next()) {
357
- var outEdge = outEdges_1_1.value;
358
- var neighbor = this.getVertex(outEdge.dest);
359
- // TODO after no-non-null-assertion not ensure the logic
360
- if (neighbor) {
361
- neighbors.push(neighbor);
362
- }
363
- }
364
- }
365
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
366
- finally {
367
- try {
368
- if (outEdges_1_1 && !outEdges_1_1.done && (_a = outEdges_1.return)) _a.call(outEdges_1);
349
+ const outEdges = this.outgoingEdgesOf(vertex);
350
+ for (const outEdge of outEdges) {
351
+ const neighbor = this._getVertex(outEdge.dest);
352
+ // TODO after no-non-null-assertion not ensure the logic
353
+ if (neighbor) {
354
+ neighbors.push(neighbor);
369
355
  }
370
- finally { if (e_5) throw e_5.error; }
371
356
  }
372
357
  }
373
358
  return neighbors;
374
- };
375
- DirectedGraph.prototype.getEndsOfEdge = function (edge) {
376
- if (!this.containsEdge(edge.src, edge.dest)) {
359
+ }
360
+ /**
361
+ * The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
362
+ * otherwise it returns null.
363
+ * @param {E} edge - The parameter `edge` is of type `E`, which represents an edge in a graph.
364
+ * @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
365
+ * graph. If the edge does not exist, it returns `null`.
366
+ */
367
+ getEndsOfEdge(edge) {
368
+ if (!this.hasEdge(edge.src, edge.dest)) {
377
369
  return null;
378
370
  }
379
- var v1 = this.getVertex(edge.src);
380
- var v2 = this.getVertex(edge.dest);
371
+ const v1 = this._getVertex(edge.src);
372
+ const v2 = this._getVertex(edge.dest);
381
373
  if (v1 && v2) {
382
374
  return [v1, v2];
383
375
  }
384
376
  else {
385
377
  return null;
386
378
  }
387
- };
388
- return DirectedGraph;
389
- }(abstract_graph_1.AbstractGraph));
379
+ }
380
+ /**
381
+ * The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertices exist.
382
+ * @param {E} edge - The parameter `edge` is of type `E`, which represents an edge in a graph. It is the edge that
383
+ * needs to be added to the graph.
384
+ * @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
385
+ * source or destination vertex does not exist in the graph.
386
+ */
387
+ _addEdgeOnly(edge) {
388
+ if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
389
+ return false;
390
+ }
391
+ const srcVertex = this._getVertex(edge.src);
392
+ const destVertex = this._getVertex(edge.dest);
393
+ // TODO after no-non-null-assertion not ensure the logic
394
+ if (srcVertex && destVertex) {
395
+ const srcOutEdges = this._outEdgeMap.get(srcVertex);
396
+ if (srcOutEdges) {
397
+ srcOutEdges.push(edge);
398
+ }
399
+ else {
400
+ this._outEdgeMap.set(srcVertex, [edge]);
401
+ }
402
+ const destInEdges = this._inEdgeMap.get(destVertex);
403
+ if (destInEdges) {
404
+ destInEdges.push(edge);
405
+ }
406
+ else {
407
+ this._inEdgeMap.set(destVertex, [edge]);
408
+ }
409
+ return true;
410
+ }
411
+ else {
412
+ return false;
413
+ }
414
+ }
415
+ _setOutEdgeMap(value) {
416
+ this._outEdgeMap = value;
417
+ }
418
+ _setInEdgeMap(value) {
419
+ this._inEdgeMap = value;
420
+ }
421
+ }
390
422
  exports.DirectedGraph = DirectedGraph;