-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.cpp
More file actions
64 lines (53 loc) · 1.95 KB
/
Copy pathprocessor.cpp
File metadata and controls
64 lines (53 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "processor.h"
#include <iostream>
#include "dependency.h"
std::set<Dependency> Processor::traverseDependencyGraph(Dependency vertex) {
if (isCycleDetected) return visitedVertices;
if (foundDependencies.find(vertex) != foundDependencies.end()) {
return foundDependencies[vertex];
}
std::set<Dependency> res;
if (visitedVertices.find(vertex) != visitedVertices.end()) {
isCycleDetected = true;
return visitedVertices;
}
visitedVertices.insert(vertex);
if (dependencyGraph.find(vertex) != dependencyGraph.end()) {
for (auto& child : dependencyGraph[vertex]) {
res.insert(child);
std::set<Dependency> childsDependencies =
traverseDependencyGraph(child);
for (auto subDependency : childsDependencies) {
res.insert(subDependency);
}
}
}
foundDependencies[vertex] = res;
return res;
}
void Processor::process() {
for (auto vertexPair : dependencyGraph) {
std::set<Dependency> childDependencies;
std::set<Dependency> visitedVertices;
if (isCycleDetected) break;
childDependencies = traverseDependencyGraph(vertexPair.first);
std::vector<Dependency> childDependenciesVector(
childDependencies.size());
copy(childDependencies.begin(), childDependencies.end(),
childDependenciesVector.begin());
resultMap[vertexPair.first] = childDependenciesVector;
}
if (isCycleDetected) {
std::cout << "A Dependency Cycle has been detected\n";
} else {
int verticesCount = outputOrder.size();
for (int i = 0; i < verticesCount; i++) {
Dependency d = Dependency(outputOrder[i]);
std::cout << outputOrder[i] << " depends on ";
for (Dependency sub : resultMap[d]) {
std::cout << sub.dependencyId << " ";
}
std::cout << std::endl;
}
}
}