Skip to content

Commit ccc48d6

Browse files
committed
aligned adjacency_matrix
1 parent 01505b1 commit ccc48d6

7 files changed

Lines changed: 703 additions & 738 deletions

File tree

include/gl/edge_descriptor.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class edge_descriptor final {
6464
return this->_vertices;
6565
}
6666

67+
// TODO: add tests
68+
[[nodiscard]] gl_attr_force_inline const types::homogeneous_pair<types::id_type> incident_vertex_ids() const {
69+
return std::make_pair(this->_vertices.first.id(), this->_vertices.second.id());
70+
}
71+
6772
[[nodiscard]] gl_attr_force_inline const vertex_type& first() const {
6873
return this->_vertices.first;
6974
}

include/gl/impl/adjacency_list.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class adjacency_list final {
9292

9393
gl_attr_force_inline void remove_vertex(const types::id_type& vertex_id) {
9494
specialized_impl::remove_vertex(*this, vertex_id);
95+
// TODO: align remaining vertex ids in edges
96+
// add tests - removing all vertices sequentially
9597
}
9698

9799
// --- edge methods ---

include/gl/impl/adjacency_matrix.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ class adjacency_matrix final {
110110
return specialized_impl::degree_map(*this);
111111
}
112112

113-
gl_attr_force_inline void remove_vertex(const vertex_type& vertex) {
114-
specialized_impl::remove_vertex(*this, vertex.id());
113+
gl_attr_force_inline void remove_vertex(const types::id_type vertex_id) {
114+
specialized_impl::remove_vertex(*this, vertex_id);
115+
// TODO: align remaining vertex ids in edges
116+
// add tests - removing all vertices sequentially
115117
}
116118

117119
// --- edge methods ---
@@ -138,8 +140,8 @@ class adjacency_matrix final {
138140
}
139141

140142
[[nodiscard]] bool has_edge(const edge_type& edge) const {
141-
const auto first_id = edge.first_id();
142-
const auto second_id = edge.second_id();
143+
const auto first_id = edge.first().id();
144+
const auto second_id = edge.second().id();
143145

144146
if (not (this->_is_valid_vertex_id(first_id) and this->_is_valid_vertex_id(second_id)))
145147
return false;

include/gl/impl/specialized/adjacency_matrix.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ template <type_traits::c_instantiation_of<adjacency_matrix> AdjacencyMatrix>
2323
typename AdjacencyMatrix::matrix_type& matrix, const typename AdjacencyMatrix::edge_type* edge
2424
) {
2525
// get the edge and validate the address
26-
auto& matrix_element = matrix.at(edge->first_id()).at(edge->second_id());
26+
auto& matrix_element = matrix.at(edge->first().id()).at(edge->second().id());
2727
if (edge != matrix_element.get())
2828
throw std::invalid_argument(std::format(
2929
"Got invalid edge [vertices = ({}, {}) | addr = {}]",
30-
edge->first_id(),
31-
edge->second_id(),
30+
edge->first().id(),
31+
edge->second().id(),
3232
io::format(edge)
3333
));
3434

@@ -136,7 +136,7 @@ struct directed_adjacency_matrix {
136136
static const edge_type& add_edge(impl_type& self, edge_ptr_type edge) {
137137
detail::check_edge_override<impl_type>(self, edge);
138138

139-
auto& matrix_element = self._matrix[edge->first_id()][edge->second_id()];
139+
auto& matrix_element = self._matrix[edge->first().id()][edge->second().id()];
140140
matrix_element = std::move(edge);
141141
++self._n_unique_edges;
142142

@@ -152,7 +152,7 @@ struct directed_adjacency_matrix {
152152

153153
auto& matrix_row_source = self._matrix[source_id];
154154
for (auto& edge : new_edges)
155-
matrix_row_source[edge->second_id()] = std::move(edge);
155+
matrix_row_source[edge->second().id()] = std::move(edge);
156156

157157
self._n_unique_edges += new_edges.size();
158158
}
@@ -231,8 +231,8 @@ struct undirected_adjacency_matrix {
231231
static const edge_type& add_edge(impl_type& self, edge_ptr_type edge) {
232232
detail::check_edge_override<impl_type>(self, edge);
233233

234-
const auto first_id = edge->first_id();
235-
const auto second_id = edge->second_id();
234+
const auto first_id = edge->first().id();
235+
const auto second_id = edge->second().id();
236236

237237
if (not edge->is_loop())
238238
self._matrix[second_id][first_id] = edge;
@@ -253,8 +253,8 @@ struct undirected_adjacency_matrix {
253253
auto& matrix_row_source = self._matrix[source_id];
254254
for (auto& edge : new_edges) {
255255
if (not edge->is_loop())
256-
self._matrix[edge->second_id()][source_id] = edge;
257-
matrix_row_source[edge->second_id()] = std::move(edge);
256+
self._matrix[edge->second().id()][source_id] = edge;
257+
matrix_row_source[edge->second().id()] = std::move(edge);
258258
}
259259

260260
self._n_unique_edges += new_edges.size();
@@ -265,8 +265,8 @@ struct undirected_adjacency_matrix {
265265
detail::strict_get<impl_type>(self._matrix, &edge) = nullptr;
266266
}
267267
else {
268-
const auto first_id = edge.first_id();
269-
const auto second_id = edge.second_id();
268+
const auto first_id = edge.first().id();
269+
const auto second_id = edge.second().id();
270270

271271
detail::strict_get<impl_type>(self._matrix, &edge) = nullptr;
272272
// if the edge was found in the first matrix cell,

tests/source/gl/test_adjacency_list.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ TEST_CASE_FIXTURE(
355355
) {
356356
init_complete_graph();
357357

358-
const auto& removed_vertex = vertices[constants::first_element_idx];
359-
sut.remove_vertex(removed_vertex.id());
358+
const auto removed_vertex_id = constants::first_element_idx;
359+
sut.remove_vertex(removed_vertex_id);
360360

361361
constexpr auto n_vertices_after_remove = constants::n_elements - constants::one_element;
362362
constexpr auto n_incident_edges_after_remove =
@@ -369,8 +369,8 @@ TEST_CASE_FIXTURE(
369369
constants::vertex_id_view | std::views::take(n_vertices_after_remove)) {
370370
const auto adjacent_edges = sut.adjacent_edges(vertex_id);
371371
REQUIRE_EQ(adjacent_edges.distance(), n_incident_edges_after_remove);
372-
CHECK_FALSE(std::ranges::any_of(adjacent_edges, [&removed_vertex](const auto& edge) {
373-
return edge.is_incident_with(removed_vertex);
372+
CHECK_FALSE(std::ranges::any_of(adjacent_edges, [removed_vertex_id](const auto& edge) {
373+
return edge.is_incident_with(removed_vertex_id);
374374
}));
375375
}
376376
}
@@ -679,8 +679,8 @@ TEST_CASE_FIXTURE(
679679
) {
680680
init_complete_graph();
681681

682-
const auto& removed_vertex = vertices[constants::first_element_idx];
683-
sut.remove_vertex(removed_vertex.id());
682+
const auto removed_vertex_id = constants::first_element_idx;
683+
sut.remove_vertex(removed_vertex_id);
684684

685685
constexpr auto n_vertices_after_remove = constants::n_elements - constants::one_element;
686686
constexpr auto n_incident_edges_after_remove =
@@ -693,8 +693,8 @@ TEST_CASE_FIXTURE(
693693
constants::vertex_id_view | std::views::take(n_vertices_after_remove)) {
694694
const auto adjacent_edges = sut.adjacent_edges(vertex_id);
695695
REQUIRE_EQ(adjacent_edges.distance(), n_incident_edges_after_remove);
696-
CHECK_FALSE(std::ranges::any_of(adjacent_edges, [&removed_vertex](const auto& edge) {
697-
return edge.is_incident_with(removed_vertex);
696+
CHECK_FALSE(std::ranges::any_of(adjacent_edges, [removed_vertex_id](const auto& edge) {
697+
return edge.is_incident_with(removed_vertex_id);
698698
}));
699699
}
700700
}

0 commit comments

Comments
 (0)