6#ifndef NURI_CORE_GRAPH_ADAPTOR_H_
7#define NURI_CORE_GRAPH_ADAPTOR_H_
12#include <boost/graph/graph_concepts.hpp>
13#include <boost/graph/properties.hpp>
14#include <boost/property_map/property_map.hpp>
15#include <boost/property_map/transform_value_property_map.hpp>
28 return lhs.
id == rhs.
id;
32 return lhs.
id != rhs.
id;
37 struct nuri_graph_traversal_tag:
virtual boost::incidence_graph_tag,
38 virtual boost::adjacency_graph_tag,
39 virtual boost::bidirectional_graph_tag,
40 virtual boost::vertex_list_graph_tag,
41 virtual boost::edge_list_graph_tag { };
43 template <
class Impl,
class VT,
class Iterator>
44 class BoostGraphIteratorAdaptorBase
45 :
public ProxyIterator<Impl, VT, typename Iterator::iterator_category,
46 typename Iterator::difference_type> {
48 BoostGraphIteratorAdaptorBase() =
default;
50 BoostGraphIteratorAdaptorBase(Iterator it): it_(it) { }
53 using Base = BoostGraphIteratorAdaptorBase;
56 VT dereference()
const {
57 return static_cast<const Impl &
>(*this).dereference_impl(it_);
60 bool equal(
const BoostGraphIteratorAdaptorBase &rhs)
const {
61 return it_ == rhs.it_;
64 typename Iterator::difference_type
65 distance_to(
const BoostGraphIteratorAdaptorBase &rhs)
const {
69 void increment() { ++it_; }
70 void decrement() { --it_; }
72 void advance(
int n) { it_ += n; }
76 friend class boost::iterator_core_access;
80 struct BoostGraphTraits {
88 using vertex_descriptor = int;
89 using edge_descriptor = BoostEdgeDesc;
91 class adjacency_iterator
92 :
public BoostGraphIteratorAdaptorBase<
93 adjacency_iterator, vertex_descriptor, nuri_cadj_iter> {
94 using Base =
typename adjacency_iterator::Base;
100 static vertex_descriptor dereference_impl(nuri_cadj_iter it) {
101 return it->dst().id();
107 class out_edge_iterator
108 :
public BoostGraphIteratorAdaptorBase<
109 out_edge_iterator, edge_descriptor, nuri_cadj_iter> {
110 using Base =
typename out_edge_iterator::Base;
116 static edge_descriptor dereference_impl(nuri_cadj_iter it) {
117 return { it->eid(), it->src().id(), it->dst().id() };
123 class in_edge_iterator
124 :
public BoostGraphIteratorAdaptorBase<
125 in_edge_iterator, edge_descriptor, nuri_cadj_iter> {
126 using Base =
typename in_edge_iterator::Base;
132 static edge_descriptor dereference_impl(nuri_cadj_iter it) {
133 return { it->eid(), it->dst().id(), it->src().id() };
139 class vertex_iterator
140 :
public BoostGraphIteratorAdaptorBase<
141 vertex_iterator, vertex_descriptor, nuri_cnode_iter> {
142 using Base =
typename vertex_iterator::Base;
148 static vertex_descriptor dereference_impl(nuri_cnode_iter it) {
156 :
public BoostGraphIteratorAdaptorBase<edge_iterator, edge_descriptor,
158 using Base =
typename edge_iterator::Base;
164 static edge_descriptor dereference_impl(nuri_cedge_iter it) {
165 return { it->id(), it->src().id(), it->dst().id() };
171 using directed_category = boost::undirected_tag;
172 using edge_parallel_category = boost::allow_parallel_edge_tag;
173 using traversal_category = nuri_graph_traversal_tag;
175 using vertices_size_type = int;
176 using edges_size_type = int;
177 using degree_size_type = int;
179 static vertex_descriptor null_vertex() {
return -1; }
185template <
class NT,
class ET>
186struct graph_traits<
nuri::Graph<NT, ET>>
187 : nuri::internal::BoostGraphTraits<nuri::Graph<NT, ET>> { };
189template <
class NT,
class ET,
bool is_const>
190struct graph_traits<
nuri::Subgraph<NT, ET, is_const>>
191 : nuri::internal::BoostGraphTraits<nuri::Subgraph<NT, ET, is_const>> { };
199template <
class NT,
class ET>
202 using Iter =
typename boost::graph_traits<Graph<NT, ET>>::out_edge_iterator;
203 return std::make_pair(Iter(g[v].begin()), Iter(g[v].end()));
206template <
class NT,
class ET>
207typename boost::graph_traits<Graph<NT, ET>>::vertex_descriptor
213template <
class NT,
class ET>
214typename boost::graph_traits<Graph<NT, ET>>::vertex_descriptor
220template <
class NT,
class ET>
228template <
class NT,
class ET>
231 using Iter =
typename boost::graph_traits<Graph<NT, ET>>::in_edge_iterator;
232 return std::make_pair(Iter(g[v].begin()), Iter(g[v].end()));
235template <
class NT,
class ET>
241template <
class NT,
class ET>
249template <
class NT,
class ET>
251 typename boost::graph_traits<
Graph<NT, ET>>::vertex_descriptor v,
253 using Iter =
typename boost::graph_traits<Graph<NT, ET>>::adjacency_iterator;
254 return std::make_pair(Iter(g[v].begin()), Iter(g[v].end()));
259template <
class NT,
class ET>
261 using Iter =
typename boost::graph_traits<Graph<NT, ET>>::vertex_iterator;
262 return std::make_pair(Iter(g.
begin()), Iter(g.
end()));
265template <
class NT,
class ET>
272template <
class NT,
class ET>
274 using Iter =
typename boost::graph_traits<Graph<NT, ET>>::edge_iterator;
278template <
class NT,
class ET>
285template <
class NT,
class ET>
290template <
class NT,
class ET>
292 typename boost::graph_traits<
Graph<NT, ET>>::vertex_descriptor v,
294 auto extract_eid = [](
auto adj) {
return adj.eid(); };
295 g.
erase_edges(internal::make_transform_iterator(g[v].begin(), extract_eid),
296 internal::make_transform_iterator(g[v].end(), extract_eid));
299template <
class NT,
class ET>
301 typename boost::graph_traits<
Graph<NT, ET>>::vertex_descriptor v,
306#pragma GCC diagnostic push
307#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
309template <
class NT,
class ET>
311 typename boost::graph_traits<
Graph<NT, ET>>::vertex_descriptor v,
314 return std::make_pair(
315 typename boost::graph_traits<
Graph<NT, ET>>::edge_descriptor { e, u, v },
319#pragma GCC diagnostic pop
321template <
class NT,
class ET>
323 typename boost::graph_traits<
Graph<NT, ET>>::vertex_descriptor u,
324 typename boost::graph_traits<
Graph<NT, ET>>::vertex_descriptor v,
329template <
class NT,
class ET>
335template <
class NT,
class ET>
343template <
class NT,
class ET>
345 return boost::typed_identity_property_map<
346 typename boost::graph_traits<Graph<NT, ET>>::vertex_descriptor> {};
349template <
class NT,
class ET>
351 typename boost::graph_traits<
Graph<NT, ET>>::vertex_descriptor v) {
355template <
class NT,
class ET>
357 return boost::make_transform_value_property_map(
358 [](
auto e) {
return e.id; },
359 boost::typed_identity_property_map<
360 typename boost::graph_traits<Graph<NT, ET>>::edge_descriptor>());
363template <
class NT,
class ET>
365 typename boost::graph_traits<
Graph<NT, ET>>::edge_descriptor e) {
372template <
class NT,
class ET,
bool is_const>
377 typename boost::graph_traits<Subgraph<NT, ET, is_const>>::out_edge_iterator;
378 return std::make_pair(Iter(g[v].begin()), Iter(g[v].end()));
381template <
class NT,
class ET,
bool is_const>
382typename boost::graph_traits<Subgraph<NT, ET, is_const>>::vertex_descriptor
389template <
class NT,
class ET,
bool is_const>
390typename boost::graph_traits<Subgraph<NT, ET, is_const>>::vertex_descriptor
397template <
class NT,
class ET,
bool is_const>
406template <
class NT,
class ET,
bool is_const>
411 typename boost::graph_traits<Subgraph<NT, ET, is_const>>::in_edge_iterator;
412 return std::make_pair(Iter(g[v].begin()), Iter(g[v].end()));
415template <
class NT,
class ET,
bool is_const>
422template <
class NT,
class ET,
bool is_const>
431template <
class NT,
class ET,
bool is_const>
435 using Iter =
typename boost::graph_traits<
437 return std::make_pair(Iter(g[v].begin()), Iter(g[v].end()));
442template <
class NT,
class ET,
bool is_const>
445 typename boost::graph_traits<Subgraph<NT, ET, is_const>>::vertex_iterator;
446 return std::make_pair(Iter(g.
begin()), Iter(g.
end()));
449template <
class NT,
class ET,
bool is_const>
456template <
class NT,
class ET,
bool is_const>
459 typename boost::graph_traits<Subgraph<NT, ET, is_const>>::edge_iterator;
463template <
class NT,
class ET,
bool is_const>
470template <
class NT,
class ET,
bool is_const>
471auto get(boost::vertex_index_t ,
473 return boost::typed_identity_property_map<
typename boost::graph_traits<
477template <
class NT,
class ET,
bool is_const>
485template <
class NT,
class ET,
bool is_const>
486auto get(boost::edge_index_t ,
488 return boost::make_transform_value_property_map(
489 [](
auto e) {
return e.id; },
490 boost::typed_identity_property_map<
typename boost::graph_traits<
494template <
class NT,
class ET,
bool is_const>
Class for very sparse graphs, especially designed for the molecular graphs.
Definition graph.h:475
int add_node(const NT &data)
Add a node to the graph.
Definition graph.h:581
int degree(int id) const
Definition graph.h:563
void erase_edge(int id)
Erase an edge from the graph.
Definition graph.h:865
int size() const
Definition graph.h:550
bool erase_edge_between(int src, int dst)
Erase an edge from the graph between two nodes.
Definition graph.h:1380
iterator begin()
Definition graph.h:778
internal::EdgeIterator< Graph, true > const_edge_iterator
Definition graph.h:500
edge_iterator edge_end()
Definition graph.h:980
iterator end()
Definition graph.h:779
int add_edge(int src, int dst, const ET &data)
Add an edge to the graph.
Definition graph.h:635
int num_edges() const
Definition graph.h:561
std::pair< int, std::vector< int > > erase_edges(const_edge_iterator begin, const_edge_iterator end)
Erase edges from the graph.
Definition graph.h:919
edge_iterator edge_begin()
Definition graph.h:979
NodesErased erase_nodes(const_iterator begin, const_iterator end)
Erase nodes and all its associated edge(s) from the graph.
Definition graph.h:724
internal::AdjIterator< Graph, true > const_adjacency_iterator
Definition graph.h:505
const_iterator const_node_iterator
Definition graph.h:495
A subgraph of a graph.
Definition subgraph.h:575
int size() const
Count number of nodes in the subgraph.
Definition subgraph.h:746
int degree(int idx) const
Count in-subgraph neighbors of a node.
Definition subgraph.h:1443
edge_iterator edge_end()
Definition subgraph.h:1413
edge_iterator edge_begin()
Definition subgraph.h:1412
iterator end()
Definition subgraph.h:1391
iterator begin()
Definition subgraph.h:1390
int num_edges() const
Count number of edges in the subgraph.
Definition subgraph.h:760
auto num_vertices(const Graph< NT, ET > &g)
Definition adaptor.h:266
auto vertices(const Graph< NT, ET > &g)
Definition adaptor.h:260
auto add_vertex(Graph< NT, ET > &g)
Definition adaptor.h:286
auto out_edges(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, const Graph< NT, ET > &g)
Definition adaptor.h:200
auto get(boost::vertex_index_t, const Graph< NT, ET > &)
Definition adaptor.h:344
auto edges(const Graph< NT, ET > &g)
Definition adaptor.h:273
void clear_vertex(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, Graph< NT, ET > &g)
Definition adaptor.h:291
auto add_edge(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor u, typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, Graph< NT, ET > &g)
Definition adaptor.h:310
auto degree(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, const Graph< NT, ET > &g)
Definition adaptor.h:242
void remove_edge(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor u, typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, Graph< NT, ET > &g)
Definition adaptor.h:322
boost::graph_traits< Graph< NT, ET > >::vertex_descriptor target(typename boost::graph_traits< Graph< NT, ET > >::edge_descriptor e, const Graph< NT, ET > &)
Definition adaptor.h:215
auto out_degree(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, const Graph< NT, ET > &g)
Definition adaptor.h:221
auto in_edges(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, const Graph< NT, ET > &g)
Definition adaptor.h:229
boost::graph_traits< Graph< NT, ET > >::vertex_descriptor source(typename boost::graph_traits< Graph< NT, ET > >::edge_descriptor e, const Graph< NT, ET > &)
Definition adaptor.h:208
auto adjacent_vertices(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, const Graph< NT, ET > &g)
Definition adaptor.h:250
void remove_vertex(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, Graph< NT, ET > &g)
Definition adaptor.h:300
auto in_degree(typename boost::graph_traits< Graph< NT, ET > >::vertex_descriptor v, const Graph< NT, ET > &g)
Definition adaptor.h:236
auto num_edges(const Graph< NT, ET > &g)
Definition adaptor.h:279
friend bool operator!=(const BoostEdgeDesc &lhs, const BoostEdgeDesc &rhs)
Definition adaptor.h:31
int src
Definition adaptor.h:24
int id
Definition adaptor.h:23
int dst
Definition adaptor.h:25
friend bool operator==(const BoostEdgeDesc &lhs, const BoostEdgeDesc &rhs)
Definition adaptor.h:27