raiigraph
C++ RAII for igraph data structures
Loading...
Searching...
No Matches
Graph.hpp
Go to the documentation of this file.
1#ifndef RAIIGRAPH_GRAPH_HPP
2#define RAIIGRAPH_GRAPH_HPP
3
4#include "igraph.h"
5#include "Vector.hpp"
6#include "error.hpp"
7
13namespace raiigraph {
14
24class Graph {
25private:
26 void setup(igraph_int_t num_vertices, igraph_bool_t directed) {
27 check_code(igraph_empty(&my_graph, num_vertices, directed));
28 }
29
30public:
37 Graph(igraph_int_t num_vertices = 0, igraph_bool_t directed = false) {
38 setup(num_vertices, directed);
39 }
40
49 Graph(const IntVector& edges, igraph_int_t num_vertices, igraph_bool_t directed) : Graph(edges.get(), num_vertices, directed) {}
50
58 Graph(const igraph_vector_int_t* edges, igraph_int_t num_vertices, igraph_bool_t directed) {
59 check_code(igraph_create(&my_graph, edges, num_vertices, directed));
60 }
61
65 Graph(igraph_t&& graph) : my_graph(std::move(graph)) {}
66
67public:
71 Graph(const Graph& other) {
72 check_code(igraph_copy(&my_graph, &(other.my_graph)));
73 }
74
78 Graph& operator=(const Graph& other) {
79 if (this != &other) {
80 check_code(igraph_copy(&my_graph, &(other.my_graph)));
81 }
82 return *this;
83 }
84
89 Graph(Graph&& other) {
90 setup(0, false);
91 std::swap(my_graph, other.my_graph);
92 }
93
98 Graph& operator=(Graph&& other) {
99 if (this != &other) {
100 std::swap(my_graph, other.my_graph);
101 }
102 return *this;
103 }
104
109 igraph_destroy(&my_graph);
110 }
111
112public:
116 igraph_int_t vcount() const {
117 return igraph_vcount(&my_graph);
118 }
119
123 igraph_int_t ecount() const {
124 return igraph_ecount(&my_graph);
125 }
126
133 IntVector get_edgelist(igraph_bool_t by_col = false) const {
134 IntVector out(ecount());
135 check_code(igraph_get_edgelist(&my_graph, out.get(), by_col));
136 return out;
137 }
138
139public:
143 igraph_bool_t is_directed() const {
144 return igraph_is_directed(&my_graph);
145 }
146
153 igraph_bool_t is_connected(igraph_connectedness_t mode = IGRAPH_WEAK) const {
154 igraph_bool_t res;
155 check_code(igraph_is_connected(&my_graph, &res, mode));
156 return res;
157 }
158
165 igraph_bool_t is_simple(igraph_bool_t directed) const {
166 igraph_bool_t res;
167 check_code(igraph_is_simple(&my_graph, &res, directed));
168 return res;
169 }
170
174 igraph_bool_t has_loop() const {
175 igraph_bool_t res;
176 check_code(igraph_has_loop(&my_graph, &res));
177 return res;
178 }
179
183 igraph_bool_t has_multiple() const {
184 igraph_bool_t res;
185 check_code(igraph_has_multiple(&my_graph, &res));
186 return res;
187 }
188
193 igraph_bool_t has_mutual(igraph_bool_t loops = false) const {
194 igraph_bool_t res;
195 check_code(igraph_has_mutual(&my_graph, &res, loops));
196 return res;
197 }
198
205 bool is_tree(igraph_neimode_t mode = IGRAPH_ALL) const {
206 igraph_bool_t res;
207 check_code(igraph_is_tree(&my_graph, &res, NULL, mode));
208 return res;
209 }
210
215 bool is_forest(igraph_neimode_t mode = IGRAPH_ALL) const {
216 igraph_bool_t res;
217 check_code(igraph_is_forest(&my_graph, &res, NULL, mode));
218 return res;
219 }
220
224 bool is_dag() const {
225 igraph_bool_t res;
226 check_code(igraph_is_dag(&my_graph, &res));
227 return res;
228 }
229
233 bool is_acyclic() const {
234 igraph_bool_t res;
235 check_code(igraph_is_acyclic(&my_graph, &res));
236 return res;
237 }
238
239public:
244 operator igraph_t*() {
245 return &my_graph;
246 }
247
252 operator const igraph_t*() const {
253 return &my_graph;
254 }
255
260 igraph_t* get() {
261 return &my_graph;
262 }
263
268 const igraph_t* get() const {
269 return &my_graph;
270 }
271
272private:
273 igraph_t my_graph;
274};
275
276}
277
278#endif
Wrapper around igraph_vector_*_t objects with RAII behavior.
Wrapper around igraph_t objects with RAII behavior.
Definition Graph.hpp:24
Graph & operator=(const Graph &other)
Definition Graph.hpp:78
IntVector get_edgelist(igraph_bool_t by_col=false) const
Definition Graph.hpp:133
Graph(const Graph &other)
Definition Graph.hpp:71
igraph_t * get()
Definition Graph.hpp:260
Graph(igraph_t &&graph)
Definition Graph.hpp:65
Graph(const igraph_vector_int_t *edges, igraph_int_t num_vertices, igraph_bool_t directed)
Definition Graph.hpp:58
Graph & operator=(Graph &&other)
Definition Graph.hpp:98
Graph(const IntVector &edges, igraph_int_t num_vertices, igraph_bool_t directed)
Definition Graph.hpp:49
igraph_bool_t has_loop() const
Definition Graph.hpp:174
igraph_bool_t is_directed() const
Definition Graph.hpp:143
const igraph_t * get() const
Definition Graph.hpp:268
bool is_dag() const
Definition Graph.hpp:224
igraph_bool_t is_simple(igraph_bool_t directed) const
Definition Graph.hpp:165
~Graph()
Definition Graph.hpp:108
igraph_int_t ecount() const
Definition Graph.hpp:123
igraph_bool_t has_mutual(igraph_bool_t loops=false) const
Definition Graph.hpp:193
igraph_int_t vcount() const
Definition Graph.hpp:116
bool is_acyclic() const
Definition Graph.hpp:233
bool is_forest(igraph_neimode_t mode=IGRAPH_ALL) const
Definition Graph.hpp:215
igraph_bool_t has_multiple() const
Definition Graph.hpp:183
bool is_tree(igraph_neimode_t mode=IGRAPH_ALL) const
Definition Graph.hpp:205
Graph(Graph &&other)
Definition Graph.hpp:89
Graph(igraph_int_t num_vertices=0, igraph_bool_t directed=false)
Definition Graph.hpp:37
igraph_bool_t is_connected(igraph_connectedness_t mode=IGRAPH_WEAK) const
Definition Graph.hpp:153
Wrapper around igraph_vector_*_t objects with RAII behavior.
Definition Vector.hpp:29
igraph_type * get()
Definition Vector.hpp:499
Error handling for raiigraph.
Utilities for manipulating igraph data structures in C++.
Definition error.hpp:11
void check_code(igraph_error_t code)
Definition error.hpp:39