-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_adjacency_matrix.hpp
More file actions
311 lines (251 loc) · 11.3 KB
/
Copy pathflat_adjacency_matrix.hpp
File metadata and controls
311 lines (251 loc) · 11.3 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
// 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/decl/impl_tags.hpp"
#include "gl/graph_traits.hpp"
#include "gl/impl/specialized/adjacency_matrix.hpp"
#include "gl/types/core.hpp"
#include "gl/types/flat_matrix.hpp"
#include <algorithm>
#include <cstddef>
#include <format>
#include <ranges>
#include <vector>
namespace gl::impl::specialized {
template <traits::c_instantiation_of<adjacency_matrix> AdjacencyMatrix>
requires(traits::c_directed_edge<typename AdjacencyMatrix::edge_type>)
struct directed_flat_adjacency_matrix {
using impl_type = AdjacencyMatrix;
using id_type = typename impl_type::id_type;
using storage_type = typename impl_type::adjacency_storage_type;
using vertex_type = typename impl_type::vertex_type;
using edge_type = typename impl_type::edge_type;
static void init(impl_type& self, size_type n_vertices) {
self._matrix = storage_type(n_vertices, n_vertices, invalid_id);
}
static void add_vertex(impl_type& self) {
const auto new_size = self._matrix.n_rows() + 1uz;
self._matrix.resize(new_size, new_size, invalid_id);
}
static void add_vertices(impl_type& self, size_type n) {
const auto new_size = self._matrix.n_rows() + n;
self._matrix.resize(new_size, new_size, invalid_id);
}
[[nodiscard]] gl_attr_force_inline static size_type in_degree(
const impl_type& self, id_type vertex_id
) {
return static_cast<size_type>(std::ranges::count_if(
self._matrix.col(to_idx(vertex_id)), [](auto edge_id) { return edge_id != invalid_id; }
));
}
[[nodiscard]] gl_attr_force_inline static size_type out_degree(
const impl_type& self, id_type vertex_id
) {
const auto row = self._matrix[to_idx(vertex_id)];
return row.size() - static_cast<size_type>(std::ranges::count(row, invalid_id_v<id_type>));
}
[[nodiscard]] gl_attr_force_inline static size_type degree(
const impl_type& self, id_type vertex_id
) {
size_type deg = 0uz;
const auto vertex_idx = to_idx(vertex_id);
const auto row = self._matrix[vertex_idx];
const auto col = self._matrix.col(vertex_idx);
for (auto v_idx = 0uz; v_idx < self._matrix.n_rows(); ++v_idx)
deg += static_cast<size_type>(row[v_idx] != invalid_id)
+ static_cast<size_type>(col[static_cast<std::ptrdiff_t>(v_idx)] != invalid_id);
return deg;
}
[[nodiscard]] static std::vector<size_type> in_degree_map(const impl_type& self) {
std::vector<size_type> in_degree_map(self._matrix.n_rows(), 0uz);
for (const auto row : self._matrix.rows())
for (auto [target_id, edge_id] : std::views::enumerate(row))
in_degree_map[static_cast<size_type>(target_id)] +=
static_cast<size_type>(edge_id != invalid_id);
return in_degree_map;
}
[[nodiscard]] static std::vector<size_type> out_degree_map(const impl_type& self) {
return std::views::iota(initial_id_v<id_type>, static_cast<id_type>(self._matrix.n_rows()))
| std::views::transform([&](id_type id) { return out_degree(self, id); })
| std::ranges::to<std::vector>();
}
[[nodiscard]] static std::vector<size_type> degree_map(const impl_type& self) {
std::vector<size_type> degree_map(self._matrix.n_rows(), 0uz);
for (auto src_idx = 0uz; src_idx < self._matrix.n_rows(); ++src_idx) {
for (auto tgt_idx = 0uz; tgt_idx < self._matrix.n_cols(); ++tgt_idx) {
if (self._matrix[src_idx, tgt_idx] != invalid_id) {
++degree_map[src_idx];
++degree_map[tgt_idx];
}
}
}
return degree_map;
}
static std::vector<id_type> remove_vertex(impl_type& self, id_type vertex_id) {
const auto vertex_idx = to_idx(vertex_id);
std::vector<id_type> removed_edges;
removed_edges.reserve(self._matrix.size());
// extract out-edges
for (auto edge_id : self._matrix[vertex_idx])
if (edge_id != invalid_id)
removed_edges.push_back(edge_id);
// extract in-edges
const auto col = self._matrix.col(vertex_idx);
for (auto r_idx = 0uz; r_idx < self._matrix.n_rows(); ++r_idx) {
if (r_idx == vertex_idx)
continue;
const auto edge_id = col[static_cast<std::ptrdiff_t>(r_idx)];
if (edge_id != invalid_id)
removed_edges.push_back(edge_id);
}
// elegantly remove from the 2D grid
self._matrix.erase_row(vertex_idx);
self._matrix.erase_col(vertex_idx);
return removed_edges;
}
static inline void add_edge(
impl_type& self, id_type edge_id, id_type source_id, id_type target_id
) {
detail::check_edge_override(self._matrix, source_id, target_id);
self._matrix[to_idx(source_id), to_idx(target_id)] = edge_id;
}
static void add_edges_from(
impl_type& self,
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
) {
for (const auto target_id : target_ids)
detail::check_edge_override(self._matrix, source_id, target_id);
auto matrix_source_row = self._matrix[to_idx(source_id)];
for (auto [edge_id, target_id] : std::views::zip(edge_ids, target_ids))
matrix_source_row[to_idx(target_id)] = edge_id;
}
static inline void remove_edge(impl_type& self, const edge_type& edge) {
detail::strict_get(self._matrix, edge) = invalid_id;
}
};
template <traits::c_instantiation_of<adjacency_matrix> AdjacencyMatrix>
requires(traits::c_undirected_edge<typename AdjacencyMatrix::edge_type>)
struct undirected_flat_adjacency_matrix {
using impl_type = AdjacencyMatrix;
using id_type = typename impl_type::id_type;
using storage_type = typename impl_type::adjacency_storage_type;
using vertex_type = typename impl_type::vertex_type;
using edge_type = typename impl_type::edge_type;
static void init(impl_type& self, size_type n_vertices) {
self._matrix = storage_type(n_vertices, n_vertices, invalid_id);
}
static void add_vertex(impl_type& self) {
const auto new_size = self._matrix.n_rows() + 1uz;
self._matrix.resize(new_size, new_size, invalid_id);
}
static void add_vertices(impl_type& self, size_type n) {
const auto new_size = self._matrix.n_rows() + n;
self._matrix.resize(new_size, new_size, invalid_id);
}
[[nodiscard]] gl_attr_force_inline static size_type in_degree(
const impl_type& self, id_type vertex_id
) {
return degree(self, vertex_id);
}
[[nodiscard]] gl_attr_force_inline static size_type out_degree(
const impl_type& self, id_type vertex_id
) {
return degree(self, vertex_id);
}
[[nodiscard]] gl_attr_force_inline static size_type degree(
const impl_type& self, id_type vertex_id
) {
const auto vertex_idx = to_idx(vertex_id);
const auto row = self._matrix[vertex_idx];
return self._matrix.n_cols()
- static_cast<size_type>(std::ranges::count(row, invalid_id_v<id_type>))
+ static_cast<size_type>(self._matrix[vertex_idx, vertex_idx] != invalid_id);
}
[[nodiscard]] gl_attr_force_inline static std::vector<size_type> in_degree_map(
const impl_type& self
) {
return degree_map(self);
}
[[nodiscard]] gl_attr_force_inline static std::vector<size_type> out_degree_map(
const impl_type& self
) {
return degree_map(self);
}
[[nodiscard]] static std::vector<size_type> degree_map(const impl_type& self) {
std::vector<size_type> degree_map(self._matrix.n_rows(), 0uz);
for (auto src_idx = 0uz; src_idx < self._matrix.n_rows(); ++src_idx) {
for (auto tgt_idx = 0uz; tgt_idx <= src_idx; ++tgt_idx) {
if (self._matrix[src_idx, tgt_idx] != invalid_id) {
++degree_map[src_idx];
++degree_map[tgt_idx];
}
}
}
return degree_map;
}
static std::vector<id_type> remove_vertex(impl_type& self, id_type vertex_id) {
const auto vertex_idx = to_idx(vertex_id);
const auto removed_edges =
self._matrix[vertex_idx]
| std::views::filter([](auto edge_id) { return edge_id != invalid_id; })
| std::ranges::to<std::vector>();
self._matrix.erase_row(vertex_idx);
self._matrix.erase_col(vertex_idx);
return removed_edges;
}
static void add_edge(impl_type& self, id_type edge_id, id_type source_id, id_type target_id) {
detail::check_edge_override(self._matrix, source_id, target_id);
const auto source_idx = to_idx(source_id);
const auto target_idx = to_idx(target_id);
self._matrix[source_idx, target_idx] = edge_id;
if (target_idx != source_idx)
self._matrix[target_idx, source_idx] = edge_id;
}
static void add_edges_from(
impl_type& self,
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
) {
for (const auto target_id : target_ids)
detail::check_edge_override(self._matrix, source_id, target_id);
const auto source_idx = to_idx(source_id);
auto matrix_source_row = self._matrix[source_idx];
for (auto [edge_id, target_id] : std::views::zip(edge_ids, target_ids)) {
const auto target_idx = to_idx(target_id);
matrix_source_row[target_idx] = edge_id;
if (target_idx != source_idx)
self._matrix[target_idx, source_idx] = edge_id;
}
}
static void remove_edge(impl_type& self, const edge_type& edge) {
if (edge.is_loop()) {
detail::strict_get(self._matrix, edge) = invalid_id;
}
else {
detail::strict_get(self._matrix, edge) = invalid_id;
self._matrix[to_idx(edge.target()), to_idx(edge.source())] = invalid_id;
}
}
};
template <traits::c_instantiation_of<adjacency_matrix> AdjacencyMatrix>
requires traits::c_directed_edge<typename AdjacencyMatrix::edge_type>
and std::same_as<typename AdjacencyMatrix::implementation_tag, flat_matrix_t>
struct adjacency_matrix_impl_traits<AdjacencyMatrix> {
using type = directed_flat_adjacency_matrix<AdjacencyMatrix>;
template <traits::c_id_type IdType>
using storage_type = flat_matrix<IdType>;
};
template <traits::c_instantiation_of<adjacency_matrix> AdjacencyMatrix>
requires traits::c_undirected_edge<typename AdjacencyMatrix::edge_type>
and std::same_as<typename AdjacencyMatrix::implementation_tag, flat_matrix_t>
struct adjacency_matrix_impl_traits<AdjacencyMatrix> {
using type = undirected_flat_adjacency_matrix<AdjacencyMatrix>;
template <traits::c_id_type IdType>
using storage_type = flat_matrix<IdType>;
};
} // namespace gl::impl::specialized