Skip to content

Commit e4ffb44

Browse files
committed
aligned test_graph
1 parent 9ce2cdc commit e4ffb44

5 files changed

Lines changed: 925 additions & 971 deletions

File tree

include/gl/graph.hpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ class graph final {
8888
requires(not type_traits::is_default_properties_type_v<vertex_properties_type>)
8989
{
9090
return std::views::enumerate(this->_vertex_properties)
91-
| std::views::transform([](types::id_type id, const auto& properties_ptr) {
92-
return vertex_descriptor{id, *properties_ptr};
91+
| std::views::transform([](const auto& vertex_data) {
92+
const auto& [id, properties_ptr] = vertex_data;
93+
return vertex_descriptor{static_cast<types::id_type>(id), *properties_ptr};
9394
});
9495
}
9596

@@ -115,14 +116,18 @@ class graph final {
115116
return vertex_id < this->_n_vertices;
116117
}
117118

119+
[[nodiscard]] gl_attr_force_inline bool has_vertex(const vertex_type& vertex) const {
120+
return this->has_vertex(vertex.id());
121+
}
122+
118123
vertex_type add_vertex() {
119124
this->_impl.add_vertex();
120125
const auto new_vertex_id = this->_n_vertices++;
121126

122127
if constexpr (type_traits::is_default_properties_type_v<vertex_properties_type>)
123-
return vertex_descriptor{new_vertex_id, *this->_vertex_properties.emplace_back()};
124-
else
125128
return vertex_descriptor{new_vertex_id};
129+
else
130+
return vertex_descriptor{new_vertex_id, *this->_vertex_properties.emplace_back()};
126131
}
127132

128133
vertex_type add_vertex(vertex_properties_type properties)
@@ -140,7 +145,7 @@ class graph final {
140145
void add_vertices(const types::size_type n) {
141146
this->_impl.add_vertices(n);
142147
this->_n_vertices += n;
143-
if constexpr (type_traits::is_default_properties_type_v<vertex_properties_type>)
148+
if constexpr (not type_traits::is_default_properties_type_v<vertex_properties_type>)
144149
this->_vertex_properties.resize(this->_n_vertices);
145150
}
146151

@@ -153,17 +158,23 @@ class graph final {
153158
this->_impl.add_vertices(n);
154159
this->_n_vertices += n;
155160

156-
if constexpr (type_traits::is_default_properties_type_v<vertex_properties_type>)
157-
this->_vertex_properties.append_range(properties_range);
161+
if constexpr (not type_traits::is_default_properties_type_v<vertex_properties_type>) {
162+
for (auto& properties : properties_range) {
163+
this->_vertex_properties.emplace_back(
164+
std::make_unique<vertex_properties_type>(properties)
165+
);
166+
}
167+
}
158168
}
159169

160170
gl_attr_force_inline void remove_vertex(const types::size_type vertex_id) {
171+
this->_verify_vertex_id(vertex_id);
161172
this->_remove_vertex_impl(vertex_id);
162173
}
163174

164175
inline void remove_vertex(const vertex_type& vertex) {
165176
this->_verify_vertex(vertex);
166-
this->_remove_vertex_impl(vertex);
177+
this->_remove_vertex_impl(vertex.id());
167178
}
168179

169180
template <type_traits::c_sized_range_of<types::id_type> IdRange>
@@ -197,7 +208,7 @@ class graph final {
197208

198209
// TODO: optimize
199210
for (const auto& vertex_ref : vertex_ref_set)
200-
this->_remove_vertex_impl(vertex_ref.get());
211+
this->_remove_vertex_impl(vertex_ref.get().id());
201212
}
202213

203214
[[nodiscard]] gl_attr_force_inline types::size_type in_degree(const vertex_type& vertex) const {
@@ -358,6 +369,9 @@ class graph final {
358369
) const {
359370
using edge_ref_set = std::vector<types::const_ref_wrap<edge_type>>;
360371

372+
this->_verify_vertex_id(first_id);
373+
this->_verify_vertex_id(second_id);
374+
361375
if constexpr (std::same_as<implementation_tag, impl::list_t>) {
362376
return this->_impl.get_edges(first_id, second_id);
363377
}
@@ -370,8 +384,6 @@ class graph final {
370384
[[nodiscard]] std::vector<types::const_ref_wrap<edge_type>> get_edges(
371385
const vertex_type& first, const vertex_type& second
372386
) const {
373-
this->_verify_vertex(first);
374-
this->_verify_vertex(second);
375387
return this->get_edges(first.id(), second.id());
376388
}
377389

@@ -497,20 +509,21 @@ class graph final {
497509

498510
void _remove_vertex_impl(const types::id_type vertex_id) {
499511
this->_impl.remove_vertex(vertex_id);
500-
if constexpr (type_traits::is_default_properties_type_v<vertex_properties_type>)
501-
this->_vertex_properties.erase(
502-
std::next(std::begin(this->_vertex_properties), vertex_id)
503-
);
504512
this->_n_vertices--;
505513

506514
// update vertex ids in edges
507515
// TODO: add tests
508516
for (auto id : this->vertex_ids()) {
509517
for (auto& edge : this->_impl.adjacent_edges(id)) {
510-
edge->_vertices.first._id -= (edge->_vertices.first._id >= vertex_id);
511-
edge->_vertices.second._id -= (edge->_vertices.second._id >= vertex_id);
518+
edge._vertices.first._id -= (edge._vertices.first._id > vertex_id);
519+
edge._vertices.second._id -= (edge._vertices.second._id > vertex_id);
512520
}
513521
}
522+
523+
if constexpr (not type_traits::is_default_properties_type_v<vertex_properties_type>)
524+
this->_vertex_properties.erase(
525+
std::next(std::begin(this->_vertex_properties), vertex_id)
526+
);
514527
}
515528

516529
// --- io methods ---

include/gl/vertex_descriptor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class vertex_descriptor final {
9999
}
100100
}
101101

102-
types::id_type _id;
102+
mutable types::id_type _id;
103103
[[no_unique_address]] properties_ref_type _properties;
104104
};
105105

tests/include/testing/gl/constants.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ IC gl::types::size_type depth = 5ull;
2929

3030
IC gl::types::size_type first_element_idx = zero;
3131
IC gl::types::size_type last_element_idx = n_elements - one_element;
32-
IC gl::types::size_type out_of_range_elemenet_idx = n_elements;
32+
IC gl::types::size_type out_of_range_element_idx = n_elements;
3333

3434
IC gl::types::id_type vertex_id_1 = first_element_idx;
3535
IC gl::types::id_type vertex_id_2 = vertex_id_1 + one_element;

0 commit comments

Comments
 (0)