From 1d2781091c4024d306d167c3fef09075c27f6644 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Wed, 9 Sep 2026 20:56:24 +0100 Subject: [PATCH] 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. --- src/V3Graph.cpp | 5 ----- src/V3Graph.h | 6 ++---- src/V3GraphAlg.cpp | 22 +++++++++++++++------- src/V3Reorder.cpp | 1 - 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/V3Graph.cpp b/src/V3Graph.cpp index 96772bf02..79e7898ee 100644 --- a/src/V3Graph.cpp +++ b/src/V3Graph.cpp @@ -198,11 +198,6 @@ void V3Graph::userClearEdges() { } } -void V3Graph::clearColors() { - // Reset colors - for (V3GraphVertex& vertex : vertices()) vertex.color(0); -} - //====================================================================== // Dumping diff --git a/src/V3Graph.h b/src/V3Graph.h index a954d43b1..a8dbe745b 100644 --- a/src/V3Graph.h +++ b/src/V3Graph.h @@ -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. diff --git a/src/V3GraphAlg.cpp b/src/V3GraphAlg.cpp index a501b96a4..ed99d0b28 100644 --- a/src/V3GraphAlg.cpp +++ b/src/V3GraphAlg.cpp @@ -27,6 +27,7 @@ #include "V3Stats.h" #include +#include #include #include #include @@ -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::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(); +} //###################################################################### //###################################################################### diff --git a/src/V3Reorder.cpp b/src/V3Reorder.cpp index 4129d4f62..bf233ea45 100644 --- a/src/V3Reorder.cpp +++ b/src/V3Reorder.cpp @@ -293,7 +293,6 @@ class ReorderVisitor final : public VNVisitor { for (AstNode* currp = nodep; currp; currp = currp->nextp()) { ReorderLogicVertex* const vtxp = currp->user3u().to(); 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; }