Skip to content

Commit 24f417b

Browse files
authored
YT-CPPHGL-37: HGL module API refinement
- Added generic hypergraph class aliases: - Directional: undirected_hypergraph, bf_directed_hypergraph - Implementation: list_hypergraph, flat_list_hypergraph, matrix_hypergraph, flat_matrix_hypergraph - Renamed the order and size methods of the hypergraph class to n_vertices and n_hyperedges (Least Astonishment) - Renamed the tail/head collection getters: - Descriptor collections: tail_vertices, head_vertices to tail, head - ID collections: tail_vertex_ids, head_vertex_ids to tail_ids, head_ids - Removed the get_ prefixes from hypergraph class getter methods - Added new vertex_unchecked and hyperedge_unchecked methods - Defined the vertex_t and hyperedge_t element tag types - Implemented convenience element accessors in the hypergraph class: at(<tag>, id) and operator[](<tag>, id) - Extended hyperedge descriptor class with operator* and operator-> property accessors
1 parent 06b7ff2 commit 24f417b

31 files changed

Lines changed: 939 additions & 837 deletions

include/gl/edge_descriptor.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ class edge_descriptor final {
2626
using id_type = IdType;
2727
using directional_tag = DirectionalTag;
2828
using properties_type = Properties;
29-
using properties_ref_type = std::conditional_t<
30-
traits::c_empty_properties<properties_type>,
31-
empty_properties,
32-
properties_type&>;
3329

3430
friend directional_tag;
3531

@@ -156,7 +152,7 @@ class edge_descriptor final {
156152
return this->_vertices.first == this->_vertices.second;
157153
}
158154

159-
[[nodiscard]] gl_attr_force_inline properties_ref_type properties() const
155+
[[nodiscard]] gl_attr_force_inline properties_type& properties() const
160156
requires(traits::c_non_empty_properties<properties_type>)
161157
{
162158
this->_validate();

include/gl/graph.hpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ class graph final {
316316
return this->successor_ids(vertex.id());
317317
}
318318

319+
[[nodiscard]] gl_attr_force_inline vertex_properties_type& vertex_properties(const id_type id
320+
) const
321+
requires(traits::c_non_empty_properties<vertex_properties_type>)
322+
{
323+
this->_verify_vertex_id(id);
324+
return *this->_vertex_properties[id];
325+
}
326+
327+
[[nodiscard]] gl_attr_force_inline auto vertex_properties_map() const noexcept
328+
requires(traits::c_non_empty_properties<vertex_properties_type>)
329+
{
330+
return util::deref_view(this->_vertex_properties);
331+
}
332+
319333
// --- degree getters ---
320334

321335
[[nodiscard]] gl_attr_force_inline size_type degree(const id_type vertex_id) const {
@@ -559,6 +573,21 @@ class graph final {
559573
return this->out_edges(vertex.id());
560574
}
561575

576+
[[nodiscard]] edge_properties_type& edge_properties(const id_type id) const
577+
requires(traits::c_non_empty_properties<edge_properties_type>)
578+
{
579+
if (id >= this->_n_edges)
580+
throw std::out_of_range(std::format("Got invalid edge id [{}]", id));
581+
582+
return *this->_edge_properties[id];
583+
}
584+
585+
[[nodiscard]] gl_attr_force_inline auto edge_properties_map() const noexcept
586+
requires(traits::c_non_empty_properties<edge_properties_type>)
587+
{
588+
return util::deref_view(this->_edge_properties);
589+
}
590+
562591
// --- adjacency and incidence methods ---
563592

564593
[[nodiscard]] gl_attr_force_inline bool are_adjacent(
@@ -595,37 +624,6 @@ class graph final {
595624
return this->are_incident(vertex, edge);
596625
}
597626

598-
// --- property getters ---
599-
600-
[[nodiscard]] gl_attr_force_inline auto vertex_properties_map() const noexcept
601-
requires(traits::c_non_empty_properties<vertex_properties_type>)
602-
{
603-
return util::deref_view(this->_vertex_properties);
604-
}
605-
606-
[[nodiscard]] gl_attr_force_inline vertex_properties_type& vertex_properties(const id_type id
607-
) const
608-
requires(traits::c_non_empty_properties<vertex_properties_type>)
609-
{
610-
this->_verify_vertex_id(id);
611-
return *this->_vertex_properties[id];
612-
}
613-
614-
[[nodiscard]] gl_attr_force_inline auto edge_properties_map() const noexcept
615-
requires(traits::c_non_empty_properties<edge_properties_type>)
616-
{
617-
return util::deref_view(this->_edge_properties);
618-
}
619-
620-
[[nodiscard]] edge_properties_type& get_edge_properties(const id_type id) const
621-
requires(traits::c_non_empty_properties<edge_properties_type>)
622-
{
623-
if (id >= this->_n_edges)
624-
throw std::out_of_range(std::format("Got invalid edge id [{}]", id));
625-
626-
return *this->_edge_properties[id];
627-
}
628-
629627
// --- comparison ---
630628

631629
[[nodiscard]] friend bool operator==(const graph& lhs, const graph& rhs) noexcept {

include/gl/vertex_descriptor.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class vertex_descriptor final {
2525
using type = std::type_identity_t<vertex_descriptor<Properties, IdType>>;
2626
using id_type = IdType;
2727
using properties_type = Properties;
28-
using properties_ref_type = std::conditional_t<
29-
traits::c_empty_properties<properties_type>,
30-
empty_properties,
31-
properties_type&>;
3228

3329
vertex_descriptor() {
3430
*this = vertex_descriptor::invalid();
@@ -86,7 +82,7 @@ class vertex_descriptor final {
8682
return this->_id;
8783
}
8884

89-
[[nodiscard]] gl_attr_force_inline properties_ref_type properties() const
85+
[[nodiscard]] gl_attr_force_inline properties_type& properties() const
9086
requires(traits::c_non_empty_properties<properties_type>)
9187
{
9288
this->_validate();

include/hgl/algorithm/core.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct traversal_policy<H, traversal_direction::forward> {
9898
}
9999

100100
static auto target_vertices(const H& h, typename H::id_type he_id) {
101-
return h.head_vertex_ids(he_id);
101+
return h.head_ids(he_id);
102102
}
103103
};
104104

@@ -109,7 +109,7 @@ struct traversal_policy<H, traversal_direction::backward> {
109109
}
110110

111111
static auto target_vertices(const H& h, typename H::id_type he_id) {
112-
return h.tail_vertex_ids(he_id);
112+
return h.tail_ids(he_id);
113113
}
114114
};
115115

include/hgl/algorithm/traversal/backward_search.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ return_type<Result, search_tree<H>> backward_bfs(
2626
) {
2727
using id_type = typename H::id_type;
2828

29-
std::vector<bool> visited_vertices(hypergraph.order(), false);
29+
std::vector<bool> visited_vertices(hypergraph.n_vertices(), false);
3030
auto tail_unvisited = hypergraph.tail_size_map() | std::ranges::to<std::vector>();
3131

3232
auto stree = init_search_tree<Result>(hypergraph);
@@ -67,7 +67,7 @@ return_type<Result, search_tree<H>> backward_dfs(
6767
) {
6868
using id_type = typename H::id_type;
6969

70-
std::vector<bool> visited_vertices(hypergraph.order(), false);
70+
std::vector<bool> visited_vertices(hypergraph.n_vertices(), false);
7171
auto tail_unvisited = hypergraph.tail_size_map() | std::ranges::to<std::vector>();
7272

7373
auto stree = init_search_tree<Result>(hypergraph);

include/hgl/algorithm/traversal/breadth_first_search.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ return_type<Result, search_tree<H>> breadth_first_search(
2121
const PreVisitCallback& pre_visit = {},
2222
const PostVisitCallback& post_visit = {}
2323
) {
24-
std::vector<bool> visited_vertices(hypergraph.order(), false);
25-
std::vector<bool> visited_hyperedges(hypergraph.size(), false);
24+
std::vector<bool> visited_vertices(hypergraph.n_vertices(), false);
25+
std::vector<bool> visited_hyperedges(hypergraph.n_hyperedges(), false);
2626

2727
auto stree = init_search_tree<Result>(hypergraph);
2828

include/hgl/algorithm/traversal/depth_first_search.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ return_type<Result, search_tree<H>> depth_first_search(
2121
const PreVisitCallback& pre_visit = {},
2222
const PostVisitCallback& post_visit = {}
2323
) {
24-
std::vector<bool> visited_vertices(hypergraph.order(), false);
25-
std::vector<bool> visited_hyperedges(hypergraph.size(), false);
24+
std::vector<bool> visited_vertices(hypergraph.n_vertices(), false);
25+
std::vector<bool> visited_hyperedges(hypergraph.n_hyperedges(), false);
2626

2727
auto stree = init_search_tree<Result>(hypergraph);
2828

include/hgl/algorithm/traversal/forward_search.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ return_type<Result, search_tree<H>> forward_bfs(
2626
) {
2727
using id_type = typename H::id_type;
2828

29-
std::vector<bool> visited_vertices(hypergraph.order(), false);
29+
std::vector<bool> visited_vertices(hypergraph.n_vertices(), false);
3030
auto head_unvisited = hypergraph.head_size_map() | std::ranges::to<std::vector>();
3131

3232
auto stree = init_search_tree<Result>(hypergraph);
@@ -67,7 +67,7 @@ return_type<Result, search_tree<H>> forward_dfs(
6767
) {
6868
using id_type = typename H::id_type;
6969

70-
std::vector<bool> visited_vertices(hypergraph.order(), false);
70+
std::vector<bool> visited_vertices(hypergraph.n_vertices(), false);
7171
auto head_unvisited = hypergraph.head_size_map() | std::ranges::to<std::vector>();
7272

7373
auto stree = init_search_tree<Result>(hypergraph);

include/hgl/algorithm/util.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ template <result_discriminator Result, traits::c_hypergraph H>
1515
) {
1616
using return_t = non_void_return_type<Result, search_tree<H>>;
1717
if constexpr (Result == ret)
18-
return return_t(hypergraph.order());
18+
return return_t(hypergraph.n_vertices());
1919
else
2020
return return_t();
2121
}

include/hgl/conversion.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ template <traits::c_hypergraph_impl_tag TargetImplTag, traits::c_hypergraph_impl
5858
struct to_impl {
5959
template <typename TargetHypergraph, typename SourceHypergraph>
6060
static void convert(TargetHypergraph& target, SourceHypergraph& source) {
61-
target._impl.add_vertices(source.order());
62-
target._impl.add_hyperedges(source.size());
61+
target._impl.add_vertices(source.n_vertices());
62+
target._impl.add_hyperedges(source.n_hyperedges());
6363

6464
if constexpr (traits::c_undirected_hypergraph<TargetHypergraph>) {
6565
for (const auto eid : source.hyperedge_ids())
@@ -68,9 +68,9 @@ struct to_impl {
6868
}
6969
else {
7070
for (const auto eid : source.hyperedge_ids()) {
71-
for (const auto vid : source.tail_vertex_ids(eid))
71+
for (const auto vid : source.tail_ids(eid))
7272
target._impl.bind_tail(vid, eid);
73-
for (const auto vid : source.head_vertex_ids(eid))
73+
for (const auto vid : source.head_ids(eid))
7474
target._impl.bind_head(vid, eid);
7575
}
7676
}
@@ -248,7 +248,7 @@ template <gl::traits::c_undirected_graph G>
248248
const auto rem = std::ranges::unique(edges);
249249
edges.erase(rem.begin(), rem.end());
250250

251-
G g{h.order()};
251+
G g{h.n_vertices()};
252252
for (const auto& edge : edges)
253253
g.add_edge(edge.first, edge.second);
254254
return g;
@@ -267,8 +267,8 @@ template <gl::traits::c_directed_graph G>
267267
std::vector<edge_vertices> edges;
268268

269269
for (const auto eid : h.hyperedge_ids()) {
270-
auto sources = h.tail_vertex_ids(eid);
271-
const auto targets = h.head_vertex_ids(eid) | std::ranges::to<std::vector>();
270+
auto sources = h.tail_ids(eid);
271+
const auto targets = h.head_ids(eid) | std::ranges::to<std::vector>();
272272
for (const auto u : sources)
273273
for (const auto v : targets)
274274
edges.emplace_back(u, v);
@@ -278,7 +278,7 @@ template <gl::traits::c_directed_graph G>
278278
const auto rem = std::ranges::unique(edges);
279279
edges.erase(rem.begin(), rem.end());
280280

281-
G g{h.order()};
281+
G g{h.n_vertices()};
282282
for (const auto& [u, v] : edges)
283283
g.add_edge(u, v);
284284

@@ -296,9 +296,9 @@ template <gl::traits::c_undirected_graph G>
296296
[[nodiscard]] G incidence_graph(const traits::c_undirected_hypergraph auto& h) {
297297
using g_id_type = typename G::id_type;
298298

299-
G g{h.order() + h.size()};
299+
G g{h.n_vertices() + h.n_hyperedges()};
300300
const auto align_edge_id =
301-
[shift = static_cast<g_id_type>(h.order())](const auto eid) -> g_id_type {
301+
[shift = static_cast<g_id_type>(h.n_vertices())](const auto eid) -> g_id_type {
302302
return eid + shift;
303303
};
304304

@@ -323,9 +323,9 @@ template <gl::traits::c_directed_graph G>
323323
[[nodiscard]] G incidence_graph(const traits::c_bf_directed_hypergraph auto& h) {
324324
using g_id_type = typename G::id_type;
325325

326-
G g{h.order() + h.size()};
326+
G g{h.n_vertices() + h.n_hyperedges()};
327327
const auto align_edge_id =
328-
[shift = static_cast<g_id_type>(h.order())](const auto eid) -> g_id_type {
328+
[shift = static_cast<g_id_type>(h.n_vertices())](const auto eid) -> g_id_type {
329329
return eid + shift;
330330
};
331331

@@ -336,7 +336,7 @@ template <gl::traits::c_directed_graph G>
336336
g.add_edges_from(vid, targets);
337337
}
338338
for (const auto eid : h.hyperedge_ids()) {
339-
const auto targets = h.head_vertex_ids(eid) | std::ranges::to<std::vector<g_id_type>>();
339+
const auto targets = h.head_ids(eid) | std::ranges::to<std::vector<g_id_type>>();
340340
g.add_edges_from(align_edge_id(eid), targets);
341341
}
342342

0 commit comments

Comments
 (0)