Skip to content

Commit 8fcf046

Browse files
committed
resolved comments
1 parent 7bf349f commit 8fcf046

9 files changed

Lines changed: 67 additions & 41 deletions

File tree

include/gl/edge_descriptor.hpp

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

67-
// TODO: add tests
6867
[[nodiscard]] gl_attr_force_inline const types::homogeneous_pair<types::id_type> incident_vertex_ids() const {
6968
return std::make_pair(this->_vertices.first.id(), this->_vertices.second.id());
7069
}

include/gl/graph.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "impl/impl_tags.hpp"
1010
#include "io/stream_options_manipulator.hpp"
1111
#include "types/iterator_range.hpp"
12+
#include "views.hpp"
1213

1314
#include <set>
1415

@@ -83,10 +84,10 @@ class graph final {
8384
[[nodiscard]] gl_attr_force_inline auto vertices() const
8485
requires(not type_traits::is_default_properties_type_v<vertex_properties_type>)
8586
{
86-
return std::views::enumerate(this->_vertex_properties)
87-
| std::views::transform([](const auto& vertex_data) {
88-
const auto& [id, properties_ptr] = vertex_data;
89-
return vertex_descriptor{static_cast<types::id_type>(id), *properties_ptr};
87+
return this->_vertex_properties | std::views::enumerate
88+
| std::views::transform([](const auto& x) {
89+
const auto& [id, ptr] = x;
90+
return vertex_descriptor{static_cast<types::id_type>(id), *ptr};
9091
});
9192
}
9293

@@ -123,7 +124,6 @@ class graph final {
123124
if constexpr (type_traits::is_default_properties_type_v<vertex_properties_type>)
124125
return vertex_descriptor{new_vertex_id};
125126
else {
126-
// ensure a properties object is created for the new vertex
127127
this->_vertex_properties.push_back(std::make_unique<vertex_properties_type>());
128128
return vertex_descriptor{new_vertex_id, *this->_vertex_properties.back()};
129129
}
@@ -259,6 +259,12 @@ class graph final {
259259
return this->_impl.degree_map();
260260
}
261261

262+
[[nodiscard]] gl_attr_force_inline auto vertex_properties_map() const noexcept
263+
requires(not type_traits::is_default_properties_type_v<vertex_properties_type>)
264+
{
265+
return views::deref(this->_vertex_properties);
266+
}
267+
262268
// --- edge methods ---
263269

264270
// clang-format off
@@ -514,7 +520,6 @@ class graph final {
514520
this->_n_vertices--;
515521

516522
// update vertex ids in edges
517-
// TODO: add tests
518523
for (auto id : this->vertex_ids()) {
519524
for (auto& edge : this->_impl.adjacent_edges(id)) {
520525
edge._vertices.first._id -= (edge._vertices.first._id > vertex_id);

include/gl/types/properties.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class name_property
3737

3838
name_property() = default;
3939

40-
name_property(std::string_view name) : _name(name) {}
40+
name_property(const std::string_view name) : _name(name) {}
4141

4242
name_property(const name_property&) = default;
4343
name_property(name_property&&) = default;
@@ -51,6 +51,11 @@ class name_property
5151
virtual ~name_property() = default;
5252
#endif
5353

54+
name_property& operator=(const std::string_view name) {
55+
this->_name = name;
56+
return *this;
57+
}
58+
5459
// clang-format off
5560
// gl_attr_force_inline misplacement
5661

@@ -63,6 +68,14 @@ class name_property
6368
[[nodiscard]] bool operator==(const name_property&) const = default;
6469
[[nodiscard]] auto operator<=>(const name_property&) const = default;
6570

71+
[[nodiscard]] bool operator==(const std::string_view name) const {
72+
return this->_name == name;
73+
}
74+
75+
[[nodiscard]] auto operator<=>(const std::string_view name) const {
76+
return this->_name <=> name;
77+
}
78+
6679
friend std::ostream& operator<<(std::ostream& os, const name_property& property) {
6780
os << std::quoted(property._name);
6881
return os;

include/gl/views.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2024-2026 Jakub Musiał
2+
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
3+
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4+
5+
#pragma once
6+
7+
namespace gl::views {
8+
9+
inline constexpr auto deref = std::views::transform([](auto&& p) -> decltype(auto) { return *p; });
10+
11+
} // namespace gl::views

task.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/source/gl/test_alg_coloring.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
#include <doctest.h>
99

10-
#include <print>
11-
1210
namespace gl_testing {
1311

1412
TEST_SUITE_BEGIN("test_alg_coloring");

tests/source/gl/test_graph.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,30 @@ TEST_CASE_TEMPLATE_INSTANTIATE(
885885
gl::matrix_graph_traits<gl::undirected_t> // undirected adjacency matrix
886886
);
887887

888-
TEST_SUITE_END(); // test_graph
888+
TEST_CASE_TEMPLATE_DEFINE(
889+
"vertex_properties_map() should return a correct map", TraitsType, vp_graph_traits_template
890+
) {
891+
using sut_type = gl::graph<TraitsType>;
892+
893+
sut_type sut{constants::n_elements};
894+
895+
for (auto vertex : sut.vertices())
896+
vertex.properties() = std::format("vertex_{}", vertex.id());
897+
898+
auto map = sut.vertex_properties_map();
899+
CHECK(map.size() == constants::n_elements);
900+
for (auto [id, property] : std::views::zip(sut.vertex_ids(), map)) {
901+
CHECK_EQ(property, std::format("vertex_{}", id));
902+
CHECK_EQ(map[id], std::format("vertex_{}", id));
903+
}
904+
}
905+
906+
TEST_CASE_TEMPLATE_INSTANTIATE(
907+
vp_graph_traits_template,
908+
gl::list_graph_traits<gl::directed_t, gl::types::name_property>, // directed adjacency list
909+
gl::list_graph_traits<gl::undirected_t, gl::types::name_property>, // undirected adjacency list
910+
gl::matrix_graph_traits<gl::directed_t, gl::types::name_property>, // directed adjacency matrix
911+
gl::matrix_graph_traits<gl::undirected_t, gl::types::name_property> // undirected adjacency matrix
912+
);
889913

890914
} // namespace gl_testing

tests/source/gl/test_graph_file_io.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ struct test_graph_file_io {
4646
// prepare vertex and edge properties
4747
std::size_t v_idx = 0, e_idx = 0;
4848
for (const auto& vertex : sut_out.vertices()) {
49-
vertex.properties() = {std::format("vertex_{}", v_idx++)};
49+
vertex.properties() = std::format("vertex_{}", v_idx++);
5050
for (const auto& edge : sut_out.adjacent_edges(vertex))
51-
edge.properties() = {std::format("edge_{}", e_idx++)};
51+
edge.properties() = std::format("edge_{}", e_idx++);
5252
}
5353
}
5454

tests/source/gl/test_graph_io.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ struct test_directed_graph_io {
2828
// prepare vertex and edge properties
2929
std::size_t v_idx = 0, e_idx = 0;
3030
for (const auto& vertex : sut_out.vertices()) {
31-
vertex.properties() = {std::format("vertex_{}", v_idx++)};
31+
vertex.properties() = std::format("vertex_{}", v_idx++);
3232
for (const auto& edge : sut_out.adjacent_edges(vertex))
33-
edge.properties() = {std::format("edge_{}", e_idx++)};
33+
edge.properties() = std::format("edge_{}", e_idx++);
3434
}
3535
}
3636

@@ -138,10 +138,10 @@ struct test_undirected_graph_io {
138138
// prepare vertex and edge properties
139139
std::size_t v_idx = 0, e_idx = 0;
140140
for (const auto& vertex : sut_out.vertices()) {
141-
vertex.properties() = {std::format("vertex_{}", v_idx++)};
141+
vertex.properties() = std::format("vertex_{}", v_idx++);
142142
for (const auto& edge : sut_out.adjacent_edges(vertex))
143143
if (edge.first().id() == vertex.id())
144-
edge.properties() = {std::format("edge_{}", e_idx++)};
144+
edge.properties() = std::format("edge_{}", e_idx++);
145145
}
146146
}
147147

0 commit comments

Comments
 (0)