SILICON
Loading...
Searching...
No Matches
CircuitDependencyGraph Class Reference

Runtime-only graph of Circuit-to-Circuit instantiation dependencies. More...

#include <circuitDependencyGraph.hpp>

Classes

struct  VertexProperty
 Per-vertex payload holding the document's project-relative path. More...

Public Types

using DocumentPathList = std::vector<std::string>
 List of project-relative document paths.

Public Member Functions

 CircuitDependencyGraph ()=default
 CircuitDependencyGraph (const CircuitDependencyGraph &)=default
CircuitDependencyGraph & operator= (const CircuitDependencyGraph &)=default
 CircuitDependencyGraph (CircuitDependencyGraph &&) noexcept=default
CircuitDependencyGraph & operator= (CircuitDependencyGraph &&) noexcept=default
void clear ()
 Removes every vertex and edge, returning the graph to an empty state.
void addDocument (std::string_view documentPath)
 Ensures a document vertex exists, creating it if necessary.
void removeDocument (std::string_view documentPath)
 Removes an unreferenced document atomically.
void validateDocumentRemoval (std::string_view documentPath) const
void rebuildFromProject (const std::vector< Document > &documents)
 Rebuilds the graph from a full project description.
void replaceDocumentDependencies (std::string_view documentPath, std::string_view contents)
 Replaces the dependency edges of a single document atomically.
void validateDocumentDependencies (std::string_view documentPath, std::string_view contents) const
bool wouldIntroduceCycle (std::string_view documentPath, std::string_view contents) const
 Tests whether rewiring a document's dependencies would create a cycle.
DocumentPathList dependentsOf (std::string_view subcircuitPath) const
 Returns the sorted list of documents that depend on a subcircuit.
bool containsDocument (std::string_view documentPath) const
 Checks whether a document vertex is present in the graph.

Private Types

enum class  VisitState { Unvisited , Visiting , Visited }
 DFS visit marker used while reconstructing a dependency cycle. More...
using Graph
 Boost graph type: set out-edges, vector vertices, directed.
using Vertex = boost::graph_traits<Graph>::vertex_descriptor
 Boost vertex descriptor (an integer index under vecS).
using PathToVertex
 O(1) index mapping a document path to its graph vertex.

Private Member Functions

std::optional< Vertex > findVertex (std::string_view documentPath) const
CircuitDependencyGraph withDocumentDependencies (std::string_view documentPath, std::string_view contents) const
void replaceDependencyEdges (std::string_view documentPath, std::string_view contents)
std::vector< std::string > findCycleTrace () const
 Finds one recursive dependency cycle and returns it as ordered slugs.
std::vector< std::string > findCycleTraceFrom (Vertex vertex, std::vector< VisitState > &states, std::vector< Vertex > &path) const
 DFS step used by findCycleTrace.
std::vector< std::string > cycleTraceEndingAt (Vertex repeated, const std::vector< Vertex > &path) const
 Builds a slug trace when DFS reaches a vertex already in path.
void throwIfCyclic () const
 Throws a runtime error with a recursion trace if the graph is cyclic.
Vertex ensureVertex (std::string_view documentPath)
 Ensures a document vertex exists in this graph (O(1)).
void rebuildPathIndex ()
 Rebuilds pathToVertex from the current graph.
void validateAcyclic () const
 Verifies the graph is acyclic.

Private Attributes

Graph graph
 The dependency graph itself.
PathToVertex pathToVertex
 Path-to-vertex index kept in sync with graph.

Detailed Description

Runtime-only graph of Circuit-to-Circuit instantiation dependencies.

Vertices are project-relative Circuit document paths such as circuits/main.json and circuits/adder.json. Edges point from a document to the reusable Circuit document it instantiates. Non-diagram documents are outside this deliberately narrow model. The graph is derived from persisted Circuit JSON and is never serialized into project archives.

The graph uses boost::vecS for its vertex container, which means vertex descriptors are integer indices. As a consequence, removing a vertex reindexes every subsequent vertex, so the pathToVertex index must be rebuilt after a removal. Path lookup remains average O(1) thanks to the std::unordered_map index. Public mutations use project-sized candidate copies for strong exception safety; removal additionally incurs an O(V) path-index rebuild.

Definition at line 50 of file circuitDependencyGraph.hpp.

Member Typedef Documentation

◆ DocumentPathList

using DocumentPathList = std::vector<std::string>

List of project-relative document paths.

Definition at line 59 of file circuitDependencyGraph.hpp.

◆ Graph

using Graph
private
Initial value:
boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, VertexProperty>

Boost graph type: set out-edges, vector vertices, directed.

