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
22class Graph {
23private:
24 void setup(igraph_integer_t num_vertices, igraph_bool_t directed) {
25 if (igraph_empty(&my_graph, num_vertices, directed)) {
26 throw std::runtime_error("failed to initialize an empty igraph graph object");
27 }
28 }
29
30public:
37 Graph(igraph_integer_t num_vertices = 0, igraph_bool_t directed = false) {
38 setup(num_vertices, directed);
39 }
40
49 Graph(const IntVector& edges, igraph_integer_t num_vertices, igraph_bool_t directed) : Graph(edges.get(), num_vertices, directed) {}
50
58 Graph(const igraph_vector_int_t* edges, igraph_integer_t num_vertices, igraph_bool_t directed) {
59 if (igraph_create(&my_graph, edges, num_vertices, directed)) {
60 throw std::runtime_error("failed to initialize an igraph graph object");
61 }
62 }
63
67 Graph(igraph_t&& graph) : my_graph(std::move(graph)) {}
68
72 Graph(const Graph& other) {
73 if (igraph_copy(&my_graph, &(other.my_graph))) {
74 throw std::runtime_error("failed to copy-construct an igraph graph object");
75 }
76 }
77
81 Graph& operator=(const Graph& other) {
82 if (this != &other) {
83 if (igraph_copy(&my_graph, &(other.my_graph))) {
84 throw std::runtime_error("failed to copy-assign an igraph graph object");
85 }
86 }
87 return *this;
88 }
89
94 Graph(Graph&& other) {
95 setup(0, false);
96 std::swap(my_graph, other.my_graph);
97 }
98
104 if (this != &other) {
105 std::swap(my_graph, other.my_graph);
106 }
107 return *this;
108 }
109
114 igraph_destroy(&my_graph);
115 }
116
117public:
121 igraph_integer_t vcount() const {
122 return igraph_vcount(&my_graph);
123 }
124
128 igraph_integer_t ecount() const {
129 return igraph_ecount(&my_graph);
130 }
131
138 IntVector get_edgelist(igraph_bool_t by_col = false) const {
139 IntVector out(ecount());
140 check_code(igraph_get_edgelist(&my_graph, out.get(), by_col));
141 return out;
142 }
143
144public:
148 igraph_bool_t is_directed() const {
149 return igraph_is_directed(&my_graph);
150 }
151
158 igraph_bool_t is_connected(igraph_connectedness_t mode = IGRAPH_WEAK) const {
159 igraph_bool_t res;
160 check_code(igraph_is_connected(&my_graph, &res, mode));
161 return res;
162 }
163
167 igraph_bool_t is_simple() const {
168 igraph_bool_t res;
169 check_code(igraph_is_simple(&my_graph, &res));
170 return res;
171 }
172
176 igraph_bool_t has_loop() const {
177 igraph_bool_t res;
178 check_code(igraph_has_loop(&my_graph, &res));
179 return res;
180 }
181
185 igraph_bool_t has_multiple() const {
186 igraph_bool_t res;
187 check_code(igraph_has_multiple(&my_graph, &res));
188 return res;
189 }
190
195 igraph_bool_t has_mutual(igraph_bool_t loops = false) const {
196 igraph_bool_t res;
197 check_code(igraph_has_mutual(&my_graph, &res, loops));
198 return res;
199 }
200
207 bool is_tree(igraph_neimode_t mode = IGRAPH_ALL) const {
208 igraph_bool_t res;
209 check_code(igraph_is_tree(&my_graph, &res, NULL, mode));
210 return res;
211 }
212
217 bool is_forest(igraph_neimode_t mode = IGRAPH_ALL) const {
218 igraph_bool_t res;
219 check_code(igraph_is_forest(&my_graph, &res, NULL, mode));
220 return res;
221 }
222
226 bool is_dag() const {
227 igraph_bool_t res;
228 check_code(igraph_is_dag(&my_graph, &res));
229 return res;
230 }
231
235 bool is_acyclic() const {
236 igraph_bool_t res;
237 check_code(igraph_is_acyclic(&my_graph, &res));
238 return res;
239 }
240
241public:
246 operator igraph_t*() {
247 return &my_graph;
248 }
249
254 operator const igraph_t*() const {
255 return &my_graph;
256 }
257
262 igraph_t* get() {
263 return &my_graph;
264 }
265
270 const igraph_t* get() const {
271 return &my_graph;
272 }
273
274private:
275 igraph_t my_graph;
276};
277
278}
279
280#endif
Wrapper around igraph_vector_*_t objects with RAII behavior.
Wrapper around igraph_t objects with RAII behavior.
Definition Graph.hpp:22
Graph & operator=(const Graph &other)
Definition Graph.hpp:81
IntVector get_edgelist(igraph_bool_t by_col=false) const
Definition Graph.hpp:138
Graph(const Graph &other)
Definition Graph.hpp:72
Graph(const igraph_vector_int_t *edges, igraph_integer_t num_vertices, igraph_bool_t directed)
Definition Graph.hpp:58
Graph(igraph_integer_t num_vertices=0, igraph_bool_t directed=false)
Definition Graph.hpp:37
igraph_t * get()
Definition Graph.hpp:262
Graph(const IntVector &edges, igraph_integer_t num_vertices, igraph_bool_t directed)
Definition Graph.hpp:49
Graph(igraph_t &&graph)
Definition Graph.hpp:67
Graph & operator=(Graph &&other)
Definition Graph.hpp:103
igraph_bool_t has_loop() const
Definition Graph.hpp:176
igraph_bool_t is_directed() const
Definition Graph.hpp:148
const igraph_t * get() const
Definition Graph.hpp:270
bool is_dag() const
Definition Graph.hpp:226
~Graph()
Definition Graph.hpp:113
igraph_bool_t has_mutual(igraph_bool_t loops=false) const
Definition Graph.hpp:195
igraph_bool_t is_simple() const
Definition Graph.hpp:167
igraph_integer_t vcount() const
Definition Graph.hpp:121
bool is_acyclic() const
Definition Graph.hpp:235
bool is_forest(igraph_neimode_t mode=IGRAPH_ALL) const
Definition Graph.hpp:217
igraph_bool_t has_multiple() const
Definition Graph.hpp:185
bool is_tree(igraph_neimode_t mode=IGRAPH_ALL) const
Definition Graph.hpp:207
igraph_integer_t ecount() const
Definition Graph.hpp:128
Graph(Graph &&other)
Definition Graph.hpp:94
igraph_bool_t is_connected(igraph_connectedness_t mode=IGRAPH_WEAK) const
Definition Graph.hpp:158
Wrapper around igraph_vector_*_t objects with RAII behavior.
Definition Vector.hpp:26
igraph_type * get()
Definition Vector.hpp:501
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:34