data-structure-typed 0.8.18 → 0.9.16

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 (213) hide show
  1. package/.idea/modules.xml +1 -1
  2. package/README.md +197 -2
  3. package/dist/data-structures/binary-tree/aa-tree.js +5 -2
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -5
  5. package/dist/data-structures/binary-tree/avl-tree.js +93 -46
  6. package/dist/data-structures/binary-tree/b-tree.js +5 -2
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -1
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +14 -13
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +20 -32
  10. package/dist/data-structures/binary-tree/binary-tree.js +480 -370
  11. package/dist/data-structures/binary-tree/bst.d.ts +4 -8
  12. package/dist/data-structures/binary-tree/bst.js +152 -107
  13. package/dist/data-structures/binary-tree/rb-tree.js +5 -2
  14. package/dist/data-structures/binary-tree/segment-tree.d.ts +3 -3
  15. package/dist/data-structures/binary-tree/segment-tree.js +91 -61
  16. package/dist/data-structures/binary-tree/splay-tree.js +5 -2
  17. package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -5
  18. package/dist/data-structures/binary-tree/tree-multiset.js +31 -11
  19. package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
  20. package/dist/data-structures/graph/abstract-graph.d.ts +5 -33
  21. package/dist/data-structures/graph/abstract-graph.js +546 -311
  22. package/dist/data-structures/graph/directed-graph.d.ts +5 -13
  23. package/dist/data-structures/graph/directed-graph.js +250 -128
  24. package/dist/data-structures/graph/undirected-graph.d.ts +4 -3
  25. package/dist/data-structures/graph/undirected-graph.js +166 -81
  26. package/dist/data-structures/hash/coordinate-map.d.ts +1 -1
  27. package/dist/data-structures/hash/coordinate-map.js +38 -20
  28. package/dist/data-structures/hash/coordinate-set.js +33 -15
  29. package/dist/data-structures/hash/index.d.ts +5 -0
  30. package/dist/data-structures/hash/index.js +5 -0
  31. package/dist/data-structures/heap/heap.d.ts +2 -8
  32. package/dist/data-structures/heap/heap.js +36 -31
  33. package/dist/data-structures/heap/max-heap.d.ts +3 -2
  34. package/dist/data-structures/heap/max-heap.js +27 -9
  35. package/dist/data-structures/heap/min-heap.d.ts +3 -2
  36. package/dist/data-structures/heap/min-heap.js +27 -9
  37. package/dist/data-structures/index.d.ts +1 -0
  38. package/dist/data-structures/index.js +1 -0
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -1
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +56 -53
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +8 -12
  42. package/dist/data-structures/linked-list/singly-linked-list.js +308 -174
  43. package/dist/data-structures/matrix/matrix.js +8 -7
  44. package/dist/data-structures/matrix/matrix2d.d.ts +5 -5
  45. package/dist/data-structures/matrix/matrix2d.js +80 -63
  46. package/dist/data-structures/matrix/navigator.d.ts +2 -16
  47. package/dist/data-structures/matrix/navigator.js +37 -18
  48. package/dist/data-structures/matrix/vector2d.d.ts +18 -18
  49. package/dist/data-structures/matrix/vector2d.js +117 -94
  50. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -2
  51. package/dist/data-structures/priority-queue/max-priority-queue.js +25 -8
  52. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -2
  53. package/dist/data-structures/priority-queue/min-priority-queue.js +25 -8
  54. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -21
  55. package/dist/data-structures/priority-queue/priority-queue.js +159 -116
  56. package/dist/data-structures/queue/deque.js +82 -56
  57. package/dist/data-structures/queue/queue.d.ts +9 -10
  58. package/dist/data-structures/queue/queue.js +34 -34
  59. package/dist/data-structures/stack/stack.d.ts +9 -10
  60. package/dist/data-structures/stack/stack.js +31 -31
  61. package/dist/data-structures/trampoline.d.ts +14 -23
  62. package/dist/data-structures/trampoline.js +103 -25
  63. package/dist/data-structures/trie/trie.d.ts +13 -3
  64. package/dist/data-structures/trie/trie.js +234 -80
  65. package/dist/data-structures/types/abstract-graph.d.ts +29 -0
  66. package/dist/data-structures/types/abstract-graph.js +2 -0
  67. package/dist/data-structures/types/avl-tree.d.ts +5 -0
  68. package/dist/data-structures/types/avl-tree.js +2 -0
  69. package/dist/data-structures/types/binary-tree.d.ts +16 -0
  70. package/dist/data-structures/types/binary-tree.js +2 -0
  71. package/dist/data-structures/types/bst.d.ts +7 -0
  72. package/dist/data-structures/types/bst.js +2 -0
  73. package/dist/data-structures/types/directed-graph.d.ts +10 -0
  74. package/dist/data-structures/types/directed-graph.js +2 -0
  75. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
  76. package/dist/data-structures/types/doubly-linked-list.js +2 -0
  77. package/dist/data-structures/types/heap.d.ts +7 -0
  78. package/dist/data-structures/types/heap.js +2 -0
  79. package/dist/data-structures/types/index.d.ts +13 -0
  80. package/dist/data-structures/types/index.js +29 -0
  81. package/dist/data-structures/types/navigator.d.ts +14 -0
  82. package/dist/data-structures/types/navigator.js +2 -0
  83. package/dist/data-structures/types/priority-queue.d.ts +7 -0
  84. package/dist/data-structures/types/priority-queue.js +2 -0
  85. package/dist/data-structures/types/segment-tree.d.ts +1 -0
  86. package/dist/data-structures/types/segment-tree.js +2 -0
  87. package/dist/data-structures/types/singly-linked-list.d.ts +5 -0
  88. package/dist/data-structures/types/singly-linked-list.js +2 -0
  89. package/dist/data-structures/types/tree-multiset.d.ts +5 -0
  90. package/dist/data-structures/types/tree-multiset.js +2 -0
  91. package/dist/{types → data-structures/types}/utils.d.ts +7 -1
  92. package/dist/{types → data-structures/types}/utils.js +20 -19
  93. package/dist/{utils.d.ts → utils/utils.d.ts} +6 -23
  94. package/dist/utils/utils.js +651 -0
  95. package/package.json +20 -42
  96. package/src/data-structures/binary-tree/avl-tree.ts +1 -6
  97. package/src/data-structures/binary-tree/binary-indexed-tree.ts +4 -4
  98. package/src/data-structures/binary-tree/binary-tree.ts +184 -139
  99. package/src/data-structures/binary-tree/bst.ts +15 -24
  100. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  101. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  102. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  103. package/src/data-structures/binary-tree/segment-tree.ts +20 -12
  104. package/src/data-structures/binary-tree/tree-multiset.ts +1 -4
  105. package/src/data-structures/diagrams/README.md +7 -0
  106. package/src/data-structures/graph/abstract-graph.ts +58 -94
  107. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  108. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  109. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  110. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  111. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  112. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  113. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  114. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  115. package/src/data-structures/graph/diagrams/mst.png +0 -0
  116. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  117. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  118. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  119. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  120. package/src/data-structures/graph/directed-graph.ts +12 -28
  121. package/src/data-structures/graph/undirected-graph.ts +11 -10
  122. package/src/data-structures/hash/coordinate-map.ts +1 -1
  123. package/src/data-structures/hash/index.ts +5 -0
  124. package/src/data-structures/heap/heap.ts +2 -11
  125. package/src/data-structures/heap/max-heap.ts +3 -2
  126. package/src/data-structures/heap/min-heap.ts +3 -2
  127. package/src/data-structures/index.ts +1 -0
  128. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -9
  129. package/src/data-structures/linked-list/singly-linked-list.ts +18 -32
  130. package/src/data-structures/matrix/matrix2d.ts +11 -11
  131. package/src/data-structures/matrix/navigator.ts +2 -14
  132. package/src/data-structures/matrix/vector2d.ts +52 -52
  133. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  134. package/src/data-structures/priority-queue/min-priority-queue.ts +4 -3
  135. package/src/data-structures/priority-queue/priority-queue.ts +70 -78
  136. package/src/data-structures/queue/deque.ts +2 -2
  137. package/src/data-structures/queue/queue.ts +12 -13
  138. package/src/data-structures/stack/stack.ts +12 -13
  139. package/src/data-structures/trampoline.ts +31 -71
  140. package/src/data-structures/trie/trie.ts +61 -11
  141. package/src/data-structures/types/abstract-graph.ts +51 -0
  142. package/src/data-structures/types/avl-tree.ts +6 -0
  143. package/src/data-structures/types/binary-tree.ts +15 -0
  144. package/src/data-structures/types/bst.ts +5 -0
  145. package/src/data-structures/types/directed-graph.ts +18 -0
  146. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  147. package/src/data-structures/types/heap.ts +8 -0
  148. package/src/data-structures/types/index.ts +13 -0
  149. package/src/data-structures/types/navigator.ts +12 -0
  150. package/src/data-structures/types/priority-queue.ts +9 -0
  151. package/src/data-structures/types/segment-tree.ts +1 -0
  152. package/src/data-structures/types/singly-linked-list.ts +15 -0
  153. package/src/data-structures/types/tree-multiset.ts +3 -0
  154. package/src/{types → data-structures/types}/utils.ts +20 -5
  155. package/src/utils/index.ts +1 -0
  156. package/src/{utils.ts → utils/utils.ts} +32 -132
  157. package/tsconfig.json +9 -6
  158. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  159. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  160. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  161. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  162. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  163. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  164. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  165. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  166. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  167. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  168. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  169. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  170. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  171. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  172. package/dist/types/data-structures/graph/index.d.ts +0 -3
  173. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  174. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  175. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  176. package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
  177. package/dist/types/data-structures/hash/index.d.ts +0 -1
  178. package/dist/types/data-structures/hash/pair.d.ts +0 -1
  179. package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
  180. package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
  181. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  182. package/dist/types/data-structures/heap/index.d.ts +0 -3
  183. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  184. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  185. package/dist/types/data-structures/index.d.ts +0 -9
  186. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  187. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  188. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  189. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
  190. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  191. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  192. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  193. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  194. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  195. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  196. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  197. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  198. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  199. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  200. package/dist/types/data-structures/queue/index.d.ts +0 -1
  201. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  202. package/dist/types/data-structures/stack/index.d.ts +0 -1
  203. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  204. package/dist/types/data-structures/trampoline.d.ts +0 -25
  205. package/dist/types/data-structures/trie/index.d.ts +0 -1
  206. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  207. package/dist/types/types/index.d.ts +0 -1
  208. package/dist/types/types/utils.d.ts +0 -46
  209. package/dist/utils.js +0 -569
  210. package/src/types/index.ts +0 -1
  211. package/src/types/patches/index.d.ts +0 -0
  212. /package/dist/{types → utils}/index.d.ts +0 -0
  213. /package/dist/{types → utils}/index.js +0 -0
