Py2Cpp 1.6.5
Loading...
Searching...
No Matches
nx2bgl.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <boost/graph/adjacency_list.hpp>
12#include <boost/graph/graph_traits.hpp>
13#include <boost/graph/graph_utility.hpp>
14
15namespace py {
16
25 template <typename Graph> class VertexView : public Graph {
26 public:
34 explicit VertexView(Graph&& gra) noexcept : Graph{std::move(gra)} {}
35
43 [[nodiscard]] auto begin() const {
44 // auto [v_iter, v_end] = boost::vertices(*this);
45 // return v_iter;
46 return boost::vertices(*this).first;
47 }
48
56 [[nodiscard]] auto end() const {
57 // auto [v_iter, v_end] = boost::vertices(*this);
58 // return v_end;
59 return boost::vertices(*this).second;
60 }
61
69 [[nodiscard]] auto cbegin() const {
70 // auto [v_iter, v_end] = boost::vertices(*this);
71 // return v_iter;
72 return boost::vertices(*this).first;
73 }
74
82 [[nodiscard]] auto cend() const {
83 // auto [v_iter, v_end] = boost::vertices(*this);
84 // return v_end;
85 return boost::vertices(*this).second;
86 }
87 };
88
97 template <typename Graph> class EdgeView {
98 private:
99 const Graph& gra;
100
101 public:
109 explicit EdgeView(const Graph& gra) : gra{gra} {}
110
118 [[nodiscard]] auto begin() const {
119 // auto [e_iter, e_end] = boost::edges(_gra);
120 // return e_iter;
121 return boost::edges(this->gra).first;
122 }
123
131 [[nodiscard]] auto end() const {
132 // auto [e_iter, e_end] = boost::edges(_gra);
133 // return e_end;
134 return boost::edges(this->gra).second;
135 }
136
144 [[nodiscard]] auto cbegin() const {
145 // auto [e_iter, e_end] = boost::edges(_gra);
146 // return e_iter;
147 return boost::edges(this->gra).first;
148 }
149
157 [[nodiscard]] auto cend() const {
158 // auto [e_iter, e_end] = boost::edges(_gra);
159 // return e_end;
160 return boost::edges(this->gra).second;
161 }
162 };
163
174 template <typename Vertex, typename Graph> class AtlasView {
175 private:
176 Vertex _v;
177 const Graph& gra;
178
179 public:
188 AtlasView(Vertex v, const Graph& gra) : _v{v}, gra{gra} {}
189
197 auto begin() const {
198 // auto [e_iter, e_end] = boost::out_edges(_v, _gra);
199 // return e_iter;
200 return boost::out_edges(this->_v, this->gra).first;
201 }
202
210 auto end() const {
211 // auto [e_iter, e_end] = boost::out_edges(_v, _gra);
212 // return e_end;
213 return boost::out_edges(this->_v, this->gra).second;
214 }
215
223 auto cbegin() const {
224 // auto [e_iter, e_end] = boost::out_edges(_v, _gra);
225 // return e_iter;
226 return boost::out_edges(this->_v, this->gra).first;
227 }
228
236 auto cend() const {
237 // auto [e_iter, e_end] = boost::out_edges(_v, _gra);
238 // return e_end;
239 return boost::out_edges(this->_v, this->gra).second;
240 }
241 };
242
255 template <typename Graph> class GrAdaptor : public VertexView<Graph> {
256 public:
257 using Vertex = typename boost::graph_traits<Graph>::vertex_descriptor;
258 using node_t = Vertex;
259 using edge_t = typename boost::graph_traits<Graph>::edge_descriptor;
260
261 // using edge_wt_t = decltype( boost::get(boost::edge_weight,
262 // std::declval<Graph>()) );
263
269 GrAdaptor() = delete;
270
278 explicit GrAdaptor(Graph&& gra) noexcept : VertexView<Graph>{std::move(gra)} {}
279
280 // GrAdaptor(const GrAdaptor&) = delete; // don't copy
281 // GrAdaptor& operator=(const GrAdaptor&) = delete; // don't assign
282 // GrAdaptor(GrAdaptor&&) noexcept = default; // don't copy
283
289 [[nodiscard]] auto number_of_nodes() const { return boost::num_vertices(*this); }
290
296 [[nodiscard]] auto number_of_edges() const { return boost::num_edges(*this); }
297
305 [[nodiscard]] auto edges() const -> EdgeView<Graph> { return EdgeView<Graph>(*this); }
306
315 [[nodiscard]] auto neighbors(Vertex v) const -> AtlasView<Vertex, Graph> {
316 return AtlasView<Vertex, Graph>(v, *this);
317 }
318
326 auto add_edge(int u, int v) {
327 return boost::add_edge(static_cast<Vertex>(u), static_cast<Vertex>(v), *this);
328 }
329
337 static auto null_vertex() -> Vertex { return boost::graph_traits<Graph>::null_vertex(); }
338
348 template <typename Edge> auto source(const Edge& e) const -> Vertex {
349 return boost::source(e, *this);
350 }
351
361 template <typename Edge> auto target(const Edge& e) const -> Vertex {
362 return boost::target(e, *this);
363 }
364
372 template <typename Edge> [[nodiscard]] auto end_points(const Edge& e) const {
373 auto s = boost::source(e, *this);
374 auto t = boost::target(e, *this);
375 return std::make_pair(s, t);
376 }
377 };
378
379} // namespace py
Atlas view for vertex adjacency in Boost Graph Library graphs.
Definition nx2bgl.hpp:174
auto end() const
Get iterator to the end of adjacent edges.
Definition nx2bgl.hpp:210
AtlasView(Vertex v, const Graph &gra)
Construct a new Atlas View object.
Definition nx2bgl.hpp:188
auto cend() const
Get const iterator to the end of adjacent edges.
Definition nx2bgl.hpp:236
auto begin() const
Get iterator to the beginning of adjacent edges.
Definition nx2bgl.hpp:197
auto cbegin() const
Get const iterator to the beginning of adjacent edges.
Definition nx2bgl.hpp:223
Edge view for Boost Graph Library graphs.
Definition nx2bgl.hpp:97
auto begin() const
Get iterator to the beginning of edges.
Definition nx2bgl.hpp:118
auto cend() const
Get const iterator to the end of edges.
Definition nx2bgl.hpp:157
auto cbegin() const
Get const iterator to the beginning of edges.
Definition nx2bgl.hpp:144
EdgeView(const Graph &gra)
Construct a new Edge View object.
Definition nx2bgl.hpp:109
auto end() const
Get iterator to the end of edges.
Definition nx2bgl.hpp:131
Graph adapter for Boost Graph Library integration.
Definition nx2bgl.hpp:255
GrAdaptor()=delete
Default constructor (deleted)
auto edges() const -> EdgeView< Graph >
Get an edge view for the graph.
Definition nx2bgl.hpp:305
auto neighbors(Vertex v) const -> AtlasView< Vertex, Graph >
Get neighbors of a vertex.
Definition nx2bgl.hpp:315
typename boost::graph_traits< Graph >::vertex_descriptor Vertex
Definition nx2bgl.hpp:257
auto add_edge(int u, int v)
Add an edge between two vertices.
Definition nx2bgl.hpp:326
Vertex node_t
Definition nx2bgl.hpp:258
GrAdaptor(Graph &&gra) noexcept
Construct a new graph adaptor object.
Definition nx2bgl.hpp:278
auto target(const Edge &e) const -> Vertex
Get the target vertex of an edge.
Definition nx2bgl.hpp:361
auto source(const Edge &e) const -> Vertex
Get the source vertex of an edge.
Definition nx2bgl.hpp:348
auto end_points(const Edge &e) const
Get the source and target vertices of an edge.
Definition nx2bgl.hpp:372
auto number_of_nodes() const
Get the number of vertices in the graph.
Definition nx2bgl.hpp:289
auto number_of_edges() const
Get the number of edges in the graph.
Definition nx2bgl.hpp:296
typename boost::graph_traits< Graph >::edge_descriptor edge_t
Definition nx2bgl.hpp:259
static auto null_vertex() -> Vertex
Get the null vertex descriptor.
Definition nx2bgl.hpp:337
Vertex view for Boost Graph Library graphs.
Definition nx2bgl.hpp:25
auto cend() const
Get const iterator to the end of vertices.
Definition nx2bgl.hpp:82
auto cbegin() const
Get const iterator to the beginning of vertices.
Definition nx2bgl.hpp:69
auto end() const
Get iterator to the end of vertices.
Definition nx2bgl.hpp:56
VertexView(Graph &&gra) noexcept
Construct a new Vertex View object.
Definition nx2bgl.hpp:34
auto begin() const
Get iterator to the beginning of vertices.
Definition nx2bgl.hpp:43
Python-like utilities and data structures for C++.
Definition dict.hpp:18