Skip to content

Commit 41c8020

Browse files
committed
replaced std::optional<bool> usage with predicate_result
1 parent 8ac2339 commit 41c8020

14 files changed

Lines changed: 91 additions & 81 deletions

docs/algoithms.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ This section covers the specific types and type traits used for the algorithm im
183183
- *Return type*:
184184
- `impl::alg_return_type<AlgReturnType, predecessors_descriptor>` - If `AlgReturnType` is `algorithm::no_return` - nothing will be returned (`void`). Otherwise the algorithm will return an instance of `predecessors_descriptor`.
185185

186-
- *Defined in*: [gl/algorithm/depth_first_search.hpp](/include/gl/algorithm/deapth_first_search.hpp)
186+
- *Defined in*: [gl/algorithm/depth_first_search.hpp](/include/gl/algorithm/depth_first_search.hpp)
187187

188188
- `recursive_depth_first_search(graph, root_vertex_id_opt, pre_visit, post_visit)`
189189
- *Description*: Performs a recursive depth-first search (DFS) on the specified graph and conditionally returns a `predecessors_descriptor` instance.
190190

191191
**NOTE:** This algoithm has the same template parameters, parameters and return type as the iterative version (`depth_first_search`)
192192

193-
- *Defined in*: [gl/algorithm/depth_first_search.hpp](/include/gl/algorithm/deapth_first_search.hpp)
193+
- *Defined in*: [gl/algorithm/depth_first_search.hpp](/include/gl/algorithm/depth_first_search.hpp)
194194

195195
### Breadth-first search
196196