@@ -1,4 +1,5 @@
1
- import { AbstractEdge, AbstractGraph, AbstractVertex, VertexId } from './abstract-graph';
1
+ import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
2
+ import type { IDirectedGraph, VertexId } from '../types';
2
3
  export declare class DirectedVertex extends AbstractVertex {
3
4
  constructor(id: VertexId);
4
5
  }
@@ -11,16 +12,7 @@ export declare class DirectedEdge extends AbstractEdge {
11
12
  get dest(): VertexId;
12
13
  set dest(v: VertexId);
13
14
  }
14
- export interface I_DirectedGraph<V, E> {
15
- incomingEdgesOf(vertex: V): E[];
16
- outgoingEdgesOf(vertex: V): E[];
17
- inDegreeOf(vertexOrId: V | VertexId): number;
18
- outDegreeOf(vertexOrId: V | VertexId): number;
19
- getEdgeSrc(e: E): V | null;
20
- getEdgeDest(e: E): V | null;
21
- }
22
- export type TopologicalStatus = 0 | 1 | 2;
23
- export declare class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements I_DirectedGraph<V, E> {
15
+ export declare class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements IDirectedGraph<V, E> {
24
16
  protected _outEdgeMap: Map<V, E[]>;
25
17
  protected _inEdgeMap: Map<V, E[]>;
26
18
  constructor();
@@ -37,13 +29,13 @@ export declare class DirectedGraph<V extends DirectedVertex, E extends DirectedE
37
29
  edgesOf(vertexOrId: VertexId | V): E[];
38
30
  getEdgeSrc(e: E): V | null;
39
31
  getEdgeDest(e: E): V | null;
40
- getDestinations(vertex: V | null): V[];
32
+ getDestinations(vertex: V | VertexId | null): V[];
41
33
  /**--- start find cycles --- */
42
34
  /**
43
35
  * when stored with adjacency list time: O(V+E)
44
36
  * when stored with adjacency matrix time: O(V^2)
45
37
  */
46
- topologicalSort(): V[] | null;
38
+ topologicalSort(): (V | VertexId)[] | null;
47
39
  /**--- end find cycles --- */
48
40
  edgeSet(): E[];
49
41
  getNeighbors(vertexOrId: V | VertexId): V[];
@@ -1,71 +1,137 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = void 0;
4
- const utils_1 = require("../../utils");
5
- const abstract_graph_1 = require("./abstract-graph");
6
- class DirectedVertex extends abstract_graph_1.AbstractVertex {
7
- constructor(id) {
8
- super(id);
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);
9
23
  }
10
- }
11
- exports.DirectedVertex = DirectedVertex;
12
- class DirectedEdge extends abstract_graph_1.AbstractEdge {
13
- constructor(src, dest, weight) {
14
- super(weight);
15
- this._src = src;
16
- this._dest = dest;
17
- }
18
- get src() {
19
- return this._src;
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; }
20
30
  }
21
- set src(v) {
22
- this._src = v;
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
+ }
23
39
  }