Definition at line 165 of file circuitDependencyGraph.hpp.

◆ PathToVertex

using PathToVertex
private
Initial value:
std::unordered_map<std::string, Vertex, core::TransparentStringHash,
boost::graph_traits< Graph >::vertex_descriptor Vertex
Boost vertex descriptor (an integer index under vecS).
Transparent equality functor for string-like keys.
Transparent hash functor for string-like keys.

O(1) index mapping a document path to its graph vertex.

Definition at line 174 of file circuitDependencyGraph.hpp.

◆ Vertex

using Vertex = boost::graph_traits<Graph>::vertex_descriptor
private

Boost vertex descriptor (an integer index under vecS).

Definition at line 168 of file circuitDependencyGraph.hpp.

Member Enumeration Documentation

◆ VisitState

enum class VisitState
strongprivate

DFS visit marker used while reconstructing a dependency cycle.

Enumerator
Unvisited 
Visiting 
Visited 

Definition at line 171 of file circuitDependencyGraph.hpp.

Constructor & Destructor Documentation

◆ CircuitDependencyGraph() [1/3]

CircuitDependencyGraph ( )
default

◆ CircuitDependencyGraph() [2/3]

CircuitDependencyGraph ( const CircuitDependencyGraph & )
default

◆ CircuitDependencyGraph() [3/3]

CircuitDependencyGraph ( CircuitDependencyGraph && )
defaultnoexcept

Member Function Documentation

◆ addDocument()

void addDocument ( std::string_view documentPath)

Ensures a document vertex exists, creating it if necessary.

Parameters
documentPathProject-relative path of the document to register.

Definition at line 147 of file circuitDependencyGraph.cpp.

◆ clear()

void clear ( )

Removes every vertex and edge, returning the graph to an empty state.

Definition at line 141 of file circuitDependencyGraph.cpp.

◆ containsDocument()

bool containsDocument ( std::string_view documentPath) const
nodiscard

Checks whether a document vertex is present in the graph.

Parameters
documentPathProject-relative path of the document to look up.
Returns
true if the document is registered.

Definition at line 257 of file circuitDependencyGraph.cpp.

◆ cycleTraceEndingAt()

std::vector< std::string > cycleTraceEndingAt ( Vertex repeated,
const std::vector< Vertex > & path ) const
nodiscardprivate

Builds a slug trace when DFS reaches a vertex already in path.

Parameters
repeatedVertex that closes the cycle.
pathCurrent DFS recursion path containing repeated.
Returns
Ordered slug trace from the first occurrence of repeated through the current path, with repeated appended again at the end.

Definition at line 348 of file circuitDependencyGraph.cpp.

◆ dependentsOf()

CircuitDependencyGraph::DocumentPathList dependentsOf ( std::string_view subcircuitPath) const
nodiscard

Returns the sorted list of documents that depend on a subcircuit.

Parameters
subcircuitPathProject-relative path of the subcircuit document.
Returns
The dependents, sorted lexicographically; empty if the subcircuit is unknown or has no dependents.

Definition at line 241 of file circuitDependencyGraph.cpp.

◆ ensureVertex()

CircuitDependencyGraph::Vertex ensureVertex ( std::string_view documentPath)
nodiscardprivate

Ensures a document vertex exists in this graph (O(1)).

Parameters
documentPathProject-relative path of the document.
Returns
The descriptor of the existing or newly created vertex.

Definition at line 365 of file circuitDependencyGraph.cpp.

◆ findCycleTrace()

std::vector< std::string > findCycleTrace ( ) const
nodiscardprivate

Finds one recursive dependency cycle and returns it as ordered slugs.

The repeated slug is appended at the end of the trace, e.g. [cpu, alu, cpu]. Returns an empty vector when the graph is acyclic.

Definition at line 307 of file circuitDependencyGraph.cpp.

◆ findCycleTraceFrom()

std::vector< std::string > findCycleTraceFrom ( Vertex vertex,
std::vector< VisitState > & states,
std::vector< Vertex > & path ) const
nodiscardprivate

DFS step used by findCycleTrace.

Parameters
vertexVertex currently being visited.
statesPer-vertex DFS state, indexed by vertex descriptor.
pathCurrent recursion path from the DFS root to vertex.
Returns
Ordered slug trace for the first cycle found, or empty when this branch is acyclic.

Definition at line 323 of file circuitDependencyGraph.cpp.

◆ findVertex()

std::optional< CircuitDependencyGraph::Vertex > findVertex ( std::string_view documentPath) const
nodiscardprivate

Definition at line 263 of file circuitDependencyGraph.cpp.

