Skip to content

Commit 1490421

Browse files
committed
alg core doc
1 parent 65b7311 commit 1490421

2 files changed

Lines changed: 109 additions & 16 deletions

File tree

include/hgl/algorithm/core.hpp

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
33
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
44

5+
/// @file hgl/algorithm/core.hpp
6+
/// @brief Core data structures and types used to control and track hypergraph algorithm execution.
7+
58
#pragma once
69

710
#include "gl/algorithm/core.hpp"
@@ -20,50 +23,107 @@ namespace algorithm {
2023

2124
// -- GL core ---
2225

23-
using gl::algorithm::empty_callback;
26+
/// @ingroup HGL-Algorithm
27+
/// @copybrief gl::algorithm::empty_callback
28+
/// @see gl::algorithm::empty_callback for a more detailed description.
29+
using empty_callback = gl::algorithm::empty_callback;
2430

25-
using gl::algorithm::decision;
31+
/// @ingroup HGL-Algorithm
32+
/// @copybrief gl::algorithm::decision
33+
/// @see gl::algorithm::decision for the full type definition
34+
using decision = gl::algorithm::decision;
2635

27-
using gl::algorithm::result_discriminator;
36+
/// @ingroup HGL-Algorithm
37+
/// @copybrief gl::algorithm::result_discriminator
38+
/// @see gl::algorithm::result_discriminator for the full type definition.
39+
using result_discriminator = gl::algorithm::result_discriminator;
2840
using enum result_discriminator;
2941

30-
using gl::algorithm::non_void_result_type;
31-
using gl::algorithm::result_type;
32-
33-
using gl::algorithm::no_root;
34-
using gl::algorithm::no_root_t;
35-
using gl::algorithm::no_root_v;
42+
/// @ingroup HGL-Algorithm
43+
/// @copybrief gl::algorithm::result_type
44+
/// @see gl::algorithm::result_type for the full type definition.
45+
template <result_discriminator Result, typename ResultType>
46+
using result_type = gl::algorithm::result_type<Result, ResultType>;
47+
48+
/// @ingroup HGL-Algorithm
49+
/// @copybrief gl::algorithm::non_void_result_type
50+
/// @see gl::algorithm::non_void_result_type for the full type definition.
51+
template <result_discriminator Result, typename ResultType>
52+
using non_void_result_type = gl::algorithm::non_void_result_type<Result, ResultType>;
53+
54+
/// @ingroup HGL-Algorithm
55+
/// @copybrief gl::algorithm::no_root_v
56+
/// @see gl::algorithm::no_root_v
57+
template <traits::c_id_type IdType>
58+
inline constexpr IdType no_root_v = gl::algorithm::no_root_v<IdType>;
59+
60+
/// @ingroup HGL-Algorithm
61+
/// @copybrief gl::algorithm::no_root_t
62+
/// @see gl::algorithm::no_root_t
63+
using no_root_t = gl::algorithm::no_root_t;
64+
65+
/// @ingroup HGL-Algorithm
66+
/// @copybrief gl::algorithm::no_root
67+
/// @see gl::algorithm::no_root
68+
inline constexpr no_root_t no_root = gl::algorithm::no_root;
3669

3770
// --- traversal types ---
3871

72+
/// @ingroup HGL-Algorithm
73+
/// @brief Represents an active node in a search container (e.g., a BFS queue or DFS stack) for hypergraph traversals.
74+
/// @tparam H The type of the hypergraph being searched. Must satisfy [**c_hypergraph**](hgl_concepts.md#hgl-traits-c-hypergraph).
3975
template <traits::c_hypergraph H>
4076
struct search_node {
77+
/// @brief The identifier type of the hypergraph elements.
4178
using id_type = typename H::id_type;
4279

80+
/// @brief Default constructor creates an invalid node.
4381
search_node() = default;
4482

83+
/// @brief Constructs a *root* search node (predecessor is itself, no incident hyperedge).
84+
/// @param vertex_id The ID of the root vertex.
4585
search_node(id_type vertex_id)
4686
: vertex_id(vertex_id), pred_id(vertex_id), hyperedge_id(invalid_id) {}
4787

88+
/// @brief Constructs a search node with an explicit predecessor vertex and the connecting hyperedge.
89+
/// @param vertex_id The ID of the currently reached vertex.
90+
/// @param pred_id The ID of the predecessor vertex from which this vertex was reached.
91+
/// @param hyperedge_id The ID of the hyperedge connecting the predecessor to this vertex.
4892
search_node(id_type vertex_id, id_type pred_id, id_type hyperedge_id)
4993
: vertex_id(vertex_id), pred_id(pred_id), hyperedge_id(hyperedge_id) {}
5094

95+
/// @brief Checks if this node is the root of a search tree.
96+
/// @return `true` if the node is valid and its predecessor is itself, `false` otherwise.
5197
[[nodiscard]] gl_attr_force_inline bool is_root() const noexcept {
5298
return this->vertex_id != invalid_id and this->vertex_id == this->pred_id;
5399
}
54100

101+
/// @brief The ID of the current vertex.
55102
id_type vertex_id = invalid_id;
103+
/// @brief The ID of the predecessor from which this vertex was reached.
56104
id_type pred_id = invalid_id;
105+
/// @brief The ID of the hyperedge via which this vertex was reached from the predecessor.
57106
id_type hyperedge_id = invalid_id;
58107
};
59108

109+
/// @ingroup HGL-Algorithm
110+
/// @brief A flat, index-mapped representation of a hypergraph search tree.
111+
///
112+
/// The $i$-th element corresponds to the vertex with `id == i`. The tree topology is formed implicitly,
113+
/// as each @ref hgl::algorithm::search_node "search_node" stores the ID of its predecessor and the
114+
/// connecting hyperedge, enabling $O(1)$ lookups and and $O(\vert V \vert)$ path reconstruction.
115+
///
116+
/// @tparam H The type of the hypergraph being searched.
60117
template <traits::c_hypergraph H>
61118
using search_tree = std::vector<search_node<H>>;
62119

63120
} // namespace algorithm
64121

