Skip to content

Commit 22c989c

Browse files
authored
YT-CPPHGL-3: Define the vertex and edge descriptor types
- Added a `hgl::vertex_descriptor` generic alias for the `gl::vertex_descriptor` type. - Defined the hyperedge directional tag types, i.e. `undirected_t` and `bf_directed_t` - Implemented a simple `hyperedge_descriptor` generic type - Aligned the directional-related type constraints in the `gl` module to match the new `hgl` constraints - Removed redundant `gl` module code
1 parent 2b1c04c commit 22c989c

55 files changed

Lines changed: 623 additions & 433 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/algorithms.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ This section covers the specific types and type traits used for the algorithm im
286286
> The `algorithm::paths_descriptor` structure is defined as follows:
287287
>
288288
> - *Template parameters*:
289-
> - `VertexDistanceType: type_traits::c_basic_arithmetic` - The type of the distance between vertices in the graph.
289+
> - `VertexDistanceType: type_traits::c_arithmetic` - The type of the distance between vertices in the graph.
290290
> - *Type definitions*:
291291
> - `distance_type` - An alias for `VertexDistanceType`.
292292
> - *Constructors*:
@@ -444,7 +444,7 @@ Additionaly you can use the depth-first/breadth-first search algorithm templates
444444

445445
- *Template parameters*:
446446
- `GraphType: type_traits::c_graph` - The type of the graph on which the search is performed.
447-
- `InitQueueRangeType: type_traits::c_sized_range_of<algorithm::vertex_info>` (default = `std::vector<algorithm::vertex_info>`) - The type of the `vertex_info` range which will be inserted into the queue at the beginning of the algorithm.
447+
- `InitQueueRangeType: type_traits::c_forward_range_of<algorithm::vertex_info>` (default = `std::vector<algorithm::vertex_info>`) - The type of the `vertex_info` range which will be inserted into the queue at the beginning of the algorithm.
448448
- `VisitVertexPredicate: type_traits::c_optional_id_callback<bool>` - The vertex visiting unary predicate type.
449449
- `VisitCallback: type_traits::c_optional_id_callback<bool, types::id_type>` - The vertex visting callback type (arguments: `vertex_id, pred_id`).
450450
- `EnqueueVertexPred: type_traits::c_id_callback<algorithm::predicate_result, const typename GraphType::edge_type&>` - The vertex enqueue predicate type (arguments: `vertex_id, in_edge`)
@@ -475,7 +475,7 @@ Additionaly you can use the depth-first/breadth-first search algorithm templates
475475
- *Template parameters*:
476476
- `GraphType: type_traits::c_graph` - The type of the graph on which the search is performed.
477477
- `PQCompare: std::predicate<algorithm::vertex_info, algorithm::vertex_info>` - The type of the vertex priority queue comparator.
478-
- `InitQueueRangeType: type_traits::c_sized_range_of<algorithm::vertex_info>` (default = `std::vector<algorithm::vertex_info>`) - The type of the `vertex_info` range which will be inserted into the queue at the beginning of the algorithm.
478+
- `InitQueueRangeType: type_traits::c_forward_range_of<algorithm::vertex_info>` (default = `std::vector<algorithm::vertex_info>`) - The type of the `vertex_info` range which will be inserted into the queue at the beginning of the algorithm.
479479
- `VisitVertexPredicate: type_traits::c_optional_id_callback<bool>` - The vertex visiting unary predicate type.
480480
- `VisitCallback: type_traits::c_optional_id_callback<bool, types::id_type>` - The vertex visting callback type (arguments: `vertex, source_id`).
481481
- `EnqueueVertexPred: type_traits::c_id_callback<algorithm::predicate_result, const typename GraphType::edge_type&>` - The vertex enqueue predicate type (arguments: `vertex, in_edge`)

