rippled
Loading...
Searching...
No Matches
Digraph.h
1#pragma once
2
3#include <boost/container/flat_map.hpp>
4#include <boost/range/adaptor/transformed.hpp>
5#include <boost/range/iterator_range.hpp>
6
7#include <fstream>
8#include <optional>
9#include <type_traits>
10#include <unordered_map>
11
12namespace xrpl {
13namespace detail {
14// Dummy class when no edge data needed for graph
16{
17};
18
19} // namespace detail
20
21namespace test {
22namespace csf {
23
34template <class Vertex, class EdgeData = detail::NoEdgeData>
36{
37 using Links = boost::container::flat_map<Vertex, EdgeData>;
38 using Graph = boost::container::flat_map<Vertex, Links>;
40
41 // Allows returning empty iterables for unknown vertices
43
44public:
53 bool
54 connect(Vertex source, Vertex target, EdgeData e)
55 {
56 return graph_[source].emplace(target, e).second;
57 }
58
66 bool
67 connect(Vertex source, Vertex target)
68 {
69 return connect(source, target, EdgeData{});
70 }
71
80 bool
81 disconnect(Vertex source, Vertex target)
82 {
83 auto it = graph_.find(source);
84 if (it != graph_.end())
85 {
86 return it->second.erase(target) > 0;
87 }
88 return false;
89 }
90
99 edge(Vertex source, Vertex target) const
100 {
101 auto it = graph_.find(source);
102 if (it != graph_.end())
103 {
104 auto edgeIt = it->second.find(target);
105 if (edgeIt != it->second.end())
106 return edgeIt->second;
107 }
108 return std::nullopt;
109 }
110
117 bool
118 connected(Vertex source, Vertex target) const
119 {
120 return edge(source, target) != std::nullopt;
121 }
122
128 auto
130 {
131 return boost::adaptors::transform(graph_, [](typename Graph::value_type const& v) { return v.first; });
132 }
133
139 auto
140 outVertices(Vertex source) const
141 {
142 auto transform = [](typename Links::value_type const& link) { return link.first; };
143 auto it = graph_.find(source);
144 if (it != graph_.end())
145 return boost::adaptors::transform(it->second, transform);
146
147 return boost::adaptors::transform(empty, transform);
148 }
149
152 struct Edge
153 {
154 Vertex source;
155 Vertex target;
156 EdgeData data;
157 };
158
165 auto
166 outEdges(Vertex source) const
167 {
168 auto transform = [source](typename Links::value_type const& link) {
169 return Edge{source, link.first, link.second};
170 };
171
172 auto it = graph_.find(source);
173 if (it != graph_.end())
174 return boost::adaptors::transform(it->second, transform);
175
176 return boost::adaptors::transform(empty, transform);
177 }
178
185 outDegree(Vertex source) const
186 {
187 auto it = graph_.find(source);
188 if (it != graph_.end())
189 return it->second.size();
190 return 0;
191 }
192
201 template <class VertexName>
202 void
203 saveDot(std::ostream& out, VertexName&& vertexName) const
204 {
205 out << "digraph {\n";
206 for (auto const& [vertex, links] : graph_)
207 {
208 auto const fromName = vertexName(vertex);
209 for (auto const& eData : links)
210 {
211 auto const toName = vertexName(eData.first);
212 out << fromName << " -> " << toName << ";\n";
213 }
214 }
215 out << "}\n";
216 }
217
218 template <class VertexName>
219 void
220 saveDot(std::string const& fileName, VertexName&& vertexName) const
221 {
222 std::ofstream out(fileName);
224 }
225};
226
227} // namespace csf
228} // namespace test
229} // namespace xrpl
Directed graph.
Definition Digraph.h:36
std::size_t outDegree(Vertex source) const
Vertex out-degree.
Definition Digraph.h:185
void saveDot(std::string const &fileName, VertexName &&vertexName) const
Definition Digraph.h:220
auto outEdges(Vertex source) const
Range of out edges.
Definition Digraph.h:166
bool connected(Vertex source, Vertex target) const
Check if two vertices are connected.
Definition Digraph.h:118
bool connect(Vertex source, Vertex target, EdgeData e)
Connect two vertices.
Definition Digraph.h:54
auto outVertices(Vertex source) const
Range over target vertices.
Definition Digraph.h:140
boost::container::flat_map< Vertex, EdgeData > Links
Definition Digraph.h:37
bool disconnect(Vertex source, Vertex target)
Disconnect two vertices.
Definition Digraph.h:81
void saveDot(std::ostream &out, VertexName &&vertexName) const
Save GraphViz dot file.
Definition Digraph.h:203
std::optional< EdgeData > edge(Vertex source, Vertex target) const
Return edge data between two vertices.
Definition Digraph.h:99
auto outVertices() const
Range over vertices in the graph.
Definition Digraph.h:129
boost::container::flat_map< Vertex, Links > Graph
Definition Digraph.h:38
bool connect(Vertex source, Vertex target)
Connect two vertices using default constructed edge data.
Definition Digraph.h:67
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Vertices and data associated with an Edge.
Definition Digraph.h:153