archunit
Version:
ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app
99 lines • 2.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrajanSCC = void 0;
class Vertex {
constructor(_id) {
this._id = _id;
this.index = -1;
this.lowlink = -1;
this.neighbours = [];
}
get id() {
return this._id;
}
equals(v) {
return v._id === this._id;
}
}
class TrajanSCC {
constructor() {
this.graph = [];
this.index = 0;
this.stack = [];
this.sccs = [];
this.edges = [];
}
findStronglyConnectedComponents(edges) {
this.init(edges);
this.graph.forEach((vertex) => {
if (vertex.index < 0) {
this.visit(vertex);
}
});
return this.sccs;
}
visit(vertex) {
vertex.index = this.index;
vertex.lowlink = this.index;
this.index = this.index + 1;
this.stack.push(vertex);
for (let i in vertex.neighbours) {
let v = vertex;
let w = this.graph.find((x) => x.id === vertex.neighbours[i]);
if (w.index < 0) {
this.visit(w);
v.lowlink = Math.min(v.lowlink, w.lowlink);
}
else if (this.stack.includes(w)) {
v.lowlink = Math.min(v.lowlink, w.index);
}
}
if (vertex.lowlink === vertex.index) {
let scc = [];
let w = null;
if (this.stack.length > 0) {
do {
w = this.stack.pop();
scc.push(w);
} while (!vertex.equals(w));
}
if (scc.length > 0) {
const sccEdges = [];
this.edges.forEach((edge) => {
if (scc.find((x) => x.id === edge.from) &&
scc.find((x) => x.id === edge.to)) {
sccEdges.push(edge);
}
});
if (sccEdges.length > 0) {
this.sccs.push(sccEdges);
}
}
}
}
init(edges) {
this.edges = edges;
this.graph = [];
edges.forEach((edge) => {
let v = this.graph.find((x) => x.id === edge.from);
if (v) {
if (!v.neighbours.includes(edge.to)) {
v.neighbours.push(edge.to);
}
}
else {
v = new Vertex(edge.from);
v.neighbours.push(edge.to);
this.graph.push(v);
}
if (!this.graph.find((x) => x.id === edge.to)) {
this.graph.push(new Vertex(edge.to));
}
});
this.index = 0;
this.stack = [];
this.sccs = [];
}
}
exports.TrajanSCC = TrajanSCC;
//# sourceMappingURL=trajan-scc.js.map