Skip to content

Commit 56fab37

Browse files
committed
wip:
1 parent 66b10a3 commit 56fab37

4 files changed

Lines changed: 82 additions & 16 deletions

File tree

include/hgl/hypergraph.hpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ class hypergraph final {
7272
}
7373

7474
[[nodiscard]] vertex_type get_vertex(const types::id_type vertex_id) const {
75-
// this->_verify_vertex_id(vertex_id);
75+
this->_verify_vertex_id(vertex_id);
7676
if constexpr (type_traits::c_non_empty_properties<vertex_properties_type>)
77-
return vertex_descriptor{vertex_id, *this->_vertex_properties[vertex_id]};
77+
return vertex_type{vertex_id, *this->_vertex_properties[vertex_id]};
7878
else
79-
return vertex_descriptor{vertex_id};
79+
return vertex_type{vertex_id};
8080
}
8181

8282
[[nodiscard]] gl_attr_force_inline bool has_vertex(const types::id_type vertex_id) const {
@@ -92,12 +92,12 @@ class hypergraph final {
9292
const auto new_vertex_id = this->_n_vertices++;
9393

9494
if constexpr (type_traits::c_non_empty_properties<vertex_properties_type>)
95-
return vertex_descriptor{
95+
return vertex_type{
9696
new_vertex_id,
9797
*this->_vertex_properties.emplace_back(std::make_unique<vertex_properties_type>())
9898
};
9999
else
100-
return vertex_descriptor{new_vertex_id};
100+
return vertex_type{new_vertex_id};
101101
}
102102

103103
const vertex_type add_vertex_with(vertex_properties_type properties)
@@ -107,7 +107,7 @@ class hypergraph final {
107107
this->_vertex_properties.push_back(
108108
std::make_unique<vertex_properties_type>(std::move(properties))
109109
);
110-
return vertex_descriptor{this->_n_vertices++, *this->_vertex_properties.back()};
110+
return vertex_type{this->_n_vertices++, *this->_vertex_properties.back()};
111111
}
112112

113113
void add_vertices(const types::size_type n) {
@@ -142,8 +142,8 @@ class hypergraph final {
142142
}
143143

144144
void remove_vertex(const types::size_type vertex_id) {
145-
// this->_verify_vertex_id(vertex_id);
146-
// this->_remove_vertex_impl(vertex_id);
145+
this->_verify_vertex_id(vertex_id);
146+
this->_remove_vertex_impl(vertex_id);
147147
}
148148

149149
gl_attr_force_inline void remove_vertex(const vertex_type& vertex) {
@@ -174,6 +174,28 @@ class hypergraph final {
174174
}
175175

176176
private:
177+
// --- vertex methods ---
178+
179+
gl_attr_force_inline void _verify_vertex_id(const types::id_type vertex_id) const {
180+
if (not this->has_vertex(vertex_id))
181+
throw std::out_of_range(std::format("Got invalid vertex id [{}]", vertex_id));
182+
}
183+
184+
void _remove_vertex_impl(const types::id_type vertex_id) {
185+
// const auto removed_edge_ids = this->_impl.remove_vertex(vertex_id);
186+
this->_n_vertices--;
187+
// this->_n_edges -= removed_edge_ids.size();
188+
189+
// if constexpr (type_traits::c_non_empty_properties<vertex_properties_type>)
190+
// this->_vertex_properties.erase(this->_vertex_properties.begin() + vertex_id);
191+
192+
// if constexpr (type_traits::c_non_empty_properties<edge_properties_type>) {
193+
// // IDs are sorted and do not contain duplicates
194+
// for (const auto& edge_id : std::views::reverse(removed_edge_ids))
195+
// this->_edge_properties.erase(this->_edge_properties.begin() + edge_id);
196+
// }
197+
}
198+
177199
types::size_type _n_vertices = 0uz;
178200
types::size_type _n_hyperedges = 0uz;
179201

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ project(cpp-gl-test)
55
if(NOT DEFINED CMAKE_CXX_FLAGS)
66
set(
77
CMAKE_CXX_FLAGS
8-
"-Wall -Wextra -Wcast-align -Wconversion -Wunreachable-code -Wuninitialized -Wunused -pedantic -g -O3"
8+
"-Wall -Wextra -Wcast-align -Wconversion -Wunreachable-code -Wuninitialized -Wunused -pedantic -g -O2"
99
CACHE STRING "Default C++ compile flags" FORCE
1010
)
1111
endif()

tests/source/gl/test_graph.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ TEST_CASE_TEMPLATE_DEFINE("graph structure tests", TraitsType, graph_traits_temp
110110
CHECK_EQ(sut.n_edges(), constants::zero_elements);
111111
}
112112

113-
SUBCASE("graph constructed with n_vertices parameter should properly initialize the vertex "
114-
"list with no edges") {
113+
SUBCASE("graph constructed with n_vertices parameter should contain n_vertices vertices and no edges") {
115114
sut_type sut{constants::n_elements};
116115

117116
REQUIRE(std::ranges::equal(

tests/source/hgl/test_hypergraph.cpp

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,61 @@ namespace hgl_testing {
66

77
TEST_SUITE_BEGIN("test_hypergraph");
88

9+
static_assert(std::same_as<hgl::vertex_descriptor<>, gl::vertex_descriptor<>>);
10+
11+
template <typename HypergraphTraits>
912
struct test_hypergraph {
10-
static constexpr hgl::types::id_type id1 = 0ull;
11-
static constexpr hgl::types::id_type id2 = 1ull;
13+
using traits_type = HypergraphTraits;
14+
using sut_type = hgl::hypergraph<traits_type>;
15+
16+
static constexpr std::size_t n_vertices = 5uz;
17+
static constexpr auto vertex_ids_view = std::views::iota(hgl::constants::initial_id, n_vertices);
1218
};
1319

14-
TEST_CASE_FIXTURE(test_hypergraph, "dummy") {
15-
hgl::hypergraph<> sut{};
16-
CHECK(true);
20+
TEST_CASE_TEMPLATE_DEFINE(
21+
"hypergraph structure tests", HypergraphTraits, hypergraph_traits_template
22+
) {
23+
using fixture_type = test_hypergraph<HypergraphTraits>;
24+
using sut_type = typename fixture_type::sut_type;
25+
using vertex_type = typename sut_type::vertex_type;
26+
27+
constexpr std::size_t n_vertices = fixture_type::n_vertices;
28+
constexpr auto vertex_ids_view = fixture_type::vertex_ids_view;
29+
30+
SUBCASE("a hypergraph should be initialized with no vertices and no edges by default") {
31+
sut_type sut{};
32+
CHECK_EQ(sut.n_vertices(), 0uz);
33+
CHECK_EQ(sut.n_hyperedges(), 0uz);
34+
}
35+
36+
SUBCASE("a hypergraph constructed with n_vertices parameter should contain n_vertices vertices and no edges") {
37+
sut_type sut{constants::n_elements};
38+
39+
REQUIRE(std::ranges::equal(
40+
sut.vertices() | std::views::transform(get_id), vertex_id_view
41+
));
42+
43+
REQUIRE(std::ranges::equal(sut.vertex_ids(), vertex_id_view));
44+
45+
CHECK_THROWS_AS(
46+
static_cast<void>(sut.get_vertex(constants::out_of_range_element_idx)),
47+
std::out_of_range
48+
);
49+
50+
// TODO: check no edges
51+
}
1752
}
1853

54+
TEST_CASE_TEMPLATE_INSTANTIATE(
55+
hypergraph_traits_template,
56+
hgl::edge_list_hg_traits<hgl::undirected_t>, // undirected edge list
57+
hgl::edge_list_hg_traits<hgl::bf_directed_t>, // bf-directed edge list
58+
hgl::adjacency_list_hg_traits<hgl::undirected_t>, // undirected adjacency list
59+
hgl::adjacency_list_hg_traits<hgl::bf_directed_t>, // bf-directed adjacency list
60+
hgl::incidence_matrix_hg_traits<hgl::undirected_t>, // undirected incidence matrix
61+
hgl::incidence_matrix_hg_traits<hgl::bf_directed_t> // bf-directed incidence matrix
62+
);
63+
1964
TEST_SUITE_END(); // test_hypergraph
2065

2166
} // namespace hgl_testing

0 commit comments

Comments
 (0)