EnTT 3.14.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
23public:
24 using value_type = std::pair<size_type, size_type>;
25 using pointer = input_iterator_pointer<value_type>;
26 using reference = value_type;
27 using difference_type = std::ptrdiff_t;
28 using iterator_category = std::input_iterator_tag;
29 using iterator_concept = std::forward_iterator_tag;
30
31 constexpr edge_iterator() noexcept = default;
32
33 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
34 constexpr edge_iterator(It base, const size_type vertices, const size_type from, const size_type to, const size_type step) noexcept
35 : it{std::move(base)},
36 vert{vertices},
37 pos{from},
38 last{to},
39 offset{step} {
40 for(; pos != last && !it[pos]; pos += offset) {}
41 }
42
43 constexpr edge_iterator &operator++() noexcept {
44 for(pos += offset; pos != last && !it[pos]; pos += offset) {}
45 return *this;
46 }
47
48 constexpr edge_iterator operator++(int) noexcept {
49 edge_iterator orig = *this;
50 return ++(*this), orig;
51 }
52
53 [[nodiscard]] constexpr reference operator*() const noexcept {
54 return *operator->();
55 }
56
57 [[nodiscard]] constexpr pointer operator->() const noexcept {
58 return std::make_pair<size_type>(pos / vert, pos % vert);
59 }
60
61 template<typename Type>
62 friend constexpr bool operator==(const edge_iterator<Type> &, const edge_iterator<Type> &) noexcept;
63
64private:
65 It it{};
66 size_type vert{};
67 size_type pos{};
68 size_type last{};
69 size_type offset{};
70};
71
72template<typename Container>
73[[nodiscard]] inline constexpr bool operator==(const edge_iterator<Container> &lhs, const edge_iterator<Container> &rhs) noexcept {
74 return lhs.pos == rhs.pos;
75}
76
77template<typename Container>
78[[nodiscard]] inline constexpr bool operator!=(const edge_iterator<Container> &lhs, const edge_iterator<Container> &rhs) noexcept {
79 return !(lhs == rhs);
80}
81
82} // namespace internal
90template<typename Category, typename Allocator>
92 using alloc_traits = std::allocator_traits<Allocator>;
93 static_assert(std::is_base_of_v<directed_tag, Category>, "Invalid graph category");
94 static_assert(std::is_same_v<typename alloc_traits::value_type, std::size_t>, "Invalid value type");
95 using container_type = std::vector<std::size_t, typename alloc_traits::template rebind_alloc<std::size_t>>;
96
97public:
99 using allocator_type = Allocator;
101 using size_type = std::size_t;
105 using edge_type = std::pair<vertex_type, vertex_type>;
109 using edge_iterator = internal::edge_iterator<typename container_type::const_iterator>;
116
121
128
138
141
148 : matrix{other.matrix, allocator},
149 vert{other.vert} {}
150
153
160 : matrix{std::move(other.matrix), allocator},
161 vert{other.vert} {}
162
164 ~adjacency_matrix() = default;
165
171
177
183 return matrix.get_allocator();
184 }
185
188 matrix.clear();
189 vert = {};
190 }
191
196 void swap(adjacency_matrix &other) noexcept {
197 using std::swap;
198 swap(matrix, other.matrix);
199 swap(vert, other.vert);
200 }
201
211 const auto iterable = edges();
212 return (iterable.begin() == iterable.end());
213 }
214
220 return vert;
221 }
222
230
236 const auto it = matrix.cbegin();
237 const auto sz = matrix.size();
238 return {{it, vert, 0u, sz, 1u}, {it, vert, sz, sz, 1u}};
239 }
240
247 const auto it = matrix.cbegin();
248 const auto from = vertex * vert;
249 const auto to = from + vert;
250 return {{it, vert, from, to, 1u}, {it, vert, to, to, 1u}};
251 }
252
259 const auto it = matrix.cbegin();
260 const auto from = vertex;
261 const auto to = vert * vert + from;
262 return {{it, vert, from, to, vert}, {it, vert, to, to, vert}};
263 }
264
271
272 for(auto [lhs, rhs]: edges()) {
273 other.insert(lhs, rhs);
274 }
275
276 other.swap(*this);
277 }
278
287 std::pair<edge_iterator, bool> insert(const vertex_type lhs, const vertex_type rhs) {
288 const auto pos = lhs * vert + rhs;
289
290 if constexpr(std::is_same_v<graph_category, undirected_tag>) {
291 const auto rev = rhs * vert + lhs;
292 ENTT_ASSERT(matrix[pos] == matrix[rev], "Something went really wrong");
293 matrix[rev] = 1u;
294 }
295
296 const auto inserted = !std::exchange(matrix[pos], 1u);
297 return {edge_iterator{matrix.cbegin(), vert, pos, matrix.size(), 1u}, inserted};
298 }
299
307 const auto pos = lhs * vert + rhs;
308
309 if constexpr(std::is_same_v<graph_category, undirected_tag>) {
310 const auto rev = rhs * vert + lhs;
311 ENTT_ASSERT(matrix[pos] == matrix[rev], "Something went really wrong");
312 matrix[rev] = 0u;
313 }
314
315 return std::exchange(matrix[pos], 0u);
316 }
317
324 [[nodiscard]] bool contains(const vertex_type lhs, const vertex_type rhs) const {
325 const auto pos = lhs * vert + rhs;
326 return pos < matrix.size() && matrix[pos];
327 }
328
329private:
330 container_type matrix;
331 size_type vert;
332};
333
334} // namespace entt
335
336#endif
Basic implementation of a directed adjacency matrix.
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.
adjacency_matrix(const adjacency_matrix &)=default
Default copy constructor.
adjacency_matrix() noexcept(noexcept(allocator_type{}))
Default constructor.
Category graph_category
Graph category tag.
bool empty() const noexcept
Returns true if an adjacency matrix is empty, false otherwise.
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.
internal::edge_iterator< typename container_type::const_iterator > edge_iterator
Edge iterator type.
void swap(adjacency_matrix &other) noexcept
Exchanges the contents with those of a given adjacency matrix.
size_type size() const noexcept
Returns the number of vertices.
std::size_t size_type
Unsigned integer type.
Allocator allocator_type
Allocator type.
adjacency_matrix & operator=(adjacency_matrix &&) noexcept=default
Default move assignment operator.
bool contains(const vertex_type lhs, const vertex_type rhs) const
Checks if an adjacency matrix contains a given edge.
void clear() noexcept
Clears the 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.
edge_iterator out_edge_iterator
Out-edge iterator type.
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.
std::pair< vertex_type, vertex_type > edge_type
Edge type.
~adjacency_matrix()=default
Default destructor.
iterable_adaptor< vertex_iterator > vertices() const noexcept
Returns an iterable object to visit all vertices of a matrix.
adjacency_matrix & operator=(const adjacency_matrix &)=default
Default copy assignment operator.
iterable_adaptor< edge_iterator > edges() const noexcept
Returns an iterable object to visit all edges of a matrix.
edge_iterator in_edge_iterator
In-edge iterator type.
void resize(const size_type vertices)
Resizes an adjacency matrix.
size_type vertex_type
Vertex type.
adjacency_matrix(const adjacency_matrix &other, const allocator_type &allocator)
Allocator-extended copy constructor.
adjacency_matrix(adjacency_matrix &&) noexcept=default
Default move constructor.
size_type erase(const vertex_type lhs, const vertex_type rhs)
Removes the edge associated with a pair of given vertices.
adjacency_matrix(const allocator_type &allocator) noexcept
Constructs an empty container with a given allocator.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
Plain iota iterator (waiting for C++20).
Definition iterator.hpp:56
EnTT default namespace.
Definition dense_map.hpp:22
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Utility class to create an iterable object from a pair of iterators.
Definition iterator.hpp:141