◆ operator=() [1/2]

CircuitDependencyGraph & operator= ( CircuitDependencyGraph && )
defaultnoexcept

◆ operator=() [2/2]

CircuitDependencyGraph & operator= ( const CircuitDependencyGraph & )
default

◆ rebuildFromProject()

void rebuildFromProject ( const std::vector< Document > & documents)

Rebuilds the graph from a full project description.

All graphical documents are first registered as vertices and then wired with their dependency edges. The resulting graph must be acyclic; if it is not, a std::runtime_error is thrown and the graph is left unchanged.

Parameters
documentsThe project's ordered documents. Non-graphical documents are ignored.
Exceptions
std::runtime_errorIf the described dependencies form a cycle.

Definition at line 189 of file circuitDependencyGraph.cpp.

◆ rebuildPathIndex()

void rebuildPathIndex ( )
private

Rebuilds pathToVertex from the current graph.

Required after remove_vertex because vecS reindexes descriptors.

Definition at line 377 of file circuitDependencyGraph.cpp.

◆ removeDocument()

void removeDocument ( std::string_view documentPath)

Removes an unreferenced document atomically.

If documentPath is not present in the graph this call is a no-op. If any document depends on it, removal is rejected and the graph is unchanged. Removing a vertex reindexes the remaining vertices (due to vecS), so the internal path index is rebuilt afterwards.

Parameters
documentPathProject-relative path of the document to remove.

Definition at line 161 of file circuitDependencyGraph.cpp.

◆ replaceDependencyEdges()

void replaceDependencyEdges ( std::string_view documentPath,
std::string_view contents )
private

Definition at line 281 of file circuitDependencyGraph.cpp.

◆ replaceDocumentDependencies()

void replaceDocumentDependencies ( std::string_view documentPath,
std::string_view contents )

Replaces the dependency edges of a single document atomically.

The new edges are computed against a private copy of the graph and only committed if every referenced subcircuit already exists and the resulting graph is acyclic; otherwise, a std::runtime_error is thrown and the graph is left unchanged.

Parameters
documentPathProject-relative path of the document to update.
contentsPersisted Circuit document JSON to inspect. Both {"components":[...]} and the serialized scene form {"circuit":{"components":[...]}} are supported. A missing components member is accepted for legacy empty circuits; a present member must be an array.
Exceptions
std::runtime_errorIf contents is malformed, references a missing subcircuit, or would create recursive dependencies.

Definition at line 212 of file circuitDependencyGraph.cpp.

◆ throwIfCyclic()

void throwIfCyclic ( ) const
private

Throws a runtime error with a recursion trace if the graph is cyclic.

Definition at line 392 of file circuitDependencyGraph.cpp.

◆ validateAcyclic()

void validateAcyclic ( ) const
private

Verifies the graph is acyclic.

Exceptions
boost::not_a_dagIf the graph contains a cycle.

Definition at line 385 of file circuitDependencyGraph.cpp.

◆ validateDocumentDependencies()

void validateDocumentDependencies ( std::string_view documentPath,
std::string_view contents ) const

Validates a candidate dependency update without mutating the graph. Missing documents, malformed references, and recursion retain distinct errors.

Definition at line 220 of file circuitDependencyGraph.cpp.

◆ validateDocumentRemoval()

void validateDocumentRemoval ( std::string_view documentPath) const

Validates that a document can be removed without losing a source reference.

Definition at line 177 of file circuitDependencyGraph.cpp.

◆ withDocumentDependencies()

CircuitDependencyGraph withDocumentDependencies ( std::string_view documentPath,
std::string_view contents ) const
nodiscardprivate

Definition at line 273 of file circuitDependencyGraph.cpp.

◆ wouldIntroduceCycle()

bool wouldIntroduceCycle ( std::string_view documentPath,
std::string_view contents ) const
nodiscard

Tests whether rewiring a document's dependencies would create a cycle.

Operates on a private copy of the graph, so it has no side effects.

Parameters
documentPathProject-relative path of the document to test.
contentsPersisted Circuit document JSON to inspect.
Returns
true only if an otherwise valid candidate introduces a cycle. Other validation failures propagate their specific exception.

Definition at line 227 of file circuitDependencyGraph.cpp.

Member Data Documentation

◆ graph

Graph graph
private

The dependency graph itself.

Definition at line 179 of file circuitDependencyGraph.hpp.

◆ pathToVertex

PathToVertex pathToVertex
private

Path-to-vertex index kept in sync with graph.

Definition at line 181 of file circuitDependencyGraph.hpp.


The documentation for this class was generated from the following files: