-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadjacency_matrix.hpp
More file actions
340 lines (283 loc) · 12.6 KB
/
adjacency_matrix.hpp
File metadata and controls
340 lines (283 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) 2024-2026 Jakub Musiał
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
#pragma once
#include "gl/constants.hpp"
#include "gl/impl/specialized/adjacency_matrix.hpp"
#include "gl/impl/specialized/flat_adjacency_matrix.hpp"
#include "gl/types/core.hpp"
#ifdef GL_TESTING
namespace gl_testing {
struct test_adjacency_matrix;
} // namespace gl_testing
#endif
namespace gl {
namespace detail {
template <traits::c_graph_impl_tag TargetImplTag, traits::c_graph_impl_tag SourceImplTag>
struct to_impl;
} // namespace detail
namespace impl {
template <traits::c_adjacency_matrix_graph_traits GraphTraits>
class adjacency_matrix final {
public:
using implementation_tag = typename GraphTraits::implementation_tag;
using id_type = typename GraphTraits::id_type;
using vertex_type = typename GraphTraits::vertex_type;
using edge_type = typename GraphTraits::edge_type;
using adjacency_storage_type = typename specialized::adjacency_matrix_impl_traits<
adjacency_matrix>::template storage_type<id_type>;
adjacency_matrix() = default;
explicit adjacency_matrix(size_type n_vertices) {
specialized_impl::init(*this, n_vertices);
}
adjacency_matrix(const adjacency_matrix&) = default;
adjacency_matrix& operator=(const adjacency_matrix&) = default;
adjacency_matrix(adjacency_matrix&&) noexcept = default;
adjacency_matrix& operator=(adjacency_matrix&&) noexcept = default;
~adjacency_matrix() = default;
// --- vertex methods ---
void add_vertex() {
specialized_impl::add_vertex(*this);
}
void add_vertices(size_type n) {
specialized_impl::add_vertices(*this, n);
}
[[nodiscard]] gl_attr_force_inline size_type in_degree(id_type vertex_id) const {
return specialized_impl::in_degree(*this, vertex_id);
}
[[nodiscard]] gl_attr_force_inline size_type out_degree(id_type vertex_id) const {
return specialized_impl::out_degree(*this, vertex_id);
}
[[nodiscard]] gl_attr_force_inline size_type degree(id_type vertex_id) const {
return specialized_impl::degree(*this, vertex_id);
}
[[nodiscard]] gl_attr_force_inline std::vector<size_type> in_degree_map() const {
return specialized_impl::in_degree_map(*this);
}
[[nodiscard]] gl_attr_force_inline std::vector<size_type> out_degree_map() const {
return specialized_impl::out_degree_map(*this);
}
[[nodiscard]] gl_attr_force_inline std::vector<size_type> degree_map() const {
return specialized_impl::degree_map(*this);
}
std::vector<id_type> remove_vertex(id_type vertex_id) {
auto removed_edge_ids = specialized_impl::remove_vertex(*this, vertex_id);
this->_remap_element_ids(removed_edge_ids);
return removed_edge_ids;
}
// --- edge methods ---
gl_attr_force_inline void add_edge(id_type id, id_type source_id, id_type target_id) {
specialized_impl::add_edge(*this, id, source_id, target_id);
}
gl_attr_force_inline void add_edges_from(
const traits::c_forward_range_of<id_type> auto& edge_ids,
id_type source_id,
const traits::c_forward_range_of<id_type> auto& target_ids
) {
specialized_impl::add_edges_from(*this, edge_ids, source_id, target_ids);
}
[[nodiscard]] gl_attr_force_inline bool has_edge(id_type source_id, id_type target_id) const {
return specialized_impl::get_entry(*this, source_id, target_id) != invalid_id;
}
[[nodiscard]] bool has_edge(const edge_type& edge) const {
return specialized_impl::get_entry(*this, edge.source(), edge.target()) == edge.id();
}
[[nodiscard]] std::optional<edge_type> get_edge(id_type source_id, id_type target_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
const auto edge_id = specialized_impl::get_entry(*this, source_id, target_id);
if (edge_id == invalid_id)
return std::nullopt;
return std::make_optional<edge_type>(edge_id, source_id, target_id);
}
[[nodiscard]] std::optional<edge_type> get_edge(
id_type source_id, id_type target_id, const auto& edge_properties_map
) const
requires(traits::c_has_non_empty_properties<edge_type>)
{
const auto edge_id = specialized_impl::get_entry(*this, source_id, target_id);
if (edge_id == invalid_id)
return std::nullopt;
return std::make_optional<edge_type>(
edge_id, source_id, target_id, *edge_properties_map[to_idx(edge_id)]
);
}
[[nodiscard]] std::vector<edge_type> get_edges(id_type source_id, id_type target_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
const auto edge_id = specialized_impl::get_entry(*this, source_id, target_id);
if (edge_id == invalid_id)
return std::vector<edge_type>();
return std::vector<edge_type>{
edge_type{edge_id, source_id, target_id}
};
}
[[nodiscard]] std::vector<edge_type> get_edges(
id_type source_id, id_type target_id, const auto& edge_properties_map
) const
requires(traits::c_has_non_empty_properties<edge_type>)
{
const auto edge_id = specialized_impl::get_entry(*this, source_id, target_id);
if (edge_id == invalid_id)
return std::vector<edge_type>();
return std::vector<edge_type>{
edge_type{edge_id, source_id, target_id, *edge_properties_map[to_idx(edge_id)]}
};
}
gl_attr_force_inline void remove_edge(const edge_type& edge) {
specialized_impl::remove_edge(*this, edge);
for (auto&& row : this->_matrix)
for (auto& edge_id : row)
if (edge_id != invalid_id and edge_id > edge.id())
edge_id--;
}
std::vector<id_type> remove_edges(const traits::c_range_of<edge_type> auto& edges) {
for (const auto& edge : edges)
specialized_impl::remove_edge(*this, edge);
auto removed_edge_ids =
edges | std::views::transform([](const auto& edge) { return edge.id(); })
| std::ranges::to<std::vector>();
this->_remap_element_ids(removed_edge_ids);
return removed_edge_ids;
}
[[nodiscard]] gl_attr_force_inline auto adjacent_edges(id_type vertex_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
return this->out_edges(vertex_id);
}
[[nodiscard]] gl_attr_force_inline auto adjacent_edges(
id_type vertex_id, const auto& edge_properties_map
) const
requires(traits::c_has_non_empty_properties<edge_type>)
{
return this->out_edges(vertex_id, edge_properties_map);
}
[[nodiscard]] gl_attr_force_inline auto in_edges(id_type vertex_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
return std::views::iota(initial_id_v<id_type>, this->_matrix.size())
| std::views::filter([this, vertex_id](const auto source_id) {
return specialized_impl::get_entry(*this, source_id, vertex_id) != invalid_id;
})
| std::views::transform([this, vertex_id](const auto source_id) {
return edge_type{
specialized_impl::get_entry(*this, source_id, vertex_id),
source_id,
vertex_id
};
});
}
[[nodiscard]] gl_attr_force_inline auto in_edges(
id_type vertex_id, const auto& edge_properties_map
) const
requires(traits::c_has_non_empty_properties<edge_type>)
{
return std::views::iota(initial_id_v<id_type>, this->_matrix.size())
| std::views::filter([this, vertex_id](const auto source_id) {
return specialized_impl::get_entry(*this, source_id, vertex_id) != invalid_id;
})
| std::views::transform([this, vertex_id, &edge_properties_map](const auto source_id) {
const auto edge_id = specialized_impl::get_entry(*this, source_id, vertex_id);
return edge_type{
edge_id, source_id, vertex_id, *edge_properties_map[to_idx(edge_id)]
};
});
}
[[nodiscard]] gl_attr_force_inline auto out_edges(id_type vertex_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
return this->_matrix[to_idx(vertex_id)] | std::views::enumerate
| std::views::filter([](const auto& edge_info) {
const auto& [target_id, edge_id] = edge_info;
return edge_id != invalid_id;
})
| std::views::transform([vertex_id](const auto& edge_info) {
const auto& [target_id, edge_id] = edge_info;
return edge_type{edge_id, vertex_id, static_cast<id_type>(target_id)};
});
}
[[nodiscard]] gl_attr_force_inline auto out_edges(
id_type vertex_id, const auto& edge_properties_map
) const
requires(traits::c_has_non_empty_properties<edge_type>)
{
return this->_matrix[to_idx(vertex_id)] | std::views::enumerate
| std::views::filter([](const auto& edge_info) {
const auto& [target_id, edge_id] = edge_info;
return edge_id != invalid_id;
})
| std::views::transform([vertex_id, &edge_properties_map](const auto& edge_info) {
const auto& [target_id, edge_id] = edge_info;
return edge_type{
edge_id,
vertex_id,
static_cast<id_type>(target_id),
*edge_properties_map[to_idx(edge_id)]
};
});
}
// --- access operators ---
[[nodiscard]] gl_attr_force_inline auto at(id_type vertex_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
return this->_matrix[to_idx(vertex_id)] | std::views::enumerate
| std::views::transform([vertex_id](const auto& edge_info) {
const auto& [target_id, edge_id] = edge_info;
return edge_id == invalid_id
? edge_type::invalid()
: edge_type{edge_id, vertex_id, static_cast<id_type>(target_id)};
});
}
[[nodiscard]] gl_attr_force_inline auto at(id_type vertex_id, const auto& edge_properties_map)
const
requires(traits::c_has_non_empty_properties<edge_type>)
{
return this->_matrix[to_idx(vertex_id)] | std::views::enumerate
| std::views::transform([vertex_id, &edge_properties_map](const auto& edge_info) {
const auto& [target_id, edge_id] = edge_info;
return edge_id == invalid_id
? edge_type::invalid()
: edge_type{
edge_id,
vertex_id,
static_cast<id_type>(target_id),
*edge_properties_map[to_idx(edge_id)]
};
});
}
// --- comparison ---
[[nodiscard]] friend bool operator==(const adjacency_matrix&, const adjacency_matrix&) =
default;
// --- friend declarations ---
template <traits::c_graph_impl_tag TargetImplTag, traits::c_graph_impl_tag SourceImplTag>
friend struct gl::detail::to_impl;
#ifdef GL_TESTING
friend struct gl_testing::test_adjacency_matrix;
#endif
private:
using specialized_impl =
typename specialized::adjacency_matrix_impl_traits<adjacency_matrix>::type;
friend specialized_impl;
void _remap_element_ids(std::vector<id_type>& removed_edge_ids) {
std::ranges::sort(removed_edge_ids);
removed_edge_ids.erase(
std::ranges::unique(removed_edge_ids).begin(), removed_edge_ids.end()
);
for (auto&& row : this->_matrix) {
for (auto& edge_id : row) {
if (edge_id == invalid_id)
continue;
auto it = std::ranges::lower_bound(removed_edge_ids, edge_id);
if (it != removed_edge_ids.end() and *it == edge_id)
edge_id = invalid_id; // edge was removed
else
// shift by the number of removed IDs < edge-id
edge_id -=
static_cast<id_type>(std::ranges::distance(removed_edge_ids.begin(), it));
}
}
}
adjacency_storage_type _matrix{};
};
} // namespace impl
} // namespace gl