Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/CXXGraph/Graph/Algorithm/BellmanFord_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const BellmanFordResult Graph<T>::bellmanford(const Node<T> &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<const Weighted>(edge))->getWeight();
if (dist[elem.first] + edge_weight < dist[elem.second])
Expand Down
4 changes: 2 additions & 2 deletions include/CXXGraph/Graph/Algorithm/Boruvka_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const MstResult Graph<T>::boruvka() const {
const auto edgeSet = Graph<T>::getEdgeSet();
std::unordered_map<CXXGraph::id_t, double> 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<const Weighted>(edge))->getWeight();
else {
Expand Down Expand Up @@ -148,7 +148,7 @@ const MstResult Graph<T>::boruvka_deterministic() const {
const auto edgeSet = Graph<T>::getEdgeSet();
std::unordered_map<CXXGraph::id_t, double> 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<const Weighted>(edge))->getWeight();
else {
Expand Down
9 changes: 3 additions & 6 deletions include/CXXGraph/Graph/Algorithm/Dial_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,12 @@ const DialResult Graph<T>::dial(const Node<T> &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<const DirectedWeightedEdge<T>> dw_edge =
std::static_pointer_cast<const DirectedWeightedEdge<T>>(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<const UndirectedWeightedEdge<T>> udw_edge =
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
i.second);
Expand Down
36 changes: 12 additions & 24 deletions include/CXXGraph/Graph/Algorithm/Dijkstra_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T>& 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<const DirectedWeightedEdge<T>> dw_edge =
std::static_pointer_cast<const DirectedWeightedEdge<T>>(
elem.second);
Expand All @@ -107,8 +105,7 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T>& 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<const UndirectedWeightedEdge<T>> udw_edge =
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
elem.second);
Expand Down Expand Up @@ -235,10 +232,8 @@ const DijkstraResult Graph<T>::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<const DirectedWeightedEdge<T>> dw_edge =
std::static_pointer_cast<const DirectedWeightedEdge<T>>(
elem.second);
Expand All @@ -253,8 +248,7 @@ const DijkstraResult Graph<T>::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<const UndirectedWeightedEdge<T>> udw_edge =
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
elem.second);
Expand Down Expand Up @@ -391,10 +385,8 @@ const DijkstraResult Graph<T>::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<const DirectedWeightedEdge<T>> dw_edge =
std::static_pointer_cast<const DirectedWeightedEdge<T>>(
elem.second);
Expand All @@ -409,8 +401,7 @@ const DijkstraResult Graph<T>::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<const UndirectedWeightedEdge<T>> udw_edge =
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
elem.second);
Expand Down Expand Up @@ -538,10 +529,8 @@ const DijkstraResult Graph<T>::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<const DirectedWeightedEdge<T>> dw_edge =
std::static_pointer_cast<const DirectedWeightedEdge<T>>(
elem.second);
Expand All @@ -556,8 +545,7 @@ const DijkstraResult Graph<T>::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<const UndirectedWeightedEdge<T>> udw_edge =
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
elem.second);
Expand Down
2 changes: 1 addition & 1 deletion include/CXXGraph/Graph/Algorithm/FloydWarshall_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const FWResult Graph<T>::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<const Weighted>(edge))->getWeight();
auto key =
Expand Down
2 changes: 1 addition & 1 deletion include/CXXGraph/Graph/Algorithm/Kruskal_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const MstResult Graph<T>::kruskal() const {
std::greater<std::pair<double, shared<const Edge<T>>>>>
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<const Weighted>(edge))->getWeight();
sortedEdges.push(std::make_pair(weight, edge));
Expand Down
3 changes: 1 addition & 2 deletions include/CXXGraph/Graph/Algorithm/Prim_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ const MstResult Graph<T>::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<const UndirectedWeightedEdge<T>> udw_edge =
std::static_pointer_cast<const UndirectedWeightedEdge<T>>(
elem.second);
Expand Down
26 changes: 8 additions & 18 deletions include/CXXGraph/Graph/Graph_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,30 +971,20 @@ Graph<T>::inOrOutEdges(shared<const Node<T>> node) const {
return inOrOutEdges;
}

inline const auto isEdgeDirected = [](const auto &edge) {
return edge->isDirected().value_or(false);
};

template <typename T>
bool Graph<T>::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 <typename T>
bool Graph<T>::isUndirectedGraph() const {
auto edgeSet = Graph<T>::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<T>::getEdgeSet();
return std::none_of(edgeSet.cbegin(), edgeSet.cend(), isEdgeDirected);
}

template <typename T>
Expand Down
58 changes: 22 additions & 36 deletions include/CXXGraph/Graph/IO/OutputOperation_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int Graph<T>::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) {
Expand All @@ -180,7 +180,7 @@ int Graph<T>::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';
Expand Down Expand Up @@ -261,10 +261,10 @@ int Graph<T>::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<const char *>(&edgeFlags), sizeof(edgeFlags));
Expand Down Expand Up @@ -313,7 +313,7 @@ int Graph<T>::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<const DirectedEdge<T>>(edgePtr);
edgeLine += '\t' + directedPtr->getFrom().getUserId() + ' ';
Expand All @@ -324,7 +324,7 @@ int Graph<T>::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<int>(
Expand All @@ -349,10 +349,7 @@ void Graph<T>::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) {
Expand All @@ -364,16 +361,13 @@ void Graph<T>::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<const Weighted>(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<const Weighted>(edge))
->getWeight()
: 0.0)
<< sep << (edge->isWeighted().value_or(false) ? 1 : 0)
<< std::endl;
}
}
}
Expand All @@ -395,27 +389,19 @@ std::ostream &operator<<(std::ostream &os, const Graph<T> &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<const DirectedWeightedEdge<T>>(*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<const DirectedEdge<T>>(*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<const UndirectedWeightedEdge<T>>(*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<const UndirectedEdge<T>>(*it) << "\n";
} else {
os << *it << "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void CoordinatedPartitionState<T>::incrementMachineWeight(
const int m, shared<const Edge<T>> e) {
std::lock_guard<std::mutex> 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<const Weighted>(e))->getWeight();
}
machines_weight_edges[m] = machines_weight_edges[m] + edge_weight;
Expand Down
24 changes: 8 additions & 16 deletions include/CXXGraph/Partitioning/Partition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,27 +332,19 @@ std::ostream &operator<<(std::ostream &os, const Partition<T> &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<const DirectedWeightedEdge<T>>(*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<const DirectedEdge<T>>(*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<const UndirectedWeightedEdge<T>>(*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<const UndirectedEdge<T>>(*it) << "\n";
} else {
// Should never happens
Expand Down
4 changes: 2 additions & 2 deletions include/CXXGraph/Partitioning/Partitioner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Partitioner<T>::Partitioner(shared<const T_EdgeSet<T>> 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<const Weighted>(edge_it)->getWeight()
: CXXGraph::NEGLIGIBLE_WEIGHT;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ Partitioner<T>::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<const Weighted>(edge_it)->getWeight()
: CXXGraph::NEGLIGIBLE_WEIGHT;
}
Expand Down
Loading