cmake/Source/cmComputeComponentGraph.cxx

140 lines
4.2 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmComputeComponentGraph.h"
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include <cassert>
2023-05-23 16:38:00 +02:00
#include <cstddef>
#include <limits>
const size_t cmComputeComponentGraph::INVALID_COMPONENT =
std::numeric_limits<size_t>::max();
2016-07-09 11:21:54 +02:00
cmComputeComponentGraph::cmComputeComponentGraph(Graph const& input)
: InputGraph(input)
2021-09-14 00:13:48 +02:00
{
}
cmComputeComponentGraph::~cmComputeComponentGraph() = default;
void cmComputeComponentGraph::Compute()
{
// Identify components.
this->Tarjan();
// Compute the component graph.
this->ComponentGraph.resize(0);
this->ComponentGraph.resize(this->Components.size());
this->TransferEdges();
}
void cmComputeComponentGraph::Tarjan()
{
2023-05-23 16:38:00 +02:00
size_t n = this->InputGraph.size();
2016-07-09 11:21:54 +02:00
TarjanEntry entry = { 0, 0 };
this->TarjanEntries.resize(0);
this->TarjanEntries.resize(n, entry);
this->TarjanComponents.resize(0);
2023-05-23 16:38:00 +02:00
this->TarjanComponents.resize(n, INVALID_COMPONENT);
this->TarjanWalkId = 0;
this->TarjanVisited.resize(0);
this->TarjanVisited.resize(n, 0);
2023-05-23 16:38:00 +02:00
for (size_t i = 0; i < n; ++i) {
// Start a new DFS from this node if it has never been visited.
2016-07-09 11:21:54 +02:00
if (!this->TarjanVisited[i]) {
assert(this->TarjanStack.empty());
++this->TarjanWalkId;
this->TarjanIndex = 0;
this->TarjanVisit(i);
}
2016-07-09 11:21:54 +02:00
}
}
2023-05-23 16:38:00 +02:00
void cmComputeComponentGraph::TarjanVisit(size_t i)
{
// We are now visiting this node.
this->TarjanVisited[i] = this->TarjanWalkId;
// Initialize the entry.
this->TarjanEntries[i].Root = i;
2023-05-23 16:38:00 +02:00
this->TarjanComponents[i] = INVALID_COMPONENT;
this->TarjanEntries[i].VisitIndex = ++this->TarjanIndex;
this->TarjanStack.push(i);
// Follow outgoing edges.
2010-11-13 01:00:53 +02:00
EdgeList const& nl = this->InputGraph[i];
2018-01-26 17:06:56 +01:00
for (cmGraphEdge const& ni : nl) {
2023-05-23 16:38:00 +02:00
size_t j = ni;
// Ignore edges to nodes that have been reached by a previous DFS
// walk. Since we did not reach the current node from that walk
// it must not belong to the same component and it has already
// been assigned to a component.
2016-07-09 11:21:54 +02:00
if (this->TarjanVisited[j] > 0 &&
this->TarjanVisited[j] < this->TarjanWalkId) {
continue;
2016-07-09 11:21:54 +02:00
}
// Visit the destination if it has not yet been visited.
2016-07-09 11:21:54 +02:00
if (!this->TarjanVisited[j]) {
this->TarjanVisit(j);
2016-07-09 11:21:54 +02:00
}
// If the destination has not yet been assigned to a component,
// check if it has a better root for the current object.
2023-05-23 16:38:00 +02:00
if (this->TarjanComponents[j] == INVALID_COMPONENT) {
2016-07-09 11:21:54 +02:00
if (this->TarjanEntries[this->TarjanEntries[j].Root].VisitIndex <
this->TarjanEntries[this->TarjanEntries[i].Root].VisitIndex) {
this->TarjanEntries[i].Root = this->TarjanEntries[j].Root;
}
}
2016-07-09 11:21:54 +02:00
}
// Check if we have found a component.
2016-07-09 11:21:54 +02:00
if (this->TarjanEntries[i].Root == i) {
// Yes. Create it.
2023-05-23 16:38:00 +02:00
size_t c = this->Components.size();
2018-04-23 21:13:27 +02:00
this->Components.emplace_back();
NodeList& component = this->Components[c];
// Populate the component list.
2023-05-23 16:38:00 +02:00
size_t j;
2016-07-09 11:21:54 +02:00
do {
// Get the next member of the component.
j = this->TarjanStack.top();
this->TarjanStack.pop();
// Assign the member to the component.
this->TarjanComponents[j] = c;
this->TarjanEntries[j].Root = i;
// Store the node in its component.
component.push_back(j);
2016-07-09 11:21:54 +02:00
} while (j != i);
// Sort the component members for clarity.
std::sort(component.begin(), component.end());
2016-07-09 11:21:54 +02:00
}
}
void cmComputeComponentGraph::TransferEdges()
{
// Map inter-component edges in the original graph to edges in the
// component graph.
2023-05-23 16:38:00 +02:00
size_t n = this->InputGraph.size();
for (size_t i = 0; i < n; ++i) {
size_t i_component = this->TarjanComponents[i];
2010-11-13 01:00:53 +02:00
EdgeList const& nl = this->InputGraph[i];
2018-01-26 17:06:56 +01:00
for (cmGraphEdge const& ni : nl) {
2023-05-23 16:38:00 +02:00
size_t j = ni;
size_t j_component = this->TarjanComponents[j];
2016-07-09 11:21:54 +02:00
if (i_component != j_component) {
2010-11-13 01:00:53 +02:00
// We do not attempt to combine duplicate edges, but instead
// store the inter-component edges with suitable multiplicity.
2019-11-11 23:01:05 +01:00
this->ComponentGraph[i_component].emplace_back(
2020-08-30 11:54:41 +02:00
j_component, ni.IsStrong(), ni.IsCross(), ni.GetBacktrace());
}
}
2016-07-09 11:21:54 +02:00
}
}