include/gl/algorithm/breadth_first_search.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
namespace gl::algorithm {
1212

1313
template <
14-
type_traits::c_alg_return_type AlgReturnType = algorithm::default_return,
14+
result_discriminator ResultDiscriminator = algorithm::ret,
1515
type_traits::c_graph GraphType,
1616
type_traits::c_optional_vertex_callback<GraphType, void> PreVisitCallback =
1717
algorithm::empty_callback,
1818
type_traits::c_optional_vertex_callback<GraphType, void> PostVisitCallback =
1919
algorithm::empty_callback>
20-
impl::alg_return_type<AlgReturnType, predecessors_descriptor> breadth_first_search(
20+
impl::alg_return_type<ResultDiscriminator, predecessors_descriptor> breadth_first_search(
2121
const GraphType& graph,
2222
const std::optional<types::id_type>& root_vertex_id_opt = no_root_vertex,
2323
const PreVisitCallback& pre_visit = {},
@@ -29,14 +29,14 @@ impl::alg_return_type<AlgReturnType, predecessors_descriptor> breadth_first_sear
2929
std::vector<bool> visited(graph.n_vertices(), false);
3030
std::vector<types::id_type> sources(graph.n_vertices());
3131

32-
auto pd = impl::init_return_value<AlgReturnType, predecessors_descriptor>(graph);
32+
auto pd = impl::init_return_value<ResultDiscriminator, predecessors_descriptor>(graph);
3333

3434
if (root_vertex_id_opt) {
3535
impl::bfs(
3636
graph,
3737
impl::init_range(root_vertex_id_opt.value()),
3838
impl::default_visit_vertex_predicate<GraphType>(visited),
39-
impl::default_visit_callback<GraphType, AlgReturnType>(visited, pd),
39+
impl::default_visit_callback<GraphType, ResultDiscriminator>(visited, pd),
4040
impl::default_enqueue_vertex_predicate<GraphType, true>(visited),
4141
pre_visit,
4242
post_visit
@@ -48,14 +48,14 @@ impl::alg_return_type<AlgReturnType, predecessors_descriptor> breadth_first_sear
4848
graph,
4949
impl::init_range(root_id),
5050
impl::default_visit_vertex_predicate<GraphType>(visited),
51-
impl::default_visit_callback<GraphType, AlgReturnType>(visited, pd),
51+
impl::default_visit_callback<GraphType, ResultDiscriminator>(visited, pd),
5252
impl::default_enqueue_vertex_predicate<GraphType, true>(visited),
5353
pre_visit,
5454
post_visit
5555
);
5656
}
5757

58-
if constexpr (not type_traits::c_alg_no_return_type<AlgReturnType>)
58+
if constexpr (ResultDiscriminator == algorithm::ret)
5959
return pd;
6060
}
6161

include/gl/algorithm/coloring.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ template <
4242
algorithm::empty_callback{}, // visit predicate
4343
algorithm::empty_callback{}, // visit callback
4444
[&coloring](const vertex_type& vertex, const edge_type& in_edge)
45-
-> std::optional<bool> { // enqueue predicate
45+
-> predicate_result { // enqueue predicate
4646
if (in_edge.is_loop())
4747
return false;
4848

4949
const auto vertex_id = vertex.id();
5050
const auto source_id = in_edge.incident_vertex(vertex).id();
5151

5252
if (coloring[vertex_id] == coloring[source_id])
53-
return std::nullopt; // graph is not bipartite
53+
return predicate_result::unknown; // graph is not bipartite
5454

5555
if (not coloring[vertex.id()].is_set()) {
5656
coloring[vertex.id()] = coloring[source_id].next();

include/gl/algorithm/deapth_first_search.hpp renamed to include/gl/algorithm/depth_first_search.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
namespace gl::algorithm {
1111

1212
template <
13-
type_traits::c_alg_return_type AlgReturnType = algorithm::default_return,
13+
result_discriminator ResultDiscriminator = algorithm::ret,
1414
type_traits::c_graph GraphType,
1515
type_traits::c_optional_vertex_callback<GraphType, void> PreVisitCallback =
1616
algorithm::empty_callback,
1717
type_traits::c_optional_vertex_callback<GraphType, void> PostVisitCallback =
1818
algorithm::empty_callback>
19-
impl::alg_return_type<AlgReturnType, predecessors_descriptor> depth_first_search(
19+
impl::alg_return_type<ResultDiscriminator, predecessors_descriptor> depth_first_search(
2020
const GraphType& graph,
2121
const std::optional<types::id_type>& root_vertex_id_opt = no_root_vertex,
2222
const PreVisitCallback& pre_visit = {},
@@ -28,14 +28,14 @@ impl::alg_return_type<AlgReturnType, predecessors_descriptor> depth_first_search
2828
std::vector<bool> visited(graph.n_vertices(), false);
2929
std::vector<types::id_type> sources(graph.n_vertices());
3030

31-
auto pd = impl::init_return_value<AlgReturnType, predecessors_descriptor>(graph);
31+
auto pd = impl::init_return_value<ResultDiscriminator, predecessors_descriptor>(graph);
3232

3333
if (root_vertex_id_opt) {
3434
impl::dfs(
3535
graph,
3636
graph.get_vertex(root_vertex_id_opt.value()),
3737
impl::default_visit_vertex_predicate<GraphType>(visited),
38-
impl::default_visit_callback<GraphType, AlgReturnType>(visited, pd),
38+
impl::default_visit_callback<GraphType, ResultDiscriminator>(visited, pd),
3939
impl::default_enqueue_vertex_predicate<GraphType>(visited),
4040
pre_visit,
4141
post_visit
@@ -47,25 +47,25 @@ impl::alg_return_type<AlgReturnType, predecessors_descriptor> depth_first_search
4747
graph,
4848
root_vertex,
4949
impl::default_visit_vertex_predicate<GraphType>(visited),
50-
impl::default_visit_callback<GraphType, AlgReturnType>(visited, pd),
50+
impl::default_visit_callback<GraphType, ResultDiscriminator>(visited, pd),
5151
impl::default_enqueue_vertex_predicate<GraphType>(visited),
5252
pre_visit,
5353
post_visit
5454
);
5555
}
5656

57-
if constexpr (not type_traits::c_alg_no_return_type<AlgReturnType>)
57+
if constexpr (ResultDiscriminator == algorithm::ret)
5858
return pd;
5959
}
6060

6161
template <
62-
type_traits::c_alg_return_type AlgReturnType = algorithm::default_return,
62+
result_discriminator ResultDiscriminator = algorithm::ret,
6363
type_traits::c_graph GraphType,
6464
type_traits::c_optional_vertex_callback<GraphType, void> PreVisitCallback =
6565
algorithm::empty_callback,
6666
type_traits::c_optional_vertex_callback<GraphType, void> PostVisitCallback =
6767
algorithm::empty_callback>
68-
impl::alg_return_type<AlgReturnType, predecessors_descriptor> recursive_depth_first_search(
68+
impl::alg_return_type<ResultDiscriminator, predecessors_descriptor> recursive_depth_first_search(
6969
const GraphType& graph,
7070
const std::optional<types::id_type>& root_vertex_id_opt = no_root_vertex,
7171
const PreVisitCallback& pre_visit = {},
@@ -77,7 +77,7 @@ impl::alg_return_type<AlgReturnType, predecessors_descriptor> recursive_depth_fi
7777
std::vector<bool> visited(graph.n_vertices(), false);
7878
std::vector<types::id_type> sources(graph.n_vertices());
7979

80-
auto pd = impl::init_return_value<AlgReturnType, predecessors_descriptor>(graph);
80+
auto pd = impl::init_return_value<ResultDiscriminator, predecessors_descriptor>(graph);
8181

8282
if (root_vertex_id_opt) {
8383
const auto root_id = root_vertex_id_opt.value();
@@ -86,7 +86,7 @@ impl::alg_return_type<AlgReturnType, predecessors_descriptor> recursive_depth_fi
8686
graph.get_vertex(root_id),
8787
root_id,
8888
impl::default_visit_vertex_predicate<GraphType>(visited),
89-
impl::default_visit_callback<GraphType, AlgReturnType>(visited, pd),
89+
impl::default_visit_callback<GraphType, ResultDiscriminator>(visited, pd),
9090
impl::default_enqueue_vertex_predicate<GraphType>(visited),
9191
pre_visit,
9292
post_visit
@@ -99,14 +99,14 @@ impl::alg_return_type<AlgReturnType, predecessors_descriptor> recursive_depth_fi
9999
root_vertex,
100100
root_vertex.id(),
101101
impl::default_visit_vertex_predicate<GraphType>(visited),
102-
impl::default_visit_callback<GraphType, AlgReturnType>(visited, pd),
102+
impl::default_visit_callback<GraphType, ResultDiscriminator>(visited, pd),
103103
impl::default_enqueue_vertex_predicate<GraphType>(visited),
104104
pre_visit,
105105
post_visit
106106
);
107107
}
108108

109-
if constexpr (not type_traits::c_alg_no_return_type<AlgReturnType>)
109+
if constexpr (ResultDiscriminator == algorithm::ret)
110110
return pd;
111111
}
112112

include/gl/algorithm/dijkstra.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ template <
9595
algorithm::empty_callback{}, // visit predicate
9696
algorithm::empty_callback{}, // visit callback
9797
[&paths, &negative_edge](const vertex_type& vertex, const edge_type& in_edge)
98-
-> std::optional<bool> { // enqueue predicate
98+
-> predicate_result { // enqueue predicate
9999
const auto vertex_id = vertex.id();
100100
const auto source_id = in_edge.incident_vertex(vertex).id();
101101

102102
const auto edge_weight = get_weight<GraphType>(in_edge);
103103
if (edge_weight < constants::zero) {
104104
negative_edge = std::cref(in_edge);
105-
return std::nullopt;
105+
return predicate_result::unknown;
106106
}
107107

108108
const auto new_distance = paths.distances[source_id] + edge_weight;

include/gl/algorithm/impl/bfs.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ template <
1616
std::vector<algorithm::vertex_info>,
1717
type_traits::c_optional_vertex_callback<GraphType, bool> VisitVertexPredicate,
1818
type_traits::c_optional_vertex_callback<GraphType, bool, types::id_type> VisitCallback,
19-
type_traits::
20-
c_vertex_callback<GraphType, std::optional<bool>, const typename GraphType::edge_type&>
21-
EnqueueVertexPred,
19+
type_traits::c_vertex_callback<GraphType, predicate_result, const typename GraphType::edge_type&>
20+
EnqueueVertexPred,
2221
type_traits::c_optional_vertex_callback<GraphType, void> PreVisitCallback =
2322
algorithm::empty_callback,
2423
type_traits::c_optional_vertex_callback<GraphType, void> PostVisitCallback =
@@ -63,10 +62,10 @@ bool bfs(
6362
const auto& incident_vertex = edge.incident_vertex(vertex);
6463

6564
const auto enqueue = enqueue_vertex_pred(incident_vertex, edge);
66-
if (not enqueue.has_value())
65+
if (enqueue == predicate_result::unknown)
6766
return false;
6867

69-
if (enqueue.value())
68+
if (enqueue)
7069
vertex_queue.emplace(incident_vertex.id(), vinfo.id);
7170
}
7271

include/gl/algorithm/impl/common.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ namespace gl::algorithm::impl {
1111
// --- common functions ---
1212

1313
template <
14-
type_traits::c_alg_return_type AlgReturnType,
14+
result_discriminator ResultDiscriminator,
1515
typename ReturnType,
1616
type_traits::c_graph GraphType>
17-
[[nodiscard]] gl_attr_force_inline alg_return_type_non_void<AlgReturnType, ReturnType>
17+
[[nodiscard]] gl_attr_force_inline alg_return_type_non_void<ResultDiscriminator, ReturnType>
1818
init_return_value(const GraphType& graph) {
19-
if constexpr (type_traits::c_alg_no_return_type<AlgReturnType>)
20-
return AlgReturnType{};
19+
using return_type = alg_return_type_non_void<ResultDiscriminator, ReturnType>;
20+
if constexpr (ResultDiscriminator == algorithm::ret)
21+
return return_type(graph.n_vertices());
2122
else
22-
return ReturnType(graph.n_vertices());
23+
return return_type();
2324
}
2425

2526
template <
@@ -36,23 +37,24 @@ template <type_traits::c_graph GraphType>
3637
};
3738
}
3839

39-
template <type_traits::c_graph GraphType, type_traits::c_alg_return_type AlgReturnType>
40+
template <type_traits::c_graph GraphType, result_discriminator ResultDiscriminator>
4041
[[nodiscard]] gl_attr_force_inline auto default_visit_callback(
41-
std::vector<bool>& visited, alg_return_type_non_void<AlgReturnType, predecessors_descriptor>& pd
42+
std::vector<bool>& visited,
43+
alg_return_type_non_void<ResultDiscriminator, predecessors_descriptor>& pd
4244
) {
4345
return [&](const typename GraphType::vertex_type& vertex, const types::id_type source_id) {
4446
const auto vertex_id = vertex.id();
4547
visited[vertex_id] = true;
46-
if constexpr (type_traits::c_alg_default_return_type<AlgReturnType>)
48+
if constexpr (ResultDiscriminator == algorithm::ret)
4749
pd[vertex_id].emplace(source_id);
4850
return true;
4951
};
5052
}
5153

52-
template <type_traits::c_graph GraphType, bool AsOptional = false>
54+
template <type_traits::c_graph GraphType, bool AsResult = false>
5355
[[nodiscard]] gl_attr_force_inline auto default_enqueue_vertex_predicate(std::vector<bool>& visited
5456
) {
55-
using return_type = std::conditional_t<AsOptional, std::optional<bool>, bool>;
57+
using return_type = std::conditional_t<AsResult, predicate_result, bool>;
5658

5759
return [&](const typename GraphType::vertex_type& vertex,
5860
const typename GraphType::edge_type& in_edge) -> return_type {

include/gl/algorithm/impl/dfs.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ template <
1414
type_traits::c_graph GraphType,
1515
type_traits::c_optional_vertex_callback<GraphType, bool> VisitVertexPredicate,
1616
type_traits::c_vertex_callback<GraphType, bool, types::id_type> VisitCallback,
17-
type_traits::
18-
c_vertex_callback<GraphType, std::optional<bool>, const typename GraphType::edge_type&>
19-
EnqueueVertexPred,
17+
type_traits::c_vertex_callback<GraphType, predicate_result, const typename GraphType::edge_type&>
18+
EnqueueVertexPred,
2019
type_traits::c_optional_vertex_callback<GraphType, void> PreVisitCallback =
2120
algorithm::empty_callback,
2221
type_traits::c_optional_vertex_callback<GraphType, void> PostVisitCallback =
@@ -70,9 +69,8 @@ template <
7069
type_traits::c_graph GraphType,
7170
type_traits::c_vertex_callback<GraphType, bool> VisitVertexPredicate,
7271
type_traits::c_vertex_callback<GraphType, bool, types::id_type> VisitCallback,
73-
type_traits::
74-
c_vertex_callback<GraphType, std::optional<bool>, const typename GraphType::edge_type&>
75-
EnqueueVertexPred,
72+
type_traits::c_vertex_callback<GraphType, predicate_result, const typename GraphType::edge_type&>
73+
EnqueueVertexPred,
7674
type_traits::c_optional_vertex_callback<GraphType, void> PreVisitCallback =
7775
algorithm::empty_callback,
7876
type_traits::c_optional_vertex_callback<GraphType, void> PostVisitCallback =

include/gl/algorithm/impl/pfs.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ template <
1717
std::vector<algorithm::vertex_info>,
1818
type_traits::c_optional_vertex_callback<GraphType, bool> VisitVertexPredicate,
1919
type_traits::c_optional_callback<GraphType, bool, types::id_type> VisitCallback,
20-
type_traits::
21-
c_vertex_callback<GraphType, std::optional<bool>, const typename GraphType::edge_type&>
22-
EnqueueVertexPred,
20+
type_traits::c_vertex_callback<GraphType, predicate_result, const typename GraphType::edge_type&>
21+
EnqueueVertexPred,
2322
type_traits::c_optional_vertex_callback<GraphType, void> PreVisitCallback =
2423
algorithm::empty_callback,
2524
type_traits::c_optional_vertex_callback<GraphType, void> PostVisitCallback =
@@ -66,10 +65,10 @@ bool pfs(
6665
const auto& incident_vertex = edge.incident_vertex(vertex);
6766

6867
const auto enqueue = enqueue_vertex_pred(incident_vertex, edge);
69-
if (not enqueue.has_value())
68+
if (enqueue == predicate_result::unknown)
7069
return false;
7170

72-
if (enqueue.value())
71+
if (enqueue)
7372
vertex_queue.emplace(incident_vertex.id(), vinfo.id);
7473
}
7574
if constexpr (not type_traits::c_empty_callback<PostVisitCallback>)

include/gl/algorithm/topological_sort.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ template <
5050
return true;
5151
},
5252
[&in_degree_map](const vertex_type& vertex, const edge_type& in_edge)
53-
-> std::optional<bool> { // enqueue predicate
53+
-> predicate_result { // enqueue predicate
5454
if (in_edge.is_loop())
5555
return false;
5656
return --in_degree_map[vertex.id()] == constants::default_size;

0 commit comments

Comments
 (0)