Skip to content

Commit 285fcca

Browse files
committed
aligned adjacency_matrix
1 parent a47665f commit 285fcca

3 files changed

Lines changed: 494 additions & 497 deletions

File tree

include/gl/impl/specialized/adjacency_matrix.hpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,9 @@ struct directed_adjacency_matrix {
5454
[[nodiscard]] gl_attr_force_inline static types::size_type in_degree(
5555
const impl_type& self, const types::id_type vertex_id
5656
) {
57-
return self._matrix.size()
58-
- std::ranges::count_if(
59-
self._matrix,
60-
constants::invalid_id,
61-
[vertex_id](const auto& row) { return row[vertex_id]; }
62-
);
57+
return std::ranges::count_if(self._matrix, [vertex_id](const auto& row) {
58+
return row[vertex_id] != constants::invalid_id;
59+
});
6360
}
6461

6562
[[nodiscard]] gl_attr_force_inline static types::size_type out_degree(
@@ -121,10 +118,14 @@ struct directed_adjacency_matrix {
121118
static std::vector<types::id_type> remove_vertex(
122119
impl_type& self, const types::id_type vertex_id
123120
) {
124-
auto removed_edges =
121+
auto removed_edges_view =
125122
self._matrix[vertex_id]
126-
| std::views::filter([](auto edge_id) { return edge_id != constants::invalid_id; })
127-
| std::ranges::to<std::vector>;
123+
| std::views::filter([](auto edge_id) { return edge_id != constants::invalid_id; });
124+
125+
// TODO: use std::ranges::to (requires newer compiler)
126+
std::vector<types::id_type> removed_edges(
127+
removed_edges_view.begin(), removed_edges_view.end()
128+
);
128129

129130
self._matrix.erase(std::next(std::begin(self._matrix), vertex_id));
130131

@@ -210,7 +211,7 @@ struct undirected_adjacency_matrix {
210211

211212
for (types::id_type source_id = constants::initial_id; source_id < self._matrix.size();
212213
++source_id) {
213-
for (types::id_type target_id = constants::initial_id; target_id < self._matrix.size();
214+
for (types::id_type target_id = constants::initial_id; target_id <= source_id;
214215
++target_id) {
215216
if (self._matrix[source_id][target_id] != constants::invalid_id) {
216217
++degree_map[source_id];
@@ -225,14 +226,18 @@ struct undirected_adjacency_matrix {
225226
static std::vector<types::id_type> remove_vertex(
226227
impl_type& self, const types::id_type vertex_id
227228
) {
229+
auto removed_edges_view =
230+
self._matrix[vertex_id]
231+
| std::views::filter([](auto edge_id) { return edge_id != constants::invalid_id; });
232+
// TODO: use std::ranges::to (requires newer compiler)
233+
std::vector<types::id_type> removed_edges(
234+
removed_edges_view.begin(), removed_edges_view.end()
235+
);
236+
237+
self._matrix.erase(std::next(std::begin(self._matrix), vertex_id));
228238
for (auto& row : self._matrix)
229239
row.erase(std::next(std::begin(row), vertex_id));
230240

231-
const auto removed_edges =
232-
self._matrix[vertex_id]
233-
| std::views::filter([](auto edge_id) { return edge_id != constants::invalid_id; })
234-
| std::ranges::to<std::vector>;
235-
self._matrix.erase(std::next(std::begin(self._matrix), vertex_id));
236241
return removed_edges;
237242
}
238243

@@ -265,10 +270,10 @@ struct undirected_adjacency_matrix {
265270

266271
static void remove_edge(impl_type& self, const edge_type& edge) {
267272
if (edge.is_loop()) {
268-
detail::strict_get<impl_type>(self._matrix, edge) = constants::invalid_id;
273+
detail::strict_get(self._matrix, edge) = constants::invalid_id;
269274
}
270275
else {
271-
detail::strict_get<impl_type>(self._matrix, edge) = constants::invalid_id;
276+
detail::strict_get(self._matrix, edge) = constants::invalid_id;
272277
// if the edge was found in the first matrix cell,
273278
// it will also be present in the second matrix cell
274279
self._matrix[edge.second()][edge.first()] = constants::invalid_id;

include/gl/util/ranges.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace gl::util {
88

9+
// TODO: add tests
910
template <std::ranges::range R>
1011
constexpr auto range_size(R&& r) {
1112
if constexpr (std::ranges::sized_range<R>)

0 commit comments

Comments
 (0)