Skip to content

Commit fb8de4e

Browse files
authored
Simplify usage of std::optional<bool> (#556)
1 parent d394fc5 commit fb8de4e

File tree

12 files changed

+62
-110
lines changed

12 files changed

+62
-110
lines changed

include/CXXGraph/Graph/Algorithm/BellmanFord_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const BellmanFordResult Graph<T>::bellmanford(const Node<T> &source,
7373
// each relaxation
7474
for (const auto &edge : edgeSet) {
7575
auto elem = edge->getNodePair();
76-
if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
76+
if (edge->isWeighted().value_or(false)) {
7777
auto edge_weight =
7878
(std::dynamic_pointer_cast<const Weighted>(edge))->getWeight();
7979
if (dist[elem.first] + edge_weight < dist[elem.second])

include/CXXGraph/Graph/Algorithm/Boruvka_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const MstResult Graph<T>::boruvka() const {
5353
const auto edgeSet = Graph<T>::getEdgeSet();
5454
std::unordered_map<CXXGraph::id_t, double> edgeWeight;
5555
for (const auto &edge : edgeSet) {
56-
if (edge->isWeighted().has_value() && edge->isWeighted().value())
56+
if (edge->isWeighted().value_or(false))
5757
edgeWeight[edge->getId()] =
5858
(std::dynamic_pointer_cast<const Weighted>(edge))->getWeight();
5959
else {
@@ -148,7 +148,7 @@ const MstResult Graph<T>::boruvka_deterministic() const {
148148
const auto edgeSet = Graph<T>::getEdgeSet();
149149
std::unordered_map<CXXGraph::id_t, double> edgeWeight;
150150
for (const auto &edge : edgeSet) {
151-
if (edge->isWeighted().has_value() && edge->isWeighted().value())
151+
if (edge->isWeighted().value_or(false))
152152
edgeWeight[edge->getId()] =
153153
(std::dynamic_pointer_cast<const Weighted>(edge))->getWeight();
154154
else {

include/CXXGraph/Graph/Algorithm/Dial_impl.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,12 @@ const DialResult Graph<T>::dial(const Node<T> &source, int maxWeight) const {
8787
for (const auto &i : (*cachedAdjListOut)[u]) {
8888
auto v = i.first;
8989
int weight = 0;
90-
if (i.second->isWeighted().has_value() &&
91-
i.second->isWeighted().value()) {
92-
if (i.second->isDirected().has_value() &&
93-
i.second->isDirected().value()) {
90+
if (i.second->isWeighted().value_or(false)) {
91+
if (i.second->isDirected().value_or(false)) {
9492
shared<const DirectedWeightedEdge<T>> dw_edge =
9593
std::static_pointer_cast<const DirectedWeightedEdge<T>>(i.second);
9694
weight = (int)dw_edge->getWeight();
97-
} else if (i.second->isDirected().has_value() &&
98-
!i.second->isDirected().value()) {
95+
} else if (i.second->isDirected() == false) {
9996
shared<const UndirectedWeightedEdge<T>> udw_edge =
10097
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
10198
i.second);

include/CXXGraph/Graph/Algorithm/Dijkstra_impl.hpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T>& source,
9191
if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
9292
for (const auto& elem : cachedAdjListOut->at(currentNode)) {
9393
// minimizing distances
94-
if (elem.second->isWeighted().has_value() &&
95-
elem.second->isWeighted().value()) {
96-
if (elem.second->isDirected().has_value() &&
97-
elem.second->isDirected().value()) {
94+
if (elem.second->isWeighted().value_or(false)) {
95+
if (elem.second->isDirected().value_or(false)) {
9896
shared<const DirectedWeightedEdge<T>> dw_edge =
9997
std::static_pointer_cast<const DirectedWeightedEdge<T>>(
10098
elem.second);
@@ -107,8 +105,7 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T>& source,
107105
parent[elem.first.get()->getUserId()] =
108106
currentNode.get()->getUserId();
109107
}
110-
} else if (elem.second->isDirected().has_value() &&
111-
!elem.second->isDirected().value()) {
108+
} else if (elem.second->isDirected() == false) {
112109
shared<const UndirectedWeightedEdge<T>> udw_edge =
113110
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
114111
elem.second);
@@ -235,10 +232,8 @@ const DijkstraResult Graph<T>::dijkstra_deterministic(
235232
if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
236233
for (const auto& elem : cachedAdjListOut->at(currentNode)) {
237234
// minimizing distances
238-
if (elem.second->isWeighted().has_value() &&
239-
elem.second->isWeighted().value()) {
240-
if (elem.second->isDirected().has_value() &&
241-
elem.second->isDirected().value()) {
235+
if (elem.second->isWeighted().value_or(false)) {
236+
if (elem.second->isDirected().value_or(false)) {
242237
shared<const DirectedWeightedEdge<T>> dw_edge =
243238
std::static_pointer_cast<const DirectedWeightedEdge<T>>(
244239
elem.second);
@@ -253,8 +248,7 @@ const DijkstraResult Graph<T>::dijkstra_deterministic(
253248
parent[elem.first.get()->getUserId()] =
254249
currentNode.get()->getUserId();
255250
}
256-
} else if (elem.second->isDirected().has_value() &&
257-
!elem.second->isDirected().value()) {
251+
} else if (!elem.second->isDirected() == false) {
258252
shared<const UndirectedWeightedEdge<T>> udw_edge =
259253
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
260254
elem.second);
@@ -391,10 +385,8 @@ const DijkstraResult Graph<T>::dijkstra_deterministic2(
391385
if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
392386
for (const auto& elem : cachedAdjListOut->at(currentNode)) {
393387
// minimizing distances
394-
if (elem.second->isWeighted().has_value() &&
395-
elem.second->isWeighted().value()) {
396-
if (elem.second->isDirected().has_value() &&
397-
elem.second->isDirected().value()) {
388+
if (elem.second->isWeighted().value_or(false)) {
389+
if (elem.second->isDirected().value_or(false)) {
398390
shared<const DirectedWeightedEdge<T>> dw_edge =
399391
std::static_pointer_cast<const DirectedWeightedEdge<T>>(
400392
elem.second);
@@ -409,8 +401,7 @@ const DijkstraResult Graph<T>::dijkstra_deterministic2(
409401
parent[elem.first.get()->getUserId()] =
410402
currentNode.get()->getUserId();
411403
}
412-
} else if (elem.second->isDirected().has_value() &&
413-
!elem.second->isDirected().value()) {
404+
} else if (!elem.second->isDirected() == false) {
414405
shared<const UndirectedWeightedEdge<T>> udw_edge =
415406
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
416407
elem.second);
@@ -538,10 +529,8 @@ const DijkstraResult Graph<T>::criticalpath_deterministic(
538529
if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
539530
for (const auto& elem : cachedAdjListOut->at(currentNode)) {
540531
// minimizing distances
541-
if (elem.second->isWeighted().has_value() &&
542-
elem.second->isWeighted().value()) {
543-
if (elem.second->isDirected().has_value() &&
544-
elem.second->isDirected().value()) {
532+
if (elem.second->isWeighted().value_or(false)) {
533+
if (elem.second->isDirected().value_or(false)) {
545534
shared<const DirectedWeightedEdge<T>> dw_edge =
546535
std::static_pointer_cast<const DirectedWeightedEdge<T>>(
547536
elem.second);
@@ -556,8 +545,7 @@ const DijkstraResult Graph<T>::criticalpath_deterministic(
556545
parent[elem.first.get()->getUserId()] =
557546
currentNode.get()->getUserId();
558547
}
559-
} else if (elem.second->isDirected().has_value() &&
560-
!elem.second->isDirected().value()) {
548+
} else if (elem.second->isDirected() == false) {
561549
shared<const UndirectedWeightedEdge<T>> udw_edge =
562550
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
563551
elem.second);

include/CXXGraph/Graph/Algorithm/FloydWarshall_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const FWResult Graph<T>::floydWarshall() const {
5151
// connected by edges
5252
for (const auto &edge : edgeSet) {
5353
const auto &elem = edge->getNodePair();
54-
if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
54+
if (edge->isWeighted().value_or(false)) {
5555
auto edgeWeight =
5656
(std::dynamic_pointer_cast<const Weighted>(edge))->getWeight();
5757
auto key =

include/CXXGraph/Graph/Algorithm/Kruskal_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const MstResult Graph<T>::kruskal() const {
4747
std::greater<std::pair<double, shared<const Edge<T>>>>>
4848
sortedEdges;
4949
for (const auto &edge : edgeSet) {
50-
if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
50+
if (edge->isWeighted().value_or(false)) {
5151
auto weight =
5252
(std::dynamic_pointer_cast<const Weighted>(edge))->getWeight();
5353
sortedEdges.push(std::make_pair(weight, edge));

include/CXXGraph/Graph/Algorithm/Prim_impl.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ const MstResult Graph<T>::prim() const {
8686
if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
8787
for (const auto &elem : cachedAdjListOut->at(currentNode)) {
8888
// minimizing distances
89-
if (elem.second->isWeighted().has_value() &&
90-
elem.second->isWeighted().value()) {
89+
if (elem.second->isWeighted().value_or(false)) {
9190
shared<const UndirectedWeightedEdge<T>> udw_edge =
9291
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
9392
elem.second);

include/CXXGraph/Graph/Graph_impl.hpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -971,30 +971,20 @@ Graph<T>::inOrOutEdges(shared<const Node<T>> node) const {
971971
return inOrOutEdges;
972972
}
973973

974+
inline const auto isEdgeDirected = [](const auto &edge) {
975+
return edge->isDirected().value_or(false);
976+
};
977+
974978
template <typename T>
975979
bool Graph<T>::isDirectedGraph() const {
976-
auto edgeSet = getEdgeSet();
977-
for (const auto &edge : edgeSet) {
978-
if (!(edge->isDirected().has_value() && edge->isDirected().value())) {
979-
// Found Undirected Edge
980-
return false;
981-
}
982-
}
983-
// No Undirected Edge
984-
return true;
980+
const auto edgeSet = getEdgeSet();
981+
return std::all_of(edgeSet.cbegin(), edgeSet.cend(), isEdgeDirected);
985982
}
986983

987984
template <typename T>
988985
bool Graph<T>::isUndirectedGraph() const {
989-
auto edgeSet = Graph<T>::getEdgeSet();
990-
for (const auto &edge : edgeSet) {
991-
if ((edge->isDirected().has_value() && edge->isDirected().value())) {
992-
// Found Directed Edge
993-
return false;
994-
}
995-
}
996-
// No Directed Edge
997-
return true;
986+
const auto edgeSet = Graph<T>::getEdgeSet();
987+
return std::none_of(edgeSet.cbegin(), edgeSet.cend(), isEdgeDirected);
998988
}
999989

1000990
template <typename T>

include/CXXGraph/Graph/IO/OutputOperation_impl.hpp

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ int Graph<T>::writeToMTXFile(const std::string &workingDir,
158158
// Check if the adjacency matrix is symmetric, i.e., if all the edges are
159159
// undirected
160160
bool symmetric = !std::any_of(edgeSet.begin(), edgeSet.end(), [](auto edge) {
161-
return (edge->isDirected().has_value() && edge->isDirected().value());
161+
return edge->isDirected().value_or(false);
162162
});
163163
// Write in the header whether the adj matrix is symmetric or not
164164
if (symmetric) {
@@ -180,7 +180,7 @@ int Graph<T>::writeToMTXFile(const std::string &workingDir,
180180
std::string line;
181181
line += edgeIt->getNodePair().first->getUserId() + delimitier;
182182
line += edgeIt->getNodePair().second->getUserId() + delimitier;
183-
if (edgeIt->isWeighted().has_value() && edgeIt->isWeighted().value()) {
183+
if (edgeIt->isWeighted().value_or(false)) {
184184
line += std::to_string(edgeIt->isWeighted().value()) + '\n';
185185
} else {
186186
line += std::to_string(1.) + '\n';
@@ -261,10 +261,10 @@ int Graph<T>::writeToBinary(const std::string &filepath, bool writeNodeFeatures,
261261
writeBinaryString(out, edge->getNodePair().second->getUserId());
262262

263263
uint8_t edgeFlags = 0;
264-
if (edge->isDirected().has_value() && edge->isDirected().value()) {
264+
if (edge->isDirected().value_or(false)) {
265265
edgeFlags |= 0x01;
266266
}
267-
if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
267+
if (edge->isWeighted().value_or(false)) {
268268
edgeFlags |= 0x02;
269269
}
270270
out.write(reinterpret_cast<const char *>(&edgeFlags), sizeof(edgeFlags));
@@ -313,7 +313,7 @@ int Graph<T>::writeToDot(const std::string &workingDir,
313313

314314
for (auto const &edgePtr : edgeSet) {
315315
std::string edgeLine = "";
316-
if (edgePtr->isDirected().has_value() && edgePtr->isDirected().value()) {
316+
if (edgePtr->isDirected().value_or(false)) {
317317
auto directedPtr =
318318
std::static_pointer_cast<const DirectedEdge<T>>(edgePtr);
319319
edgeLine += '\t' + directedPtr->getFrom().getUserId() + ' ';
@@ -324,7 +324,7 @@ int Graph<T>::writeToDot(const std::string &workingDir,
324324
edgeLine += linkSymbol + ' ';
325325
edgeLine += edgePtr->getNodePair().second->getUserId();
326326
}
327-
if (edgePtr->isWeighted().has_value() && edgePtr->isWeighted().value()) {
327+
if (edgePtr->isWeighted().value_or(false)) {
328328
// Weights in dot files must be integers
329329
edgeLine += " [weight=" +
330330
std::to_string(static_cast<int>(
@@ -349,10 +349,7 @@ void Graph<T>::writeGraphToStream(std::ostream &oGraph, std::ostream &oNodeFeat,
349349
for (const auto &edge : edgeSet) {
350350
oGraph << edge->getUserId() << sep << edge->getNodePair().first->getUserId()
351351
<< sep << edge->getNodePair().second->getUserId() << sep
352-
<< ((edge->isDirected().has_value() && edge->isDirected().value())
353-
? 1
354-
: 0)
355-
<< std::endl;
352+
<< (edge->isDirected().value_or(false) ? 1 : 0) << std::endl;
356353
}
357354

358355
if (writeNodeFeat) {
@@ -364,16 +361,13 @@ void Graph<T>::writeGraphToStream(std::ostream &oGraph, std::ostream &oNodeFeat,
364361

365362
if (writeEdgeWeight) {
366363
for (const auto &edge : edgeSet) {
367-
oEdgeWeight
368-
<< edge->getUserId() << sep
369-
<< (edge->isWeighted().has_value() && edge->isWeighted().value()
370-
? (std::dynamic_pointer_cast<const Weighted>(edge))
371-
->getWeight()
372-
: 0.0)
373-
<< sep
374-
<< (edge->isWeighted().has_value() && edge->isWeighted().value() ? 1
375-
: 0)
376-
<< std::endl;
364+
oEdgeWeight << edge->getUserId() << sep
365+
<< (edge->isWeighted().value_or(false)
366+
? (std::dynamic_pointer_cast<const Weighted>(edge))
367+
->getWeight()
368+
: 0.0)
369+
<< sep << (edge->isWeighted().value_or(false) ? 1 : 0)
370+
<< std::endl;
377371
}
378372
}
379373
}
@@ -395,27 +389,19 @@ std::ostream &operator<<(std::ostream &os, const Graph<T> &graph) {
395389
if (!(*it)->isDirected().has_value() && !(*it)->isWeighted().has_value()) {
396390
// Edge Case
397391
os << **it << "\n";
398-
} else if (((*it)->isDirected().has_value() &&
399-
(*it)->isDirected().value()) &&
400-
((*it)->isWeighted().has_value() &&
401-
(*it)->isWeighted().value())) {
392+
} else if ((*it)->isDirected().value_or(false) &&
393+
(*it)->isWeighted().value_or(false)) {
402394
os << *std::static_pointer_cast<const DirectedWeightedEdge<T>>(*it)
403395
<< "\n";
404-
} else if (((*it)->isDirected().has_value() &&
405-
(*it)->isDirected().value()) &&
406-
!((*it)->isWeighted().has_value() &&
407-
(*it)->isWeighted().value())) {
396+
} else if ((*it)->isDirected().value_or(false) &&
397+
!((*it)->isWeighted().value_or(false))) {
408398
os << *std::static_pointer_cast<const DirectedEdge<T>>(*it) << "\n";
409-
} else if (!((*it)->isDirected().has_value() &&
410-
(*it)->isDirected().value()) &&
411-
((*it)->isWeighted().has_value() &&
412-
(*it)->isWeighted().value())) {
399+
} else if (!((*it)->isDirected().value_or(false)) &&
400+
(*it)->isWeighted().value_or(false)) {
413401
os << *std::static_pointer_cast<const UndirectedWeightedEdge<T>>(*it)
414402
<< "\n";
415-
} else if (!((*it)->isDirected().has_value() &&
416-
(*it)->isDirected().value()) &&
417-
!((*it)->isWeighted().has_value() &&
418-
(*it)->isWeighted().value())) {
403+
} else if (!((*it)->isDirected().value_or(false)) &&
404+
!((*it)->isWeighted().value_or(false))) {
419405
os << *std::static_pointer_cast<const UndirectedEdge<T>>(*it) << "\n";
420406
} else {
421407
os << *it << "\n";

include/CXXGraph/Partitioning/CoordinatedPartitionState.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void CoordinatedPartitionState<T>::incrementMachineWeight(
145145
const int m, shared<const Edge<T>> e) {
146146
std::lock_guard<std::mutex> lock(*machines_weight_edges_mutex);
147147
double edge_weight = CXXGraph::NEGLIGIBLE_WEIGHT;
148-
if (e->isWeighted().has_value() && e->isWeighted().value()) {
148+
if (e->isWeighted().value_or(false)) {
149149
edge_weight = (std::dynamic_pointer_cast<const Weighted>(e))->getWeight();
150150
}
151151
machines_weight_edges[m] = machines_weight_edges[m] + edge_weight;

0 commit comments

Comments
 (0)