|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
#include <algorithm>
|
|
|
#include <unordered_map>
|
|
|
#include <unordered_set>
|
|
|
|
|
|
#include <ade/util/filter_range.hpp>
|
|
|
|
|
|
#include <opencv2/gapi/own/assert.hpp>
|
|
|
#include "compiler/passes/helpers.hpp"
|
|
|
|
|
|
namespace {
|
|
|
namespace Cycles
|
|
|
{
|
|
|
|
|
|
|
|
|
enum class TraverseState
|
|
|
{
|
|
|
visiting,
|
|
|
visited,
|
|
|
};
|
|
|
using state_t = std::unordered_map<ade::Node*, TraverseState>;
|
|
|
|
|
|
bool inline checkCycle(state_t& state, const ade::NodeHandle& node)
|
|
|
{
|
|
|
GAPI_Assert(nullptr != node);
|
|
|
state[node.get()] = TraverseState::visiting;
|
|
|
for (auto adj: node->outNodes())
|
|
|
{
|
|
|
auto it = state.find(adj.get());
|
|
|
if (state.end() == it)
|
|
|
{
|
|
|
|
|
|
if (checkCycle(state, adj))
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
else if (TraverseState::visiting == it->second)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
state[node.get()] = TraverseState::visited;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
bool inline hasCycles(const ade::Graph &graph)
|
|
|
{
|
|
|
state_t state;
|
|
|
bool detected = false;
|
|
|
for (auto node: graph.nodes())
|
|
|
{
|
|
|
if (state.end() == state.find(node.get()))
|
|
|
{
|
|
|
|
|
|
detected |= checkCycle(state, node);
|
|
|
if (detected) break;
|
|
|
}
|
|
|
}
|
|
|
return detected;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
namespace TopoSort
|
|
|
{
|
|
|
using sorted_t = std::vector<ade::NodeHandle>;
|
|
|
using visited_t = std::unordered_set<ade::Node*>;
|
|
|
|
|
|
struct NonEmpty final
|
|
|
{
|
|
|
bool operator()(const ade::NodeHandle& node) const
|
|
|
{
|
|
|
return nullptr != node;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
void inline visit(sorted_t& sorted, visited_t& visited, const ade::NodeHandle& node)
|
|
|
{
|
|
|
if (visited.end() == visited.find(node.get()))
|
|
|
{
|
|
|
for (auto adj: node->inNodes())
|
|
|
{
|
|
|
visit(sorted, visited, adj);
|
|
|
}
|
|
|
sorted.push_back(node);
|
|
|
visited.insert(node.get());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sorted_t inline topoSort(const ade::Graph &g)
|
|
|
{
|
|
|
sorted_t sorted;
|
|
|
visited_t visited;
|
|
|
for (auto node: g.nodes())
|
|
|
{
|
|
|
visit(sorted, visited, node);
|
|
|
}
|
|
|
|
|
|
auto r = ade::util::filter<NonEmpty>(ade::util::toRange(sorted));
|
|
|
return sorted_t(r.begin(), r.end());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
bool cv::gimpl::pass_helpers::hasCycles(const ade::Graph &g)
|
|
|
{
|
|
|
return Cycles::hasCycles(g);
|
|
|
}
|
|
|
|
|
|
std::vector<ade::NodeHandle> cv::gimpl::pass_helpers::topoSort(const ade::Graph &g)
|
|
|
{
|
|
|
return TopoSort::topoSort(g);
|
|
|
}
|
|
|
|