diff --git a/include/CXXGraph/Graph/Algorithm/BellmanFord_impl.hpp b/include/CXXGraph/Graph/Algorithm/BellmanFord_impl.hpp index e9d725081..15dae4f8f 100644 --- a/include/CXXGraph/Graph/Algorithm/BellmanFord_impl.hpp +++ b/include/CXXGraph/Graph/Algorithm/BellmanFord_impl.hpp @@ -73,7 +73,7 @@ const BellmanFordResult Graph::bellmanford(const Node &source, // each relaxation for (const auto &edge : edgeSet) { auto elem = edge->getNodePair(); - if (edge->isWeighted().has_value() && edge->isWeighted().value()) { + if (edge->isWeighted().value_or(false)) { auto edge_weight = (std::dynamic_pointer_cast(edge))->getWeight(); if (dist[elem.first] + edge_weight < dist[elem.second]) diff --git a/include/CXXGraph/Graph/Algorithm/Boruvka_impl.hpp b/include/CXXGraph/Graph/Algorithm/Boruvka_impl.hpp index b093da1e2..bdd55ab6f 100644 --- a/include/CXXGraph/Graph/Algorithm/Boruvka_impl.hpp +++ b/include/CXXGraph/Graph/Algorithm/Boruvka_impl.hpp @@ -53,7 +53,7 @@ const MstResult Graph::boruvka() const { const auto edgeSet = Graph::getEdgeSet(); std::unordered_map edgeWeight; for (const auto &edge : edgeSet) { - if (edge->isWeighted().has_value() && edge->isWeighted().value()) + if (edge->isWeighted().value_or(false)) edgeWeight[edge->getId()] = (std::dynamic_pointer_cast(edge))->getWeight(); else { @@ -148,7 +148,7 @@ const MstResult Graph::boruvka_deterministic() const { const auto edgeSet = Graph::getEdgeSet(); std::unordered_map edgeWeight; for (const auto &edge : edgeSet) { - if (edge->isWeighted().has_value() && edge->isWeighted().value()) + if (edge->isWeighted().value_or(false)) edgeWeight[edge->getId()] = (std::dynamic_pointer_cast(edge))->getWeight(); else { diff --git a/include/CXXGraph/Graph/Algorithm/Dial_impl.hpp b/include/CXXGraph/Graph/Algorithm/Dial_impl.hpp index e936d8e63..d1679c0d8 100644 --- a/include/CXXGraph/Graph/Algorithm/Dial_impl.hpp +++ b/include/CXXGraph/Graph/Algorithm/Dial_impl.hpp @@ -87,15 +87,12 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { for (const auto &i : (*cachedAdjListOut)[u]) { auto v = i.first; int weight = 0; - if (i.second->isWeighted().has_value() && - i.second->isWeighted().value()) { - if (i.second->isDirected().has_value() && - i.second->isDirected().value()) { + if (i.second->isWeighted().value_or(false)) { + if (i.second->isDirected().value_or(false)) { shared> dw_edge = std::static_pointer_cast>(i.second); weight = (int)dw_edge->getWeight(); - } else if (i.second->isDirected().has_value() && - !i.second->isDirected().value()) { + } else if (i.second->isDirected() == false) { shared> udw_edge = std::static_pointer_cast>( i.second); diff --git a/include/CXXGraph/Graph/Algorithm/Dijkstra_impl.hpp b/include/CXXGraph/Graph/Algorithm/Dijkstra_impl.hpp index b75bc9173..8f2d4a438 100644 --- a/include/CXXGraph/Graph/Algorithm/Dijkstra_impl.hpp +++ b/include/CXXGraph/Graph/Algorithm/Dijkstra_impl.hpp @@ -91,10 +91,8 @@ const DijkstraResult Graph::dijkstra(const Node& source, if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) { for (const auto& elem : cachedAdjListOut->at(currentNode)) { // minimizing distances - if (elem.second->isWeighted().has_value() && - elem.second->isWeighted().value()) { - if (elem.second->isDirected().has_value() && - elem.second->isDirected().value()) { + if (elem.second->isWeighted().value_or(false)) { + if (elem.second->isDirected().value_or(false)) { shared> dw_edge = std::static_pointer_cast>( elem.second); @@ -107,8 +105,7 @@ const DijkstraResult Graph::dijkstra(const Node& source, parent[elem.first.get()->getUserId()] = currentNode.get()->getUserId(); } - } else if (elem.second->isDirected().has_value() && - !elem.second->isDirected().value()) { + } else if (elem.second->isDirected() == false) { shared> udw_edge = std::static_pointer_cast>( elem.second); @@ -235,10 +232,8 @@ const DijkstraResult Graph::dijkstra_deterministic( if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) { for (const auto& elem : cachedAdjListOut->at(currentNode)) { // minimizing distances - if (elem.second->isWeighted().has_value() && - elem.second->isWeighted().value()) { - if (elem.second->isDirected().has_value() && - elem.second->isDirected().value()) { + if (elem.second->isWeighted().value_or(false)) { + if (elem.second->isDirected().value_or(false)) { shared> dw_edge = std::static_pointer_cast>( elem.second); @@ -253,8 +248,7 @@ const DijkstraResult Graph::dijkstra_deterministic( parent[elem.first.get()->getUserId()] = currentNode.get()->getUserId(); } - } else if (elem.second->isDirected().has_value() && - !elem.second->isDirected().value()) { + } else if (!elem.second->isDirected() == false) { shared> udw_edge = std::static_pointer_cast>( elem.second); @@ -391,10 +385,8 @@ const DijkstraResult Graph::dijkstra_deterministic2( if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) { for (const auto& elem : cachedAdjListOut->at(currentNode)) { // minimizing distances - if (elem.second->isWeighted().has_value() && - elem.second->isWeighted().value()) { - if (elem.second->isDirected().has_value() && - elem.second->isDirected().value()) { + if (elem.second->isWeighted().value_or(false)) { + if (elem.second->isDirected().value_or(false)) { shared> dw_edge = std::static_pointer_cast>( elem.second); @@ -409,8 +401,7 @@ const DijkstraResult Graph::dijkstra_deterministic2( parent[elem.first.get()->getUserId()] = currentNode.get()->getUserId(); } - } else if (elem.second->isDirected().has_value() && - !elem.second->isDirected().value()) { + } else if (!elem.second->isDirected() == false) { shared> udw_edge = std::static_pointer_cast>( elem.second); @@ -538,10 +529,8 @@ const DijkstraResult Graph::criticalpath_deterministic( if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) { for (const auto& elem : cachedAdjListOut->at(currentNode)) { // minimizing distances - if (elem.second->isWeighted().has_value() && - elem.second->isWeighted().value()) { - if (elem.second->isDirected().has_value() && - elem.second->isDirected().value()) { + if (elem.second->isWeighted().value_or(false)) { + if (elem.second->isDirected().value_or(false)) { shared> dw_edge = std::static_pointer_cast>( elem.second); @@ -556,8 +545,7 @@ const DijkstraResult Graph::criticalpath_deterministic( parent[elem.first.get()->getUserId()] = currentNode.get()->getUserId(); } - } else if (elem.second->isDirected().has_value() && - !elem.second->isDirected().value()) { + } else if (elem.second->isDirected() == false) { shared> udw_edge = std::static_pointer_cast>( elem.second); diff --git a/include/CXXGraph/Graph/Algorithm/FloydWarshall_impl.hpp b/include/CXXGraph/Graph/Algorithm/FloydWarshall_impl.hpp index 79305a5d6..a7282af1f 100644 --- a/include/CXXGraph/Graph/Algorithm/FloydWarshall_impl.hpp +++ b/include/CXXGraph/Graph/Algorithm/FloydWarshall_impl.hpp @@ -51,7 +51,7 @@ const FWResult Graph::floydWarshall() const { // connected by edges for (const auto &edge : edgeSet) { const auto &elem = edge->getNodePair(); - if (edge->isWeighted().has_value() && edge->isWeighted().value()) { + if (edge->isWeighted().value_or(false)) { auto edgeWeight = (std::dynamic_pointer_cast(edge))->getWeight(); auto key = diff --git a/include/CXXGraph/Graph/Algorithm/Kruskal_impl.hpp b/include/CXXGraph/Graph/Algorithm/Kruskal_impl.hpp index 710272218..01a5b356e 100644 --- a/include/CXXGraph/Graph/Algorithm/Kruskal_impl.hpp +++ b/include/CXXGraph/Graph/Algorithm/Kruskal_impl.hpp @@ -47,7 +47,7 @@ const MstResult Graph::kruskal() const { std::greater>>>> sortedEdges; for (const auto &edge : edgeSet) { - if (edge->isWeighted().has_value() && edge->isWeighted().value()) { + if (edge->isWeighted().value_or(false)) { auto weight = (std::dynamic_pointer_cast(edge))->getWeight(); sortedEdges.push(std::make_pair(weight, edge)); diff --git a/include/CXXGraph/Graph/Algorithm/Prim_impl.hpp b/include/CXXGraph/Graph/Algorithm/Prim_impl.hpp index 757cb67de..1ad0ddcd8 100644 --- a/include/CXXGraph/Graph/Algorithm/Prim_impl.hpp +++ b/include/CXXGraph/Graph/Algorithm/Prim_impl.hpp @@ -86,8 +86,7 @@ const MstResult Graph::prim() const { if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) { for (const auto &elem : cachedAdjListOut->at(currentNode)) { // minimizing distances - if (elem.second->isWeighted().has_value() && - elem.second->isWeighted().value()) { + if (elem.second->isWeighted().value_or(false)) { shared> udw_edge = std::static_pointer_cast>( elem.second); diff --git a/include/CXXGraph/Graph/Graph_impl.hpp b/include/CXXGraph/Graph/Graph_impl.hpp index 4e5bbd198..bd6c5cc7c 100644 --- a/include/CXXGraph/Graph/Graph_impl.hpp +++ b/include/CXXGraph/Graph/Graph_impl.hpp @@ -971,30 +971,20 @@ Graph::inOrOutEdges(shared> node) const { return inOrOutEdges; } +inline const auto isEdgeDirected = [](const auto &edge) { + return edge->isDirected().value_or(false); +}; + template bool Graph::isDirectedGraph() const { - auto edgeSet = getEdgeSet(); - for (const auto &edge : edgeSet) { - if (!(edge->isDirected().has_value() && edge->isDirected().value())) { - // Found Undirected Edge - return false; - } - } - // No Undirected Edge - return true; + const auto edgeSet = getEdgeSet(); + return std::all_of(edgeSet.cbegin(), edgeSet.cend(), isEdgeDirected); } template bool Graph::isUndirectedGraph() const { - auto edgeSet = Graph::getEdgeSet(); - for (const auto &edge : edgeSet) { - if ((edge->isDirected().has_value() && edge->isDirected().value())) { - // Found Directed Edge - return false; - } - } - // No Directed Edge - return true; + const auto edgeSet = Graph::getEdgeSet(); + return std::none_of(edgeSet.cbegin(), edgeSet.cend(), isEdgeDirected); } template diff --git a/include/CXXGraph/Graph/IO/OutputOperation_impl.hpp b/include/CXXGraph/Graph/IO/OutputOperation_impl.hpp index 6a0a796a1..a3b6a975a 100644 --- a/include/CXXGraph/Graph/IO/OutputOperation_impl.hpp +++ b/include/CXXGraph/Graph/IO/OutputOperation_impl.hpp @@ -158,7 +158,7 @@ int Graph::writeToMTXFile(const std::string &workingDir, // Check if the adjacency matrix is symmetric, i.e., if all the edges are // undirected bool symmetric = !std::any_of(edgeSet.begin(), edgeSet.end(), [](auto edge) { - return (edge->isDirected().has_value() && edge->isDirected().value()); + return edge->isDirected().value_or(false); }); // Write in the header whether the adj matrix is symmetric or not if (symmetric) { @@ -180,7 +180,7 @@ int Graph::writeToMTXFile(const std::string &workingDir, std::string line; line += edgeIt->getNodePair().first->getUserId() + delimitier; line += edgeIt->getNodePair().second->getUserId() + delimitier; - if (edgeIt->isWeighted().has_value() && edgeIt->isWeighted().value()) { + if (edgeIt->isWeighted().value_or(false)) { line += std::to_string(edgeIt->isWeighted().value()) + '\n'; } else { line += std::to_string(1.) + '\n'; @@ -261,10 +261,10 @@ int Graph::writeToBinary(const std::string &filepath, bool writeNodeFeatures, writeBinaryString(out, edge->getNodePair().second->getUserId()); uint8_t edgeFlags = 0; - if (edge->isDirected().has_value() && edge->isDirected().value()) { + if (edge->isDirected().value_or(false)) { edgeFlags |= 0x01; } - if (edge->isWeighted().has_value() && edge->isWeighted().value()) { + if (edge->isWeighted().value_or(false)) { edgeFlags |= 0x02; } out.write(reinterpret_cast(&edgeFlags), sizeof(edgeFlags)); @@ -313,7 +313,7 @@ int Graph::writeToDot(const std::string &workingDir, for (auto const &edgePtr : edgeSet) { std::string edgeLine = ""; - if (edgePtr->isDirected().has_value() && edgePtr->isDirected().value()) { + if (edgePtr->isDirected().value_or(false)) { auto directedPtr = std::static_pointer_cast>(edgePtr); edgeLine += '\t' + directedPtr->getFrom().getUserId() + ' '; @@ -324,7 +324,7 @@ int Graph::writeToDot(const std::string &workingDir, edgeLine += linkSymbol + ' '; edgeLine += edgePtr->getNodePair().second->getUserId(); } - if (edgePtr->isWeighted().has_value() && edgePtr->isWeighted().value()) { + if (edgePtr->isWeighted().value_or(false)) { // Weights in dot files must be integers edgeLine += " [weight=" + std::to_string(static_cast( @@ -349,10 +349,7 @@ void Graph::writeGraphToStream(std::ostream &oGraph, std::ostream &oNodeFeat, for (const auto &edge : edgeSet) { oGraph << edge->getUserId() << sep << edge->getNodePair().first->getUserId() << sep << edge->getNodePair().second->getUserId() << sep - << ((edge->isDirected().has_value() && edge->isDirected().value()) - ? 1 - : 0) - << std::endl; + << (edge->isDirected().value_or(false) ? 1 : 0) << std::endl; } if (writeNodeFeat) { @@ -364,16 +361,13 @@ void Graph::writeGraphToStream(std::ostream &oGraph, std::ostream &oNodeFeat, if (writeEdgeWeight) { for (const auto &edge : edgeSet) { - oEdgeWeight - << edge->getUserId() << sep - << (edge->isWeighted().has_value() && edge->isWeighted().value() - ? (std::dynamic_pointer_cast(edge)) - ->getWeight() - : 0.0) - << sep - << (edge->isWeighted().has_value() && edge->isWeighted().value() ? 1 - : 0) - << std::endl; + oEdgeWeight << edge->getUserId() << sep + << (edge->isWeighted().value_or(false) + ? (std::dynamic_pointer_cast(edge)) + ->getWeight() + : 0.0) + << sep << (edge->isWeighted().value_or(false) ? 1 : 0) + << std::endl; } } } @@ -395,27 +389,19 @@ std::ostream &operator<<(std::ostream &os, const Graph &graph) { if (!(*it)->isDirected().has_value() && !(*it)->isWeighted().has_value()) { // Edge Case os << **it << "\n"; - } else if (((*it)->isDirected().has_value() && - (*it)->isDirected().value()) && - ((*it)->isWeighted().has_value() && - (*it)->isWeighted().value())) { + } else if ((*it)->isDirected().value_or(false) && + (*it)->isWeighted().value_or(false)) { os << *std::static_pointer_cast>(*it) << "\n"; - } else if (((*it)->isDirected().has_value() && - (*it)->isDirected().value()) && - !((*it)->isWeighted().has_value() && - (*it)->isWeighted().value())) { + } else if ((*it)->isDirected().value_or(false) && + !((*it)->isWeighted().value_or(false))) { os << *std::static_pointer_cast>(*it) << "\n"; - } else if (!((*it)->isDirected().has_value() && - (*it)->isDirected().value()) && - ((*it)->isWeighted().has_value() && - (*it)->isWeighted().value())) { + } else if (!((*it)->isDirected().value_or(false)) && + (*it)->isWeighted().value_or(false)) { os << *std::static_pointer_cast>(*it) << "\n"; - } else if (!((*it)->isDirected().has_value() && - (*it)->isDirected().value()) && - !((*it)->isWeighted().has_value() && - (*it)->isWeighted().value())) { + } else if (!((*it)->isDirected().value_or(false)) && + !((*it)->isWeighted().value_or(false))) { os << *std::static_pointer_cast>(*it) << "\n"; } else { os << *it << "\n"; diff --git a/include/CXXGraph/Partitioning/CoordinatedPartitionState.hpp b/include/CXXGraph/Partitioning/CoordinatedPartitionState.hpp index 7068554b5..be2c638a8 100644 --- a/include/CXXGraph/Partitioning/CoordinatedPartitionState.hpp +++ b/include/CXXGraph/Partitioning/CoordinatedPartitionState.hpp @@ -145,7 +145,7 @@ void CoordinatedPartitionState::incrementMachineWeight( const int m, shared> e) { std::lock_guard lock(*machines_weight_edges_mutex); double edge_weight = CXXGraph::NEGLIGIBLE_WEIGHT; - if (e->isWeighted().has_value() && e->isWeighted().value()) { + if (e->isWeighted().value_or(false)) { edge_weight = (std::dynamic_pointer_cast(e))->getWeight(); } machines_weight_edges[m] = machines_weight_edges[m] + edge_weight; diff --git a/include/CXXGraph/Partitioning/Partition.hpp b/include/CXXGraph/Partitioning/Partition.hpp index 457eb456a..17a8ab030 100644 --- a/include/CXXGraph/Partitioning/Partition.hpp +++ b/include/CXXGraph/Partitioning/Partition.hpp @@ -332,27 +332,19 @@ std::ostream &operator<<(std::ostream &os, const Partition &partition) { if (!(*it)->isDirected().has_value() && !(*it)->isWeighted().has_value()) { // Edge Case os << **it << "\n"; - } else if (((*it)->isDirected().has_value() && - (*it)->isDirected().value()) && - ((*it)->isWeighted().has_value() && - (*it)->isWeighted().value())) { + } else if ((*it)->isDirected().value_or(false) && + ((*it)->isWeighted().value_or(false))) { os << *std::static_pointer_cast>(*it) << "\n"; - } else if (((*it)->isDirected().has_value() && - (*it)->isDirected().value()) && - !((*it)->isWeighted().has_value() && - (*it)->isWeighted().value())) { + } else if ((*it)->isDirected().value_or(false) && + !((*it)->isWeighted().value_or(false))) { os << *std::static_pointer_cast>(*it) << "\n"; - } else if (!((*it)->isDirected().has_value() && - (*it)->isDirected().value()) && - ((*it)->isWeighted().has_value() && - (*it)->isWeighted().value())) { + } else if (!((*it)->isDirected().value_or(false)) && + ((*it)->isWeighted().value_or(false))) { os << *std::static_pointer_cast>(*it) << "\n"; - } else if (!((*it)->isDirected().has_value() && - (*it)->isDirected().value()) && - !((*it)->isWeighted().has_value() && - (*it)->isWeighted().value())) { + } else if (!((*it)->isDirected().value_or(false)) && + !((*it)->isWeighted().value_or(false))) { os << *std::static_pointer_cast>(*it) << "\n"; } else { // Should never happens diff --git a/include/CXXGraph/Partitioning/Partitioner.hpp b/include/CXXGraph/Partitioning/Partitioner.hpp index e94adbde8..a66259fda 100644 --- a/include/CXXGraph/Partitioning/Partitioner.hpp +++ b/include/CXXGraph/Partitioning/Partitioner.hpp @@ -99,7 +99,7 @@ Partitioner::Partitioner(shared> dataset, Globals &G) double weight_sum = 0.0; for (const auto &edge_it : *(this->dataset)) { weight_sum += - (edge_it->isWeighted().has_value() && edge_it->isWeighted().value()) + (edge_it->isWeighted().value_or(false)) ? std::dynamic_pointer_cast(edge_it)->getWeight() : CXXGraph::NEGLIGIBLE_WEIGHT; } @@ -142,7 +142,7 @@ Partitioner::Partitioner(const Partitioner &other) { double weight_sum = 0.0; for (const auto &edge_it : *(this->dataset)) { weight_sum += - (edge_it->isWeighted().has_value() && edge_it->isWeighted().value()) + (edge_it->isWeighted().value_or(false)) ? std::dynamic_pointer_cast(edge_it)->getWeight() : CXXGraph::NEGLIGIBLE_WEIGHT; }