31#ifndef FENNEC_CONTAINERS_GRAPH_H
32#define FENNEC_CONTAINERS_GRAPH_H
90template<
typename VertexT,
typename EdgeT = empty_t>
101 static constexpr size_t npos = -1;
146 return _vertex_pool.
size();
152 return _edge_pool.
size();
172 constexpr bool exists(
size_t a,
size_t b)
const {
173 return _edge_map[a][b] !=
nullptr;
192 const auto* e0 = _edge_map[a][b];
193 const auto* e1 = _edge_map[b][a];
194 if (not (e0 !=
nullptr && e1 !=
nullptr)) {
215 return _vertex_pool[vertex];
223 return _vertex_pool[vertex];
235 edge_t* it = _edge_map[a][b];
237 return _edge_pool[*it];
251 const edge_t* it = _edge_map[a][b];
253 return _edge_pool[*it];
264 if (
empty() || vertex >= _edge_map.
size()) {
267 for (
const auto& it : _edge_map[vertex]) {
279 if (
empty() || vertex >= _edge_map.
size()) {
282 for (
size_t n = 0; n < _edge_map.
size(); ++n) {
283 if (_edge_map[n][vertex]) {
296 if (
empty() || vertex >= _edge_map.
size()) {
299 for (
const auto& it : _edge_map[vertex]) {
300 if (_edge_map[it.first][vertex]) {
317 if (
empty() || vertex >= _edge_map.
size()) {
320 for (
const auto& it : _edge_map[vertex]) {
321 const auto* at = _edge_map[it.first][vertex];
322 if (at !=
nullptr && *at == it.second) {
335 if (
empty() || vertex >= _edge_map.
size()) {
338 return &_edge_map[vertex];
354 return this->_insert(fennec::forward<vertex_t>(vertex));
362 return this->_insert(vertex);
370 template<
typename...ArgsT>
372 return this->_insert(fennec::forward<ArgsT>(args)...);
378 constexpr void erase(
size_t vertex) {
380 _vertex_pool.
erase(vertex);
389 template<
typename...ArgsT>
390 constexpr void make_edge(
size_t a,
size_t b, ArgsT&&...args) {
399 auto it = _edge_map[a][b];
403 _edge_pool[conn] =
vertex_t(fennec::forward<ArgsT>(args)...);
405 conn = _edge_pool.
emplace(fennec::forward<ArgsT>(args)...);
416 template<
typename...ArgsT>
417 constexpr void make_edge2(
size_t a,
size_t b, ArgsT&&...args) {
426 auto it = _edge_map[a][b];
430 _edge_pool[conn] =
vertex_t(fennec::forward<ArgsT>(args)...);
432 conn = _edge_pool.
emplace(fennec::forward<ArgsT>(args)...);
446 const auto* it = _edge_map[a][b];
453 const auto* at = _edge_map[b][a];
454 if (not at || *at != c) {
459 _edge_map[a].erase(b);
467 const auto* ita = _edge_map[a][b];
468 const auto* itb = _edge_map[a][b];
469 if (not (ita || itb)) {
472 if (ita) _edge_pool.
erase(*ita);
473 if (itb) _edge_pool.
erase(*itb);
474 _edge_map[a].erase(b);
475 _edge_map[b].erase(a);
493 _vertex_pool.
clear();
509 template<
typename...ArgsT>
510 size_t _insert(ArgsT&&...args) {
511 return _vertex_pool.
emplace(fennec::forward<ArgsT>(args)...);
A header containing the definition for a dynamically allocated array.
A header containing the definition for a linked list of values.
A header containing the definition for a mapping of keys to values.
A header containing the definition for a pool of objects associated by ids.
A header containing the definition for a set of unique values.
constexpr void clear()
Clears the contents of the dynarray, destructing all elements and releasing the allocation.
Definition dynarray.h:534
constexpr void emplace(size_t i, ArgsT &&...args)
Emplace Insertion.
Definition dynarray.h:460
constexpr size_t size() const
Definition dynarray.h:331
constexpr void resize(size_t n)
Resize the dynarray, invoking the default constructor for all new elements.
Definition dynarray.h:512
Graph Data Structure, describes sets of arbitrarily connected vertices.
Definition graph.h:91
void cut(size_t n)
Break all edges to and from n
Definition graph.h:481
constexpr void make_edge(size_t a, size_t b, ArgsT &&...args)
Form an edge from vertex a to vertex b
Definition graph.h:390
constexpr size_t emplace(ArgsT &&...args)
Construct a new vertex in the graph.
Definition graph.h:371
constexpr void erase(size_t vertex)
Erase a vertex from the graph.
Definition graph.h:378
constexpr const edge_t * operator[](size_t a, size_t b) const
edge Const Access Operator
Definition graph.h:247
constexpr graph & operator=(const graph &g)=default
Copy Assignment Operator.
VertexT vertex_t
Alias for the vertex type.
Definition graph.h:96
constexpr const vertex_t & operator[](size_t vertex) const
vertex Const Access Operator
Definition graph.h:222
void clear()
Clear the graph, destructing all vertices and edges.
Definition graph.h:492
constexpr graph & operator=(graph &&g)=default
Move Assignment Operator.
constexpr size_t capacity() const
Definition graph.h:157
constexpr ~graph()=default
Destructor.
list< size_t > symmetric(size_t vertex)
Getter for a list of vertices x that vertex has an edge to and from x...
Definition graph.h:294
list< size_t > incoming(size_t vertex)
Getter for a list of vertices x that vertex has an edge from x...
Definition graph.h:277
constexpr size_t insert(vertex_t &&vertex)
Move a new vertex into the graph.
Definition graph.h:353
constexpr bool is_undirected(size_t a, size_t b) const
Checks if there exists an edge e between a and b
Definition graph.h:191
const auto * edges(size_t vertex)
Getter for the internal storage of mapped edges from this vertex. Use this when you want to iterate o...
Definition graph.h:334
object_pool< vertex_t > vertex_pool_t
Alias for a pool of vertices.
Definition graph.h:97
EdgeT edge_t
Alias for the edge type.
Definition graph.h:95
constexpr void make_edge2(size_t a, size_t b, ArgsT &&...args)
Form an undirected edge between vertex a and vertex b
Definition graph.h:417
list< size_t > outgoing(size_t vertex)
Getter for a list of vertices x that vertex has an edge to x...
Definition graph.h:262
object_pool< edge_t > edge_pool_t
Alias for a pool of edges.
Definition graph.h:99
constexpr bool is_symmetric(size_t a, size_t b) const
Checks if there exists an edge e0 that starts from a and ends at b and e1 that starts from b and ends...
Definition graph.h:182
list< size_t > undirected(size_t vertex)
Getter for a list of vertices x that vertex has an edge to and from x... and share the same value.
Definition graph.h:315
constexpr size_t num_vertices() const
Definition graph.h:145
constexpr bool empty() const
Definition graph.h:163
constexpr void cut_edge2(size_t a, size_t b)
Disconnect both directed edges between vertices a and b
Definition graph.h:466
constexpr void cut_edge(size_t a, size_t b)
Disconnect an edge from vertex a to vertex b
Definition graph.h:443
dynarray< map< size_t, size_t > > edge_map_t
Alias for edge mapping.
Definition graph.h:98
constexpr size_t num_edges() const
Definition graph.h:151
constexpr size_t insert(const vertex_t &vertex)
Copy a new vertex into the graph.
Definition graph.h:361
constexpr bool exists(size_t a, size_t b) const
Checks if there exists an edge e that starts from a and ends at b
Definition graph.h:172
constexpr edge_t * operator[](size_t a, size_t b)
edge Access Operator
Definition graph.h:231
constexpr vertex_t & operator[](size_t vertex)
vertex Access Operator
Definition graph.h:214
static constexpr size_t npos
Constant for a non-existent vertex.
Definition graph.h:101
constexpr graph()=default
Default Constructor, initializes empty graph.
Data Structure defining lists of elements.
Definition list.h:67
constexpr size_t push_back(const value_t &x)
Push Back Copy.
Definition list.h:342
constexpr size_t emplace(ArgsT &&...args)
Emplacement, constructs a new object using args...
Definition object_pool.h:170
constexpr void clear()
Clear the object pool.
Definition object_pool.h:185
constexpr size_t capacity() const
Definition object_pool.h:90
constexpr void erase(size_t i)
Erase an object from the pool.
Definition object_pool.h:177
constexpr size_t size() const
Definition object_pool.h:84