docs/core_util_types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ This section describes the type traits that are associated with the property typ
240240
concept c_weight_properties_type = c_properties<Properties> && requires(Properties p) {
241241
typename Properties::weight_type;
242242
{ p.weight } -> std::same_as<typename Properties::weight_type&>;
243-
requires c_basic_arithmetic<typename Properties::weight_type>;
243+
requires c_arithmetic<typename Properties::weight_type>;
244244
};
245245
```
246246

docs/graph.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ Based on the specified traits, the `graph` class defines the following types:
9090
| `implementation_type` | The underlying data structure used to store the graph's edges and the adjacency information |
9191
| `vertex_type` | The type of the vertex element (an instantiation of `vertex_descriptor`) |
9292
| `vertex_properties_type` | The type of the properties element associated with each vertex |
93-
| `vertex_iterator_type` | The iterator type used for vertex traversal in the graph |
93+
| `vertex_properties_map_type` | The type of the properties map element associated with each vertex |
9494
| `edge_type` | The type of the edge element (an instantiation of `edge_descriptor`) |
9595
| `edge_directional_tag` | The `EdgeDirectionalTag` parameter of the `graph_traits` structure |
9696
| `edge_properties_type` | The type of the properties element associated with each edge |
97-
| `edge_iterator_type` | The iterator type used for edge traversal in the graph |
97+
| `edge_properties_map_type` | The type of the properties map element associated with each edge |
9898

9999
<br />
100100
<br />
@@ -106,7 +106,7 @@ Based on the specified traits, the `graph` class defines the following types:
106106
- **`graph()`**:
107107
- Default constructor. Creates an empty graph with no vertices or edges.
108108

109-
- **`graph(n_vertices)`**:
109+
- **`explicit graph(n_vertices)`**:
110110
- Constructs a graph with the specified number of vertices. Each vertex is initialized with default properties and no adjacent edges.
111111

112112
> [!IMPORTANT]
@@ -195,6 +195,9 @@ Based on the specified traits, the `graph` class defines the following types:
195195
> [!IMPORTANT]
196196
> Simliarily to the graph constructors using the `add_vertices*` methods is more efficient than calling `add_vertex` multiple times which might cause more vector reallocations.
197197
198+
> [!WARNING]
199+
> Removing vertices may invalidate previously created vertex and edge descriptor objects.
200+
198201
- **`graph.remove_vertex(vertex_id)`**:
199202
- *Description*: Removes the vertex with the given ID from the graph.
200203
- *Parameters*:
@@ -210,15 +213,15 @@ Based on the specified traits, the `graph` class defines the following types:
210213
- **`graph.remove_vertices_from(vertex_id_range)`**:
211214
- *Description*: Removes multiple vertices from the graph based on a range of vertex IDs. The IDs are sorted in descending order and duplicate IDs are removed before deletion.
212215
- *Template parameters*:
213-
- `IdRange: type_traits::c_sized_range_of<types::id_type>` – A range of vertex IDs, which must satisfy the size and type constraints.
216+
- `IdRange: type_traits::c_forward_range_of<types::id_type>` – A range of vertex IDs, which must satisfy the size and type constraints.
214217
- *Parameters*:
215218
- `vertex_id_range: const IdRange&` – A range of vertex IDs to be removed.
216219
- *Return type*: `void`
217220

218-
- **`graph.remove_vertices_from(vertex_ref_range)`**:
221+
- **`graph.remove_vertices_from(vertex_range)`**:
219222
- *Description*: Removes multiple vertices from the graph based on a range of vertex references. The references are sorted in descending order and duplicates are removed before deletion.
220223
- *Parameters*:
221-
- `vertex_ref_range: const type_traits::c_sized_range_of<types::id_type> auto&` – A range of vertex references to be removed.
224+
- `vertex_range: const type_traits::c_forward_range_of<types::id_type> auto&` – A range of vertex references to be removed.
222225
- *Return type*: `void`
223226

224227
- **`graph.in_degree(vertex) const`**:
@@ -434,6 +437,9 @@ Based on the specified traits, the `graph` class defines the following types:
434437
- `target: const vertex_type&` – the target vertex.
435438
- *Return type*: `std::vector<edge_type>`
436439

440+
> [!WARNING]
441+
> Removing edges may invalidate previously created edge descriptor objects.
442+
437443
- **`graph.remove_edge(edge)`**:
438444
- *Description*: Removes the specified edge from the graph.
439445
- *Parameters*:
@@ -524,10 +530,6 @@ Based on the specified traits, the `graph` class defines the following types:
524530
<br />
525531
<br />
526532

527-
## Additional utility
528-
529-
In addition to the core functionality of the `graph` class, the [gl/graph_utility.hpp](/include/gl/graph_utility.hpp) file provides a set of utility functions and type traits that offer extended support for graph manipulation and property handling. Below is an overview of the key utilities provided.
530-
531533
### Type traits
532534

533535
To write safe and more expressive graph utility of your own, you can use the defined concepts and type traits:
@@ -537,9 +539,9 @@ To write safe and more expressive graph utility of your own, you can use the def
537539
538540
| **Trait** | **Description** |
539541
| :- | :- |
540-
| `c_graph<T>` | Ensures that the template parameter `T` is a specialization of the `graph` class |
541-
| `c_directed_graph<T>` | Equivalent to `c_graph<T> and is_directed_v<T>`<br/>Ensures that the template parameter `T` is a *directed* specialization of the `graph` class |
542-
| `c_undirected_graph<T>` | Equivalent to `c_graph<T> and is_undirected_v<T>`<br/>Ensures that the template parameter `T` is an *undirected* specialization of the `graph` class |
542+
| `c_graph<G>` | Ensures that the template parameter `G` is a specialization of the `graph` class |
543+
| `c_directed_graph<G>` | Equivalent to `c_graph<G> and c_directed_edge<typename G::edge_type>`<br/>Ensures that the template parameter `G` is a *directed* specialization of the `graph` class |
544+
| `c_undirected_graph<G>` | Equivalent to `c_graph<G> and c_undirected_edge<typename G::edge_type>`<br/>Ensures that the template parameter `G` is an *undirected* specialization of the `graph` class |
543545

544546
> [!TIP]
545547
> More (not graph class specific) type traits and concepts are defined in the [gl/types/traits/](/include/gl/types/traits/) directory.

docs/graph_elements.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,24 @@ The destructor is *defaulted*, allowing proper cleanup of the `edge_descriptor`
262262
template <type_traits::c_properties Properties = types::empty_properties>
263263
using undirected_edge = edge_descriptor<undirected_t, Properties>;
264264
```
265+
266+
- `type_traits::c_directed_edge`: A type constraint that accepts only directed edges.
267+
268+
```cpp
269+
template <typename E>
270+
concept c_directed_edge =
271+
c_instantiation_of<E, edge_descriptor>
272+
and std::same_as<typename E::directional_tag, gl::directed_t>;
273+
```
274+
275+
- `type_traits::c_undirected_edge`: A type constraint that accepts only undirected edges.
276+
277+
```cpp
278+
template <typename E>
279+
concept c_undirected_edge =
280+
c_instantiation_of<E, edge_descriptor>
281+
and std::same_as<typename E::directional_tag, gl::undirected_t>;
282+
```
265283

