3#include <boost/container/flat_map.hpp>
4#include <boost/range/adaptor/transformed.hpp>
5#include <boost/range/iterator_range.hpp>
34template <
class Vertex,
class EdgeData = detail::NoEdgeData>
37 using Links = boost::container::flat_map<Vertex, EdgeData>;
38 using Graph = boost::container::flat_map<Vertex, Links>;
54 connect(Vertex source, Vertex target, EdgeData e)
56 return graph_[source].emplace(target, e).second;
69 return connect(source, target, EdgeData{});
84 auto it =
graph_.find(source);
87 return it->second.erase(target) > 0;
100 edge(Vertex source, Vertex target)
const
102 auto it =
graph_.find(source);
105 auto edgeIt = it->second.find(target);
106 if (edgeIt != it->second.end())
107 return edgeIt->second;
122 return edge(source, target) != std::nullopt;
134 return boost::adaptors::transform(
135 graph_, [](Graph::value_type
const& v) {
return v.first; });
147 auto transform = [](Links::value_type
const& link) {
return link.first; };
148 auto it =
graph_.find(source);
150 return boost::adaptors::transform(it->second, transform);
152 return boost::adaptors::transform(
empty_, transform);
175 auto transform = [source](Links::value_type
const& link) {
176 return Edge{source, link.first, link.second};
179 auto it =
graph_.find(source);
181 return boost::adaptors::transform(it->second, transform);
183 return boost::adaptors::transform(
empty_, transform);
195 auto it =
graph_.find(source);
197 return it->second.size();
210 template <
class VertexName>
214 out <<
"digraph {\n";
215 for (
auto const& [vertex, links] :
graph_)
217 auto const fromName = vertexName(vertex);
218 for (
auto const& eData : links)
220 auto const toName = vertexName(eData.first);
221 out << fromName <<
" -> " << toName <<
";\n";
227 template <
class VertexName>
std::size_t outDegree(Vertex source) const
Vertex out-degree.
void saveDot(std::string const &fileName, VertexName &&vertexName) const
auto outEdges(Vertex source) const
Range of out edges.
bool connected(Vertex source, Vertex target) const
Check if two vertices are connected.
bool connect(Vertex source, Vertex target, EdgeData e)
Connect two vertices.
auto outVertices(Vertex source) const
Range over target vertices.
boost::container::flat_map< Vertex, EdgeData > Links
bool disconnect(Vertex source, Vertex target)
Disconnect two vertices.
void saveDot(std::ostream &out, VertexName &&vertexName) const
Save GraphViz dot file.
std::optional< EdgeData > edge(Vertex source, Vertex target) const
Return edge data between two vertices.
auto outVertices() const
Range over vertices in the graph.
boost::container::flat_map< Vertex, Links > Graph
bool connect(Vertex source, Vertex target)
Connect two vertices using default constructed edge data.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Vertices and data associated with an Edge.