24
- get dest() {
25
- return this._dest;
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
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ 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;
26
61
  }
27
- set dest(v) {
28
- this._dest = v;
62
+ return DirectedVertex;
63
+ }(abstract_graph_1.AbstractVertex));
64
+ 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;
29
72
  }
30
- }
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));
31
95
  exports.DirectedEdge = DirectedEdge;
32
96
  // Strongly connected, One direction connected, Weakly connected
33
- class DirectedGraph extends abstract_graph_1.AbstractGraph {
34
- constructor() {
35
- super();
36
- this._outEdgeMap = new Map();
37
- this._inEdgeMap = new Map();
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;
38
104
  }
39
- getEdge(srcOrId, destOrId) {
40
- let edges = [];
105
+ DirectedGraph.prototype.getEdge = function (srcOrId, destOrId) {
106
+ var edges = [];
41
107
  if (srcOrId !== null && destOrId !== null) {
42
- const src = this.getVertex(srcOrId);
43
- const dest = this.getVertex(destOrId);
44
- if (src && dest) {
45
- const srcOutEdges = this._outEdgeMap.get(src);
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);
46
112
  if (srcOutEdges) {
47
- edges = srcOutEdges.filter(edge => edge.dest === dest.id);
113
+ edges = srcOutEdges.filter(function (edge) { return edge.dest === dest_1.id; });
48
114
  }
49
115
  }
50
116
  }
51
117
  return edges[0] || null;
52
- }
53
- addEdge(edge) {
118
+ };
119
+ DirectedGraph.prototype.addEdge = function (edge) {
54
120
  if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
55
121
  return false;
56
122
  }
57
- const srcVertex = this.getVertex(edge.src);
58
- const destVertex = this.getVertex(edge.dest);
123
+ var srcVertex = this.getVertex(edge.src);
124
+ var destVertex = this.getVertex(edge.dest);
59
125
  // TODO after no-non-null-assertion not ensure the logic
60
126
  if (srcVertex && destVertex) {
61
- const srcOutEdges = this._outEdgeMap.get(srcVertex);
127
+ var srcOutEdges = this._outEdgeMap.get(srcVertex);
62
128
  if (srcOutEdges) {
63
129
  srcOutEdges.push(edge);
64
130
  }
65
131
  else {
66
132
  this._outEdgeMap.set(srcVertex, [edge]);
67
133
  }
68
- const destInEdges = this._inEdgeMap.get(destVertex);
134
+ var destInEdges = this._inEdgeMap.get(destVertex);
69
135
  if (destInEdges) {
70
136
  destInEdges.push(edge);
71
137
  }
@@ -77,95 +143,108 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
77
143
  else {
78
144
  return false;
79
145
  }
80
- }
81
- removeEdgeBetween(srcOrId, destOrId) {
82
- const src = this.getVertex(srcOrId);
83
- const dest = this.getVertex(destOrId);
84
- let removed = null;
146
+ };
147
+ DirectedGraph.prototype.removeEdgeBetween = function (srcOrId, destOrId) {
148
+ var src = this.getVertex(srcOrId);
149
+ var dest = this.getVertex(destOrId);
150
+ var removed = null;
85
151
  if (!src || !dest) {
86
152
  return null;
87
153
  }
88
- const srcOutEdges = this._outEdgeMap.get(src);
154
+ var srcOutEdges = this._outEdgeMap.get(src);
89
155
  if (srcOutEdges) {
90
- (0, utils_1.arrayRemove)(srcOutEdges, edge => edge.dest === dest.id);
156
+ (0, utils_1.arrayRemove)(srcOutEdges, function (edge) { return edge.dest === dest.id; });
91
157
  }
92
- const destInEdges = this._inEdgeMap.get(dest);
158
+ var destInEdges = this._inEdgeMap.get(dest);
93
159
  if (destInEdges) {
94
- removed = (0, utils_1.arrayRemove)(destInEdges, edge => edge.src === src.id)[0] || null;
160
+ removed = (0, utils_1.arrayRemove)(destInEdges, function (edge) { return edge.src === src.id; })[0] || null;
95
161
  }
96
162
  return removed;
97
- }
98
- removeEdge(edge) {
99
- let removed = null;
100
- const src = this.getVertex(edge.src);
101
- const dest = this.getVertex(edge.dest);
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);
102
168
  if (src && dest) {
103
- const srcOutEdges = this._outEdgeMap.get(src);
169
+ var srcOutEdges = this._outEdgeMap.get(src);
104
170
  if (srcOutEdges && srcOutEdges.length > 0) {
105
- (0, utils_1.arrayRemove)(srcOutEdges, edge => edge.src === src.id);
171
+ (0, utils_1.arrayRemove)(srcOutEdges, function (edge) { return edge.src === src.id; });
106
172
  }
107
- const destInEdges = this._inEdgeMap.get(dest);
173
+ var destInEdges = this._inEdgeMap.get(dest);
108
174
  if (destInEdges && destInEdges.length > 0) {
109
- removed = (0, utils_1.arrayRemove)(destInEdges, edge => edge.dest === dest.id)[0];
175
+ removed = (0, utils_1.arrayRemove)(destInEdges, function (edge) { return edge.dest === dest.id; })[0];
110
176
  }
111
177
  }
112
178
  return removed;
113
- }
114
- removeAllEdges(src, dest) {
179
+ };
180
+ DirectedGraph.prototype.removeAllEdges = function (src, dest) {
115
181
  return [];
116
- }
117
- incomingEdgesOf(vertexOrId) {
118
- const target = this.getVertex(vertexOrId);
182
+ };
183
+ DirectedGraph.prototype.incomingEdgesOf = function (vertexOrId) {
184
+ var target = this.getVertex(vertexOrId);
119
185
  if (target) {
120
186
  return this._inEdgeMap.get(target) || [];
121
187
  }
122
188
  return [];
123
- }
124
- outgoingEdgesOf(vertexOrId) {
125
- const target = this.getVertex(vertexOrId);
189
+ };
190
+ DirectedGraph.prototype.outgoingEdgesOf = function (vertexOrId) {
191
+ var target = this.getVertex(vertexOrId);
126
192
  if (target) {
127
193
  return this._outEdgeMap.get(target) || [];
128
194
  }
129
195
  return [];
130
- }
131
- degreeOf(vertexOrId) {
196
+ };
197
+ DirectedGraph.prototype.degreeOf = function (vertexOrId) {
132
198
  return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
133
- }
134
- inDegreeOf(vertexOrId) {
199
+ };
200
+ DirectedGraph.prototype.inDegreeOf = function (vertexOrId) {
135
201
  return this.incomingEdgesOf(vertexOrId).length;
136
- }
137
- outDegreeOf(vertexOrId) {
202
+ };
203
+ DirectedGraph.prototype.outDegreeOf = function (vertexOrId) {
138
204
  return this.outgoingEdgesOf(vertexOrId).length;
139
- }
140
- edgesOf(vertexOrId) {
141
- return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
142
- }
143
- getEdgeSrc(e) {
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) {
144
210
  return this.getVertex(e.src);
145
- }
146
- getEdgeDest(e) {
211
+ };
212
+ DirectedGraph.prototype.getEdgeDest = function (e) {
147
213
  return this.getVertex(e.dest);
148
- }
149
- getDestinations(vertex) {
214
+ };
215
+ DirectedGraph.prototype.getDestinations = function (vertex) {
216
+ var e_1, _a;
150
217
  if (vertex === null) {
151
218
  return [];
152
219
  }
153
- const destinations = [];
154
- const outgoingEdges = this.outgoingEdgesOf(vertex);
155
- for (const outEdge of outgoingEdges) {
156
- const child = this.getEdgeDest(outEdge);
157
- if (child) {
158
- destinations.push(child);
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);
159
235
  }
236
+ finally { if (e_1) throw e_1.error; }
160
237
  }
