Internals: Color densely in V3Graph::weaklyConnected, and return the count

No algorithm depends on this, so can be changed, later improvement will.
No functional change.
This commit is contained in:
Geza Lore
2026-09-09 21:11:23 +01:00
parent 5bf74e34b1
commit 1d2781091c
4 changed files with 17 additions and 17 deletions
-5
View File
@@ -198,11 +198,6 @@ void V3Graph::userClearEdges() {
}
}
void V3Graph::clearColors() {
// Reset colors
for (V3GraphVertex& vertex : vertices()) vertex.color(0);
}
//======================================================================
// Dumping
+2 -4
View File
@@ -364,12 +364,10 @@ public:
// METHODS - ALGORITHMS
/// Clears color
void clearColors() VL_MT_DISABLED;
/// Assign same color to all vertices in the same weakly connected component
/// Thus different color if there's no edges between the two subgraphs
void weaklyConnected(V3EdgeFuncP edgeFuncp) VL_MT_DISABLED;
/// Colors are assigned densely, as 0 .. n-1, and 'n' is returned
uint32_t weaklyConnected(V3EdgeFuncP edgeFuncp) VL_MT_DISABLED;
/// Assign same color to all vertices that are strongly connected
/// Thus different color if there's no directional circuit within the subgraphs.
+15 -7
View File
@@ -27,6 +27,7 @@
#include "V3Stats.h"
#include <algorithm>
#include <limits>
#include <list>
#include <map>
#include <numeric>
@@ -135,21 +136,24 @@ void V3Graph::removeTransitiveEdges() { GraphAlgRemoveTransitiveEdges{this}.go()
// Changes color()
class GraphAlgWeakly final : GraphAlg<> {
// Sentinel color, meaning not colored yet. Colors themselves are 0 .. m_numColors-1.
static constexpr uint32_t UNCOLORED = std::numeric_limits<uint32_t>::max();
uint32_t m_numColors = 0; // Number of colors assigned
void main() {
// Initialize state
m_graphp->clearColors();
// Color graph
uint32_t currentColor = 0;
for (V3GraphVertex& vertex : m_graphp->vertices()) vertex.color(UNCOLORED);
// Color graph, without gaps
for (V3GraphVertex& vertex : m_graphp->vertices()) {
currentColor++;
vertexIterate(&vertex, currentColor);
if (vertex.color() == UNCOLORED) vertexIterate(&vertex, m_numColors++);
}
}
void vertexIterate(V3GraphVertex* vertexp, uint32_t currentColor) {
// Assign new color to each unvisited node
// then visit each of its edges, giving them the same color
if (vertexp->color()) return; // Already colored it
if (vertexp->color() != UNCOLORED) return; // Already colored it
vertexp->color(currentColor);
for (V3GraphEdge& edge : vertexp->outEdges()) {
if (followEdge(&edge)) vertexIterate(edge.top(), currentColor);
@@ -165,9 +169,13 @@ public:
main();
}
~GraphAlgWeakly() = default;
uint32_t numColors() const { return m_numColors; }
};
void V3Graph::weaklyConnected(V3EdgeFuncP edgeFuncp) { GraphAlgWeakly{this, edgeFuncp}; }
uint32_t V3Graph::weaklyConnected(V3EdgeFuncP edgeFuncp) {
return GraphAlgWeakly{this, edgeFuncp}.numColors();
}
//######################################################################
//######################################################################
-1
View File
@@ -293,7 +293,6 @@ class ReorderVisitor final : public VNVisitor {
for (AstNode* currp = nodep; currp; currp = currp->nextp()) {
ReorderLogicVertex* const vtxp = currp->user3u().to<ReorderLogicVertex*>();
const uint32_t color = vtxp->color();
UASSERT_OBJ(color, currp, "No node color assigned");
if (lastOfColor[color]) new ReorderStrictEdge{m_graphp, lastOfColor[color], vtxp};
lastOfColor[color] = vtxp;
}