From b3436d23d8d1826778eb00c2b263258b4abf675e Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Fri, 2 Dec 2022 14:17:09 +0000 Subject: [PATCH] Rename PluginGraph::EdgeCreatesCycle It's really got nothing to do with cycles. --- src/api/sorting/plugin_graph.cpp | 39 +++++++++++++++----------------- src/api/sorting/plugin_graph.h | 2 +- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/api/sorting/plugin_graph.cpp b/src/api/sorting/plugin_graph.cpp index fe85acc8..ce67d0ab 100644 --- a/src/api/sorting/plugin_graph.cpp +++ b/src/api/sorting/plugin_graph.cpp @@ -304,36 +304,33 @@ void PluginGraph::CheckForCycles() const { graph_, visitor(CycleDetector()).vertex_index_map(vertexIndexMap)); } -bool PluginGraph::EdgeCreatesCycle(const vertex_t& fromVertex, - const vertex_t& toVertex) { - if (pathsCache_.count(GraphPath{toVertex, fromVertex}) != 0) { +bool PluginGraph::PathExists(const vertex_t& fromVertex, + const vertex_t& toVertex) { + if (pathsCache_.count(GraphPath{fromVertex, toVertex}) != 0) { return true; } - auto start = toVertex; - auto end = fromVertex; - std::queue forwardQueue; std::queue reverseQueue; std::unordered_set forwardVisited; std::unordered_set reverseVisited; - forwardQueue.push(start); - forwardVisited.insert(start); - reverseQueue.push(end); - reverseVisited.insert(end); + forwardQueue.push(fromVertex); + forwardVisited.insert(fromVertex); + reverseQueue.push(toVertex); + reverseVisited.insert(toVertex); while (!forwardQueue.empty() && !reverseQueue.empty()) { if (!forwardQueue.empty()) { - auto v = forwardQueue.front(); + const auto v = forwardQueue.front(); forwardQueue.pop(); - if (v == end || reverseVisited.count(v) > 0) { + if (v == toVertex || reverseVisited.count(v) > 0) { return true; } - for (auto adjacentV : + for (const auto adjacentV : boost::make_iterator_range(boost::adjacent_vertices(v, graph_))) { if (forwardVisited.count(adjacentV) == 0) { - pathsCache_.insert(GraphPath{start, adjacentV}); + pathsCache_.insert(GraphPath{fromVertex, adjacentV}); forwardVisited.insert(adjacentV); forwardQueue.push(adjacentV); @@ -341,15 +338,15 @@ bool PluginGraph::EdgeCreatesCycle(const vertex_t& fromVertex, } } if (!reverseQueue.empty()) { - auto v = reverseQueue.front(); + const auto v = reverseQueue.front(); reverseQueue.pop(); - if (v == start || forwardVisited.count(v) > 0) { + if (v == fromVertex || forwardVisited.count(v) > 0) { return true; } - for (auto adjacentV : boost::make_iterator_range( + for (const auto adjacentV : boost::make_iterator_range( boost::inv_adjacent_vertices(v, graph_))) { if (reverseVisited.count(adjacentV) == 0) { - pathsCache_.insert(GraphPath{adjacentV, end}); + pathsCache_.insert(GraphPath{adjacentV, toVertex}); reverseVisited.insert(adjacentV); reverseQueue.push(adjacentV); @@ -605,7 +602,7 @@ void PluginGraph::AddGroupEdges( continue; } - if (EdgeCreatesCycle(parentVertex.value(), vertex)) { + if (PathExists(vertex, parentVertex.value())) { auto& fromPlugin = graph_[parentVertex.value()]; auto& toPlugin = graph_[vertex]; @@ -708,7 +705,7 @@ void PluginGraph::AddOverlapEdges() { thisPluginOverridesMoreFormIDs ? vertex : otherVertex; vertex_t toVertex = thisPluginOverridesMoreFormIDs ? otherVertex : vertex; - if (!EdgeCreatesCycle(fromVertex, toVertex)) + if (!PathExists(toVertex, fromVertex)) AddEdge(fromVertex, toVertex, EdgeType::overlap); } } @@ -773,7 +770,7 @@ void PluginGraph::AddTieBreakEdges() { vertex_t fromVertex = thisPluginShouldLoadEarlier ? vertex : otherVertex; vertex_t toVertex = thisPluginShouldLoadEarlier ? otherVertex : vertex; - if (!EdgeCreatesCycle(fromVertex, toVertex)) + if (!PathExists(toVertex, fromVertex)) AddEdge(fromVertex, toVertex, EdgeType::tieBreak); } } diff --git a/src/api/sorting/plugin_graph.h b/src/api/sorting/plugin_graph.h index 41c9831e..e1147fc9 100644 --- a/src/api/sorting/plugin_graph.h +++ b/src/api/sorting/plugin_graph.h @@ -90,7 +90,7 @@ public: private: std::optional GetVertexByName(const std::string& name) const; - bool EdgeCreatesCycle(const vertex_t& u, const vertex_t& v); + bool PathExists(const vertex_t& fromVertex, const vertex_t& toVertex); void AddEdge(const vertex_t& fromVertex, const vertex_t& toVertex,