65122
namespace traits {
66123

124+
/// @ingroup HGL-Traits
125+
/// @brief Validates if a type is a valid hypergraph search tree (a random access range of @ref hgl::algorithm::search_node "search_node"s).
126+
/// @tparam T The type to evaluate against the concept.
67127
template <typename T>
68128
concept c_search_tree =
69129
c_random_access_range<T>
@@ -75,39 +135,70 @@ namespace algorithm {
75135

76136
// --- generic algorithm traits ---
77137

78-
enum class traversal_direction : bool { forward, backward };
138+
/// @ingroup HGL-Algorithm
139+
/// @brief Specifies the direction of traversal for *BF-directed* hypergraphs.
140+
///
141+
/// > [!IMPORTANT] API Simplicity
142+
/// >
143+
/// > To ensure API simplicity, the `traversal_direction` is used for undirected hypergraphs as well by
144+
/// > the generic traversal templates. However, due to the structural nature of undirected hypergraphs,
145+
/// > both direction values implicitly yield the exact same traversal pattern for undirected hypergraphs.
146+
enum class traversal_direction : bool {
147+
forward, ///< Traverse following the forward star (tail to head).
148+
backward ///< Traverse following the backward star (head to tail).
149+
};
79150

151+
/// @ingroup HGL-Algorithm
152+
/// @brief Policy defining how to extract incident hyperedges and target vertices during traversal.
153+
///
154+
/// This template is specialized based on the hypergraph's directionality to route standard
155+
/// traversal algorithms over the correct incidence structures (e.g., following tails to heads).
156+
///
157+
/// @tparam H The type of the hypergraph.
158+
/// @tparam Dir The direction of traversal.
80159
template <traits::c_hypergraph H, traversal_direction Dir>
81160
struct traversal_policy;
82161

162+
/// @ingroup HGL-Algorithm
163+
/// @brief Traversal policy specialization for undirected hypergraphs.
83164
template <traits::c_undirected_hypergraph H, traversal_direction Dir>
84165
struct traversal_policy<H, Dir> {
166+
/// @brief Retrieves the hyperedges incident to the given vertex.
85167
static auto target_hyperedges(const H& h, typename H::id_type v_id) {
86168
return h.incident_hyperedge_ids(v_id);
87169
}
88170

171+
/// @brief Retrieves the vertices incident to the given hyperedge.
89172
static auto target_vertices(const H& h, typename H::id_type he_id) {
90173
return h.incident_vertex_ids(he_id);
91174
}
92175
};
93176

177+
/// @ingroup HGL-Algorithm
178+
/// @brief Traversal policy specialization for forward searches on BF-directed hypergraphs.
94179
template <traits::c_bf_directed_hypergraph H>
95180
struct traversal_policy<H, traversal_direction::forward> {
181+
/// @brief Retrieves the hyperedges originating from the given vertex (forward star).
96182
static auto target_hyperedges(const H& h, typename H::id_type v_id) {
97183
return h.out_hyperedge_ids(v_id); // forward star
98184
}
99185

186+
/// @brief Retrieves the vertices targeted by the given hyperedge (head nodes).
100187
static auto target_vertices(const H& h, typename H::id_type he_id) {
101188
return h.head_ids(he_id);
102189
}
103190
};
104191

192+
/// @ingroup HGL-Algorithm
193+
/// @brief Traversal policy specialization for backward searches on BF-directed hypergraphs.
105194
template <traits::c_bf_directed_hypergraph H>
106195
struct traversal_policy<H, traversal_direction::backward> {
196+
/// @brief Retrieves the hyperedges entering the given vertex (backward star).
107197
static auto target_hyperedges(const H& h, typename H::id_type v_id) {
108198
return h.in_hyperedge_ids(v_id); // backward star
109199
}
110200

201+
/// @brief Retrieves the vertices originating the given hyperedge (tail nodes).
111202
static auto target_vertices(const H& h, typename H::id_type he_id) {
112203
return h.tail_ids(he_id);
113204
}

include/hgl/constants.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,33 @@ namespace hgl {
1515
/// @ingroup HGL-Core
1616
/// @brief A constant representing the initial ID value of 0 for hypergraph elements.
1717
/// @see gl::initial_id_v
18-
using gl::initial_id_v;
18+
template <traits::c_id_type IdType>
19+
inline constexpr IdType initial_id_v = gl::initial_id_v<IdType>;
1920

2021
/// @ingroup HGL-Core
2122
/// @brief A helper type that can be implicitly converted to the initial ID value of 0 for any valid ID type.
2223
/// @see gl::initial_id_t
23-
using gl::initial_id_t;
24+
using initial_id_t = gl::initial_id_t;
2425

2526
/// @ingroup HGL-Core
2627
/// @brief An `initial_id_t` tag constant that can be used to represent the initial ID value of 0 for hypergraph elements in a type-safe manner.
2728
/// @see gl::initial_id
28-
using gl::initial_id;
29+
inline constexpr initial_id_t initial_id = gl::initial_id;
2930

3031
/// @ingroup HGL-Core
3132
/// @brief A constant representing the invalid ID value for hypergraph elements, defined as the maximum value of the specified ID type.
3233
/// @see gl::invalid_id_v
33-
using gl::invalid_id_v;
34+
template <traits::c_id_type IdType>
35+
inline constexpr IdType invalid_id_v = gl::invalid_id_v<IdType>;
3436

3537
/// @ingroup HGL-Core
3638
/// @brief A helper type that can be implicitly converted to the invalid ID value for any valid ID type.
3739
/// @see gl::invalid_id_t
38-
using gl::invalid_id_t;
40+
using invalid_id_t = gl::invalid_id_t;
3941

4042
/// @ingroup HGL-Core
4143
/// @brief An `invalid_id_t` tag constant that can be used to represent the invalid ID value for hypergraph elements in a type-safe manner.
4244
/// @see gl::invalid_id
43-
using gl::invalid_id;
45+
inline constexpr invalid_id_t invalid_id = gl::invalid_id;
4446

4547
} // namespace hgl

0 commit comments

Comments
 (0)