SILICON
Loading...
Searching...
No Matches
circuitDependencyGraph.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <optional>
21#include <string>
22#include <string_view>
23#include <unordered_map>
24#include <vector>
25
26#include <boost/graph/adjacency_list.hpp>
27
30
31namespace SILICON::project {
32
51public:
56 CircuitDependencyGraph& operator=(CircuitDependencyGraph&&) noexcept = default;
57
59 using DocumentPathList = std::vector<std::string>;
60
64 void clear();
65
71 void addDocument(std::string_view documentPath);
72
83 void removeDocument(std::string_view documentPath);
84
86 void validateDocumentRemoval(std::string_view documentPath) const;
87
99 void rebuildFromProject(const std::vector<Document>& documents);
100
118 void replaceDocumentDependencies(std::string_view documentPath,
119 std::string_view contents);
120
125 void validateDocumentDependencies(std::string_view documentPath,
126 std::string_view contents) const;
127
138 [[nodiscard]] bool wouldIntroduceCycle(std::string_view documentPath,
139 std::string_view contents) const;
140
148 [[nodiscard]] DocumentPathList dependentsOf(std::string_view subcircuitPath) const;
149
156 [[nodiscard]] bool containsDocument(std::string_view documentPath) const;
157
158private:
161 std::string path;
162 };
163
165 using Graph =
166 boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, VertexProperty>;
168 using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
169
172
175 std::unordered_map<std::string, Vertex, core::TransparentStringHash,
177
182
183 [[nodiscard]] std::optional<Vertex> findVertex(std::string_view documentPath) const;
184
185 [[nodiscard]] CircuitDependencyGraph
186 withDocumentDependencies(std::string_view documentPath,
187 std::string_view contents) const;
188
189 void replaceDependencyEdges(std::string_view documentPath, std::string_view contents);
190
197 [[nodiscard]] std::vector<std::string> findCycleTrace() const;
198
208 [[nodiscard]] std::vector<std::string>
209 findCycleTraceFrom(Vertex vertex, std::vector<VisitState>& states,
210 std::vector<Vertex>& path) const;
211
220 [[nodiscard]] std::vector<std::string>
221 cycleTraceEndingAt(Vertex repeated, const std::vector<Vertex>& path) const;
222
226 void throwIfCyclic() const;
227
234 [[nodiscard]] Vertex ensureVertex(std::string_view documentPath);
235
241 void rebuildPathIndex();
242
248 void validateAcyclic() const;
249};
250
251} // namespace SILICON::project
std::vector< std::string > DocumentPathList
List of project-relative document paths.
CircuitDependencyGraph & operator=(const CircuitDependencyGraph &)=default
void throwIfCyclic() const
Throws a runtime error with a recursion trace if the graph is cyclic.
void rebuildPathIndex()
Rebuilds pathToVertex from the current graph.
std::optional< Vertex > findVertex(std::string_view documentPath) const
std::unordered_map< std::string, Vertex, core::TransparentStringHash, core::TransparentStringEqual > PathToVertex
O(1) index mapping a document path to its graph vertex.
bool containsDocument(std::string_view documentPath) const
Checks whether a document vertex is present in the graph.
CircuitDependencyGraph(CircuitDependencyGraph &&) noexcept=default
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.
boost::adjacency_list< boost::setS, boost::vecS, boost::directedS, VertexProperty > Graph
Boost graph type: set out-edges, vector vertices, directed.
bool wouldIntroduceCycle(std::string_view documentPath, std::string_view contents) const
Tests whether rewiring a document's dependencies would create a cycle.
CircuitDependencyGraph withDocumentDependencies(std::string_view documentPath, std::string_view contents) const
void removeDocument(std::string_view documentPath)
Removes an unreferenced document atomically.
void validateDocumentDependencies(std::string_view documentPath, std::string_view contents) const
VisitState
DFS visit marker used while reconstructing a dependency cycle.
DocumentPathList dependentsOf(std::string_view subcircuitPath) const
Returns the sorted list of documents that depend on a subcircuit.
void replaceDocumentDependencies(std::string_view documentPath, std::string_view contents)
Replaces the dependency edges of a single document atomically.
PathToVertex pathToVertex
Path-to-vertex index kept in sync with graph.
CircuitDependencyGraph(const CircuitDependencyGraph &)=default
void addDocument(std::string_view documentPath)
Ensures a document vertex exists, creating it if necessary.
void clear()
Removes every vertex and edge, returning the graph to an empty state.
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.
boost::graph_traits< Graph >::vertex_descriptor Vertex
Boost vertex descriptor (an integer index under vecS).
void rebuildFromProject(const std::vector< Document > &documents)
Rebuilds the graph from a full project description.
Vertex ensureVertex(std::string_view documentPath)
Ensures a document vertex exists in this graph (O(1)).
void validateAcyclic() const
Verifies the graph is acyclic.
void validateDocumentRemoval(std::string_view documentPath) const
std::vector< std::string > findCycleTraceFrom(Vertex vertex, std::vector< VisitState > &states, std::vector< Vertex > &path) const
DFS step used by findCycleTrace.
Transparent equality functor for string-like keys.
Transparent hash functor for string-like keys.
Per-vertex payload holding the document's project-relative path.