Skip to content

Commit e81bba4

Browse files
committed
algs aligned
1 parent 61e03b5 commit e81bba4

11 files changed

Lines changed: 1400 additions & 1402 deletions

include/gl/algorithm/dijkstra.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ template <
8282
paths.predecessors.at(source_id).emplace(source_id);
8383
paths.distances[source_id] = distance_type{};
8484

85-
std::optional<types::const_ref_wrap<edge_type>> negative_edge;
85+
std::optional<edge_type> negative_edge;
8686

8787
impl::pfs(
8888
graph,
@@ -98,7 +98,7 @@ template <
9898

9999
const auto edge_weight = get_weight<GraphType>(in_edge);
100100
if (edge_weight < constants::zero) {
101-
negative_edge = std::cref(in_edge);
101+
negative_edge.emplace(in_edge);
102102
return predicate_result::unknown;
103103
}
104104

@@ -115,7 +115,7 @@ template <
115115
);
116116

117117
if (negative_edge.has_value()) {
118-
const auto& edge = negative_edge.value().get();
118+
const auto& edge = negative_edge.value();
119119
throw std::invalid_argument(std::format(
120120
"[alg::dijkstra_shortest_paths] Found an edge with a negative weight: [{}, {} | w={}]",
121121
edge.first(),

include/gl/algorithm/mst.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct mst_descriptor {
2222
edges.reserve(n_vertices - constants::one);
2323
}
2424

25-
std::vector<types::const_ref_wrap<edge_type>> edges;
25+
std::vector<edge_type> edges;
2626
weight_type weight = static_cast<weight_type>(constants::zero);
2727
};
2828

@@ -41,7 +41,7 @@ template <type_traits::c_undirected_graph GraphType>
4141
[[nodiscard]] gl_attr_force_inline bool operator()(
4242
const edge_info_type& lhs, const edge_info_type& rhs
4343
) const {
44-
return get_weight<GraphType>(lhs.edge.get()) > get_weight<GraphType>(rhs.edge.get());
44+
return get_weight<GraphType>(lhs.edge) > get_weight<GraphType>(rhs.edge);
4545
}
4646
};
4747

@@ -69,7 +69,7 @@ template <type_traits::c_undirected_graph GraphType>
6969
const auto min_edge_info = edge_queue.top();
7070
edge_queue.pop();
7171

72-
const auto& min_edge = min_edge_info.edge.get();
72+
const auto& min_edge = min_edge_info.edge;
7373
const auto min_weight = get_weight<GraphType>(min_edge);
7474

7575
const auto& target_id = min_edge.incident_vertex(min_edge_info.source_id);
@@ -107,7 +107,7 @@ requires type_traits::c_has_numeric_limits_max<types::vertex_distance_type<Graph
107107

108108
std::vector<bool> in_mst(n_vertices, false);
109109
std::vector<distance_type> min_cost(n_vertices, std::numeric_limits<distance_type>::max());
110-
std::vector<const edge_type*> min_cost_edges(n_vertices, nullptr);
110+
std::vector<std::optional<edge_type>> min_cost_edges(n_vertices, std::nullopt);
111111

112112
// set the distance to the root vertex to 0
113113
min_cost.at(root_id_opt.value_or(constants::zero)) = constants::zero;
@@ -132,8 +132,8 @@ requires type_traits::c_has_numeric_limits_max<types::vertex_distance_type<Graph
132132

133133
in_mst[vertex_id] = true;
134134

135-
const auto* min_cost_edge = min_cost_edges[vertex_id];
136-
if (min_cost_edge != nullptr) { // Add the corresponding edge to MST
135+
const auto min_cost_edge = min_cost_edges[vertex_id];
136+
if (min_cost_edge.has_value()) { // Add the corresponding edge to MST
137137
mst.edges.emplace_back(*min_cost_edge);
138138
mst.weight += min_cost[vertex_id];
139139
}
@@ -145,7 +145,7 @@ requires type_traits::c_has_numeric_limits_max<types::vertex_distance_type<Graph
145145

146146
if (not in_mst[incident_vertex_id] && edge_weight < min_cost[incident_vertex_id]) {
147147
min_cost[incident_vertex_id] = edge_weight;
148-
min_cost_edges[incident_vertex_id] = &edge;
148+
min_cost_edges[incident_vertex_id].emplace(edge);
149149
}
150150
}
151151

include/gl/algorithm/types.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ template <type_traits::c_instantiation_of<edge_descriptor> EdgeType>
3131
struct edge_info {
3232
using edge_type = EdgeType;
3333

34-
types::const_ref_wrap<edge_type> edge;
34+
edge_type edge;
3535
types::id_type source_id;
3636
};
3737

include/gl/edge_descriptor.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class edge_descriptor final {
159159

160160
[[nodiscard]] gl_attr_force_inline properties_ref_type properties() const {
161161
// TODO: throw if edge is invalid
162-
return this->_properties;
162+
return this->_properties.get();
163163
}
164164

165165
friend inline std::ostream& operator<<(std::ostream& os, const edge_descriptor& edge) {
@@ -181,11 +181,11 @@ class edge_descriptor final {
181181

182182
if (io::is_option_set(os, io::graph_option::verbose)) {
183183
os << "[first: " << this->_vertices.first << ", second: " << this->_vertices.second
184-
<< " | properties: " << this->_properties << "]";
184+
<< " | properties: " << this->_properties.get() << "]";
185185
}
186186
else {
187187
os << "[" << this->_vertices.first << ", " << this->_vertices.second << " | "
188-
<< this->_properties << "]";
188+
<< this->_properties.get() << "]";
189189
}
190190
}
191191
}
@@ -200,7 +200,10 @@ class edge_descriptor final {
200200

201201
types::id_type _id;
202202
types::homogeneous_pair<types::id_type> _vertices;
203-
[[no_unique_address]] properties_ref_type _properties;
203+
[[no_unique_address]] std::conditional_t<
204+
type_traits::c_empty_properties<properties_type>,
205+
types::empty_properties,
206+
std::reference_wrapper<properties_type>> _properties;
204207
};
205208

206209
template <

include/gl/vertex_descriptor.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class vertex_descriptor final {
5656
}
5757

5858
[[nodiscard]] gl_attr_force_inline properties_ref_type properties() const {
59-
return this->_properties;
59+
return this->_properties.get();
6060
}
6161

6262
friend inline std::ostream& operator<<(std::ostream& os, const vertex_descriptor& vertex) {
@@ -77,10 +77,10 @@ class vertex_descriptor final {
7777
}
7878

7979
if (io::is_option_set(os, io::graph_option::verbose)) {
80-
os << "[id: " << this->_id << " | properties: " << this->_properties << "]";
80+
os << "[id: " << this->_id << " | properties: " << this->_properties.get() << "]";
8181
}
8282
else {
83-
os << "[" << this->_id << " | " << this->_properties << "]";
83+
os << "[" << this->_id << " | " << this->_properties.get() << "]";
8484
}
8585
}
8686
}
@@ -95,7 +95,10 @@ class vertex_descriptor final {
9595
}
9696

9797
types::id_type _id;
98-
[[no_unique_address]] properties_ref_type _properties;
98+
[[no_unique_address]] std::conditional_t<
99+
type_traits::c_empty_properties<properties_type>,
100+
types::empty_properties,
101+
std::reference_wrapper<properties_type>> _properties;
99102
};
100103

101104
template <type_traits::c_properties Properties = types::empty_properties>

0 commit comments

Comments
 (0)