161
238
  return destinations;
162
- }
239
+ };
163
240
  /**--- start find cycles --- */
164
241
  /**
165
242
  * when stored with adjacency list time: O(V+E)
166
243
  * when stored with adjacency matrix time: O(V^2)
167
244
  */
168
- topologicalSort() {
245
+ DirectedGraph.prototype.topologicalSort = function () {
246
+ var e_2, _a, e_3, _b;
247
+ var _this = this;
169
248
  // vector<vector<int>> g;
170
249
  // vector<int> color;
171
250
  // int last;
@@ -197,72 +276,115 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
197
276
  // }
198
277
  // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
199
278
  // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
200
- const statusMap = new Map();
201
- for (const entry of this._vertices) {
202
- statusMap.set(entry[1], 0);
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; }
203
292
  }
204
- const sorted = [];
205
- let hasCycle = false;
206
- const dfs = (cur) => {
293
+ var sorted = [];
294
+ var hasCycle = false;
295
+ var dfs = function (cur) {
296
+ var e_4, _a;
207
297
  statusMap.set(cur, 1);
208
- const children = this.getDestinations(cur);
209
- for (const child of children) {
210
- const childStatus = statusMap.get(child);
211
- if (childStatus === 0) {
212
- dfs(child);
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
+ }
213
309
  }
214
- else if (childStatus === 1) {
215
- hasCycle = true;
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);
216
315
  }
316
+ finally { if (e_4) throw e_4.error; }
217
317
  }
218
318
  statusMap.set(cur, 2);
219
319
  sorted.push(cur);
220
320
  };
