EnTT: src/entt/graph/adjacency_matrix.hpp Source File

EnTT 3.15.0

Loading...
Searching...
No Matches
adjacency_matrix.hpp
1#ifndef ENTT_GRAPH_ADJACENCY_MATRIX_HPP
2#define ENTT_GRAPH_ADJACENCY_MATRIX_HPP
3
4#include <cstddef>
5#include <iterator>
6#include <memory>
7#include <type_traits>
8#include <utility>
9#include <vector>
10#include "../config/config.h"
11#include "../core/iterator.hpp"
12#include "fwd.hpp"
13
14namespace entt {
15
17namespace internal {
18
19template<typename It>
20class edge_iterator {
21 using size_type = std::size_t;
22
23 void find_next() noexcept {
24 for(; pos != last && !it[static_cast<typename It::difference_type>(pos)]; pos += offset) {}
25 }
26
27public:
28 using value_type = std::pair<size_type, size_type>;
29 using pointer = input_iterator_pointer<value_type>;
30 using reference = value_type;
31 using difference_type = std::ptrdiff_t;
32 using iterator_category = std::input_iterator_tag;
33 using iterator_concept = std::forward_iterator_tag;
34
35 constexpr edge_iterator() noexcept = default;
36
37 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
38 constexpr edge_iterator(It base, const size_type vertices, const size_type from, const size_type to, const size_type step) noexcept
39 : it{std::move(base)},
40 vert{vertices},
41 pos{from},
42 last{to},
43 offset{step} {
44 find_next();
45 }
46
47 constexpr edge_iterator &operator++() noexcept {
48 pos += offset;
49 find_next();
50 return *this;
51 }
52
53 constexpr edge_iterator operator++(int) noexcept {
54 const edge_iterator orig = *this;
55 return ++(*this), orig;
56 }
57
58 [[nodiscard]] constexpr reference operator*() const noexcept {
59 return *operator->();
60 }
61
62 [[nodiscard]] constexpr pointer operator->() const noexcept {
63 return std::make_pair<size_type>(pos / vert, pos % vert);
64 }
65
66 template<typename Type>
67 friend constexpr bool operator==(const edge_iterator<Type> &, const edge_iterator<Type> &) noexcept;
68
69private:
70 It it{};
71 size_type vert{};
72 size_type pos{};
73 size_type last{};
74 size_type offset{};
75};
76
77template<typename Container>
78[[nodiscard]] constexpr bool operator==(const edge_iterator<Container> &lhs, const edge_iterator<Container> &rhs) noexcept {
79 return lhs.pos == rhs.pos;
80}
81
82template<typename Container>
83[[nodiscard]] constexpr bool operator!=(const edge_iterator<Container> &lhs, const edge_iterator<Container> &rhs) noexcept {
84 return !(lhs == rhs);
85}
86
87} // namespace internal
89
95template<typename Category, typename Allocator>
96 class adjacency_matrix {
97 using alloc_traits = std::allocator_traits<Allocator>;
98 static_assert(std::is_base_of_v<directed_tag, Category>, "Invalid graph category");
99 static_assert(std::is_same_v<typename alloc_traits::value_type, std::size_t>, "Invalid value type");
100 using container_type = std::vector<std::size_t, typename alloc_traits::template rebind_alloc<std::size_t>>;
101
102public:
104 using allocator_type = Allocator;
106 using size_type = std::size_t;
108 using vertex_type = size_type;
110 using edge_type = std::pair<vertex_type, vertex_type>;
112 using vertex_iterator = iota_iterator<vertex_type>;
114 using edge_iterator = internal::edge_iterator<typename container_type::const_iterator>;
116 using out_edge_iterator = edge_iterator;
118 using in_edge_iterator = edge_iterator;
120 using graph_category = Category;
121
123 adjacency_matrix() noexcept(noexcept(allocator_type{}))
124 : adjacency_matrix{0u} {
125 }
126
131 explicit adjacency_matrix(const allocator_type &allocator) noexcept
132 : adjacency_matrix{0u, allocator} {}
133
140 adjacency_matrix(const size_type vertices, const allocator_type &allocator = allocator_type{})
141 : matrix{vertices * vertices, allocator},
142 vert{vertices} {}
143
145 adjacency_matrix(const adjacency_matrix &) = default;
146
152 adjacency_matrix(const adjacency_matrix &other, const allocator_type &allocator)
153 : matrix{other.matrix, allocator},
154 vert{other.vert} {}
155
157 adjacency_matrix(adjacency_matrix &&) noexcept = default;
158
164 adjacency_matrix(adjacency_matrix &&other, const allocator_type &allocator)
165 : matrix{std::move(other.matrix), allocator},
166 vert{other.vert} {}
167
169 ~adjacency_matrix() = default;
170
175 adjacency_matrix &operator=(const adjacency_matrix &) = default;
176
181 adjacency_matrix &operator=(adjacency_matrix &&) noexcept = default;
182
187 void swap(adjacency_matrix &other) noexcept {
188 using std::swap;
189 swap(matrix, other.matrix);
190 swap(vert, other.vert);
191 }
192
197 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
198 return matrix.get_allocator();
199 }
200
202 void clear() noexcept {
203 matrix.clear();
204 vert = {};
205 }
206
215 [[nodiscard]] bool empty() const noexcept {
216 const auto iterable = edges();
217 return (iterable.begin() == iterable.end());
218 }
219
224 [[nodiscard]] size_type size() const noexcept {
225 return vert;
226 }
227
232 [[nodiscard]] iterable_adaptor<vertex_iterator> vertices() const noexcept {
233 return {0u, vert};
234 }
235
240 [[nodiscard]] iterable_adaptor<edge_iterator> edges() const noexcept {
241 const auto it = matrix.cbegin();
242 const auto sz = matrix.size();
243 return {{it, vert, 0u, sz, 1u}, {it, vert, sz, sz, 1u}};
244 }
245
251 [[nodiscard]] iterable_adaptor<out_edge_iterator> out_edges(const vertex_type vertex) const noexcept {
252 const auto it = matrix.cbegin();
253 const auto from = vertex * vert;
254 const auto to = from + vert;
255 return {{it, vert, from, to, 1u}, {it, vert, to, to, 1u}};
256 }
257
263 [[nodiscard]] iterable_adaptor<in_edge_iterator> in_edges(const vertex_type vertex) const noexcept {
264 const auto it = matrix.cbegin();
265 const auto from = vertex;
266 const auto to = vert * vert + from;
267 return {{it, vert, from, to, vert}, {it, vert, to, to, vert}};
268 }
269
274 void resize(const size_type vertices) {
275 adjacency_matrix other{vertices, get_allocator()};
276
277 for(auto [lhs, rhs]: edges()) {
278 other.insert(lhs, rhs);
279 }
280
281 other.swap(*this);
282 }
283
292 std::pair<edge_iterator, bool> insert(const vertex_type lhs, const vertex_type rhs) {
293 const auto pos = lhs * vert + rhs;
294
295 if constexpr(std::is_same_v<graph_category, undirected_tag>) {
296 const auto rev = rhs * vert + lhs;
297 ENTT_ASSERT(matrix[pos] == matrix[rev], "Something went really wrong");
298 matrix[rev] = 1u;
299 }
300
301 const auto inserted = !std::exchange(matrix[pos], 1u);
302 return {edge_iterator{matrix.cbegin(), vert, pos, matrix.size(), 1u}, inserted};
303 }
304
311 size_type erase(const vertex_type lhs, const vertex_type rhs) {
312 const auto pos = lhs * vert + rhs;
313
314 if constexpr(std::is_same_v<graph_category, undirected_tag>) {
315 const auto rev = rhs * vert + lhs;
316 ENTT_ASSERT(matrix[pos] == matrix[rev], "Something went really wrong");
317 matrix[rev] = 0u;
318 }
319
320 return std::exchange(matrix[pos], 0u);
321 }
322
329 [[nodiscard]] bool contains(const vertex_type lhs, const vertex_type rhs) const {
330 const auto pos = lhs * vert + rhs;
331 return pos < matrix.size() && matrix[pos];
332 }
333
334private:
335 container_type matrix;
336 size_type vert;
337};
338
339} // namespace entt
340
341#endif
entt::adjacency_matrix
Basic implementation of a directed adjacency matrix.
Definition adjacency_matrix.hpp:96
entt::adjacency_matrix::insert
std::pair< edge_iterator, bool > insert(const vertex_type lhs, const vertex_type rhs)
Inserts an edge into the adjacency matrix, if it does not exist.
Definition adjacency_matrix.hpp:292
entt::adjacency_matrix::adjacency_matrix
adjacency_matrix(const adjacency_matrix &)=default
Default copy constructor.
entt::adjacency_matrix::vertex_iterator
iota_iterator< vertex_type > vertex_iterator
Vertex iterator type.
Definition adjacency_matrix.hpp:112
entt::adjacency_matrix::adjacency_matrix
adjacency_matrix() noexcept(noexcept(allocator_type{}))
Default constructor.
Definition adjacency_matrix.hpp:123
entt::adjacency_matrix::graph_category
Category graph_category
Graph category tag.
Definition adjacency_matrix.hpp:120
entt::adjacency_matrix::empty
bool empty() const noexcept
Returns true if an adjacency matrix is empty, false otherwise.
Definition adjacency_matrix.hpp:215
entt::adjacency_matrix::out_edges
iterable_adaptor< out_edge_iterator > out_edges(const vertex_type vertex) const noexcept
Returns an iterable object to visit all out-edges of a vertex.
Definition adjacency_matrix.hpp:251
entt::adjacency_matrix::edge_iterator
internal::edge_iterator< typename container_type::const_iterator > edge_iterator
Edge iterator type.
Definition adjacency_matrix.hpp:114
entt::adjacency_matrix< directed_tag, typename alloc_traits::template rebind_alloc< std::size_t > >::swap
void swap(adjacency_matrix &other) noexcept
Definition adjacency_matrix.hpp:187
entt::adjacency_matrix::size
size_type size() const noexcept
Returns the number of vertices.
Definition adjacency_matrix.hpp:224
entt::adjacency_matrix::size_type
std::size_t size_type
Unsigned integer type.
Definition adjacency_matrix.hpp:106
entt::adjacency_matrix::allocator_type
Allocator allocator_type
Allocator type.
Definition adjacency_matrix.hpp:104
entt::adjacency_matrix::operator=
adjacency_matrix & operator=(adjacency_matrix &&) noexcept=default
Default move assignment operator.
entt::adjacency_matrix::contains
bool contains(const vertex_type lhs, const vertex_type rhs) const
Checks if an adjacency matrix contains a given edge.
Definition adjacency_matrix.hpp:329
entt::adjacency_matrix::clear
void clear() noexcept
Clears the adjacency matrix.
Definition adjacency_matrix.hpp:202
entt::adjacency_matrix::adjacency_matrix
adjacency_matrix(const size_type vertices, const allocator_type &allocator=allocator_type{})
Constructs an empty container with a given allocator and user supplied number of vertices.
Definition adjacency_matrix.hpp:140
entt::adjacency_matrix::out_edge_iterator
edge_iterator out_edge_iterator
Out-edge iterator type.
Definition adjacency_matrix.hpp:116
entt::adjacency_matrix::in_edges
iterable_adaptor< in_edge_iterator > in_edges(const vertex_type vertex) const noexcept
Returns an iterable object to visit all in-edges of a vertex.
Definition adjacency_matrix.hpp:263
entt::adjacency_matrix::edge_type
std::pair< vertex_type, vertex_type > edge_type
Edge type.
Definition adjacency_matrix.hpp:110
entt::adjacency_matrix::~adjacency_matrix
~adjacency_matrix()=default
Default destructor.
entt::adjacency_matrix< directed_tag, typename alloc_traits::template rebind_alloc< std::size_t > >::vertices
iterable_adaptor< vertex_iterator > vertices() const noexcept
Definition adjacency_matrix.hpp:232
entt::adjacency_matrix::operator=
adjacency_matrix & operator=(const adjacency_matrix &)=default
Default copy assignment operator.
entt::adjacency_matrix::edges
iterable_adaptor< edge_iterator > edges() const noexcept
Returns an iterable object to visit all edges of a matrix.
Definition adjacency_matrix.hpp:240
entt::adjacency_matrix::in_edge_iterator
edge_iterator in_edge_iterator
In-edge iterator type.
Definition adjacency_matrix.hpp:118
entt::adjacency_matrix::resize
void resize(const size_type vertices)
Resizes an adjacency matrix.
Definition adjacency_matrix.hpp:274
entt::adjacency_matrix::vertex_type
size_type vertex_type
Vertex type.
Definition adjacency_matrix.hpp:108
entt::adjacency_matrix::adjacency_matrix
adjacency_matrix(const adjacency_matrix &other, const allocator_type &allocator)
Allocator-extended copy constructor.
Definition adjacency_matrix.hpp:152
entt::adjacency_matrix::adjacency_matrix
adjacency_matrix(adjacency_matrix &&) noexcept=default
Default move constructor.
entt::adjacency_matrix::erase
size_type erase(const vertex_type lhs, const vertex_type rhs)
Removes the edge associated with a pair of given vertices.
Definition adjacency_matrix.hpp:311
entt::adjacency_matrix::adjacency_matrix
adjacency_matrix(const allocator_type &allocator) noexcept
Constructs an empty container with a given allocator.
Definition adjacency_matrix.hpp:131
entt::adjacency_matrix::get_allocator
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition adjacency_matrix.hpp:197
entt::iota_iterator
Plain iota iterator (waiting for C++20).
Definition iterator.hpp:56
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:232
entt::operator==
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:220
entt::iterable_adaptor
Utility class to create an iterable object from a pair of iterators.
Definition iterator.hpp:141