266284
<br />
267285
<br />

include/gl/algorithm/coloring.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ requires(type_traits::c_binary_color_properties_type<typename GraphType::vertex_
7878
bool apply_coloring(GraphType& graph, const ColorRange& color_range) {
7979
using color_type = typename GraphType::vertex_properties_type::color_type;
8080

81-
if (color_range.size() != graph.n_vertices())
81+
if (std::ranges::size(color_range) != graph.n_vertices())
8282
return false;
8383

8484
auto vertices = graph.vertices(); // store the view to extend its lifetime

include/gl/algorithm/dijkstra.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
#pragma once
66

7-
#include "gl/graph_utility.hpp"
87
#include "impl/pfs.hpp"
98

109
#include <deque>
1110

1211
namespace gl::algorithm {
1312

14-
template <type_traits::c_basic_arithmetic VertexDistanceType>
13+
template <type_traits::c_arithmetic VertexDistanceType>
1514
struct paths_descriptor : public predecessors_descriptor {
1615
using predecessor_type = typename predecessors_descriptor::predecessor_type;
1716
using distance_type = VertexDistanceType;
@@ -97,7 +96,7 @@ template <
9796
const auto pred_id = in_edge.incident_vertex(vertex_id);
9897

9998
const auto edge_weight = get_weight<GraphType>(in_edge);
100-
if (edge_weight < constants::zero) {
99+
if (edge_weight < 0) {
101100
negative_edge.emplace(in_edge);
102101
return predicate_result::unknown;
103102
}

include/gl/algorithm/impl/bfs.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace gl::algorithm::impl {
1212

1313
template <
1414
type_traits::c_graph GraphType,
15-
type_traits::c_sized_range_of<algorithm::vertex_info> InitQueueRangeType =
15+
type_traits::c_forward_range_of<algorithm::vertex_info> InitQueueRangeType =
1616
std::vector<algorithm::vertex_info>,
1717
type_traits::c_optional_id_callback<bool> VisitVertexPredicate,
1818
type_traits::c_optional_id_callback<bool, types::id_type> VisitCallback,
@@ -29,7 +29,7 @@ bool bfs(
2929
const PreVisitCallback& pre_visit = {},
3030
const PostVisitCallback& post_visit = {}
3131
) {
32-
if (initial_queue_content.size() == constants::default_size)
32+
if (std::ranges::empty(initial_queue_content))
3333
return false;
3434

3535
// prepare the vertex queue

include/gl/algorithm/impl/common.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ init_return_value(const GraphType& graph) {
2424
}
2525

2626
template <
27-
type_traits::c_sized_range_of<algorithm::vertex_info> InitRangeType =
27+
type_traits::c_forward_range_of<algorithm::vertex_info> InitRangeType =
2828
std::vector<algorithm::vertex_info>>
2929
[[nodiscard]] gl_attr_force_inline InitRangeType init_range(types::id_type root_vertex_id) {
3030
return InitRangeType{algorithm::vertex_info{root_vertex_id}};

include/gl/algorithm/impl/pfs.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace gl::algorithm::impl {
1313
template <
1414
type_traits::c_graph GraphType,
1515
std::predicate<algorithm::vertex_info, algorithm::vertex_info> PQCompare,
16-
type_traits::c_sized_range_of<algorithm::vertex_info> InitQueueRangeType =
16+
type_traits::c_forward_range_of<algorithm::vertex_info> InitQueueRangeType =
1717
std::vector<algorithm::vertex_info>,
1818
type_traits::c_optional_id_callback<GraphType, bool> VisitVertexPredicate,
1919
type_traits::c_optional_id_callback<bool, types::id_type> VisitCallback,
@@ -31,7 +31,7 @@ bool pfs(
3131
const PreVisitCallback& pre_visit = {},
3232
const PostVisitCallback& post_visit = {}
3333
) {
34-
if (initial_queue_content.size() == constants::default_size)
34+
if (std::ranges::empty(initial_queue_content))
3535
return false;
3636

3737
// prepare the vertex queue

include/gl/algorithm/mst.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ struct mst_descriptor {
1919
using weight_type = types::vertex_distance_type<graph_type>;
2020

2121
mst_descriptor(const types::size_type n_vertices) {
22-
edges.reserve(n_vertices - constants::one);
22+
edges.reserve(n_vertices - 1uz);
2323
}
2424

2525
std::vector<edge_type> edges;
26-
weight_type weight = static_cast<weight_type>(constants::zero);
26+
weight_type weight = static_cast<weight_type>(0);
2727
};
2828

2929
template <type_traits::c_undirected_graph GraphType>
@@ -53,14 +53,14 @@ template <type_traits::c_undirected_graph GraphType>
5353
queue_type edge_queue;
5454

5555
// insert the edges adjacent to the root vertex to the queue
56-
const types::id_type root_id = root_id_opt.value_or(constants::zero);
56+
const types::id_type root_id = root_id_opt.value_or(constants::initial_id);
5757

5858
for (const auto& edge : graph.adjacent_edges(root_id))
5959
edge_queue.emplace(edge);
6060

6161
// mark the root vertex as visited
6262
visited[root_id] = true;
63-
types::size_type n_vertices_in_mst = constants::one;
63+
types::size_type n_vertices_in_mst = 1uz;
6464

6565
// find the mst
6666
while (n_vertices_in_mst < n_vertices) {
@@ -104,7 +104,7 @@ requires type_traits::c_has_numeric_limits_max<types::vertex_distance_type<Graph
104104
std::vector<std::optional<edge_type>> min_cost_edges(n_vertices, std::nullopt);
105105

106106
// set the distance to the root vertex to 0
107-
min_cost.at(root_id_opt.value_or(constants::zero)) = constants::zero;
107+
min_cost.at(root_id_opt.value_or(constants::initial_id)) = static_cast<distance_type>(0);
108108

109109
auto heap_comparator = [&min_cost](const types::id_type lhs, const types::id_type rhs) {
110110
return min_cost[lhs] > min_cost[rhs]; // min-heap based on min_cost

0 commit comments

Comments
 (0)