221
- for (const entry of this._vertices) {
222
- if (statusMap.get(entry[1]) === 0) {
223
- dfs(entry[1]);
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
+ }
224
327
  }
225
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);
333
+ }
334
+ finally { if (e_3) throw e_3.error; }
335
+ }
226
336
  if (hasCycle) {
227
337
  return null;
228
338
  }
229
339
  return sorted.reverse();
230
- }
340
+ };
231
341
  /**--- end find cycles --- */
232
- edgeSet() {
233
- let edges = [];
234
- this._outEdgeMap.forEach(outEdges => {
235
- edges = [...edges, ...outEdges];
342
+ DirectedGraph.prototype.edgeSet = function () {
343
+ var edges = [];
344
+ this._outEdgeMap.forEach(function (outEdges) {
345
+ edges = __spreadArray(__spreadArray([], __read(edges), false), __read(outEdges), false);
236
346
  });
237
347
  return edges;
238
- }
239
- getNeighbors(vertexOrId) {
240
- const neighbors = [];
241
- const vertex = this.getVertex(vertexOrId);
348
+ };
349
+ DirectedGraph.prototype.getNeighbors = function (vertexOrId) {
350
+ var e_5, _a;
351
+ var neighbors = [];
352
+ var vertex = this.getVertex(vertexOrId);
242
353
  if (vertex) {
243
- const outEdges = this.outgoingEdgesOf(vertex);
244
- for (const outEdge of outEdges) {
245
- const neighbor = this.getVertex(outEdge.dest);
246
- // TODO after no-non-null-assertion not ensure the logic
247
- if (neighbor) {
248
- neighbors.push(neighbor);
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);
249
369
  }
370
+ finally { if (e_5) throw e_5.error; }
250
371
  }
251
372
  }
252
373
  return neighbors;
253
- }
254
- getEndsOfEdge(edge) {
374
+ };
375
+ DirectedGraph.prototype.getEndsOfEdge = function (edge) {
255
376
  if (!this.containsEdge(edge.src, edge.dest)) {
256
377
  return null;
257
378
  }
258
- const v1 = this.getVertex(edge.src);
259
- const v2 = this.getVertex(edge.dest);
379
+ var v1 = this.getVertex(edge.src);
380
+ var v2 = this.getVertex(edge.dest);
260
381
  if (v1 && v2) {
261
382
  return [v1, v2];
262
383
  }
263
384
  else {
264
385
  return null;
265
386
  }
266
- }
267
- }
387
+ };
388
+ return DirectedGraph;
389
+ }(abstract_graph_1.AbstractGraph));
268
390
  exports.DirectedGraph = DirectedGraph;
@@ -1,16 +1,17 @@
1
- import { AbstractEdge, AbstractGraph, AbstractVertex, VertexId } from './abstract-graph';
1
+ import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
2
+ import type { VertexId } from '../types';
2
3
  export declare class UndirectedVertex extends AbstractVertex {
3
4
  constructor(id: VertexId);
4
5
  }
5
6
  export declare class UndirectedEdge extends AbstractEdge {
7
+ constructor(v1: VertexId, v2: VertexId, weight?: number);
6
8
  private _vertices;
7
9
  get vertices(): [VertexId, VertexId];
8
10
  set vertices(v: [VertexId, VertexId]);
9
- constructor(v1: VertexId, v2: VertexId, weight?: number);
10
11
  }
11
12
  export declare class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
12
- constructor();
13
13
  protected _edges: Map<V, E[]>;
14
+ constructor();
14
15
  getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null;
15
16
  addEdge(edge: E): boolean;
16
17
  removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null;