1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
6 #include "passes/check_cycles.hpp"
8 #include <unordered_map>
10 #include "util/assert.hpp"
11 #include "util/map_range.hpp"
20 enum class TraverseState
26 using state_t = std::unordered_map<Node*, TraverseState>;
28 static void visit(state_t& state, const NodeHandle& node)
30 ASSERT(nullptr != node);
31 state[node.get()] = TraverseState::visiting;
33 util::map(node->outEdges(), [](const EdgeHandle& e) { return e->dstNode(); }))
35 auto it = state.find(adj.get());
36 if (state.end() == it) // not visited
40 else if (TraverseState::visiting == it->second)
42 throw_error(CycleFound());
45 state[node.get()] = TraverseState::visited;
49 void CheckCycles::operator()(const PassContext& context) const
52 for (auto node: context.graph.nodes())
54 if (state.end() == state.find(node.get()))
56 // not yet visited during recursion
62 std::string CheckCycles::name()
67 const char* CycleFound::what() const noexcept
69 return "Cycle was detected in graph";