From 45760d997a636a0ab340a17edbf7a2323b2a8d07 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Fri, 12 May 2023 15:19:47 +0200 Subject: [PATCH 01/28] Remove static_cast of 0 to template type T in best_first_search --- include/Graph/Graph.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index 16f04d7c0..f6496d0eb 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -1631,7 +1631,7 @@ BestFirstSearchResult Graph::best_first_search( std::vector> visited; visited.push_back(source); - pq.push(std::make_pair(static_cast(0), &source)); + pq.push(std::make_pair(0.0, &source)); while (!pq.empty()) { const Node *currentNode = pq.top().second; From 4c51acb38fd52f5799a2400805a8128a10ecfc55 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 24 May 2023 10:39:50 +0200 Subject: [PATCH 02/28] Change all the raw pointers in the library to smart pointers --- include/Edge/DirectedEdge.hpp | 25 +- include/Edge/DirectedWeightedEdge.hpp | 31 +- include/Edge/Edge.hpp | 43 +- include/Edge/UndirectedEdge.hpp | 25 +- include/Edge/UndirectedWeightedEdge.hpp | 31 +- include/Graph/Graph.hpp | 502 ++++++++++-------- .../CoordinatedPartitionState.hpp | 21 +- include/Partitioning/EBV.hpp | 34 +- .../Partitioning/EdgeBalancedVertexCut.hpp | 36 +- include/Partitioning/GreedyVertexCut.hpp | 52 +- include/Partitioning/HDRF.hpp | 38 +- include/Partitioning/Partition.hpp | 18 +- include/Partitioning/PartitionState.hpp | 18 +- include/Partitioning/PartitionStrategy.hpp | 13 +- include/Partitioning/Partitioner.hpp | 63 ++- include/Partitioning/PartitionerThread.hpp | 21 +- include/Partitioning/WeightBalancedLibra.hpp | 48 +- include/Utility/Typedef.hpp | 11 +- 18 files changed, 670 insertions(+), 360 deletions(-) diff --git a/include/Edge/DirectedEdge.hpp b/include/Edge/DirectedEdge.hpp index 74c16b351..266dfadb3 100755 --- a/include/Edge/DirectedEdge.hpp +++ b/include/Edge/DirectedEdge.hpp @@ -25,6 +25,15 @@ #include "Edge.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + template class UndirectedEdge; @@ -38,8 +47,12 @@ class DirectedEdge : public Edge { public: DirectedEdge(const unsigned long id, const Node &node1, const Node &node2); + DirectedEdge(const unsigned long id, shared> node1, + shared> node2); DirectedEdge(const unsigned long id, const std::pair *, const Node *> &nodepair); + DirectedEdge(const unsigned long id, + const std::pair>, shared>> &nodepair); DirectedEdge(const Edge &edge); virtual ~DirectedEdge() = default; const Node &getFrom() const; @@ -60,12 +73,22 @@ DirectedEdge::DirectedEdge(const unsigned long id, const Node &node1, const Node &node2) : Edge(id, node1, node2) {} +template +DirectedEdge::DirectedEdge(const unsigned long id, shared> node1, + shared> node2) : Edge(id, node1, node2) {} + template DirectedEdge::DirectedEdge( const unsigned long id, const std::pair *, const Node *> &nodepair) : Edge(id, nodepair) {} +template +DirectedEdge::DirectedEdge( + const unsigned long id, + const std::pair>, shared>> &nodepair) + : Edge(id, nodepair) {} + template DirectedEdge::DirectedEdge(const Edge &edge) : DirectedEdge(edge.getId(), *(edge.getNodePair().first), @@ -99,4 +122,4 @@ std::ostream &operator<<(std::ostream &os, const DirectedEdge &edge) { } } // namespace CXXGraph -#endif // __CXXGRAPH_DIRECTEDEDGE_H__ \ No newline at end of file +#endif // __CXXGRAPH_DIRECTEDEDGE_H__ diff --git a/include/Edge/DirectedWeightedEdge.hpp b/include/Edge/DirectedWeightedEdge.hpp index 03f326503..e8bdd90ca 100755 --- a/include/Edge/DirectedWeightedEdge.hpp +++ b/include/Edge/DirectedWeightedEdge.hpp @@ -25,6 +25,15 @@ #include "Weighted.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // Foward Declaration template class UndirectedWeightedEdge; @@ -41,10 +50,16 @@ class DirectedWeightedEdge : public DirectedEdge, public Weighted { public: DirectedWeightedEdge(const unsigned long id, const Node &node1, const Node &node2, const double weight); + DirectedWeightedEdge(const unsigned long id, shared> node1, + shared> node2, const double weight); DirectedWeightedEdge( const unsigned long id, const std::pair *, const Node *> &nodepair, const double weight); + DirectedWeightedEdge( + const unsigned long id, + const std::pair>, shared>> &nodepair, + const double weight); DirectedWeightedEdge(const DirectedEdge &edge, const double weight); DirectedWeightedEdge(const Edge &edge, const double weight); DirectedWeightedEdge(const DirectedEdge &edge); @@ -69,6 +84,13 @@ DirectedWeightedEdge::DirectedWeightedEdge(const unsigned long id, const double weight) : DirectedEdge(id, node1, node2), Weighted(weight) {} +template +DirectedWeightedEdge::DirectedWeightedEdge(const unsigned long id, + shared> node1, + shared> node2, + const double weight) + : DirectedEdge(id, node1, node2), Weighted(weight) {} + template DirectedWeightedEdge::DirectedWeightedEdge( const unsigned long id, @@ -76,6 +98,13 @@ DirectedWeightedEdge::DirectedWeightedEdge( const double weight) : DirectedEdge(id, nodepair), Weighted(weight) {} +template +DirectedWeightedEdge::DirectedWeightedEdge( + const unsigned long id, + const std::pair>, shared>> &nodepair, + const double weight) + : DirectedEdge(id, nodepair), Weighted(weight) {} + template DirectedWeightedEdge::DirectedWeightedEdge(const DirectedEdge &edge, const double weight) @@ -115,4 +144,4 @@ std::ostream &operator<<(std::ostream &os, } // namespace CXXGraph -#endif // __CXXGRAPH_DIRECTEDWEIGHTEDEDGE_H__ \ No newline at end of file +#endif // __CXXGRAPH_DIRECTEDWEIGHTEDEDGE_H__ diff --git a/include/Edge/Edge.hpp b/include/Edge/Edge.hpp index 73a073d0d..08457d32d 100755 --- a/include/Edge/Edge.hpp +++ b/include/Edge/Edge.hpp @@ -22,12 +22,22 @@ #pragma once +#include #include #include #include "Node/Node.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared = std::shared_ptr; + +using std::make_unique; +using std::make_shared; + template class Edge; // ostream operator @@ -37,15 +47,18 @@ template class Edge { private: unsigned long long id = 0; - std::pair *, const Node *> nodePair; + std::pair>, shared>> nodePair; public: Edge(const unsigned long long id, const Node &node1, const Node &node2); + Edge(const unsigned long long id, shared> node1, shared> node2); Edge(const unsigned long long id, const std::pair *, const Node *> &nodepair); + Edge(const unsigned long long id, + const std::pair>, shared>> &nodepair); virtual ~Edge() = default; const unsigned long long &getId() const; - const std::pair *, const Node *> &getNodePair() const; + const std::pair>, shared>> &getNodePair() const; virtual const std::optional isDirected() const; virtual const std::optional isWeighted() const; // operator @@ -60,14 +73,30 @@ class Edge { template Edge::Edge(const unsigned long long id, const Node &node1, - const Node &node2) - : nodePair(&node1, &node2) { + const Node &node2) { + this->nodePair.first = make_shared>(node1); + this->nodePair.second = make_shared>(node2); + this->id = id; +} + +template +Edge::Edge(const unsigned long long id, shared> node1, shared> node2) { + this->nodePair.first = node1; + this->nodePair.second = node2; + this->id = id; +} + +template +Edge::Edge(const unsigned long long id, + const std::pair *, const Node *> &nodepair) { + this->nodePair.first = make_shared>(*(nodepair.first)); + this->nodePair.second = make_shared>(*(nodepair.second)); this->id = id; } template Edge::Edge(const unsigned long long id, - const std::pair *, const Node *> &nodepair) + const std::pair>, shared>> &nodepair) : nodePair(nodepair) { this->id = id; } @@ -78,7 +107,7 @@ const unsigned long long &Edge::getId() const { } template -const std::pair *, const Node *> &Edge::getNodePair() +const std::pair>, shared>> &Edge::getNodePair() const { return nodePair; } @@ -112,4 +141,4 @@ std::ostream &operator<<(std::ostream &os, const Edge &edge) { } } // namespace CXXGraph -#endif // __CXXGRAPH_EDGE_H__ \ No newline at end of file +#endif // __CXXGRAPH_EDGE_H__ diff --git a/include/Edge/UndirectedEdge.hpp b/include/Edge/UndirectedEdge.hpp index 5b2026fa5..8bb0161f0 100755 --- a/include/Edge/UndirectedEdge.hpp +++ b/include/Edge/UndirectedEdge.hpp @@ -25,6 +25,14 @@ #include "Edge.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; template class UndirectedEdge; @@ -38,8 +46,12 @@ class UndirectedEdge : public Edge { public: UndirectedEdge(const unsigned long id, const Node &node1, const Node &node2); + UndirectedEdge(const unsigned long id, shared> node1, + shared> node2); UndirectedEdge(const unsigned long id, const std::pair *, const Node *> &nodepair); + UndirectedEdge(const unsigned long id, + const std::pair>, shared>> &nodepair); UndirectedEdge(const Edge &edge); virtual ~UndirectedEdge() = default; const Node &getNode1() const; @@ -60,12 +72,23 @@ UndirectedEdge::UndirectedEdge(const unsigned long id, const Node &node1, const Node &node2) : Edge(id, node1, node2) {} +template +UndirectedEdge::UndirectedEdge(const unsigned long id, shared> node1, + shared> node2) + : Edge(id, node1, node2) {} + template UndirectedEdge::UndirectedEdge( const unsigned long id, const std::pair *, const Node *> &nodepair) : Edge(id, nodepair) {} +template +UndirectedEdge::UndirectedEdge( + const unsigned long id, + const std::pair>, shared>> &nodepair) + : Edge(id, nodepair) {} + template UndirectedEdge::UndirectedEdge(const Edge &edge) : UndirectedEdge(edge.getId(), *(edge.getNodePair().first), @@ -99,4 +122,4 @@ std::ostream &operator<<(std::ostream &os, const UndirectedEdge &edge) { } } // namespace CXXGraph -#endif // __CXXGRAPH_UNDIRECTEDEDGE_H__ \ No newline at end of file +#endif // __CXXGRAPH_UNDIRECTEDEDGE_H__ diff --git a/include/Edge/UndirectedWeightedEdge.hpp b/include/Edge/UndirectedWeightedEdge.hpp index 3f6e79903..c92dfee26 100755 --- a/include/Edge/UndirectedWeightedEdge.hpp +++ b/include/Edge/UndirectedWeightedEdge.hpp @@ -26,6 +26,15 @@ #include "Weighted.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // Foward Declaration template class DirectedWeightedEdge; @@ -43,10 +52,16 @@ class UndirectedWeightedEdge : public UndirectedEdge, public Weighted { public: UndirectedWeightedEdge(const unsigned long id, const Node &node1, const Node &node2, const double weight); + UndirectedWeightedEdge(const unsigned long id, shared> node1, + shared> node2, const double weight); UndirectedWeightedEdge( const unsigned long id, const std::pair *, const Node *> &nodepair, const double weight); + UndirectedWeightedEdge( + const unsigned long id, + const std::pair>, shared>> &nodepair, + const double weight); UndirectedWeightedEdge(const UndirectedEdge &edge, const double weight); UndirectedWeightedEdge(const Edge &edge, const double weight); UndirectedWeightedEdge(const UndirectedEdge &edge); @@ -71,6 +86,13 @@ UndirectedWeightedEdge::UndirectedWeightedEdge(const unsigned long id, const double weight) : UndirectedEdge(id, node1, node2), Weighted(weight) {} +template +UndirectedWeightedEdge::UndirectedWeightedEdge(const unsigned long id, + shared> node1, + shared> node2, + const double weight) + : UndirectedEdge(id, node1, node2), Weighted(weight) {} + template UndirectedWeightedEdge::UndirectedWeightedEdge( const unsigned long id, @@ -78,6 +100,13 @@ UndirectedWeightedEdge::UndirectedWeightedEdge( const double weight) : UndirectedEdge(id, nodepair), Weighted(weight) {} +template +UndirectedWeightedEdge::UndirectedWeightedEdge( + const unsigned long id, + const std::pair>, shared>> &nodepair, + const double weight) + : UndirectedEdge(id, nodepair), Weighted(weight) {} + template UndirectedWeightedEdge::UndirectedWeightedEdge(const UndirectedEdge &edge, const double weight) @@ -117,4 +146,4 @@ std::ostream &operator<<(std::ostream &os, } // namespace CXXGraph -#endif // __CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_H__ \ No newline at end of file +#endif // __CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_H__ diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index dd36feb5c..adef7a629 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -69,8 +70,17 @@ #endif namespace CXXGraph { +// Smart pointers alias template -using T_EdgeSet = std::unordered_set *>; +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + +template +using T_EdgeSet = std::unordered_set>>; namespace Partitioning { template @@ -89,7 +99,7 @@ class Graph { T_EdgeSet edgeSet = {}; // Private non-const getter for the set of nodes - std::set *> nodeSet(); + std::set>> nodeSet(); std::optional> getExtenstionAndSeparator( InputOutputFormat format) const; @@ -141,12 +151,14 @@ class Graph { /** * \brief * Function add an Edge to the Graph Edge Set + * First check if a pointer to a node with the same userId has + * already been added, and if not add it * Note: No Thread Safe * * @param edge The Edge to insert * */ - virtual void addEdge(const Edge *edge); + virtual void addEdge(shared> edge); /** * \brief * Function remove an Edge from the Graph Edge Set @@ -164,7 +176,7 @@ class Graph { * @returns a list of Nodes of the graph * */ - virtual const std::set *> getNodeSet() const; + virtual const std::set>> getNodeSet() const; /** * \brief * Function that sets the data contained in a node @@ -191,7 +203,7 @@ class Graph { * @returns the Edge if exist * */ - virtual const std::optional *> getEdge( + virtual const std::optional>> getEdge( const unsigned long long edgeId) const; /** * @brief This function generate a list of adjacency matrix with every element @@ -210,7 +222,7 @@ class Graph { * Note: No Thread Safe */ virtual unsigned long long setFind( - std::unordered_map *, + shared>, const unsigned long long elem) const; /** * @brief This function modifies the original subset array @@ -221,7 +233,7 @@ class Graph { * NOTE: Original subset is no longer available after union. * Note: No Thread Safe */ - virtual void setUnion(std::unordered_map *, + virtual void setUnion(shared>, const unsigned long long set1, const unsigned long long elem2) const; /** @@ -381,7 +393,7 @@ class Graph { * * @return true if a cycle is detected, else false */ - virtual bool containsCycle(const T_EdgeSet *) const; + virtual bool containsCycle(shared>) const; /** * @brief * This function checks if the given Subset @@ -390,8 +402,8 @@ class Graph { * @return true if a cycle is detected, else false */ virtual bool containsCycle( - const T_EdgeSet *edgeSet, - std::unordered_map *) const; + shared> edgeSet, + shared>) const; /** * \brief @@ -618,16 +630,40 @@ void Graph::setEdgeSet(const T_EdgeSet &edgeSet) { } template -void Graph::addEdge(const Edge *edge) { - /* - if (std::find_if(edgeSet.begin(), edgeSet.end(), [edge](const Edge *edge_a) - { return (*edge == *edge_a); }) == - edgeSet.end()) - { - edgeSet.insert(edge); +void Graph::addEdge(shared> edge) { + // If the edge was already in the set, I don't add it again + if (std::find_if(edgeSet.begin(), edgeSet.end(), [edge](auto edge_inSet){ + return (edge_inSet->getNodePair().first->getUserId() == edge->getNodePair().first->getUserId() && + edge_inSet->getNodePair().second->getUserId() == edge->getNodePair().second->getUserId()); + }) != edgeSet.end()) { + return; + } + + // Check that the nodes pointed by the pointers in 'edge' have not already been + // added in other edges + // + // We don't pointers pointing to different memory regions which represent the + // same nodes + shared> node1; + auto edgeIt1 = std::find_if(edgeSet.begin(), edgeSet.end(), [edge](auto edge_inSet){ + return edge_inSet->getNodePair().first->getUserId() == edge->getNodePair().first->getUserId(); + }); + if (edgeIt1 == edgeSet.end()) { + node1 = edge->getNodePair().first; + } else { + node1 = (*edgeIt1)->getNodePair().first; + } + shared> node2; + auto edgeIt2 = std::find_if(edgeSet.begin(), edgeSet.end(), [edge](auto edge_inSet){ + return edge_inSet->getNodePair().second->getUserId() == edge->getNodePair().second->getUserId(); + }); + if (edgeIt2 == edgeSet.end()) { + node2 = edge->getNodePair().second; + } else { + node2 = (*edgeIt2)->getNodePair().second; } - */ - edgeSet.insert(edge); + shared> newEdge = make_shared>(edge->getId(), node1, node2); + edgeSet.insert(newEdge); } template @@ -643,8 +679,8 @@ void Graph::removeEdge(const unsigned long long edgeId) { } template -const std::set *> Graph::getNodeSet() const { - std::set *> nodeSet; +const std::set>> Graph::getNodeSet() const { + std::set>> nodeSet; for (const auto &edgeSetIt : edgeSet) { nodeSet.insert(edgeSetIt->getNodePair().first); nodeSet.insert(edgeSetIt->getNodePair().second); @@ -686,7 +722,7 @@ void Graph::setNodeData(std::map &dataMap) { } template -const std::optional *> Graph::getEdge( +const std::optional>> Graph::getEdge( const unsigned long long edgeId) const { for (const auto &it : edgeSet) { if (it->getId() == edgeId) { @@ -698,11 +734,11 @@ const std::optional *> Graph::getEdge( } template -std::set *> Graph::nodeSet() { - std::set *> nodeSet; +std::set>> Graph::nodeSet() { + std::set>> nodeSet; for (auto &edgeSetIt : edgeSet) { - nodeSet.insert(const_cast *>(edgeSetIt->getNodePair().first)); - nodeSet.insert(const_cast *>(edgeSetIt->getNodePair().second)); + nodeSet.insert(std::const_pointer_cast>(edgeSetIt->getNodePair().first)); + nodeSet.insert(std::const_pointer_cast>(edgeSetIt->getNodePair().second)); } return nodeSet; @@ -745,23 +781,23 @@ int Graph::writeToDot(const std::string &workingDir, } ofileGraph << headerLine; - for (auto const &edgeIt : edgeSet) { + for (auto const &edgePtr : edgeSet) { std::string edgeLine = ""; - if (edgeIt->isDirected().has_value() && edgeIt->isDirected().value()) { - auto directedIt = dynamic_cast *>(edgeIt); - edgeLine += '\t' + directedIt->getFrom().getUserId() + ' '; + if (edgePtr->isDirected().has_value() && edgePtr->isDirected().value()) { + auto directedPtr = std::static_pointer_cast>(edgePtr); + edgeLine += '\t' + directedPtr->getFrom().getUserId() + ' '; edgeLine += directedLinkSymbol + ' '; - edgeLine += directedIt->getTo().getUserId(); + edgeLine += directedPtr->getTo().getUserId(); } else { - edgeLine += '\t' + edgeIt->getNodePair().first->getUserId() + ' '; + edgeLine += '\t' + edgePtr->getNodePair().first->getUserId() + ' '; edgeLine += linkSymbol + ' '; - edgeLine += edgeIt->getNodePair().second->getUserId(); + edgeLine += edgePtr->getNodePair().second->getUserId(); } - if (edgeIt->isWeighted().has_value() && edgeIt->isWeighted().value()) { + if (edgePtr->isWeighted().has_value() && edgePtr->isWeighted().value()) { // Weights in dot files must be integers edgeLine += " [weight=" + std::to_string(static_cast( - dynamic_cast(edgeIt)->getWeight())) + + dynamic_cast(edgePtr.get())->getWeight())) + ']'; } edgeLine += ";\n"; @@ -812,7 +848,7 @@ void Graph::writeGraphToStream(std::ostream &oGraph, std::ostream &oNodeFeat, oEdgeWeight << edge->getId() << sep << (edge->isWeighted().has_value() && edge->isWeighted().value() - ? (dynamic_cast(edge))->getWeight() + ? (dynamic_cast(edge.get()))->getWeight() : 0.0) << sep << (edge->isWeighted().has_value() && edge->isWeighted().value() ? 1 @@ -955,17 +991,17 @@ void Graph::recreateGraph( std::unordered_map &edgeDirectedMap, std::unordered_map &nodeFeatMap, std::unordered_map &edgeWeightMap) { - std::unordered_map *> nodeMap; + std::unordered_map>> nodeMap; for (const auto &edgeIt : edgeMap) { - Node *node1 = nullptr; - Node *node2 = nullptr; + shared> node1(nullptr); + shared> node2(nullptr); if (nodeMap.find(edgeIt.second.first) == nodeMap.end()) { // Create new Node T feat; if (nodeFeatMap.find(edgeIt.second.first) != nodeFeatMap.end()) { feat = std::move(nodeFeatMap.at(edgeIt.second.first)); } - node1 = new Node(edgeIt.second.first, feat); + node1 = make_shared>(edgeIt.second.first, feat); nodeMap[edgeIt.second.first] = node1; } else { node1 = nodeMap.at(edgeIt.second.first); @@ -976,7 +1012,7 @@ void Graph::recreateGraph( if (nodeFeatMap.find(edgeIt.second.second) != nodeFeatMap.end()) { feat = std::move(nodeFeatMap.at(edgeIt.second.second)); } - node2 = new Node(edgeIt.second.second, feat); + node2 = make_shared>(edgeIt.second.second, feat); nodeMap[edgeIt.second.second] = node2; } else { node2 = nodeMap.at(edgeIt.second.second); @@ -985,21 +1021,21 @@ void Graph::recreateGraph( if (edgeWeightMap.find(edgeIt.first) != edgeWeightMap.end()) { if (edgeDirectedMap.find(edgeIt.first) != edgeDirectedMap.end() && edgeDirectedMap.at(edgeIt.first)) { - auto edge = new DirectedWeightedEdge(edgeIt.first, *node1, *node2, + auto edge = make_shared>(edgeIt.first, node1, node2, edgeWeightMap.at(edgeIt.first)); addEdge(edge); } else { - auto edge = new UndirectedWeightedEdge( - edgeIt.first, *node1, *node2, edgeWeightMap.at(edgeIt.first)); + auto edge = make_shared>( + edgeIt.first, node1, node2, edgeWeightMap.at(edgeIt.first)); addEdge(edge); } } else { if (edgeDirectedMap.find(edgeIt.first) != edgeDirectedMap.end() && edgeDirectedMap.at(edgeIt.first)) { - auto edge = new DirectedEdge(edgeIt.first, *node1, *node2); + auto edge = make_shared>(edgeIt.first, node1, node2); addEdge(edge); } else { - auto edge = new UndirectedEdge(edgeIt.first, *node1, *node2); + auto edge = make_shared>(edgeIt.first, node1, node2); addEdge(edge); } } @@ -1072,7 +1108,7 @@ int Graph::decompressFile(const std::string &inputFile, template unsigned long long Graph::setFind( - std::unordered_map *subsets, + shared> subsets, const unsigned long long nodeId) const { // find root and make root as parent of i // (path compression) @@ -1085,7 +1121,7 @@ unsigned long long Graph::setFind( } template -void Graph::setUnion(std::unordered_map *subsets, +void Graph::setUnion(shared> subsets, const unsigned long long elem1, const unsigned long long elem2) const { // if both sets have same parent @@ -1109,7 +1145,7 @@ std::shared_ptr>> Graph::eulerianPath() const { const auto adj = Graph::getAdjMatrix(); std::shared_ptr>> eulerPath = std::make_shared>>(); - std::vector *> currentPath; + std::vector>> currentPath; auto currentNode = *(nodeSet.begin()); currentPath.push_back(currentNode); while (currentPath.size() > 0) { @@ -1134,26 +1170,30 @@ std::shared_ptr>> Graph::eulerianPath() const { template const std::shared_ptr> Graph::getAdjMatrix() const { auto adj = std::make_shared>(); - auto addElementToAdjMatrix = [&adj](const Node *nodeFrom, - const Node *nodeTo, - const Edge *edge) { - std::pair *, const Edge *> elem = {nodeTo, edge}; + auto addElementToAdjMatrix = [&adj](shared> nodeFrom, + shared> nodeTo, + shared> edge) { + std::pair>, shared>> elem = {nodeTo, edge}; (*adj)[nodeFrom].push_back(std::move(elem)); }; for (const auto &edgeSetIt : edgeSet) { if (edgeSetIt->isDirected().has_value() && edgeSetIt->isDirected().value()) { - const DirectedEdge *d_edge = - dynamic_cast *>(edgeSetIt); - addElementToAdjMatrix(&(d_edge->getFrom()), &(d_edge->getTo()), d_edge); + shared> d_edge = + std::static_pointer_cast>(edgeSetIt); + addElementToAdjMatrix(d_edge->getNodePair().first, + d_edge->getNodePair().second, + d_edge); } else if (edgeSetIt->isDirected().has_value() && !edgeSetIt->isDirected().value()) { - const UndirectedEdge *ud_edge = - dynamic_cast *>(edgeSetIt); + shared> ud_edge = + std::static_pointer_cast>(edgeSetIt); ; - addElementToAdjMatrix(&(ud_edge->getNode1()), &(ud_edge->getNode2()), + addElementToAdjMatrix(ud_edge->getNodePair().first, + ud_edge->getNodePair().second, ud_edge); - addElementToAdjMatrix(&(ud_edge->getNode2()), &(ud_edge->getNode1()), + addElementToAdjMatrix(ud_edge->getNodePair().first, + ud_edge->getNodePair().second, ud_edge); } else { // is a simple edge we cannot create adj matrix return adj; @@ -1167,12 +1207,18 @@ const DijkstraResult Graph::dijkstra(const Node &source, const Node &target) const { DijkstraResult result; auto nodeSet = Graph::getNodeSet(); - if (std::find(nodeSet.begin(), nodeSet.end(), &source) == nodeSet.end()) { + auto source_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ + return node->getUserId() == source.getUserId(); + }); + if (source_node_it == nodeSet.end()) { // check if source node exist in the graph result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH; return result; } - if (std::find(nodeSet.begin(), nodeSet.end(), &target) == nodeSet.end()) { + auto target_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&target](auto node){ + return node->getUserId() == target.getUserId(); + }); + if (target_node_it == nodeSet.end()) { // check if target node exist in the graph result.errorMessage = ERR_TARGET_NODE_NOT_IN_GRAPH; return result; @@ -1182,7 +1228,7 @@ const DijkstraResult Graph::dijkstra(const Node &source, int n = adj->size(); // setting all the distances initially to INF_DOUBLE - std::unordered_map *, double> dist; + std::unordered_map>, double> dist; for (const auto &node : nodeSet) { dist[node] = INF_DOUBLE; @@ -1191,20 +1237,20 @@ const DijkstraResult Graph::dijkstra(const Node &source, // creating a min heap using priority queue // first element of pair contains the distance // second element of pair contains the vertex - std::priority_queue *>, - std::vector *>>, - std::greater *>>> + std::priority_queue>>, + std::vector>>>, + std::greater>>>> pq; // pushing the source vertex 's' with 0 distance in min heap - pq.push(std::make_pair(0.0, &source)); + pq.push(std::make_pair(0.0, *source_node_it)); // marking the distance of source as 0 - dist[&source] = 0; + dist[*source_node_it] = 0; while (!pq.empty()) { // second element of pair denotes the node / vertex - const Node *currentNode = pq.top().second; + shared> currentNode = pq.top().second; // first element of pair denotes the distance double currentDist = pq.top().first; @@ -1220,8 +1266,8 @@ const DijkstraResult Graph::dijkstra(const Node &source, elem.second->isWeighted().value()) { if (elem.second->isDirected().has_value() && elem.second->isDirected().value()) { - const DirectedWeightedEdge *dw_edge = - dynamic_cast *>(elem.second); + shared> dw_edge = + std::static_pointer_cast>(elem.second); if (dw_edge->getWeight() < 0) { result.errorMessage = ERR_NEGATIVE_WEIGHTED_EDGE; return result; @@ -1231,8 +1277,8 @@ const DijkstraResult Graph::dijkstra(const Node &source, } } else if (elem.second->isDirected().has_value() && !elem.second->isDirected().value()) { - const UndirectedWeightedEdge *udw_edge = - dynamic_cast *>(elem.second); + shared> udw_edge = + std::static_pointer_cast>(elem.second); if (udw_edge->getWeight() < 0) { result.errorMessage = ERR_NEGATIVE_WEIGHTED_EDGE; return result; @@ -1254,10 +1300,10 @@ const DijkstraResult Graph::dijkstra(const Node &source, } } } - if (dist[&target] != INF_DOUBLE) { + if (dist[*target_node_it] != INF_DOUBLE) { result.success = true; result.errorMessage = ""; - result.result = dist[&target]; + result.result = dist[*target_node_it]; return result; } result.errorMessage = ERR_TARGET_NODE_NOT_REACHABLE; @@ -1272,18 +1318,24 @@ const BellmanFordResult Graph::bellmanford(const Node &source, result.errorMessage = ""; result.result = INF_DOUBLE; auto nodeSet = Graph::getNodeSet(); - if (std::find(nodeSet.begin(), nodeSet.end(), &source) == nodeSet.end()) { + auto source_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ + return node->getUserId() == source.getUserId(); + }); + if (source_node_it == nodeSet.end()) { // check if source node exist in the graph result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH; return result; } - if (std::find(nodeSet.begin(), nodeSet.end(), &target) == nodeSet.end()) { + auto target_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&target](auto node){ + return node->getUserId() == target.getUserId(); + }); + if (target_node_it == nodeSet.end()) { // check if target node exist in the graph result.errorMessage = ERR_TARGET_NODE_NOT_IN_GRAPH; return result; } // setting all the distances initially to INF_DOUBLE - std::unordered_map *, double> dist, currentDist; + std::unordered_map>, double> dist, currentDist; // n denotes the number of vertices in graph auto n = nodeSet.size(); for (const auto &elem : nodeSet) { @@ -1292,7 +1344,7 @@ const BellmanFordResult Graph::bellmanford(const Node &source, } // marking the distance of source as 0 - dist[&source] = 0; + dist[*source_node_it] = 0; // set if node distances in two consecutive // iterations remain the same. auto earlyStopping = false; @@ -1304,7 +1356,7 @@ const BellmanFordResult Graph::bellmanford(const Node &source, for (const auto &edge : edgeSet) { auto elem = edge->getNodePair(); if (edge->isWeighted().has_value() && edge->isWeighted().value()) { - auto edge_weight = (dynamic_cast(edge))->getWeight(); + auto edge_weight = (dynamic_cast(edge.get()))->getWeight(); if (dist[elem.first] + edge_weight < dist[elem.second]) dist[elem.second] = dist[elem.first] + edge_weight; } else { @@ -1334,7 +1386,7 @@ const BellmanFordResult Graph::bellmanford(const Node &source, auto edgeSet = Graph::getEdgeSet(); for (const auto &edge : edgeSet) { auto elem = edge->getNodePair(); - auto edge_weight = (dynamic_cast(edge))->getWeight(); + auto edge_weight = (dynamic_cast(edge.get()))->getWeight(); if (dist[elem.first] + edge_weight < dist[elem.second]) { result.success = true; result.negativeCycle = true; @@ -1344,11 +1396,11 @@ const BellmanFordResult Graph::bellmanford(const Node &source, } } - if (dist[&target] != INF_DOUBLE) { + if (dist[*target_node_it] != INF_DOUBLE) { result.success = true; result.errorMessage = ""; result.negativeCycle = false; - result.result = dist[&target]; + result.result = dist[*target_node_it]; return result; } result.errorMessage = ERR_TARGET_NODE_NOT_REACHABLE; @@ -1383,7 +1435,7 @@ const FWResult Graph::floydWarshall() const { for (const auto &edge : edgeSet) { const auto &elem = edge->getNodePair(); if (edge->isWeighted().has_value() && edge->isWeighted().value()) { - auto edgeWeight = (dynamic_cast(edge))->getWeight(); + auto edgeWeight = (dynamic_cast(edge.get()))->getWeight(); auto key = std::make_pair(elem.first->getUserId(), elem.second->getUserId()); pairwise_dist[key] = edgeWeight; @@ -1449,7 +1501,7 @@ const MstResult Graph::prim() const { const auto adj = Graph::getAdjMatrix(); // setting all the distances initially to INF_DOUBLE - std::unordered_map *, double> dist; + std::unordered_map>, double> dist; for (const auto &elem : (*adj)) { dist[elem.first] = INF_DOUBLE; } @@ -1457,9 +1509,9 @@ const MstResult Graph::prim() const { // creating a min heap using priority queue // first element of pair contains the distance // second element of pair contains the vertex - std::priority_queue *>, - std::vector *>>, - std::greater *>>> + std::priority_queue>>, + std::vector>>>, + std::greater>>>> pq; // pushing the source vertex 's' with 0 distance in min heap @@ -1475,7 +1527,7 @@ const MstResult Graph::prim() const { std::unordered_map parentNode; while (!pq.empty()) { // second element of pair denotes the node / vertex - const Node *currentNode = pq.top().second; + shared> currentNode = pq.top().second; auto nodeId = currentNode->getId(); if (std::find(doneNode.begin(), doneNode.end(), nodeId) == doneNode.end()) { auto pair = std::make_pair(parentNode[nodeId], currentNode->getUserId()); @@ -1492,8 +1544,8 @@ const MstResult Graph::prim() const { // minimizing distances if (elem.second->isWeighted().has_value() && elem.second->isWeighted().value()) { - const UndirectedWeightedEdge *udw_edge = - dynamic_cast *>(elem.second); + shared> udw_edge = + std::static_pointer_cast>(elem.second); if ((udw_edge->getWeight() < dist[elem.first]) && (std::find(doneNode.begin(), doneNode.end(), elem.first->getId()) == doneNode.end())) { @@ -1527,7 +1579,7 @@ const MstResult Graph::boruvka() const { const auto n = nodeSet.size(); // Use std map for storing n subsets. - std::unordered_map subsets; + shared> subsets; // Initially there are n different trees. // Finally there will be one tree that will be MST @@ -1540,7 +1592,7 @@ const MstResult Graph::boruvka() const { for (const auto &edge : edgeSet) { if (edge->isWeighted().has_value() && edge->isWeighted().value()) edgeWeight[edge->getId()] = - (dynamic_cast(edge))->getWeight(); + (dynamic_cast(edge.get()))->getWeight(); else { // No Weighted Edge result.errorMessage = ERR_NO_WEIGHTED_EDGE; @@ -1550,7 +1602,7 @@ const MstResult Graph::boruvka() const { for (const auto &node : nodeSet) { Subset set{node->getId(), 0}; - subsets[node->getId()] = set; + (*subsets)[node->getId()] = set; } result.mstCost = 0; // we will store the cost here @@ -1567,8 +1619,8 @@ const MstResult Graph::boruvka() const { auto elem = edge->getNodePair(); auto edgeId = edge->getId(); // Find sets of two corners of current edge - auto set1 = Graph::setFind(&subsets, elem.first->getId()); - auto set2 = Graph::setFind(&subsets, elem.second->getId()); + auto set1 = Graph::setFind(subsets, elem.first->getId()); + auto set2 = Graph::setFind(subsets, elem.second->getId()); // If two corners of current edge belong to // same set, ignore current edge @@ -1591,15 +1643,15 @@ const MstResult Graph::boruvka() const { // Check if cheapest for current set exists if (edgeId != INT_MAX) { auto cheapestNode = Graph::getEdge(edgeId).value()->getNodePair(); - auto set1 = Graph::setFind(&subsets, cheapestNode.first->getId()); - auto set2 = Graph::setFind(&subsets, cheapestNode.second->getId()); + auto set1 = Graph::setFind(subsets, cheapestNode.first->getId()); + auto set2 = Graph::setFind(subsets, cheapestNode.second->getId()); if (set1 == set2) continue; result.mstCost += edgeWeight[edgeId]; auto newEdgeMST = std::make_pair(cheapestNode.first->getUserId(), cheapestNode.second->getUserId()); result.mst.push_back(newEdgeMST); // take union of set1 and set2 and decrease number of trees - Graph::setUnion(&subsets, set1, set2); + Graph::setUnion(subsets, set1, set2); numTrees--; } } @@ -1624,13 +1676,13 @@ const MstResult Graph::kruskal() const { // check if all edges are weighted and store the weights // in a map whose keys are the edge ids and values are the edge weights auto edgeSet = Graph::getEdgeSet(); - std::priority_queue *>, - std::vector *>>, - std::greater *>>> + std::priority_queue>>, + std::vector>>>, + std::greater>>>> sortedEdges; for (const auto &edge : edgeSet) { if (edge->isWeighted().has_value() && edge->isWeighted().value()) { - auto weight = (dynamic_cast(edge))->getWeight(); + auto weight = (dynamic_cast(edge.get()))->getWeight(); sortedEdges.push(std::make_pair(weight, edge)); } else { // No Weighted Edge @@ -1639,25 +1691,25 @@ const MstResult Graph::kruskal() const { } } - std::unordered_map subset; + shared> subset; for (const auto &node : nodeSet) { Subset set{node->getId(), 0}; - subset[node->getId()] = set; + (*subset)[node->getId()] = set; } result.mstCost = 0; while ((!sortedEdges.empty()) && (result.mst.size() < n)) { auto [edgeWeight, cheapestEdge] = sortedEdges.top(); sortedEdges.pop(); auto &[first, second] = cheapestEdge->getNodePair(); - auto set1 = Graph::setFind(&subset, first->getId()); - auto set2 = Graph::setFind(&subset, second->getId()); + auto set1 = Graph::setFind(subset, first->getId()); + auto set2 = Graph::setFind(subset, second->getId()); if (set1 != set2) { result.mst.push_back( std::make_pair(first->getUserId(), second->getUserId())); result.mstCost += edgeWeight; } - Graph::setUnion(&subset, set1, set2); + Graph::setUnion(subset, set1, set2); } result.success = true; return result; @@ -1668,14 +1720,20 @@ BestFirstSearchResult Graph::best_first_search( const Node &source, const Node &target) const { BestFirstSearchResult result; auto &nodeSet = Graph::getNodeSet(); - using pq_type = std::pair *>; + using pq_type = std::pair>>; - if (std::find(nodeSet.begin(), nodeSet.end(), &source) == nodeSet.end()) { + auto source_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ + return node->getUserId() == source.getUserId(); + }); + if (source_node_it == nodeSet.end()) { result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH; return result; } - if (std::find(nodeSet.begin(), nodeSet.end(), &target) == nodeSet.end()) { + auto target_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&target](auto node){ + return node->getUserId() == target.getUserId(); + }); + if (target_node_it == nodeSet.end()) { result.errorMessage = ERR_TARGET_NODE_NOT_IN_GRAPH; return result; } @@ -1685,10 +1743,10 @@ BestFirstSearchResult Graph::best_first_search( std::vector> visited; visited.push_back(source); - pq.push(std::make_pair(0.0, &source)); + pq.push(std::make_pair(0.0, *source_node_it)); while (!pq.empty()) { - const Node *currentNode = pq.top().second; + shared> currentNode = pq.top().second; pq.pop(); result.nodesInBestSearchOrder.push_back(*currentNode); @@ -1699,16 +1757,16 @@ BestFirstSearchResult Graph::best_first_search( for (const auto &elem : adj->at(currentNode)) { if (elem.second->isWeighted().has_value()) { if (elem.second->isDirected().has_value()) { - const DirectedWeightedEdge *dw_edge = - static_cast *>(elem.second); + shared> dw_edge = + std::static_pointer_cast>(elem.second); if (std::find(visited.begin(), visited.end(), *(elem.first)) == visited.end()) { visited.push_back(*(elem.first)); pq.push(std::make_pair(dw_edge->getWeight(), elem.first)); } } else { - const UndirectedWeightedEdge *dw_edge = - static_cast *>(elem.second); + shared> dw_edge = + std::static_pointer_cast>(elem.second); if (std::find(visited.begin(), visited.end(), *(elem.first)) == visited.end()) { visited.push_back(*(elem.first)); @@ -1735,18 +1793,21 @@ const std::vector> Graph::breadth_first_search( std::vector> visited; auto &nodeSet = Graph::getNodeSet(); // check is exist node in the graph - if (std::find(nodeSet.begin(), nodeSet.end(), &start) == nodeSet.end()) { + auto start_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&start](auto node){ + return node->getUserId() == start.getUserId(); + }); + if (start_node_it == nodeSet.end()) { return visited; } const auto adj = Graph::getAdjMatrix(); // queue that stores vertices that need to be further explored - std::queue *> tracker; + std::queue>> tracker; // mark the starting node as visited visited.push_back(start); - tracker.push(&start); + tracker.push(*start_node_it); while (!tracker.empty()) { - const Node *node = tracker.front(); + shared> node = tracker.front(); tracker.pop(); if (adj->find(node) != adj->end()) { for (const auto &elem : adj->at(node)) { @@ -1769,11 +1830,14 @@ const std::vector> Graph::concurrency_breadth_first_search( std::vector> bfs_result; // check is exist node in the graph auto &nodeSet = Graph::getNodeSet(); - if (std::find(nodeSet.begin(), nodeSet.end(), &start) == nodeSet.end()) { + auto start_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&start](auto node){ + return node->getUserId() == start.getUserId(); + }); + if (start_node_it == nodeSet.end()) { return bfs_result; } - std::unordered_map *, int> node_to_index; + std::unordered_map>, int> node_to_index; for (const auto &node : nodeSet) { node_to_index[node] = node_to_index.size(); } @@ -1788,13 +1852,13 @@ const std::vector> Graph::concurrency_breadth_first_search( const auto &adj = Graph::getAdjMatrix(); // vector that stores vertices to be visit - std::vector *> level_tracker, next_level_tracker; + std::vector>> level_tracker, next_level_tracker; level_tracker.reserve(static_cast(1.0 * nodeSet.size())); next_level_tracker.reserve(static_cast(1.0 * nodeSet.size())); // mark the starting node as visited - visited[node_to_index[&start]] = 1; - level_tracker.push_back(&start); + visited[node_to_index[*start_node_it]] = 1; + level_tracker.push_back(*start_node_it); // a worker is assigned a small part of tasks for each time // assignments of tasks in current level and updates of tasks in next @@ -1823,7 +1887,7 @@ const std::vector> Graph::concurrency_breadth_first_search( }; auto submit_result = [&next_level_tracker, &next_tracker_mutex]( - std::vector *> &submission) -> void { + std::vector>> &submission) -> void { std::lock_guard tracker_guard(next_tracker_mutex); next_level_tracker.insert(std::end(next_level_tracker), std::begin(submission), std::end(submission)); @@ -1838,8 +1902,8 @@ const std::vector> Graph::concurrency_breadth_first_search( // algorithm is not done while (!level_tracker.empty()) { // search for nodes in a level is not done - std::vector *> local_tracker; - while (1) { + std::vector>> local_tracker; + while (true) { auto [start_index, end_index] = extract_tasks(); if (start_index >= end_index) { break; @@ -1922,27 +1986,30 @@ const std::vector> Graph::depth_first_search( std::vector> visited; auto nodeSet = Graph::getNodeSet(); // check is exist node in the graph - if (std::find(nodeSet.begin(), nodeSet.end(), &start) == nodeSet.end()) { + auto start_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&start](auto node){ + return node->getUserId() == start.getUserId(); + }); + if (start_node_it == nodeSet.end()) { return visited; } const auto adj = Graph::getAdjMatrix(); - std::function>, const Node &, + std::function>, shared>, std::vector> &)> explore; explore = [&explore](const std::shared_ptr> adj, - const Node &node, + shared> node, std::vector> &visited) -> void { - visited.push_back(node); - if (adj->find(&node) != adj->end()) { - for (const auto &x : adj->at(&node)) { + visited.push_back(*node); + if (adj->find(node) != adj->end()) { + for (const auto &x : adj->at(node)) { if (std::find(visited.begin(), visited.end(), *(x.first)) == visited.end()) { - explore(adj, *(x.first), visited); + explore(adj, x.first, visited); } } } }; - explore(adj, start, visited); + explore(adj, *start_node_it, visited); return visited; } @@ -1978,13 +2045,13 @@ bool Graph::isCyclicDirectedGraphDFS() const { // Check for cycle. std::function>, std::unordered_map &, - const Node *)> + shared>)> isCyclicDFSHelper; isCyclicDFSHelper = [this, &isCyclicDFSHelper]( const std::shared_ptr> adjMatrix, std::unordered_map &states, - const Node *node) { + shared> node) { // Add node "in_stack" state. states[node->getId()] = in_stack; @@ -2028,8 +2095,8 @@ bool Graph::isCyclicDirectedGraphDFS() const { } template -bool Graph::containsCycle(const T_EdgeSet *edgeSet) const { - std::unordered_map subset; +bool Graph::containsCycle(shared> edgeSet) const { + shared> subset; // initialize the subset parent and rank values for (const auto &edge : *edgeSet) { auto &[first, second] = edge->getNodePair(); @@ -2041,22 +2108,22 @@ bool Graph::containsCycle(const T_EdgeSet *edgeSet) const { return (id == (it.second).parent); }; - if (std::find_if(subset.begin(), subset.end(), nodeExists) == - subset.end()) { + if (std::find_if((*subset).begin(), (*subset).end(), nodeExists) == + (*subset).end()) { Subset set; set.parent = id; set.rank = 0; - subset[id] = set; + (*subset)[id] = set; } } } - return Graph::containsCycle(edgeSet, &subset); + return Graph::containsCycle(edgeSet, subset); } template bool Graph::containsCycle( - const T_EdgeSet *edgeSet, - std::unordered_map *subset) const { + shared> edgeSet, + shared> subset) const { for (const auto &edge : *edgeSet) { auto &[first, second] = edge->getNodePair(); auto set1 = Graph::setFind(subset, first->getId()); @@ -2087,12 +2154,12 @@ bool Graph::isCyclicDirectedGraphBFS() const { } } - std::queue *> can_be_solved; + std::queue>> can_be_solved; for (const auto &node : nodeSet) { // If a node doesn't have any input edges, then that node will // definately not result in a cycle and can be visited safely. if (!indegree[node->getId()]) { - can_be_solved.emplace(&(*node)); + can_be_solved.emplace(node); } } @@ -2163,14 +2230,14 @@ bool Graph::isConnectedGraph() const { for (const auto &node : nodeSet) { visited[node->getId()] = false; } - std::function *)> dfs_helper = - [this, &adjMatrix, &visited, &dfs_helper](const Node *source) { + std::function>)> dfs_helper = + [this, &adjMatrix, &visited, &dfs_helper](shared> source) { // mark the vertex visited visited[source->getId()] = true; // travel the neighbors for (int i = 0; i < (*adjMatrix)[source].size(); i++) { - const Node *neighbor = (*adjMatrix)[source].at(i).first; + shared> neighbor = (*adjMatrix)[source].at(i).first; if (visited[neighbor->getId()] == false) { // make recursive call from neighbor dfs_helper(neighbor); @@ -2203,14 +2270,14 @@ bool Graph::isStronglyConnectedGraph() const { for (const auto &node : nodeSet) { visited[node->getId()] = false; } - std::function *)> dfs_helper = - [this, &adjMatrix, &visited, &dfs_helper](const Node *source) { + std::function>)> dfs_helper = + [this, &adjMatrix, &visited, &dfs_helper](shared> source) { // mark the vertex visited visited[source->getId()] = true; // travel the neighbors for (int i = 0; i < (*adjMatrix)[source].size(); i++) { - const Node *neighbor = (*adjMatrix)[source].at(i).first; + shared> neighbor = (*adjMatrix)[source].at(i).first; if (visited[neighbor->getId()] == false) { // make recursive call from neighbor dfs_helper(neighbor); @@ -2245,11 +2312,11 @@ TopoSortResult Graph::topologicalSort() const { } else { const auto &adjMatrix = getAdjMatrix(); const auto &nodeSet = getNodeSet(); - std::unordered_map *, bool> visited; + std::unordered_map>, bool> visited; - std::function *)> postorder_helper = + std::function>)> postorder_helper = [&postorder_helper, &adjMatrix, &visited, - &result](const Node *curNode) { + &result](shared> curNode) { visited[curNode] = true; if (adjMatrix->find(curNode) != adjMatrix->end()) { @@ -2303,7 +2370,7 @@ TopoSortResult Graph::kahn() const { } } - std::queue *> topologicalOrder; + std::queue>> topologicalOrder; for (const auto &node : nodeSet) { if (!indegree[node->getId()]) { @@ -2313,7 +2380,7 @@ TopoSortResult Graph::kahn() const { size_t visited = 0; while (!topologicalOrder.empty()) { - const Node *currentNode = topologicalOrder.front(); + shared> currentNode = topologicalOrder.front(); topologicalOrder.pop(); result.nodesInTopoOrder.push_back(*currentNode); @@ -2355,15 +2422,15 @@ SCCResult Graph::kosaraju() const { visited[node->getId()] = false; } - std::stack *> st; - std::function *)> dfs_helper = - [this, &adjMatrix, &visited, &dfs_helper, &st](const Node *source) { + std::stack>> st; + std::function>)> dfs_helper = + [this, &adjMatrix, &visited, &dfs_helper, &st](shared> source) { // mark the vertex visited visited[source->getId()] = true; // travel the neighbors for (int i = 0; i < (*adjMatrix)[source].size(); i++) { - const Node *neighbor = (*adjMatrix)[source].at(i).first; + shared> neighbor = (*adjMatrix)[source].at(i).first; if (visited[neighbor->getId()] == false) { // make recursive call from neighbor dfs_helper(neighbor); @@ -2381,23 +2448,25 @@ SCCResult Graph::kosaraju() const { // construct the transpose of the given graph AdjacencyMatrix rev; - auto addElementToAdjMatrix = [&rev](const Node *nodeFrom, - const Node *nodeTo, - const Edge *edge) { - std::pair *, const Edge *> elem = {nodeTo, edge}; + auto addElementToAdjMatrix = [&rev](shared> nodeFrom, + shared> nodeTo, + shared> edge) { + std::pair> , shared>> elem = {nodeTo, edge}; rev[nodeFrom].push_back(std::move(elem)); }; for (const auto &edgeSetIt : edgeSet) { - const DirectedEdge *d_edge = - dynamic_cast *>(edgeSetIt); + shared> d_edge = + std::static_pointer_cast>(edgeSetIt); // Add the reverse edge to the reverse adjacency matrix - addElementToAdjMatrix(&(d_edge->getTo()), &(d_edge->getFrom()), d_edge); + addElementToAdjMatrix(d_edge->getNodePair().first, + d_edge->getNodePair().second, + d_edge); } visited.clear(); - std::function *, std::vector> &)> dfs_helper1 = - [this, &rev, &visited, &dfs_helper1](const Node *source, + std::function>, std::vector> &)> dfs_helper1 = + [this, &rev, &visited, &dfs_helper1](shared> source, std::vector> &comp) { // mark the vertex visited visited[source->getId()] = true; @@ -2407,7 +2476,7 @@ SCCResult Graph::kosaraju() const { // travel the neighbors for (int i = 0; i < rev[source].size(); i++) { - const Node *neighbor = rev[source].at(i).first; + shared> neighbor = rev[source].at(i).first; if (visited[neighbor->getId()] == false) { // make recursive call from neighbor dfs_helper1(neighbor, comp); @@ -2438,7 +2507,10 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { const auto adj = getAdjMatrix(); auto nodeSet = getNodeSet(); - if (std::find(nodeSet.begin(), nodeSet.end(), &source) == nodeSet.end()) { + auto source_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ + return node->getUserId() == source.getUserId(); + }); + if (source_node_it == nodeSet.end()) { // check if source node exist in the graph result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH; return result; @@ -2449,22 +2521,22 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { dist[i].first = distance of ith vertex from src vertex dits[i].second = vertex i in bucket number */ unsigned int V = nodeSet.size(); - std::unordered_map *, std::pair *>> dist; + std::unordered_map>, std::pair>>> dist; // Initialize all distances as infinite (INF) for (const auto &node : nodeSet) { - dist[&(*node)].first = std::numeric_limits::max(); + dist[node].first = std::numeric_limits::max(); } // Create buckets B[]. // B[i] keep vertex of distance label i - std::vector *>> B((maxWeight * V + 1)); + std::vector>>> B((maxWeight * V + 1)); - B[0].push_back(&source); - dist[&source].first = 0; + B[0].push_back(*source_node_it); + dist[*source_node_it].first = 0; int idx = 0; - while (1) { + while (true) { // Go sequentially through buckets till one non-empty // bucket is found while (B[idx].size() == 0 && idx < maxWeight * V) { @@ -2489,13 +2561,13 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { i.second->isWeighted().value()) { if (i.second->isDirected().has_value() && i.second->isDirected().value()) { - const DirectedWeightedEdge *dw_edge = - dynamic_cast *>(i.second); + 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()) { - const UndirectedWeightedEdge *udw_edge = - dynamic_cast *>(i.second); + shared> udw_edge = + std::static_pointer_cast>(i.second); weight = (int)udw_edge->getWeight(); } else { // ERROR it shouldn't never returned ( does not exist a Node @@ -2510,12 +2582,12 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { } auto u_i = std::find_if( dist.begin(), dist.end(), - [u](std::pair *, std::pair *>> const + [u](std::pair>, std::pair>>> const &it) { return (*u == *(it.first)); }); auto v_i = std::find_if( dist.begin(), dist.end(), - [v](std::pair *, std::pair *>> const + [v](std::pair>, std::pair>>> const &it) { return (*v == *(it.first)); }); long du = u_i->second.first; long dv = v_i->second.first; @@ -2557,16 +2629,16 @@ double Graph::fordFulkersonMaxFlow(const Node &source, return -1; } double maxFlow = 0; - std::unordered_map *, const Node *> parent; - std::map *, std::map *, double>> weightMap; + std::unordered_map>, shared>> parent; + std::map>, std::map>, double>> weightMap; // build weight map auto edgeSet = this->getEdgeSet(); for (const auto &edge : edgeSet) { // The Edge are all Directed at this point because is checked at the // start if (edge->isWeighted().value_or(false)) { - const DirectedWeightedEdge *dw_edge = - dynamic_cast *>(edge); + shared> dw_edge = + std::static_pointer_cast>(edge); weightMap[edge->getNodePair().first][edge->getNodePair().second] = dw_edge->getWeight(); } else { @@ -2575,12 +2647,21 @@ double Graph::fordFulkersonMaxFlow(const Node &source, } } - auto bfs_helper = [this, &source, &target, &parent, &weightMap]() -> bool { - std::unordered_map *, bool> visited; - std::queue *> queue; - queue.push(&source); - visited[&source] = true; - parent[&source] = nullptr; + // Constuct iterators for source and target nodes in nodeSet + auto nodeSet = getNodeSet(); + auto source_node_ptr = *std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ + return node->getUserId() == source.getUserId(); + }); + auto target_node_ptr = *std::find_if(nodeSet.begin(), nodeSet.end(), [&target](auto node){ + return node->getUserId() == target.getUserId(); + }); + + auto bfs_helper = [this, &source_node_ptr, &target_node_ptr, &parent, &weightMap]() -> bool { + std::unordered_map>, bool> visited; + std::queue>> queue; + queue.push(source_node_ptr); + visited[source_node_ptr] = true; + parent[source_node_ptr] = nullptr; while (!queue.empty()) { auto u = queue.front(); queue.pop(); @@ -2593,16 +2674,16 @@ double Graph::fordFulkersonMaxFlow(const Node &source, } } - return (visited[&target]); + return (visited[target_node_ptr]); }; // Updating the residual values of edges while (bfs_helper()) { double pathFlow = std::numeric_limits::max(); - for (auto v = ⌖ v != &source; v = parent[v]) { + for (auto v = target_node_ptr; v != source_node_ptr; v = parent[v]) { auto u = parent[v]; pathFlow = std::min(pathFlow, weightMap[u][v]); } - for (auto v = ⌖ v != &source; v = parent[v]) { + for (auto v = target_node_ptr; v != source_node_ptr; v = parent[v]) { auto u = parent[v]; weightMap[u][v] -= pathFlow; weightMap[v][u] += pathFlow; @@ -2620,11 +2701,14 @@ const std::vector> Graph::graph_slicing(const Node &start) const { auto nodeSet = Graph::getNodeSet(); // check if start node in the graph - if (std::find(nodeSet.begin(), nodeSet.end(), &start) == nodeSet.end()) { + auto start_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&start](auto node){ + return node->getUserId() == start.getUserId(); + }); + if (start_node_it == nodeSet.end()) { return result; } std::vector> C = Graph::depth_first_search(start); - std::deque *> C1; // complement of C i.e. nodeSet - C + std::deque>> C1; // complement of C i.e. nodeSet - C for (auto const &node : nodeSet) { // from the set of all nodes, remove nodes that exist in C if (std::find_if(C.begin(), C.end(), [node](const Node nodeC) { @@ -3047,10 +3131,10 @@ PartitionMap Graph::partitionGraph( PartitionMap partitionMap; Partitioning::Globals globals(numberOfPartitions, algorithm, param1, param2, param3, numberOfThreads); - const T_EdgeSet &edgeSet = getEdgeSet(); - globals.edgeCardinality = edgeSet.size(); + auto edgeSet_ptr = make_shared>(getEdgeSet()); + globals.edgeCardinality = edgeSet_ptr->size(); globals.vertexCardinality = this->getNodeSet().size(); - Partitioning::Partitioner partitioner(&edgeSet, globals); + Partitioning::Partitioner partitioner(edgeSet_ptr, globals); Partitioning::CoordinatedPartitionState partitionState = partitioner.performCoordinatedPartition(); partitionMap = partitionState.getPartitionMap(); @@ -3070,18 +3154,18 @@ std::ostream &operator<<(std::ostream &os, const Graph &graph) { (*it)->isDirected().value()) && ((*it)->isWeighted().has_value() && (*it)->isWeighted().value())) { - os << dynamic_cast &>(*it) << "\n"; + os << std::static_pointer_cast>(*it) << "\n"; } else if (((*it)->isDirected().has_value() && (*it)->isDirected().value()) && !((*it)->isWeighted().has_value() && (*it)->isWeighted().value())) { - os << dynamic_cast &>(*it) << "\n"; + os << std::static_pointer_cast>(*it) << "\n"; } else if (!(it->isDirected().has_value() && it->isDirected().value()) && (it->isWeighted().has_value() && it->isWeighted().value())) { - os << dynamic_cast &>(*it) << "\n"; + os << std::static_pointer_cast>(*it) << "\n"; } else if (!(it->isDirected().has_value() && it->isDirected().value()) && !(it->isWeighted().has_value() && it->isWeighted().value())) { - os << dynamic_cast &>(*it) << "\n"; + os << std::static_pointer_cast>(*it) << "\n"; } else { os << *it << "\n"; } diff --git a/include/Partitioning/CoordinatedPartitionState.hpp b/include/Partitioning/CoordinatedPartitionState.hpp index bf2f5b9db..d37385c18 100755 --- a/include/Partitioning/CoordinatedPartitionState.hpp +++ b/include/Partitioning/CoordinatedPartitionState.hpp @@ -34,6 +34,15 @@ #include "Record.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + namespace Partitioning { template class CoordinatedPartitionState : public PartitionState { @@ -58,8 +67,8 @@ class CoordinatedPartitionState : public PartitionState { int getMachineLoad(const int m) const override; int getMachineWeight(const int m) const override; int getMachineLoadVertices(const int m) const override; - void incrementMachineLoad(const int m, const Edge *e) override; - void incrementMachineWeight(const int m, const Edge *e) override; + void incrementMachineLoad(const int m, shared> e) override; + void incrementMachineWeight(const int m, shared> e) override; int getMinLoad() const override; int getMaxLoad() const override; int getMachineWithMinWeight() const override; @@ -126,7 +135,7 @@ int CoordinatedPartitionState::getMachineLoadVertices(const int m) const { } template void CoordinatedPartitionState::incrementMachineLoad(const int m, - const Edge *e) { + shared> e) { std::lock_guard lock(*machines_load_edges_mutex); machines_load_edges[m] = machines_load_edges[m] + 1; int new_value = machines_load_edges.at(m); @@ -137,11 +146,11 @@ void CoordinatedPartitionState::incrementMachineLoad(const int m, } template void CoordinatedPartitionState::incrementMachineWeight(const int m, - const Edge *e) { + shared> e) { std::lock_guard lock(*machines_weight_edges_mutex); double edge_weight = CXXGraph::NEGLIGIBLE_WEIGHT; if (e->isWeighted().has_value() && e->isWeighted().value()) { - edge_weight = (dynamic_cast(e))->getWeight(); + edge_weight = (dynamic_cast(e.get()))->getWeight(); } machines_weight_edges[m] = machines_weight_edges[m] + edge_weight; // double new_value = machines_weight_edges[m]; @@ -262,4 +271,4 @@ const PartitionMap &CoordinatedPartitionState::getPartitionMap() const { } // namespace Partitioning } // namespace CXXGraph -#endif // __CXXGRAPH_PARTITIONING_COORDINATEDPARTITIONSTATE_H__ \ No newline at end of file +#endif // __CXXGRAPH_PARTITIONING_COORDINATEDPARTITIONSTATE_H__ diff --git a/include/Partitioning/EBV.hpp b/include/Partitioning/EBV.hpp index fad32d3e2..e303de568 100755 --- a/include/Partitioning/EBV.hpp +++ b/include/Partitioning/EBV.hpp @@ -20,6 +20,7 @@ #ifndef __CXXGRAPH_PARTITIONING_EBV_H__ #define __CXXGRAPH_PARTITIONING_EBV_H__ +#include #pragma once #include @@ -30,6 +31,15 @@ #include "Partitioning/Utility/Globals.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + namespace Partitioning { /** * @brief A Vertex Cut Partioning Algorithm ( as described by this paper @@ -47,7 +57,7 @@ class EBV : public PartitionStrategy { explicit EBV(const Globals &G); ~EBV(); - void performStep(const Edge &e, PartitionState &Sstate) override; + void performStep(shared> e, shared> Sstate) override; }; template EBV::EBV(const Globals &G) : GLOBALS(G) { @@ -56,7 +66,7 @@ EBV::EBV(const Globals &G) : GLOBALS(G) { template EBV::~EBV() {} template -void EBV::performStep(const Edge &e, PartitionState &state){ +void EBV::performStep(shared> e, shared> state){ GLOBALS.edgeAnalyzed++; int P = GLOBALS.numberOfPartition; @@ -64,12 +74,12 @@ void EBV::performStep(const Edge &e, PartitionState &state){ double beta = GLOBALS.param2; unsigned long long edgeCardinality = GLOBALS.edgeCardinality; unsigned long long vertexCardinality = GLOBALS.vertexCardinality; - auto nodePair = e.getNodePair(); + auto nodePair = e->getNodePair(); int u = nodePair.first->getId(); int v = nodePair.second->getId(); - std::shared_ptr> u_record = state.getRecord(u); - std::shared_ptr> v_record = state.getRecord(v); + std::shared_ptr> u_record = state->getRecord(u); + std::shared_ptr> v_record = state->getRecord(v); //*** ASK FOR LOCK bool locks_taken = false; @@ -112,8 +122,8 @@ void EBV::performStep(const Edge &e, PartitionState &state){ if (v_partition.empty() || v_partition.find(i) == v_partition.end()) { eva[i] += 1; } - eva[i] += alpha * (state.getMachineLoad(i) / optimalEdgesNumber) + - beta * (state.getMachineLoadVertices(i) / optimalVerticesNumber); + eva[i] += alpha * (state->getMachineLoad(i) / optimalEdgesNumber) + + beta * (state->getMachineLoadVertices(i) / optimalVerticesNumber); } // find min between eva double min = eva.at(0); @@ -125,17 +135,17 @@ void EBV::performStep(const Edge &e, PartitionState &state){ } } try { - CoordinatedPartitionState &cord_state = - dynamic_cast &>(state); + shared> cord_state = + std::static_pointer_cast>(state); // NEW UPDATE RECORDS RULE TO UPFDATE THE SIZE OF THE PARTITIONS EXPRESSED // AS THE NUMBER OF VERTICES THEY CONTAINS if (!u_record->hasReplicaInPartition(machine_id)) { u_record->addPartition(machine_id); - cord_state.incrementMachineLoadVertices(machine_id); + cord_state->incrementMachineLoadVertices(machine_id); } if (!v_record->hasReplicaInPartition(machine_id)) { v_record->addPartition(machine_id); - cord_state.incrementMachineLoadVertices(machine_id); + cord_state->incrementMachineLoadVertices(machine_id); } } catch (const std::bad_cast &e) { // use employee's member functions @@ -149,7 +159,7 @@ void EBV::performStep(const Edge &e, PartitionState &state){ } // 2-UPDATE EDGES - state.incrementMachineLoad(machine_id, &e); + state->incrementMachineLoad(machine_id, e); // 3-UPDATE DEGREES u_record->incrementDegree(); diff --git a/include/Partitioning/EdgeBalancedVertexCut.hpp b/include/Partitioning/EdgeBalancedVertexCut.hpp index ba72d2afe..7e3c3c006 100755 --- a/include/Partitioning/EdgeBalancedVertexCut.hpp +++ b/include/Partitioning/EdgeBalancedVertexCut.hpp @@ -20,6 +20,7 @@ #ifndef __CXXGRAPH_PARTITIONING_EDGEBALANCEDVERTEXCUT_H__ #define __CXXGRAPH_PARTITIONING_EDGEBALANCEDVERTEXCUT_H__ +#include #pragma once #include @@ -29,6 +30,15 @@ #include "Partitioning/Utility/Globals.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + namespace Partitioning { /** * @brief A Vertex Cut Partioning Algorithm that assign an edge in the partition @@ -45,7 +55,7 @@ class EdgeBalancedVertexCut : public PartitionStrategy { explicit EdgeBalancedVertexCut(const Globals &G); ~EdgeBalancedVertexCut(); - void performStep(const Edge &e, PartitionState &Sstate) override; + void performStep(shared> e, shared> Sstate) override; }; template EdgeBalancedVertexCut::EdgeBalancedVertexCut(const Globals &G) : GLOBALS(G) { @@ -54,15 +64,15 @@ EdgeBalancedVertexCut::EdgeBalancedVertexCut(const Globals &G) : GLOBALS(G) { template EdgeBalancedVertexCut::~EdgeBalancedVertexCut() {} template -void EdgeBalancedVertexCut::performStep(const Edge &e, - PartitionState &state) { +void EdgeBalancedVertexCut::performStep(shared> e, + shared> state) { int P = GLOBALS.numberOfPartition; - auto nodePair = e.getNodePair(); + auto nodePair = e->getNodePair(); int u = nodePair.first->getId(); int v = nodePair.second->getId(); - std::shared_ptr> u_record = state.getRecord(u); - std::shared_ptr> v_record = state.getRecord(v); + std::shared_ptr> u_record = state->getRecord(u); + std::shared_ptr> v_record = state->getRecord(v); //*** ASK FOR LOCK bool locks_taken = false; @@ -91,29 +101,29 @@ void EdgeBalancedVertexCut::performStep(const Edge &e, //*** LOCK TAKEN //*** Check which partition has the less load - int MIN_LOAD = state.getMachineLoad(0); + int MIN_LOAD = state->getMachineLoad(0); int machine_id = 0; double MAX_SCORE = 0.0; for (int m = 0; m < P; m++) { - int load = state.getMachineLoad(m); + int load = state->getMachineLoad(m); if (load <= MIN_LOAD) { MIN_LOAD = load; machine_id = m; } } try { - CoordinatedPartitionState &cord_state = - dynamic_cast &>(state); + shared> cord_state = + std::static_pointer_cast>(state); // NEW UPDATE RECORDS RULE TO UPFDATE THE SIZE OF THE PARTITIONS EXPRESSED // AS THE NUMBER OF VERTICES THEY CONTAINS if (!u_record->hasReplicaInPartition(machine_id)) { u_record->addPartition(machine_id); - cord_state.incrementMachineLoadVertices(machine_id); + cord_state->incrementMachineLoadVertices(machine_id); } if (!v_record->hasReplicaInPartition(machine_id)) { v_record->addPartition(machine_id); - cord_state.incrementMachineLoadVertices(machine_id); + cord_state->incrementMachineLoadVertices(machine_id); } } catch (const std::bad_cast &e) { // use employee's member functions @@ -127,7 +137,7 @@ void EdgeBalancedVertexCut::performStep(const Edge &e, } // 2-UPDATE EDGES - state.incrementMachineLoad(machine_id, &e); + state->incrementMachineLoad(machine_id, e); // 3-UPDATE DEGREES u_record->incrementDegree(); diff --git a/include/Partitioning/GreedyVertexCut.hpp b/include/Partitioning/GreedyVertexCut.hpp index 7012f3706..173fd60f4 100755 --- a/include/Partitioning/GreedyVertexCut.hpp +++ b/include/Partitioning/GreedyVertexCut.hpp @@ -20,6 +20,7 @@ #ifndef __CXXGRAPH_PARTITIONING_GREEDYVERTEXCUT_H__ #define __CXXGRAPH_PARTITIONING_GREEDYVERTEXCUT_H__ +#include #pragma once #include @@ -30,6 +31,15 @@ #include "Partitioning/Utility/Globals.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + namespace Partitioning { /** * @brief A Greedy Vertex Cut Partioning Algorithm @@ -45,7 +55,7 @@ class GreedyVertexCut : public PartitionStrategy { explicit GreedyVertexCut(const Globals &G); ~GreedyVertexCut(); - void performStep(const Edge &e, PartitionState &Sstate) override; + void performStep(shared> e, shared> Sstate) override; }; template @@ -55,15 +65,15 @@ template GreedyVertexCut::~GreedyVertexCut() {} template -void GreedyVertexCut::performStep(const Edge &e, - PartitionState &state) { +void GreedyVertexCut::performStep(shared> e, + shared> state) { int P = GLOBALS.numberOfPartition; - auto nodePair = e.getNodePair(); + auto nodePair = e->getNodePair(); int u = nodePair.first->getId(); int v = nodePair.second->getId(); - std::shared_ptr> u_record = state.getRecord(u); - std::shared_ptr> v_record = state.getRecord(v); + std::shared_ptr> u_record = state->getRecord(u); + std::shared_ptr> v_record = state->getRecord(v); //*** ASK FOR LOCK bool locks_taken = false; @@ -99,8 +109,8 @@ void GreedyVertexCut::performStep(const Edge &e, int min_load = INT_MAX; int machine_id = 0; for (int i = 0; i < P; i++) { - if (state.getMachineLoad(i) < min_load) { - min_load = state.getMachineLoad(i); + if (state->getMachineLoad(i) < min_load) { + min_load = state->getMachineLoad(i); machine_id = i; } } @@ -111,8 +121,8 @@ void GreedyVertexCut::performStep(const Edge &e, int min_load = INT_MAX; int machine_id = 0; for (auto &partition : u_record->getPartitions()) { - if (state.getMachineLoad(partition) < min_load) { - min_load = state.getMachineLoad(partition); + if (state->getMachineLoad(partition) < min_load) { + min_load = state->getMachineLoad(partition); machine_id = partition; } } @@ -123,8 +133,8 @@ void GreedyVertexCut::performStep(const Edge &e, int min_load = INT_MAX; int machine_id = 0; for (auto &partition : v_record->getPartitions()) { - if (state.getMachineLoad(partition) < min_load) { - min_load = state.getMachineLoad(partition); + if (state->getMachineLoad(partition) < min_load) { + min_load = state->getMachineLoad(partition); machine_id = partition; } } @@ -142,8 +152,8 @@ void GreedyVertexCut::performStep(const Edge &e, int min_load = INT_MAX; int machine_id = 0; for (auto &partition : intersection) { - if (state.getMachineLoad(partition) < min_load) { - min_load = state.getMachineLoad(partition); + if (state->getMachineLoad(partition) < min_load) { + min_load = state->getMachineLoad(partition); machine_id = partition; } } @@ -158,8 +168,8 @@ void GreedyVertexCut::performStep(const Edge &e, int min_load = INT_MAX; int machine_id = 0; for (auto &partition : part_union) { - if (state.getMachineLoad(partition) < min_load) { - min_load = state.getMachineLoad(partition); + if (state->getMachineLoad(partition) < min_load) { + min_load = state->getMachineLoad(partition); machine_id = partition; } } @@ -186,17 +196,17 @@ void GreedyVertexCut::performStep(const Edge &e, int choice = distribution(rand) % candidates.size(); machine_id = candidates.at(choice); try { - CoordinatedPartitionState &cord_state = - dynamic_cast &>(state); + shared> cord_state = + std::static_pointer_cast>(state); // NEW UPDATE RECORDS RULE TO UPFDATE THE SIZE OF THE PARTITIONS EXPRESSED // AS THE NUMBER OF VERTICES THEY CONTAINS if (!u_record->hasReplicaInPartition(machine_id)) { u_record->addPartition(machine_id); - cord_state.incrementMachineLoadVertices(machine_id); + cord_state->incrementMachineLoadVertices(machine_id); } if (!v_record->hasReplicaInPartition(machine_id)) { v_record->addPartition(machine_id); - cord_state.incrementMachineLoadVertices(machine_id); + cord_state->incrementMachineLoadVertices(machine_id); } } catch (const std::bad_cast &e) { // use employee's member functions @@ -210,7 +220,7 @@ void GreedyVertexCut::performStep(const Edge &e, } // 2-UPDATE EDGES - state.incrementMachineLoad(machine_id, &e); + state->incrementMachineLoad(machine_id, e); // 3-UPDATE DEGREES u_record->incrementDegree(); diff --git a/include/Partitioning/HDRF.hpp b/include/Partitioning/HDRF.hpp index 9f98c9dde..9a241a7f9 100755 --- a/include/Partitioning/HDRF.hpp +++ b/include/Partitioning/HDRF.hpp @@ -20,6 +20,7 @@ #ifndef __CXXGRAPH_PARTITIONING_HDRF_H__ #define __CXXGRAPH_PARTITIONING_HDRF_H__ +#include #pragma once #include @@ -30,6 +31,15 @@ #include "Partitioning/Utility/Globals.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared = std::shared_ptr; + +using std::make_unique; +using std::make_shared; + namespace Partitioning { /** * @brief A Vertex Cut Partioning Algorithm ( as described by this paper @@ -47,7 +57,7 @@ class HDRF : public PartitionStrategy { explicit HDRF(const Globals &G); ~HDRF(); - void performStep(const Edge &e, PartitionState &Sstate) override; + void performStep(shared> e, shared> Sstate) override; }; template HDRF::HDRF(const Globals &G) : GLOBALS(G) { @@ -56,15 +66,15 @@ HDRF::HDRF(const Globals &G) : GLOBALS(G) { template HDRF::~HDRF() {} template -void HDRF::performStep(const Edge &e, PartitionState &state) { +void HDRF::performStep(shared> e, shared> state) { int P = GLOBALS.numberOfPartition; double lambda = GLOBALS.param1; double epsilon = GLOBALS.param2; - auto nodePair = e.getNodePair(); + auto nodePair = e->getNodePair(); int u = nodePair.first->getId(); int v = nodePair.second->getId(); - std::shared_ptr> u_record = state.getRecord(u); - std::shared_ptr> v_record = state.getRecord(v); + std::shared_ptr> u_record = state->getRecord(u); + std::shared_ptr> v_record = state->getRecord(v); //*** ASK FOR LOCK bool locks_taken = false; @@ -94,8 +104,8 @@ void HDRF::performStep(const Edge &e, PartitionState &state) { int machine_id = -1; //*** COMPUTE MAX AND MIN LOAD - int MIN_LOAD = state.getMinLoad(); - int MAX_LOAD = state.getMaxLoad(); + int MIN_LOAD = state->getMinLoad(); + int MAX_LOAD = state->getMaxLoad(); //*** COMPUTE SCORES, FIND MIN SCORE, AND COMPUTE CANDIDATES PARITIONS std::vector candidates; @@ -116,7 +126,7 @@ void HDRF::performStep(const Edge &e, PartitionState &state) { fv /= SUM; fv = 1 + (1 - fv); } - int load = state.getMachineLoad(m); + int load = state->getMachineLoad(m); double bal = (MAX_LOAD - load); bal /= (epsilon + MAX_LOAD - MIN_LOAD); if (bal < 0) { @@ -158,17 +168,17 @@ void HDRF::performStep(const Edge &e, PartitionState &state) { int choice = distribution(rand) % candidates.size(); machine_id = candidates.at(choice); try { - CoordinatedPartitionState &cord_state = - dynamic_cast &>(state); + shared> cord_state = + std::static_pointer_cast>(state); // NEW UPDATE RECORDS RULE TO UPFDATE THE SIZE OF THE PARTITIONS EXPRESSED // AS THE NUMBER OF VERTICES THEY CONTAINS if (!u_record->hasReplicaInPartition(machine_id)) { u_record->addPartition(machine_id); - cord_state.incrementMachineLoadVertices(machine_id); + cord_state->incrementMachineLoadVertices(machine_id); } if (!v_record->hasReplicaInPartition(machine_id)) { v_record->addPartition(machine_id); - cord_state.incrementMachineLoadVertices(machine_id); + cord_state->incrementMachineLoadVertices(machine_id); } } catch (const std::bad_cast &e) { // use employee's member functions @@ -182,7 +192,7 @@ void HDRF::performStep(const Edge &e, PartitionState &state) { } // 2-UPDATE EDGES - state.incrementMachineLoad(machine_id, &e); + state->incrementMachineLoad(machine_id, e); // 3-UPDATE DEGREES u_record->incrementDegree(); @@ -196,4 +206,4 @@ void HDRF::performStep(const Edge &e, PartitionState &state) { } // namespace Partitioning } // namespace CXXGraph -#endif // __CXXGRAPH_PARTITIONING_HDRF_H__ \ No newline at end of file +#endif // __CXXGRAPH_PARTITIONING_HDRF_H__ diff --git a/include/Partitioning/Partition.hpp b/include/Partitioning/Partition.hpp index 6c86fe737..2603a1a4f 100755 --- a/include/Partitioning/Partition.hpp +++ b/include/Partitioning/Partition.hpp @@ -23,17 +23,27 @@ #pragma once #include +#include #include #include "PartitioningStats.hpp" #include "Utility/Typedef.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + template class Graph; template -using T_EdgeSet = std::unordered_set *>; +using T_EdgeSet = std::unordered_set>>; namespace Partitioning { template std::ostream &operator<<(std::ostream &o, const Partition &partition); @@ -280,10 +290,10 @@ unsigned int getNumberOfEdges(const PartitionMap &partitionMap) { template unsigned int getNumberOfNodes(const PartitionMap &partitionMap) { unsigned int numberOfNodes = 0; - std::set *> nodeSet; + std::set>> nodeSet; for (const auto &it : partitionMap) { - const std::set *> partitionNodeSet = it.second->getNodeSet(); + const std::set>> partitionNodeSet = it.second->getNodeSet(); for (const auto &it2 : partitionNodeSet) { // if (std::find_if(nodeSet.begin(), nodeSet.end(), [it2](const Node // *node) @@ -349,4 +359,4 @@ std::ostream &operator<<(std::ostream &os, const Partition &partition) { } // namespace CXXGraph -#endif // __CXXGRAPH_PARTITION_H__ \ No newline at end of file +#endif // __CXXGRAPH_PARTITION_H__ diff --git a/include/Partitioning/PartitionState.hpp b/include/Partitioning/PartitionState.hpp index b7a417f9c..25629acd6 100755 --- a/include/Partitioning/PartitionState.hpp +++ b/include/Partitioning/PartitionState.hpp @@ -22,19 +22,29 @@ #pragma once +#include #include "Record.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared = std::shared_ptr; + +using std::make_unique; +using std::make_shared; + namespace Partitioning { template class PartitionState { public: - virtual std::shared_ptr> getRecord(const int x) = 0; + virtual shared> getRecord(const int x) = 0; virtual int getMachineLoad(const int m) const = 0; virtual int getMachineWeight(const int m) const = 0; virtual int getMachineLoadVertices(const int m) const = 0; - virtual void incrementMachineLoad(const int m, const Edge* e) = 0; - virtual void incrementMachineWeight(const int m, const Edge* e) = 0; + virtual void incrementMachineLoad(const int m, shared> e) = 0; + virtual void incrementMachineWeight(const int m, shared> e) = 0; virtual int getMinLoad() const = 0; virtual int getMaxLoad() const = 0; virtual int getMachineWithMinWeight() const = 0; @@ -48,4 +58,4 @@ class PartitionState { } // namespace Partitioning } // namespace CXXGraph -#endif // __CXXGRAPH_PARTITIONING_PARTITIONSTATE_H__ \ No newline at end of file +#endif // __CXXGRAPH_PARTITIONING_PARTITIONSTATE_H__ diff --git a/include/Partitioning/PartitionStrategy.hpp b/include/Partitioning/PartitionStrategy.hpp index 96542bbb7..894b2fc0e 100755 --- a/include/Partitioning/PartitionStrategy.hpp +++ b/include/Partitioning/PartitionStrategy.hpp @@ -26,13 +26,22 @@ #include "PartitionState.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + namespace Partitioning { template class PartitionStrategy { public: - virtual void performStep(const Edge &t, PartitionState &Sstate) = 0; + virtual void performStep(shared> t, shared> Sstate) = 0; }; } // namespace Partitioning } // namespace CXXGraph -#endif // __CXXGRAPH_PARTITIONING_PARTITIONSTRATEGY_H__ \ No newline at end of file +#endif // __CXXGRAPH_PARTITIONING_PARTITIONSTRATEGY_H__ diff --git a/include/Partitioning/Partitioner.hpp b/include/Partitioning/Partitioner.hpp index 04afe62c2..0b864fe8e 100755 --- a/include/Partitioning/Partitioner.hpp +++ b/include/Partitioning/Partitioner.hpp @@ -37,44 +37,52 @@ #include "WeightBalancedLibra.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + namespace Partitioning { template class Partitioner { private: - const T_EdgeSet *dataset = nullptr; - PartitionStrategy *algorithm = nullptr; + shared> dataset = nullptr; + shared> algorithm = nullptr; Globals GLOBALS; CoordinatedPartitionState startCoordinated(); public: - Partitioner(const T_EdgeSet *dataset, Globals &G); + Partitioner(shared> dataset, Globals &G); Partitioner(const Partitioner &other); - ~Partitioner(); CoordinatedPartitionState performCoordinatedPartition(); }; template -Partitioner::Partitioner(const T_EdgeSet *dataset, Globals &G) +Partitioner::Partitioner(shared> dataset, Globals &G) : GLOBALS(G) { // this->GLOBALS = G; this->dataset = dataset; if (GLOBALS.partitionStategy == PartitionAlgorithm::HDRF_ALG) { - algorithm = new HDRF(GLOBALS); + algorithm = make_shared>(GLOBALS); } else if (GLOBALS.partitionStategy == PartitionAlgorithm::EDGEBALANCED_VC_ALG) { - algorithm = new EdgeBalancedVertexCut(GLOBALS); + algorithm = make_shared>(GLOBALS); } else if (GLOBALS.partitionStategy == PartitionAlgorithm::GREEDY_VC_ALG) { - algorithm = new GreedyVertexCut(GLOBALS); + algorithm = make_shared>(GLOBALS); } else if (GLOBALS.partitionStategy == PartitionAlgorithm::EBV_ALG) { - algorithm = new EBV(GLOBALS); + algorithm = make_shared>(GLOBALS); } else if (GLOBALS.partitionStategy == PartitionAlgorithm::WB_LIBRA) { // precompute weight sum double weight_sum = 0.0; for (const auto &edge_it : *(this->dataset)) { weight_sum += (edge_it->isWeighted().has_value() && edge_it->isWeighted().value()) - ? dynamic_cast(edge_it)->getWeight() + ? dynamic_cast(edge_it.get())->getWeight() : CXXGraph::NEGLIGIBLE_WEIGHT; } double lambda = std::max(1.0, GLOBALS.param1); @@ -93,8 +101,8 @@ Partitioner::Partitioner(const T_EdgeSet *dataset, Globals &G) vertices_degrees[v]++; } - algorithm = new WeightBalancedLibra(GLOBALS, weight_sum_bound, - move(vertices_degrees)); + algorithm = make_shared>(GLOBALS, weight_sum_bound, + std::move(vertices_degrees)); } } @@ -103,21 +111,21 @@ Partitioner::Partitioner(const Partitioner &other) { this->dataset = other.dataset; this->GLOBALS = other.GLOBALS; if (GLOBALS.partitionStategy == PartitionAlgorithm::HDRF_ALG) { - algorithm = new HDRF(GLOBALS); + algorithm = make_shared>(GLOBALS); } else if (GLOBALS.partitionStategy == PartitionAlgorithm::EDGEBALANCED_VC_ALG) { - algorithm = new EdgeBalancedVertexCut(GLOBALS); + algorithm = make_shared>(GLOBALS); } else if (GLOBALS.partitionStategy == PartitionAlgorithm::GREEDY_VC_ALG) { - algorithm = new GreedyVertexCut(GLOBALS); + algorithm = make_shared>(GLOBALS); } else if (GLOBALS.partitionStategy == PartitionAlgorithm::EBV_ALG) { - algorithm = new EBV(GLOBALS); + algorithm = make_shared>(GLOBALS); } else if (GLOBALS.partitionStategy == PartitionAlgorithm::WB_LIBRA) { // precompute weight sum double weight_sum = 0.0; for (const auto &edge_it : *(this->dataset)) { weight_sum += (edge_it->isWeighted().has_value() && edge_it->isWeighted().value()) - ? dynamic_cast(edge_it)->getWeight() + ? dynamic_cast(edge_it.get())->getWeight() : CXXGraph::NEGLIGIBLE_WEIGHT; } double lambda = GLOBALS.param1; @@ -136,8 +144,8 @@ Partitioner::Partitioner(const Partitioner &other) { vertices_degrees[v]++; } - algorithm = new WeightBalancedLibra(GLOBALS, weight_sum_bound, - move(vertices_degrees)); + algorithm = make_shared>(GLOBALS, weight_sum_bound, + std::move(vertices_degrees)); } } @@ -148,7 +156,7 @@ CoordinatedPartitionState Partitioner::startCoordinated() { std::vector myThreads(processors); std::vector> myRunnable(processors); - std::vector *>> list_vector(processors); + std::vector>>> list_vector(processors); int n = dataset->size(); int subSize = n / processors + 1; for (int t = 0; t < processors; ++t) { @@ -156,10 +164,12 @@ CoordinatedPartitionState Partitioner::startCoordinated() { int iEnd = std::min((t + 1) * subSize, n); if (iEnd >= iStart) { list_vector[t] = - std::vector *>(std::next(dataset->begin(), iStart), + std::vector>>(std::next(dataset->begin(), iStart), std::next(dataset->begin(), iEnd)); myRunnable[t].reset( - new PartitionerThread(list_vector[t], &state, algorithm)); + new PartitionerThread(list_vector[t], + make_shared>(state), + algorithm)); myThreads[t] = std::thread(&Runnable::run, myRunnable[t].get()); } } @@ -180,12 +190,7 @@ CoordinatedPartitionState Partitioner::startCoordinated() { */ return state; } -template -Partitioner::~Partitioner() { - if (algorithm != nullptr) { - delete algorithm; - } -} + template CoordinatedPartitionState Partitioner::performCoordinatedPartition() { return startCoordinated(); @@ -194,4 +199,4 @@ CoordinatedPartitionState Partitioner::performCoordinatedPartition() { } // namespace Partitioning } // namespace CXXGraph -#endif // __CXXGRAPH_PARTITIONING_PARTITIONER_H__ \ No newline at end of file +#endif // __CXXGRAPH_PARTITIONING_PARTITIONER_H__ diff --git a/include/Partitioning/PartitionerThread.hpp b/include/Partitioning/PartitionerThread.hpp index 05b5de2e0..e2551287b 100755 --- a/include/Partitioning/PartitionerThread.hpp +++ b/include/Partitioning/PartitionerThread.hpp @@ -35,13 +35,14 @@ namespace Partitioning { template class PartitionerThread : public Runnable { private: - std::vector *> list = {}; - PartitionState *state = nullptr; - PartitionStrategy *algorithm = nullptr; + std::vector>> list = {}; + shared> state = nullptr; + shared> algorithm = nullptr; public: - PartitionerThread(std::vector *> &list, - PartitionState *state, PartitionStrategy *algorithm); + PartitionerThread(std::vector>> &list, + shared> state, + shared> algorithm); ~PartitionerThread(); void run() override; @@ -49,9 +50,9 @@ class PartitionerThread : public Runnable { std::list id_partitions; }; template -PartitionerThread::PartitionerThread(std::vector *> &list, - PartitionState *state, - PartitionStrategy *algorithm) { +PartitionerThread::PartitionerThread(std::vector>> &list, + shared> state, + shared> algorithm) { this->list = list; this->state = state; this->algorithm = algorithm; @@ -61,10 +62,10 @@ PartitionerThread::~PartitionerThread() {} template void PartitionerThread::run() { for (const auto &edge_it : list) { - algorithm->performStep(*edge_it, *state); + algorithm->performStep(edge_it, state); } } } // namespace Partitioning } // namespace CXXGraph -#endif // __CXXGRAPH_PARTITIONING_PARTITIONERTHREAD_H__ \ No newline at end of file +#endif // __CXXGRAPH_PARTITIONING_PARTITIONERTHREAD_H__ diff --git a/include/Partitioning/WeightBalancedLibra.hpp b/include/Partitioning/WeightBalancedLibra.hpp index 2b66a70f6..f3001f630 100755 --- a/include/Partitioning/WeightBalancedLibra.hpp +++ b/include/Partitioning/WeightBalancedLibra.hpp @@ -50,7 +50,7 @@ class WeightBalancedLibra : public PartitionStrategy { std::unordered_map &&_vertices_degrees); ~WeightBalancedLibra(); - void performStep(const Edge &e, PartitionState &Sstate) override; + void performStep(shared> e, shared> Sstate) override; }; template WeightBalancedLibra::WeightBalancedLibra( @@ -62,14 +62,14 @@ WeightBalancedLibra::WeightBalancedLibra( template WeightBalancedLibra::~WeightBalancedLibra() {} template -void WeightBalancedLibra::performStep(const Edge &e, - PartitionState &state) { +void WeightBalancedLibra::performStep(shared> e, + shared> state) { int P = GLOBALS.numberOfPartition; - auto nodePair = e.getNodePair(); + auto nodePair = e->getNodePair(); size_t u = nodePair.first->getId(); size_t v = nodePair.second->getId(); - std::shared_ptr> u_record = state.getRecord(u); - std::shared_ptr> v_record = state.getRecord(v); + std::shared_ptr> u_record = state->getRecord(u); + std::shared_ptr> v_record = state->getRecord(v); //*** ASK FOR LOCK bool locks_taken = false; @@ -103,22 +103,22 @@ void WeightBalancedLibra::performStep(const Edge &e, // Case 1: no edges of two nodes have been assigned if (u_partition.empty() && v_partition.empty()) { - machine_id = state.getMachineWithMinWeight(); + machine_id = state->getMachineWithMinWeight(); } // Case 2: one or more edges of node v have been assigned but that of node u // haven't else if (u_partition.empty() && !v_partition.empty()) { - machine_id = state.getMachineWithMinWeight(v_partition); - if (state.getMachineWeight(machine_id) >= weight_sum_bound) { - machine_id = state.getMachineWithMinWeight(); + machine_id = state->getMachineWithMinWeight(v_partition); + if (state->getMachineWeight(machine_id) >= weight_sum_bound) { + machine_id = state->getMachineWithMinWeight(); } } // Case 3: one or more edges of node u have been assigned but that of node v // haven't else if (!u_partition.empty() && v_partition.empty()) { - machine_id = state.getMachineWithMinWeight(u_partition); - if (state.getMachineWeight(machine_id) >= weight_sum_bound) { - machine_id = state.getMachineWithMinWeight(); + machine_id = state->getMachineWithMinWeight(u_partition); + if (state->getMachineWeight(machine_id) >= weight_sum_bound) { + machine_id = state->getMachineWithMinWeight(); } } // Case 4: one or more edges of both nodes have been assigned @@ -144,22 +144,22 @@ void WeightBalancedLibra::performStep(const Edge &e, const std::set &t_partition = (u_degree > v_degree) ? u_partition : v_partition; - machine_id = state.getMachineWithMinWeight(s_partition); - if (state.getMachineWeight(machine_id) >= weight_sum_bound) { - machine_id = state.getMachineWithMinWeight(t_partition); - if (state.getMachineWeight(machine_id) >= weight_sum_bound) { - machine_id = state.getMachineWithMinWeight(); + machine_id = state->getMachineWithMinWeight(s_partition); + if (state->getMachineWeight(machine_id) >= weight_sum_bound) { + machine_id = state->getMachineWithMinWeight(t_partition); + if (state->getMachineWeight(machine_id) >= weight_sum_bound) { + machine_id = state->getMachineWithMinWeight(); } } } // Case 4.2: there are some common partitions for both nodes else { - machine_id = state.getMachineWithMinWeight(uv_intersection); - if (state.getMachineWeight(machine_id) >= weight_sum_bound) { + machine_id = state->getMachineWithMinWeight(uv_intersection); + if (state->getMachineWeight(machine_id) >= weight_sum_bound) { const auto &uv_union = u_coord_record->partition_union(v_coord_record); - machine_id = state.getMachineWithMinWeight(uv_union); + machine_id = state->getMachineWithMinWeight(uv_union); if (machine_id >= weight_sum_bound) { - machine_id = state.getMachineWithMinWeight(); + machine_id = state->getMachineWithMinWeight(); } } } @@ -179,7 +179,7 @@ void WeightBalancedLibra::performStep(const Edge &e, } // 2-UPDATE EDGES - state.incrementMachineWeight(machine_id, &e); + state->incrementMachineWeight(machine_id, e); //*** RELEASE LOCK u_record->releaseLock(); @@ -189,4 +189,4 @@ void WeightBalancedLibra::performStep(const Edge &e, } // namespace Partitioning } // namespace CXXGraph -#endif // __CXXGRAPH_PARTITIONING_WEIGHTBALANCEDLIBRA_H__ \ No newline at end of file +#endif // __CXXGRAPH_PARTITIONING_WEIGHTBALANCEDLIBRA_H__ diff --git a/include/Utility/Typedef.hpp b/include/Utility/Typedef.hpp index acc2afd2f..151c36306 100755 --- a/include/Utility/Typedef.hpp +++ b/include/Utility/Typedef.hpp @@ -24,11 +24,20 @@ #include #include +#include #include #include "ConstValue.hpp" namespace CXXGraph { +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; template class Node; @@ -199,7 +208,7 @@ using BestFirstSearchResult = BestFirstSearchResult_struct; template using AdjacencyMatrix = std::unordered_map< - const Node *, std::vector *, const Edge *>>>; + shared>, std::vector>, shared>>>>; template using PartitionMap = From f0cdb12a495bf85616a84de46fdf39c9fb7f2956 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 24 May 2023 10:41:19 +0200 Subject: [PATCH 03/28] Rewrite tests to use smart pointers --- test/BFSTest.cpp | 97 ++++++----- test/BellmanFordTest.cpp | 52 +++--- test/BestFirstSearchTest.cpp | 62 ++++--- test/BoruvkaTest.cpp | 88 +++++----- test/ConnectivityTest.cpp | 250 +++++++++++++++------------- test/CycleCheckTest.cpp | 54 +++--- test/DFSTest.cpp | 48 +++--- test/DOTTest.cpp | 40 +++-- test/DialTest.cpp | 38 +++-- test/DijkstraTest.cpp | 98 ++++++----- test/DirectedEdgeTest.cpp | 21 ++- test/DirectedWeightedEdgeTest.cpp | 29 ++-- test/EdgeTest.cpp | 21 ++- test/EulerPathTest.cpp | 46 +++-- test/FWTest.cpp | 40 +++-- test/FordFulkersonTest.cpp | 74 ++++---- test/GraphSlicingTest.cpp | 66 ++++---- test/GraphTest.cpp | 53 +++--- test/KahnTest.cpp | 76 +++++---- test/KosarajuTest.cpp | 72 ++++---- test/KruskalTest.cpp | 88 +++++----- test/MTXTest.cpp | 37 ++-- test/NodeTest.cpp | 11 +- test/PartitionTest.cpp | 11 +- test/PrimTest.cpp | 11 +- test/RWOutputTest.cpp | 11 +- test/TopologicalSortTest.cpp | 11 +- test/UndirectedEdgeTest.cpp | 9 + test/UndirectedWeightedEdgeTest.cpp | 9 + test/UnionFindTest.cpp | 13 +- test/main.cpp | 2 +- 31 files changed, 914 insertions(+), 624 deletions(-) diff --git a/test/BFSTest.cpp b/test/BFSTest.cpp index 6fdf8eca7..be1ee27ba 100644 --- a/test/BFSTest.cpp +++ b/test/BFSTest.cpp @@ -4,6 +4,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(BFSTest, test_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -14,9 +23,9 @@ TEST(BFSTest, test_1) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.breadth_first_search(node1); ASSERT_EQ(res.size(), 3); @@ -35,9 +44,9 @@ TEST(BFSTest, test_2) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.breadth_first_search(node2); ASSERT_EQ(res.size(), 2); @@ -56,9 +65,9 @@ TEST(BFSTest, test_3) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.breadth_first_search(node2); ASSERT_EQ(res.size(), 3); @@ -77,9 +86,9 @@ TEST(BFSTest, test_4) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.breadth_first_search(node3); ASSERT_EQ(res.size(), 3); @@ -98,9 +107,9 @@ TEST(BFSTest, test_5) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.breadth_first_search(node3); ASSERT_EQ(res.size(), 1); @@ -120,9 +129,9 @@ TEST(BFSTest, test_6) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.breadth_first_search(node4); ASSERT_EQ(res.size(), 0); @@ -143,9 +152,9 @@ TEST(BFSTest, test_7) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.concurrency_breadth_first_search(node1, 4); @@ -165,9 +174,9 @@ TEST(BFSTest, test_8) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.concurrency_breadth_first_search(node2, 4); @@ -187,9 +196,9 @@ TEST(BFSTest, test_9) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.concurrency_breadth_first_search(node2, 4); @@ -209,9 +218,9 @@ TEST(BFSTest, test_10) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.concurrency_breadth_first_search(node3, 4); @@ -231,9 +240,9 @@ TEST(BFSTest, test_11) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.concurrency_breadth_first_search(node3, 4); @@ -263,14 +272,14 @@ TEST(BFSTest, test_12) { CXXGraph::UndirectedWeightedEdge edge7(3, node8, node6, 6); CXXGraph::UndirectedWeightedEdge edge8(3, node8, node7, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.concurrency_breadth_first_search(node8, 4); @@ -300,14 +309,14 @@ TEST(BFSTest, test_13) { int randomNumber1 = (distribution(rand) % MaxValue); int randomNumber2 = (distribution(rand) % MaxValue); if (randomNumber1 != randomNumber2) { - CXXGraph::UndirectedEdge *newEdge = - new CXXGraph::UndirectedEdge(index, *(nodes.at(randomNumber1)), + shared> newEdge = + make_shared>(index, *(nodes.at(randomNumber1)), *(nodes.at(randomNumber2))); edgeSet.insert(newEdge); } } for (int i = 1; i < nodes.size(); i += 2) { - CXXGraph::UndirectedEdge *newEdge = new CXXGraph::UndirectedEdge( + shared> newEdge = make_shared>( edges_size + i + 1, *(nodes.at(0)), *(nodes.at(i))); edgeSet.insert(newEdge); } diff --git a/test/BellmanFordTest.cpp b/test/BellmanFordTest.cpp index 934cf6467..01844f0ba 100644 --- a/test/BellmanFordTest.cpp +++ b/test/BellmanFordTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // check if algorithm works using a complicated test case TEST(BellmanFordTest, test_1) { CXXGraph::Node node0("0", 0); @@ -24,16 +34,16 @@ TEST(BellmanFordTest, test_1) { CXXGraph::DirectedWeightedEdge edge10(1, node4, node1, -2); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); CXXGraph::Graph graph(edgeSet); CXXGraph::BellmanFordResult res = graph.bellmanford(node0, node3); @@ -52,9 +62,9 @@ TEST(BellmanFordTest, test_2) { CXXGraph::DirectedWeightedEdge edge2(2, node1, node2, 3); CXXGraph::DirectedWeightedEdge edge3(3, node2, node0, -7); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::BellmanFordResult res = graph.bellmanford(node0, node2); ASSERT_TRUE(res.success); @@ -74,9 +84,9 @@ TEST(BellmanFordTest, test_3) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::BellmanFordResult res = graph.bellmanford(node1, node3); ASSERT_TRUE(res.success); @@ -96,9 +106,9 @@ TEST(BellmanFordTest, test_4) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::BellmanFordResult res = graph.bellmanford(node1, node3); ASSERT_FALSE(res.success); @@ -116,7 +126,7 @@ TEST(BellmanFordTest, test_5) { CXXGraph::Node node3("3", 3); CXXGraph::DirectedWeightedEdge edge1(2, node1, node2, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); + edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); CXXGraph::BellmanFordResult res = graph.bellmanford(node3, node1); ASSERT_FALSE(res.success); @@ -131,7 +141,7 @@ TEST(BellmanFordTest, test_5) { ASSERT_EQ(res.result, CXXGraph::INF_DOUBLE); CXXGraph::DirectedWeightedEdge edge2(2, node3, node2, 1); - edgeSet.insert(&edge2); + edgeSet.insert(make_shared>(edge2)); CXXGraph::Graph graph1(edgeSet); res = graph1.bellmanford(node1, node3); diff --git a/test/BestFirstSearchTest.cpp b/test/BestFirstSearchTest.cpp index 7f99df9d5..db38e6c89 100644 --- a/test/BestFirstSearchTest.cpp +++ b/test/BestFirstSearchTest.cpp @@ -1,8 +1,18 @@ +#include #include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(BestFirstSearchTest, source_node_missing) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -14,9 +24,9 @@ TEST(BestFirstSearchTest, source_node_missing) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 3); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::BestFirstSearchResult res = graph.best_first_search(node4, node1); @@ -36,9 +46,9 @@ TEST(BestFirstSearchTest, target_node_missing) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 3); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::BestFirstSearchResult res = graph.best_first_search(node1, node4); @@ -57,9 +67,9 @@ TEST(BestFirstSearchTest, correct_example_small) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 3); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::BestFirstSearchResult res = graph.best_first_search(node1, node2); @@ -78,9 +88,9 @@ TEST(BestFirstSearchTest, source_target_same) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 3); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::BestFirstSearchResult res = graph.best_first_search(node1, node1); @@ -120,23 +130,23 @@ TEST(BestFirstSearchTest, correct_example_big) { CXXGraph::DirectedWeightedEdge edge12(10, node10, node13, 10); CXXGraph::DirectedWeightedEdge edge13(10, node10, node14, 2); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); CXXGraph::Graph graph(edgeSet); CXXGraph::BestFirstSearchResult res = graph.best_first_search(node1, node10); ASSERT_EQ(res.success, true); ASSERT_EQ(res.nodesInBestSearchOrder.size(), 6); ASSERT_EQ(res.errorMessage, ""); -} \ No newline at end of file +} diff --git a/test/BoruvkaTest.cpp b/test/BoruvkaTest.cpp index db990006a..113fbc74b 100644 --- a/test/BoruvkaTest.cpp +++ b/test/BoruvkaTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // minimum spanning tree can differ so instead of checking // the exact order of elements, we can check some properties // like the length & cost of mst which must remain the same @@ -34,20 +44,20 @@ TEST(BoruvkaTest, test_1) { CXXGraph::UndirectedWeightedEdge edge14(14, node5, node4, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.boruvka(); @@ -80,15 +90,15 @@ TEST(BoruvkaTest, test_2) { CXXGraph::UndirectedWeightedEdge edge9(9, node4, node3, 12); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.boruvka(); @@ -123,18 +133,18 @@ TEST(BoruvkaTest, test_3) { CXXGraph::UndirectedWeightedEdge edge12(12, node6, node7, 12); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.boruvka(); @@ -152,8 +162,8 @@ TEST(BoruvkaTest, test_4) { CXXGraph::DirectedWeightedEdge edge1(1, node1, node2, 1); CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.boruvka(); @@ -162,10 +172,10 @@ TEST(BoruvkaTest, test_4) { CXXGraph::UndirectedEdge edge3(3, node1, node2); CXXGraph::T_EdgeSet edgeSet1; - edgeSet1.insert(&edge3); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph1(edgeSet1); res = graph1.boruvka(); ASSERT_FALSE(res.success); ASSERT_EQ(res.errorMessage, CXXGraph::ERR_NO_WEIGHTED_EDGE); -} \ No newline at end of file +} diff --git a/test/ConnectivityTest.cpp b/test/ConnectivityTest.cpp index b8c34a76e..52f34479d 100644 --- a/test/ConnectivityTest.cpp +++ b/test/ConnectivityTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(ConnectivityTest, test_1) { CXXGraph::Node node0("0", 0); CXXGraph::Node node1("1", 1); @@ -28,20 +38,20 @@ TEST(ConnectivityTest, test_1) { CXXGraph::UndirectedWeightedEdge edge14(3, node5, node4, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); CXXGraph::Graph graph(edgeSet); @@ -75,20 +85,20 @@ TEST(ConnectivityTest, test_2) { CXXGraph::UndirectedWeightedEdge edge14(3, node5, node4, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); CXXGraph::Graph graph(edgeSet); @@ -130,28 +140,28 @@ TEST(ConnectivityTest, test_3) { CXXGraph::DirectedWeightedEdge edge22(3, node8, node0, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); - edgeSet.insert(&edge15); - edgeSet.insert(&edge16); - edgeSet.insert(&edge17); - edgeSet.insert(&edge18); - edgeSet.insert(&edge19); - edgeSet.insert(&edge20); - edgeSet.insert(&edge21); - edgeSet.insert(&edge22); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); + edgeSet.insert(make_shared>(edge15)); + edgeSet.insert(make_shared>(edge16)); + edgeSet.insert(make_shared>(edge17)); + edgeSet.insert(make_shared>(edge18)); + edgeSet.insert(make_shared>(edge19)); + edgeSet.insert(make_shared>(edge20)); + edgeSet.insert(make_shared>(edge21)); + edgeSet.insert(make_shared>(edge22)); CXXGraph::Graph graph(edgeSet); @@ -185,20 +195,20 @@ TEST(ConnectivityTest, test_4) { CXXGraph::DirectedWeightedEdge edge14(3, node5, node4, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); CXXGraph::Graph graph(edgeSet); @@ -291,25 +301,25 @@ TEST(ConnectivityTest, test_6) { CXXGraph::DirectedWeightedEdge edge21(3, node7, node0, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); - edgeSet.insert(&edge15); - edgeSet.insert(&edge16); - edgeSet.insert(&edge17); - edgeSet.insert(&edge18); - edgeSet.insert(&edge19); - edgeSet.insert(&edge20); - edgeSet.insert(&edge21); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); + edgeSet.insert(make_shared>(edge15)); + edgeSet.insert(make_shared>(edge16)); + edgeSet.insert(make_shared>(edge17)); + edgeSet.insert(make_shared>(edge18)); + edgeSet.insert(make_shared>(edge19)); + edgeSet.insert(make_shared>(edge20)); + edgeSet.insert(make_shared>(edge21)); CXXGraph::Graph graph(edgeSet); @@ -343,20 +353,20 @@ TEST(ConnectivityTest, test_7) { CXXGraph::UndirectedWeightedEdge edge14(3, node5, node4, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); CXXGraph::Graph graph(edgeSet); @@ -398,30 +408,30 @@ TEST(ConnectivityTest, test_8) { CXXGraph::DirectedWeightedEdge edge22(3, node8, node0, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); - edgeSet.insert(&edge15); - edgeSet.insert(&edge16); - edgeSet.insert(&edge17); - edgeSet.insert(&edge18); - edgeSet.insert(&edge19); - edgeSet.insert(&edge20); - edgeSet.insert(&edge21); - edgeSet.insert(&edge22); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); + edgeSet.insert(make_shared>(edge15)); + edgeSet.insert(make_shared>(edge16)); + edgeSet.insert(make_shared>(edge17)); + edgeSet.insert(make_shared>(edge18)); + edgeSet.insert(make_shared>(edge19)); + edgeSet.insert(make_shared>(edge20)); + edgeSet.insert(make_shared>(edge21)); + edgeSet.insert(make_shared>(edge22)); CXXGraph::Graph graph(edgeSet); ASSERT_FALSE(graph.isStronglyConnectedGraph()); -} \ No newline at end of file +} diff --git a/test/CycleCheckTest.cpp b/test/CycleCheckTest.cpp index dcfb96be9..591c6be57 100644 --- a/test/CycleCheckTest.cpp +++ b/test/CycleCheckTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(CycleCheckTest, test_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -11,9 +21,9 @@ TEST(CycleCheckTest, test_1) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); bool res = graph.isCyclicDirectedGraphDFS(); ASSERT_EQ(res, false); @@ -28,8 +38,8 @@ TEST(CycleCheckTest, test_2) { CXXGraph::DirectedWeightedEdge edge1(1, pairNode, 1); CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); CXXGraph::Graph graph(edgeSet); bool res = graph.isCyclicDirectedGraphDFS(); ASSERT_EQ(res, false); @@ -45,9 +55,9 @@ TEST(CycleCheckTest, test_3) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); bool res = graph.isCyclicDirectedGraphDFS(); ASSERT_EQ(res, false); @@ -63,9 +73,9 @@ TEST(CycleCheckTest, test_4) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node3, node1, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); bool res = graph.isCyclicDirectedGraphDFS(); ASSERT_EQ(res, true); @@ -81,9 +91,9 @@ TEST(CycleCheckTest, test_5) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); bool res = graph.isCyclicDirectedGraphBFS(); ASSERT_EQ(res, false); @@ -98,8 +108,8 @@ TEST(CycleCheckTest, test_6) { CXXGraph::DirectedWeightedEdge edge1(1, pairNode, 1); CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); CXXGraph::Graph graph(edgeSet); bool res = graph.isCyclicDirectedGraphBFS(); ASSERT_EQ(res, false); @@ -115,9 +125,9 @@ TEST(CycleCheckTest, test_7) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); bool res = graph.isCyclicDirectedGraphBFS(); ASSERT_EQ(res, false); @@ -133,9 +143,9 @@ TEST(CycleCheckTest, test_8) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node3, node1, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); bool res = graph.isCyclicDirectedGraphBFS(); ASSERT_EQ(res, true); diff --git a/test/DFSTest.cpp b/test/DFSTest.cpp index df8344844..09f69602f 100644 --- a/test/DFSTest.cpp +++ b/test/DFSTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(DFSTest, test_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -11,9 +21,9 @@ TEST(DFSTest, test_1) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.depth_first_search(node1); ASSERT_EQ(res.size(), 3); @@ -32,9 +42,9 @@ TEST(DFSTest, test_2) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.depth_first_search(node2); ASSERT_EQ(res.size(), 2); @@ -53,9 +63,9 @@ TEST(DFSTest, test_3) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.depth_first_search(node2); ASSERT_EQ(res.size(), 3); @@ -74,9 +84,9 @@ TEST(DFSTest, test_4) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.depth_first_search(node3); ASSERT_EQ(res.size(), 3); @@ -95,9 +105,9 @@ TEST(DFSTest, test_5) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.depth_first_search(node3); ASSERT_EQ(res.size(), 1); @@ -117,9 +127,9 @@ TEST(DFSTest, test_6) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.depth_first_search(node4); ASSERT_EQ(res.size(), 0); @@ -127,4 +137,4 @@ TEST(DFSTest, test_6) { ASSERT_FALSE(std::find(res.begin(), res.end(), node2) != res.end()); ASSERT_FALSE(std::find(res.begin(), res.end(), node3) != res.end()); ASSERT_FALSE(std::find(res.begin(), res.end(), node4) != res.end()); -} \ No newline at end of file +} diff --git a/test/DOTTest.cpp b/test/DOTTest.cpp index df65d7213..1651ad9e5 100644 --- a/test/DOTTest.cpp +++ b/test/DOTTest.cpp @@ -1,9 +1,19 @@ #include #include +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(DOTTest, WriteToDotDirectedWeighted) { // Generate a simple test graph with few nodes and edges CXXGraph::Node node1("1", 1); @@ -15,9 +25,9 @@ TEST(DOTTest, WriteToDotDirectedWeighted) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 3); CXXGraph::DirectedWeightedEdge edge3(3, node3, node1, 7); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); // Write the graph to a DOT file @@ -36,9 +46,9 @@ TEST(DOTTest, WriteToDotUndirected) { CXXGraph::UndirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedEdge edge3(3, node3, node1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); // Write the graph to a DOT file @@ -55,9 +65,9 @@ TEST(DOTTest, WriteToDotMixed) { CXXGraph::DirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); graph.writeToDotFile("./", "mixed", "example_graph"); @@ -98,7 +108,7 @@ TEST(DOTTest, ReadFromDotUndirectedWeighted) { ASSERT_EQ(graph.getEdge(0).value()->getNodePair().first->getUserId(), "0"); ASSERT_EQ(graph.getEdge(0).value()->getNodePair().second->getUserId(), "1"); ASSERT_EQ(dynamic_cast *>( - graph.getEdge(0).value()) + graph.getEdge(0).value().get()) ->getWeight(), 6); ASSERT_EQ(graph.getEdge(0).value()->isDirected(), false); @@ -108,7 +118,7 @@ TEST(DOTTest, ReadFromDotUndirectedWeighted) { ASSERT_EQ(graph.getEdge(210).value()->getNodePair().second->getUserId(), "28"); ASSERT_EQ(dynamic_cast *>( - graph.getEdge(210).value()) + graph.getEdge(210).value().get()) ->getWeight(), 2); ASSERT_EQ(graph.getEdge(210).value()->isDirected(), false); @@ -128,12 +138,12 @@ TEST(DOTTest, ReadFromDotDirected) { ASSERT_EQ(graph.getEdge(0).value()->getNodePair().first->getUserId(), "0"); ASSERT_EQ(graph.getEdge(0).value()->getNodePair().second->getUserId(), "1"); ASSERT_EQ(dynamic_cast *>( - graph.getEdge(0).value()) + graph.getEdge(0).value().get()) ->getFrom() .getUserId(), "0"); ASSERT_EQ(dynamic_cast *>( - graph.getEdge(0).value()) + graph.getEdge(0).value().get()) ->getTo() .getUserId(), "1"); @@ -144,12 +154,12 @@ TEST(DOTTest, ReadFromDotDirected) { ASSERT_EQ(graph.getEdge(409).value()->getNodePair().second->getUserId(), "27"); ASSERT_EQ(dynamic_cast *>( - graph.getEdge(409).value()) + graph.getEdge(409).value().get()) ->getFrom() .getUserId(), "29"); ASSERT_EQ(dynamic_cast *>( - graph.getEdge(409).value()) + graph.getEdge(409).value().get()) ->getTo() .getUserId(), "27"); diff --git a/test/DialTest.cpp b/test/DialTest.cpp index 26441dc5c..27b0376e6 100644 --- a/test/DialTest.cpp +++ b/test/DialTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(DialTest, test_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -11,9 +21,9 @@ TEST(DialTest, test_1) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); auto res = graph.dial(node1, 6); ASSERT_TRUE(res.success); @@ -33,9 +43,9 @@ TEST(DialTest, test_2) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 4); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); auto res = graph.dial(node1, 6); ; @@ -56,9 +66,9 @@ TEST(DialTest, test_3) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); auto res = graph.dial(node1, 6); ASSERT_FALSE(res.success); @@ -73,8 +83,8 @@ TEST(DialTest, test_4) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); auto res = graph.dial(node1, 6); ASSERT_TRUE(res.success); @@ -93,11 +103,11 @@ TEST(DialTest, test_5) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); auto res = graph.dial(node4, 6); ASSERT_FALSE(res.success); ASSERT_EQ(res.errorMessage, CXXGraph::ERR_SOURCE_NODE_NOT_IN_GRAPH); ASSERT_TRUE(res.minDistanceMap.empty()); -} \ No newline at end of file +} diff --git a/test/DijkstraTest.cpp b/test/DijkstraTest.cpp index 92a0846e3..b9deab69f 100644 --- a/test/DijkstraTest.cpp +++ b/test/DijkstraTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(DijkstraTest, correct_example_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -13,9 +23,9 @@ TEST(DijkstraTest, correct_example_1) { CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node1, node3); @@ -36,9 +46,9 @@ TEST(DijkstraTest, correct_example_2) { CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node1, node3); @@ -65,13 +75,13 @@ TEST(DijkstraTest, correct_example_3) { CXXGraph::UndirectedWeightedEdge edge7(7, nodeD, nodeE, 7); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(nodeC, nodeE); @@ -114,15 +124,15 @@ TEST(DijkstraTest, correct_example_4) { CXXGraph::UndirectedWeightedEdge edge9(9, node5, node6, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node0, node1); @@ -171,15 +181,15 @@ TEST(DijkstraTest, correct_example_5) { CXXGraph::UndirectedWeightedEdge edge9(9, node6, node5, 9); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node1, node5); @@ -200,9 +210,9 @@ TEST(DijkstraTest, non_weigthed_node_test) { CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node1, node3); @@ -223,9 +233,9 @@ TEST(DijkstraTest, negative_weigthed_node_test) { CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node1, node3); @@ -243,8 +253,8 @@ TEST(DijkstraTest, unreachable_node_test) { CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node1, node2); @@ -263,8 +273,8 @@ TEST(DijkstraTest, source_not_connected_test) { CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node4, node2); @@ -283,12 +293,12 @@ TEST(DijkstraTest, target_not_connected_test) { CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node1, node4); ASSERT_FALSE(res.success); ASSERT_EQ(res.errorMessage, CXXGraph::ERR_TARGET_NODE_NOT_IN_GRAPH); ASSERT_EQ(res.result, CXXGraph::INF_DOUBLE); -} \ No newline at end of file +} diff --git a/test/DirectedEdgeTest.cpp b/test/DirectedEdgeTest.cpp index 17e8855d2..bb75d6548 100644 --- a/test/DirectedEdgeTest.cpp +++ b/test/DirectedEdgeTest.cpp @@ -1,6 +1,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(DirectedEdgeTest, Constructor_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -15,7 +24,7 @@ TEST(DirectedEdgeTest, Constructor_2) { std::pair *, const CXXGraph::Node *> pairNode( &node1, &node2); CXXGraph::DirectedEdge edge(1, pairNode); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); } @@ -45,7 +54,7 @@ TEST(DirectedEdgeTest, Constructor_5) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::DirectedEdge edge(base_edge); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); @@ -68,7 +77,7 @@ TEST(DirectedEdgeTest, Bool_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::DirectedEdge edge2(2, pairNode); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); ASSERT_TRUE(edge2.isDirected().value()); @@ -91,7 +100,7 @@ TEST(DirectedEdgeTest, String_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::DirectedEdge edge2(2, pairNode); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); ASSERT_TRUE(edge2.isDirected().value()); @@ -105,7 +114,7 @@ TEST(DirectedEdgeTest, Cast_1) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::DirectedEdge edge = base_edge; - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); @@ -120,7 +129,7 @@ TEST(DirectedEdgeTest, Cast_2) { CXXGraph::UndirectedEdge base_edge(1, pairNode); CXXGraph::DirectedEdge edge = base_edge; ASSERT_FALSE(base_edge.isDirected().value()); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); diff --git a/test/DirectedWeightedEdgeTest.cpp b/test/DirectedWeightedEdgeTest.cpp index 8eed0f700..5f380823b 100644 --- a/test/DirectedWeightedEdgeTest.cpp +++ b/test/DirectedWeightedEdgeTest.cpp @@ -1,6 +1,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(DirectedWeightedEdgeTest, Constructor_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -16,7 +25,7 @@ TEST(DirectedWeightedEdgeTest, Constructor_2) { std::pair *, const CXXGraph::Node *> pairNode( &node1, &node2); CXXGraph::DirectedWeightedEdge edge(1, pairNode, 10); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_EQ(edge.getWeight(), 10); @@ -47,7 +56,7 @@ TEST(DirectedWeightedEdgeTest, Constructor_5) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::DirectedWeightedEdge edge(base_edge, 10); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); @@ -62,7 +71,7 @@ TEST(DirectedWeightedEdgeTest, Constructor_6) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::DirectedWeightedEdge edge(base_edge); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); @@ -77,7 +86,7 @@ TEST(DirectedWeightedEdgeTest, Constructor_7) { &node1, &node2); CXXGraph::DirectedEdge base_edge(1, pairNode); CXXGraph::DirectedWeightedEdge edge(base_edge); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); @@ -101,7 +110,7 @@ TEST(DirectedWeightedEdgeTest, Bool_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::DirectedWeightedEdge edge2(2, pairNode, 2); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); ASSERT_TRUE(edge2.isDirected().value()); @@ -124,7 +133,7 @@ TEST(DirectedWeightedEdgeTest, String_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::DirectedWeightedEdge edge2(2, pairNode, 6); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); ASSERT_TRUE(edge2.isDirected().value()); @@ -138,7 +147,7 @@ TEST(DirectedWeightedEdgeTest, Cast_1) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::DirectedWeightedEdge edge = base_edge; - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); @@ -154,7 +163,7 @@ TEST(DirectedWeightedEdgeTest, Cast_2) { CXXGraph::UndirectedEdge base_edge(1, pairNode); CXXGraph::DirectedWeightedEdge edge = base_edge; ASSERT_FALSE(base_edge.isDirected().value()); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); @@ -170,7 +179,7 @@ TEST(DirectedWeightedEdgeTest, Cast_3) { CXXGraph::DirectedEdge base_edge(1, pairNode); CXXGraph::DirectedWeightedEdge edge = base_edge; ASSERT_TRUE(base_edge.isDirected().value()); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); @@ -187,7 +196,7 @@ TEST(DirectedWeightedEdgeTest, Cast_4) { CXXGraph::DirectedWeightedEdge edge = base_edge; ASSERT_FALSE(base_edge.isDirected().value()); ASSERT_TRUE(base_edge.isWeighted().value()); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_TRUE(edge.isDirected().value()); diff --git a/test/EdgeTest.cpp b/test/EdgeTest.cpp index fd4a6e823..4d88c39e9 100644 --- a/test/EdgeTest.cpp +++ b/test/EdgeTest.cpp @@ -1,6 +1,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(EdgeTest, Constructor_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -15,7 +24,7 @@ TEST(EdgeTest, Constructor_2) { std::pair *, const CXXGraph::Node *> pairNode( &node1, &node2); CXXGraph::Edge edge(1, pairNode); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); } @@ -42,7 +51,7 @@ TEST(EdgeTest, Bool_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::Edge edge2(2, pairNode); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); } @@ -61,7 +70,7 @@ TEST(EdgeTest, String_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::Edge edge2(2, pairNode); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); } @@ -76,9 +85,9 @@ TEST(EdgeTest, test) { CXXGraph::DirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph; graph.setEdgeSet(edgeSet); diff --git a/test/EulerPathTest.cpp b/test/EulerPathTest.cpp index 49b2c0760..d10b527a6 100644 --- a/test/EulerPathTest.cpp +++ b/test/EulerPathTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // example taken from // https://www.geeksforgeeks.org/hierholzers-algorithm-directed-graph/ // different eulerian paths (i.e different node visit order) @@ -16,9 +26,9 @@ TEST(EulerPathTest, test_1) { CXXGraph::DirectedWeightedEdge edge2(2, node1, node2, 8); CXXGraph::DirectedWeightedEdge edge3(3, node2, node0, 11); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); auto res = graph.eulerianPath(); @@ -47,12 +57,12 @@ TEST(EulerPathTest, test_2) { CXXGraph::DirectedWeightedEdge edge6(6, node4, node1, 11); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); CXXGraph::Graph graph(edgeSet); auto res = graph.eulerianPath(); @@ -85,14 +95,14 @@ TEST(EulerPathTest, test_3) { CXXGraph::DirectedEdge edge8(6, node6, node4); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); CXXGraph::Graph graph(edgeSet); auto res = graph.eulerianPath(); @@ -104,4 +114,4 @@ TEST(EulerPathTest, test_3) { ASSERT_FALSE(check); } -} \ No newline at end of file +} diff --git a/test/FWTest.cpp b/test/FWTest.cpp index 92b98dcf3..e1d361993 100644 --- a/test/FWTest.cpp +++ b/test/FWTest.cpp @@ -1,8 +1,18 @@ #include +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm#Example TEST(FWTest, test_1) { CXXGraph::Node node1("1", 1); @@ -17,11 +27,11 @@ TEST(FWTest, test_1) { CXXGraph::DirectedWeightedEdge edge5(3, node2, node3, 3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); CXXGraph::Graph graph(edgeSet); CXXGraph::FWResult res = graph.floydWarshall(); @@ -63,9 +73,9 @@ TEST(FWTest, test_2) { CXXGraph::DirectedWeightedEdge edge2(2, node1, node2, 3); CXXGraph::DirectedWeightedEdge edge3(3, node2, node0, -7); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::FWResult res = graph.floydWarshall(); ASSERT_TRUE(res.success); @@ -84,9 +94,9 @@ TEST(FWTest, test_3) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::FWResult res = graph.floydWarshall(); ASSERT_TRUE(res.success); @@ -105,11 +115,11 @@ TEST(FWTest, test_4) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::FWResult res = graph.floydWarshall(); ASSERT_FALSE(res.success); ASSERT_EQ(res.errorMessage, CXXGraph::ERR_NO_WEIGHTED_EDGE); -} \ No newline at end of file +} diff --git a/test/FordFulkersonTest.cpp b/test/FordFulkersonTest.cpp index b5ca0e2ff..389d1a572 100644 --- a/test/FordFulkersonTest.cpp +++ b/test/FordFulkersonTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(FordFulkersonTest, test_1) { CXXGraph::Node node0("0", 0); CXXGraph::Node node1("1", 1); @@ -18,14 +28,14 @@ TEST(FordFulkersonTest, test_1) { CXXGraph::DirectedWeightedEdge edge7(7, node4, node5, 4); CXXGraph::DirectedWeightedEdge edge8(8, node3, node5, 19); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); CXXGraph::Graph graph(edgeSet); @@ -49,14 +59,14 @@ TEST(FordFulkersonTest, test_2) { CXXGraph::DirectedWeightedEdge edge7(7, node4, node5, 4); CXXGraph::DirectedWeightedEdge edge8(8, node3, node5, 19); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); CXXGraph::Graph graph(edgeSet); @@ -80,14 +90,14 @@ TEST(FordFulkersonTest, test_3) { CXXGraph::DirectedWeightedEdge edge7(7, node4, node5, 4); CXXGraph::DirectedWeightedEdge edge8(8, node3, node5, 4); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); CXXGraph::Graph graph(edgeSet); @@ -111,14 +121,14 @@ TEST(FordFulkersonTest, test_4) { CXXGraph::DirectedEdge edge7(7, node4, node5); CXXGraph::DirectedWeightedEdge edge8(8, node3, node5, 4); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); CXXGraph::Graph graph(edgeSet); diff --git a/test/GraphSlicingTest.cpp b/test/GraphSlicingTest.cpp index e6d35f547..72e0a4420 100644 --- a/test/GraphSlicingTest.cpp +++ b/test/GraphSlicingTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // DirectedEdge TEST(GraphSlicingTest, test_1) { CXXGraph::Node node1("1", 1); @@ -18,12 +28,12 @@ TEST(GraphSlicingTest, test_1) { CXXGraph::DirectedEdge edge6(6, node4, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.graph_slicing(node1); @@ -51,12 +61,12 @@ TEST(GraphSlicingTest, test_2) { CXXGraph::DirectedWeightedEdge edge6(6, node4, node3, 0); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.graph_slicing(node1); @@ -83,12 +93,12 @@ TEST(GraphSlicingTest, test_3) { CXXGraph::UndirectedEdge edge6(6, node4, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.graph_slicing(node1); @@ -113,12 +123,12 @@ TEST(GraphSlicingTest, test_4) { CXXGraph::UndirectedWeightedEdge edge6(6, node4, node3, 0); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.graph_slicing(node1); @@ -135,10 +145,10 @@ TEST(GraphSlicingTest, test_5) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::vector> res = graph.graph_slicing(node4); ASSERT_EQ(res.size(), 0); -} \ No newline at end of file +} diff --git a/test/GraphTest.cpp b/test/GraphTest.cpp index e28f8659c..a844fcddf 100644 --- a/test/GraphTest.cpp +++ b/test/GraphTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(GraphTest, Constructor_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -8,7 +18,7 @@ TEST(GraphTest, Constructor_1) { &node1, &node2); CXXGraph::Edge edge(1, pairNode); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); + edgeSet.insert(make_shared>(edge)); CXXGraph::Graph graph(edgeSet); ASSERT_EQ(graph.getEdgeSet(), edgeSet); } @@ -20,7 +30,7 @@ TEST(GraphTest, GetEdge_1) { &node1, &node2); CXXGraph::Edge edge(1, pairNode); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); + edgeSet.insert(make_shared>(edge)); CXXGraph::Graph graph(edgeSet); ASSERT_FALSE(graph.getEdge(2).has_value()); } @@ -32,7 +42,7 @@ TEST(GraphTest, GetEdge_2) { &node1, &node2); CXXGraph::Edge edge(1, pairNode); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); + edgeSet.insert(make_shared>(edge)); CXXGraph::Graph graph(edgeSet); ASSERT_TRUE(graph.getEdge(1).has_value()); } @@ -56,7 +66,7 @@ TEST(GraphTest, GetNodeSet_1) { &node1, &node2); CXXGraph::Edge edge(1, pairNode); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); + edgeSet.insert(make_shared>(edge)); CXXGraph::Graph graph(edgeSet); auto nodeSet = graph.getNodeSet(); ASSERT_EQ(nodeSet.size(), 2); @@ -75,8 +85,8 @@ TEST(GraphTest, GetNodeSet_2) { CXXGraph::Edge edge(1, pairNode); CXXGraph::UndirectedEdge edge2(2, node2, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); - edgeSet.insert(&edge2); + edgeSet.insert(make_shared>(edge)); + edgeSet.insert(make_shared>(edge2)); CXXGraph::Graph graph(edgeSet); auto nodeSet = graph.getNodeSet(); ASSERT_EQ(nodeSet.size(), 3); @@ -95,7 +105,7 @@ TEST(GraphTest, adj_print_1) { &node1, &node2); CXXGraph::Edge edge(1, pairNode); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); + edgeSet.insert(make_shared>(edge)); CXXGraph::Graph graph(edgeSet); std::cout << "Test Print Adjacency Matrix" << std::endl; std::cout << *graph.getAdjMatrix() << std::endl; @@ -108,7 +118,7 @@ TEST(GraphTest, adj_print_2) { &node1, &node2); CXXGraph::DirectedEdge edge(1, pairNode); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); + edgeSet.insert(make_shared>(edge)); CXXGraph::Graph graph(edgeSet); std::cout << "Test Print Adjacency Matrix" << std::endl; std::cout << *graph.getAdjMatrix() << std::endl; @@ -121,7 +131,7 @@ TEST(GraphTest, adj_print_3) { &node1, &node2); CXXGraph::UndirectedEdge edge(1, pairNode); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); + edgeSet.insert(make_shared>(edge)); CXXGraph::Graph graph(edgeSet); std::cout << "Test Print Adjacency Matrix" << std::endl; std::cout << *graph.getAdjMatrix() << std::endl; @@ -137,9 +147,9 @@ TEST(GraphTest, adj_print_4) { CXXGraph::UndirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::cout << "Test Print Adjacency Matrix" << std::endl; std::cout << *graph.getAdjMatrix() << std::endl; @@ -155,9 +165,9 @@ TEST(GraphTest, adj_print_5) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::DirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::cout << "Test Print Adjacency Matrix" << std::endl; std::cout << *graph.getAdjMatrix() << std::endl; @@ -173,9 +183,9 @@ TEST(GraphTest, adj_print_6) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); std::cout << "Test Print Adjacency Matrix" << std::endl; std::cout << *graph.getAdjMatrix() << std::endl; @@ -192,12 +202,11 @@ TEST(GraphTest, set_data) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); - std::map initial_values; // Construct map with the initial values of the nodes data for (const auto& nodeIt : graph.getNodeSet()) { diff --git a/test/KahnTest.cpp b/test/KahnTest.cpp index 5bb40c33e..1ebea029d 100644 --- a/test/KahnTest.cpp +++ b/test/KahnTest.cpp @@ -1,8 +1,18 @@ +#include #include "CXXGraph.hpp" #include "Utility/ConstString.hpp" #include "Utility/Typedef.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(KahnTest, error_cyclic_graph) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -13,9 +23,9 @@ TEST(KahnTest, error_cyclic_graph) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 2); CXXGraph::DirectedWeightedEdge edge3(3, node3, node1, 3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::TopoSortResult res = graph.kahn(); @@ -36,10 +46,10 @@ TEST(KahnTest, error_undirected_graph) { CXXGraph::UndirectedWeightedEdge edge3(3, node3, node4, 1); CXXGraph::UndirectedWeightedEdge edge4(3, node3, node5, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); CXXGraph::Graph graph(edgeSet); CXXGraph::TopoSortResult &&res = graph.kahn(); @@ -67,14 +77,14 @@ TEST(KahnTest, correct_example_small) { CXXGraph::DirectedWeightedEdge edge7(7, node8, node6, 6); CXXGraph::DirectedWeightedEdge edge8(8, node8, node7, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); CXXGraph::Graph graph(edgeSet); CXXGraph::TopoSortResult res = graph.kahn(); @@ -126,24 +136,24 @@ TEST(KahnTest, correct_example_big) { CXXGraph::DirectedEdge edge17(17, node11, node12); CXXGraph::DirectedEdge edge18(18, node12, node8); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); - edgeSet.insert(&edge15); - edgeSet.insert(&edge16); - edgeSet.insert(&edge17); - edgeSet.insert(&edge18); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); + edgeSet.insert(make_shared>(edge15)); + edgeSet.insert(make_shared>(edge16)); + edgeSet.insert(make_shared>(edge17)); + edgeSet.insert(make_shared>(edge18)); CXXGraph::Graph graph(edgeSet); CXXGraph::TopoSortResult res = graph.kahn(); diff --git a/test/KosarajuTest.cpp b/test/KosarajuTest.cpp index 0be3853f6..feeea26f6 100644 --- a/test/KosarajuTest.cpp +++ b/test/KosarajuTest.cpp @@ -1,9 +1,19 @@ +#include #include "CXXGraph.hpp" #include "Edge/DirectedEdge.hpp" #include "Edge/UndirectedEdge.hpp" #include "Graph/Graph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // helper function to compare strongly connected components (SCC) as computed // by the algorithm with expected (correct) SCC void compareComponents(CXXGraph::Components& comp1, @@ -30,7 +40,7 @@ TEST(KosarajuTest, test_1) { CXXGraph::Node node2("2", 2); CXXGraph::UndirectedEdge edge(1, node1, node2); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); + edgeSet.insert(make_shared>(edge)); CXXGraph::Graph graph(edgeSet); auto res = graph.kosaraju(); ASSERT_EQ(res.stronglyConnectedComps.size(), 0); @@ -45,8 +55,8 @@ TEST(KosarajuTest, test_2) { CXXGraph::DirectedEdge edge1(1, node1, node2); CXXGraph::DirectedEdge edge2(2, node2, node1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); CXXGraph::Graph graph(edgeSet); auto res = graph.kosaraju(); ASSERT_TRUE(res.success); @@ -64,9 +74,9 @@ TEST(KosarajuTest, test_3) { CXXGraph::DirectedEdge edge2(2, node2, node1); CXXGraph::DirectedEdge edge3(3, node3, node1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); auto res = graph.kosaraju(); ASSERT_TRUE(res.success); @@ -87,10 +97,10 @@ TEST(KosarajuTest, test_4) { CXXGraph::DirectedEdge edge3(3, node3, node4); CXXGraph::DirectedEdge edge4(4, node4, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); CXXGraph::Graph graph(edgeSet); auto res = graph.kosaraju(); ASSERT_TRUE(res.success); @@ -138,27 +148,27 @@ TEST(KosarajuTest, test_5) { CXXGraph::DirectedEdge edge21(21, node13, node10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); - edgeSet.insert(&edge15); - edgeSet.insert(&edge16); - edgeSet.insert(&edge17); - edgeSet.insert(&edge18); - edgeSet.insert(&edge19); - edgeSet.insert(&edge20); - edgeSet.insert(&edge21); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); + edgeSet.insert(make_shared>(edge15)); + edgeSet.insert(make_shared>(edge16)); + edgeSet.insert(make_shared>(edge17)); + edgeSet.insert(make_shared>(edge18)); + edgeSet.insert(make_shared>(edge19)); + edgeSet.insert(make_shared>(edge20)); + edgeSet.insert(make_shared>(edge21)); CXXGraph::Graph graph(edgeSet); auto res = graph.kosaraju(); diff --git a/test/KruskalTest.cpp b/test/KruskalTest.cpp index 80a50af24..691483625 100644 --- a/test/KruskalTest.cpp +++ b/test/KruskalTest.cpp @@ -1,6 +1,16 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // minimum spanning tree can differ so instead of checking // the exact order of elements, we can check some properties // like the length & cost of mst which must remain the same @@ -35,20 +45,20 @@ TEST(KruskalTest, test_1) { CXXGraph::UndirectedWeightedEdge edge14(14, node5, node4, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.kruskal(); @@ -81,15 +91,15 @@ TEST(KruskalTest, test_2) { CXXGraph::UndirectedWeightedEdge edge9(9, node4, node3, 12); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.kruskal(); @@ -124,18 +134,18 @@ TEST(KruskalTest, test_3) { CXXGraph::UndirectedWeightedEdge edge12(12, node6, node7, 12); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.kruskal(); @@ -153,8 +163,8 @@ TEST(KruskalTest, test_4) { CXXGraph::DirectedWeightedEdge edge1(1, node1, node2, 1); CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.kruskal(); @@ -163,10 +173,10 @@ TEST(KruskalTest, test_4) { CXXGraph::UndirectedEdge edge3(3, node1, node2); CXXGraph::T_EdgeSet edgeSet1; - edgeSet1.insert(&edge3); + edgeSet1.insert(make_shared>(edge3)); CXXGraph::Graph graph1(edgeSet1); res = graph1.kruskal(); ASSERT_FALSE(res.success); ASSERT_EQ(res.errorMessage, CXXGraph::ERR_NO_WEIGHTED_EDGE); -} \ No newline at end of file +} diff --git a/test/MTXTest.cpp b/test/MTXTest.cpp index 822ddc3c6..b5b78440d 100644 --- a/test/MTXTest.cpp +++ b/test/MTXTest.cpp @@ -1,10 +1,20 @@ #include #include #include +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(MTXTest, WriteToMtxDirectedWeighted) { // Generate a simple test graph with few nodes and edges CXXGraph::Node node1("1", 1); @@ -16,9 +26,9 @@ TEST(MTXTest, WriteToMtxDirectedWeighted) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 3); CXXGraph::DirectedWeightedEdge edge3(3, node3, node1, 7); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); // Write the graph to a DOT file @@ -39,6 +49,9 @@ TEST(MTXTest, WriteToMtxUndirected) { edgeSet.insert(&edge1); edgeSet.insert(&edge2); edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); // Write the graph to a DOT file @@ -55,9 +68,9 @@ TEST(MTXTest, WriteToMtxMixed) { CXXGraph::DirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); graph.writeToMTXFile(".", "example_graph_mixed", ' '); @@ -78,7 +91,7 @@ TEST(MTXTest, ReadFromMtxDirectedWeighted) { ASSERT_EQ(graph.getEdge(0).value()->isDirected(), false); // Check that it's weighted and the value of the weight ASSERT_EQ(graph.getEdge(0).value()->isWeighted(), true); - ASSERT_EQ(dynamic_cast(graph.getEdge(0).value()) + ASSERT_EQ(dynamic_cast(graph.getEdge(0).value().get()) ->getWeight(), 1.); @@ -89,7 +102,7 @@ TEST(MTXTest, ReadFromMtxDirectedWeighted) { ASSERT_EQ(graph.getEdge(7).value()->isDirected(), false); // Check that it's weighted and the value of the weight ASSERT_EQ(graph.getEdge(7).value()->isWeighted(), true); - ASSERT_EQ(dynamic_cast(graph.getEdge(7).value()) + ASSERT_EQ(dynamic_cast(graph.getEdge(7).value().get()) ->getWeight(), 12.); @@ -100,7 +113,7 @@ TEST(MTXTest, ReadFromMtxDirectedWeighted) { ASSERT_EQ(graph.getEdge(3).value()->isDirected(), true); // Check that it's weighted and the value of the weight ASSERT_EQ(graph.getEdge(3).value()->isWeighted(), true); - ASSERT_EQ(dynamic_cast(graph.getEdge(3).value()) + ASSERT_EQ(dynamic_cast(graph.getEdge(3).value().get()) ->getWeight(), 6.); } @@ -120,7 +133,7 @@ TEST(MTXTest, ReadFromMtxUndirectedWeighted) { ASSERT_EQ(graph.getEdge(0).value()->isDirected(), false); // Check that it's weighted and the value of the weight ASSERT_EQ(graph.getEdge(0).value()->isWeighted(), true); - ASSERT_EQ(dynamic_cast(graph.getEdge(0).value()) + ASSERT_EQ(dynamic_cast(graph.getEdge(0).value().get()) ->getWeight(), 1.); @@ -131,7 +144,7 @@ TEST(MTXTest, ReadFromMtxUndirectedWeighted) { ASSERT_EQ(graph.getEdge(7).value()->isDirected(), false); // Check that it's weighted and the value of the weight ASSERT_EQ(graph.getEdge(7).value()->isWeighted(), true); - ASSERT_EQ(dynamic_cast(graph.getEdge(7).value()) + ASSERT_EQ(dynamic_cast(graph.getEdge(7).value().get()) ->getWeight(), 12.); @@ -142,7 +155,7 @@ TEST(MTXTest, ReadFromMtxUndirectedWeighted) { ASSERT_EQ(graph.getEdge(3).value()->isDirected(), false); // Check that it's weighted and the value of the weight ASSERT_EQ(graph.getEdge(3).value()->isWeighted(), true); - ASSERT_EQ(dynamic_cast(graph.getEdge(3).value()) + ASSERT_EQ(dynamic_cast(graph.getEdge(3).value().get()) ->getWeight(), 6.); } diff --git a/test/NodeTest.cpp b/test/NodeTest.cpp index dc73e3964..76de0263a 100644 --- a/test/NodeTest.cpp +++ b/test/NodeTest.cpp @@ -1,6 +1,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // Struct for testing Node as a template of different items struct testStruct { int testInt = 0; @@ -145,4 +154,4 @@ TEST(StringNodeTest, StringprintOperator) { ASSERT_EQ(resultDouble.str(), expectedDouble.str()); ASSERT_EQ(resultString.str(), expectedString.str()); ASSERT_EQ(resultStruct.str(), expectedStruct.str()); -} \ No newline at end of file +} diff --git a/test/PartitionTest.cpp b/test/PartitionTest.cpp index fcb4a6f22..4e06b5201 100644 --- a/test/PartitionTest.cpp +++ b/test/PartitionTest.cpp @@ -2,6 +2,15 @@ #include "Utilities.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + static auto nodes = generateRandomNodes(10000, 2); static auto edges = generateRandomEdges(10000, nodes); @@ -316,4 +325,4 @@ TEST(PartitionTest, test_9) { for (int i = 0; i < 4; ++i) { ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); } -} \ No newline at end of file +} diff --git a/test/PrimTest.cpp b/test/PrimTest.cpp index 7376a3fda..bc84e441f 100644 --- a/test/PrimTest.cpp +++ b/test/PrimTest.cpp @@ -1,6 +1,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // minimum spanning tree can differ so instead of checking // the exact order of elements, we can check some properties // like the length & cost of mst which must remain the same @@ -169,4 +178,4 @@ TEST(PrimTest, test_4) { res = graph1.prim(); ASSERT_FALSE(res.success); ASSERT_EQ(res.errorMessage, CXXGraph::ERR_NO_WEIGHTED_EDGE); -} \ No newline at end of file +} diff --git a/test/RWOutputTest.cpp b/test/RWOutputTest.cpp index f6a0fe971..3a1f1d920 100644 --- a/test/RWOutputTest.cpp +++ b/test/RWOutputTest.cpp @@ -3,6 +3,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + inline bool exists_test(const std::string &name) { struct stat buffer; return (stat(name.c_str(), &buffer) == 0); @@ -1231,4 +1240,4 @@ TEST(RWOutputTest, test_29) { ASSERT_FALSE(exists_test("test_29.tsv.gz")); ASSERT_FALSE(exists_test("test_29_NodeFeat.tsv.gz")); ASSERT_FALSE(exists_test("test_29_EdgeWeight.tsv.gz")); -} \ No newline at end of file +} diff --git a/test/TopologicalSortTest.cpp b/test/TopologicalSortTest.cpp index 3b156758b..6e289e036 100644 --- a/test/TopologicalSortTest.cpp +++ b/test/TopologicalSortTest.cpp @@ -3,6 +3,15 @@ #include "Utility/Typedef.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + // topological sort test in a cyclic graph TEST(TopologicalSortTest, test_1) { CXXGraph::Node node1("1", 1); @@ -100,4 +109,4 @@ TEST(TopologicalSortTest, test_3) { } } -// more test cases to be needed \ No newline at end of file +// more test cases to be needed diff --git a/test/UndirectedEdgeTest.cpp b/test/UndirectedEdgeTest.cpp index 6b73fcea0..4fa074961 100644 --- a/test/UndirectedEdgeTest.cpp +++ b/test/UndirectedEdgeTest.cpp @@ -1,6 +1,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(UndirectedEdgeTest, Constructor_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); diff --git a/test/UndirectedWeightedEdgeTest.cpp b/test/UndirectedWeightedEdgeTest.cpp index 2fd84e627..801c59fed 100644 --- a/test/UndirectedWeightedEdgeTest.cpp +++ b/test/UndirectedWeightedEdgeTest.cpp @@ -1,6 +1,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(UndirectedWeightedEdgeTest, Constructor_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); diff --git a/test/UnionFindTest.cpp b/test/UnionFindTest.cpp index 3c9bda644..3566f3b2c 100644 --- a/test/UnionFindTest.cpp +++ b/test/UnionFindTest.cpp @@ -1,6 +1,15 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +// Smart pointers alias +template +using unique = std::unique_ptr; +template +using shared= std::shared_ptr; + +using std::make_unique; +using std::make_shared; + TEST(UnionFindTest, setFindTest1) { CXXGraph::Node node0("0", 0); CXXGraph::Node node1("1", 1); @@ -20,7 +29,7 @@ TEST(UnionFindTest, setFindTest1) { // setUnion are functions belonging to graph class // can be removed if Subset becomes a class of its own CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); + edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); // every element is a subset of itself @@ -113,4 +122,4 @@ TEST(UnionFindTest, containsCycle) { edgeSet.insert(&edge2); containsCycle = graph.containsCycle(&edgeSet); ASSERT_EQ(containsCycle, true); -} \ No newline at end of file +} diff --git a/test/main.cpp b/test/main.cpp index 8ac875673..f8343a4f2 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -51,4 +51,4 @@ int main(int argc, char **argv) { << "." << CXXGraph_VERSION_MINOR << "." << CXXGraph_VERSION_PATCH << std::endl; return RUN_ALL_TESTS(); -} \ No newline at end of file +} From edc2a41e86f83ac352a5f54ec19cbd3d53935927 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Thu, 25 May 2023 12:05:54 +0200 Subject: [PATCH 04/28] Finish converting tests to smart pointers --- include/Partitioning/Partition.hpp | 8 +- test/GraphTest.cpp | 22 +++- test/MTXTest.cpp | 3 - test/PartitionTest.cpp | 108 +++++++++--------- test/PrimTest.cpp | 77 ++++++------- test/RWOutputTest.cpp | 164 ++++++++++++++-------------- test/TopologicalSortTest.cpp | 31 +++--- test/UndirectedEdgeTest.cpp | 12 +- test/UndirectedWeightedEdgeTest.cpp | 20 ++-- test/UnionFindTest.cpp | 38 ++++--- 10 files changed, 251 insertions(+), 232 deletions(-) diff --git a/include/Partitioning/Partition.hpp b/include/Partitioning/Partition.hpp index 2603a1a4f..0750504e8 100755 --- a/include/Partitioning/Partition.hpp +++ b/include/Partitioning/Partition.hpp @@ -337,16 +337,16 @@ std::ostream &operator<<(std::ostream &os, const Partition &partition) { (*it)->isDirected().value()) && ((*it)->isWeighted().has_value() && (*it)->isWeighted().value())) { - os << dynamic_cast &>(*it) << "\n"; + os << std::static_pointer_cast>(*it) << "\n"; } else if ((it->isDirected().has_value() && it->isDirected().value()) && !(it->isWeighted().has_value() && it->isWeighted().value())) { - os << dynamic_cast &>(*it) << "\n"; + os << std::static_pointer_cast>(*it) << "\n"; } else if (!(it->isDirected().has_value() && it->isDirected().value()) && (it->isWeighted().has_value() && it->isWeighted().value())) { - os << dynamic_cast &>(*it) << "\n"; + os << std::static_pointer_cast>(*it) << "\n"; } else if (!(it->isDirected().has_value() && it->isDirected().value()) && !(it->isWeighted().has_value() && it->isWeighted().value())) { - os << dynamic_cast &>(*it) << "\n"; + os << std::static_pointer_cast>(*it) << "\n"; } else { // Should never happens os << "Wrong Edge Class" diff --git a/test/GraphTest.cpp b/test/GraphTest.cpp index a844fcddf..24a6592ba 100644 --- a/test/GraphTest.cpp +++ b/test/GraphTest.cpp @@ -54,7 +54,7 @@ TEST(GraphTest, GetEdge_3) { &node1, &node2); CXXGraph::Edge edge(1, pairNode); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge); + edgeSet.insert(make_shared>(edge)); CXXGraph::Graph graph(edgeSet); ASSERT_EQ(*(graph.getEdge(1).value()), edge); } @@ -70,9 +70,13 @@ TEST(GraphTest, GetNodeSet_1) { CXXGraph::Graph graph(edgeSet); auto nodeSet = graph.getNodeSet(); ASSERT_EQ(nodeSet.size(), 2); - ASSERT_TRUE(std::find(nodeSet.begin(), nodeSet.end(), &node1) != + ASSERT_TRUE(std::find_if(nodeSet.begin(), nodeSet.end(), [&node1](auto node){ + return node->getUserId() == node1.getUserId(); + }) != nodeSet.end()); - ASSERT_TRUE(std::find(nodeSet.begin(), nodeSet.end(), &node2) != + ASSERT_TRUE(std::find_if(nodeSet.begin(), nodeSet.end(), [&node2](auto node){ + return node->getUserId() == node2.getUserId(); + }) != nodeSet.end()); } @@ -90,11 +94,17 @@ TEST(GraphTest, GetNodeSet_2) { CXXGraph::Graph graph(edgeSet); auto nodeSet = graph.getNodeSet(); ASSERT_EQ(nodeSet.size(), 3); - ASSERT_TRUE(std::find(nodeSet.begin(), nodeSet.end(), &node1) != + ASSERT_TRUE(std::find_if(nodeSet.begin(), nodeSet.end(), [&node1](auto node){ + return node->getUserId() == node1.getUserId(); + }) != nodeSet.end()); - ASSERT_TRUE(std::find(nodeSet.begin(), nodeSet.end(), &node2) != + ASSERT_TRUE(std::find_if(nodeSet.begin(), nodeSet.end(), [&node2](auto node){ + return node->getUserId() == node2.getUserId(); + }) != nodeSet.end()); - ASSERT_TRUE(std::find(nodeSet.begin(), nodeSet.end(), &node3) != + ASSERT_TRUE(std::find_if(nodeSet.begin(), nodeSet.end(), [&node3](auto node){ + return node->getUserId() == node3.getUserId(); + }) != nodeSet.end()); } diff --git a/test/MTXTest.cpp b/test/MTXTest.cpp index b5b78440d..89fbc1887 100644 --- a/test/MTXTest.cpp +++ b/test/MTXTest.cpp @@ -46,9 +46,6 @@ TEST(MTXTest, WriteToMtxUndirected) { CXXGraph::UndirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedEdge edge3(3, node3, node1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); edgeSet.insert(make_shared>(edge1)); edgeSet.insert(make_shared>(edge2)); edgeSet.insert(make_shared>(edge3)); diff --git a/test/PartitionTest.cpp b/test/PartitionTest.cpp index 4e06b5201..a1c18b181 100644 --- a/test/PartitionTest.cpp +++ b/test/PartitionTest.cpp @@ -1,3 +1,5 @@ +#include +#include #include "CXXGraph.hpp" #include "Utilities.hpp" #include "gtest/gtest.h" @@ -38,18 +40,18 @@ TEST(PartitionTest, test_1) { CXXGraph::UndirectedWeightedEdge edge11(11, node3, node6, 1); CXXGraph::UndirectedWeightedEdge edge12(12, node4, node8, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); CXXGraph::Graph graph(edgeSet); ASSERT_EQ(graph.getEdgeSet().size(), 12); auto partitionMap = graph.partitionGraph( @@ -70,7 +72,7 @@ TEST(PartitionTest, test_1) { TEST(PartitionTest, test_2) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(&(*e.second)); + graph.addEdge(make_shared>(*e.second)); } auto partitionMap = graph.partitionGraph( CXXGraph::Partitioning::PartitionAlgorithm::HDRF_ALG, 4, 1, 0.001); @@ -111,18 +113,18 @@ TEST(PartitionTest, test_3) { CXXGraph::UndirectedWeightedEdge edge11(11, node3, node6, 1); CXXGraph::UndirectedWeightedEdge edge12(12, node4, node8, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); CXXGraph::Graph graph(edgeSet); ASSERT_EQ(graph.getEdgeSet().size(), 12); auto partitionMap = graph.partitionGraph( @@ -143,7 +145,7 @@ TEST(PartitionTest, test_3) { TEST(PartitionTest, test_4) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(&(*e.second)); + graph.addEdge(make_shared>(*e.second)); } auto partitionMap = graph.partitionGraph( CXXGraph::Partitioning::PartitionAlgorithm::EDGEBALANCED_VC_ALG, 4); @@ -184,18 +186,18 @@ TEST(PartitionTest, test_5) { CXXGraph::UndirectedWeightedEdge edge11(11, node3, node6, 1); CXXGraph::UndirectedWeightedEdge edge12(12, node4, node8, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); CXXGraph::Graph graph(edgeSet); ASSERT_EQ(graph.getEdgeSet().size(), 12); auto partitionMap = graph.partitionGraph( @@ -216,7 +218,7 @@ TEST(PartitionTest, test_5) { TEST(PartitionTest, test_6) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(&(*e.second)); + graph.addEdge(make_shared>(*e.second)); } auto partitionMap = graph.partitionGraph( CXXGraph::Partitioning::PartitionAlgorithm::GREEDY_VC_ALG, 4); @@ -257,18 +259,18 @@ TEST(PartitionTest, test_7) { CXXGraph::UndirectedWeightedEdge edge11(11, node3, node6, 1); CXXGraph::UndirectedWeightedEdge edge12(12, node4, node8, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); CXXGraph::Graph graph(edgeSet); ASSERT_EQ(graph.getEdgeSet().size(), 12); auto partitionMap = graph.partitionGraph( @@ -289,7 +291,7 @@ TEST(PartitionTest, test_7) { TEST(PartitionTest, test_8) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(&(*e.second)); + graph.addEdge(make_shared>(*e.second)); } auto partitionMap = graph.partitionGraph( CXXGraph::Partitioning::PartitionAlgorithm::EBV_ALG, 4, 1, 1); @@ -309,7 +311,7 @@ TEST(PartitionTest, test_8) { TEST(PartitionTest, test_9) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(&(*e.second)); + graph.addEdge(make_shared>(*e.second)); } auto partitionMap = graph.partitionGraph(CXXGraph::Partitioning::PartitionAlgorithm::WB_LIBRA, diff --git a/test/PrimTest.cpp b/test/PrimTest.cpp index bc84e441f..1dbf74bb2 100644 --- a/test/PrimTest.cpp +++ b/test/PrimTest.cpp @@ -1,3 +1,4 @@ +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" @@ -44,20 +45,20 @@ TEST(PrimTest, test_1) { CXXGraph::UndirectedWeightedEdge edge14(3, node5, node4, 10); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); - edgeSet.insert(&edge13); - edgeSet.insert(&edge14); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + edgeSet.insert(make_shared>(edge13)); + edgeSet.insert(make_shared>(edge14)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.prim(); @@ -90,15 +91,15 @@ TEST(PrimTest, test_2) { CXXGraph::UndirectedWeightedEdge edge9(9, node4, node3, 12); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.prim(); @@ -133,18 +134,18 @@ TEST(PrimTest, test_3) { CXXGraph::UndirectedWeightedEdge edge12(9, node6, node7, 12); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); - edgeSet.insert(&edge12); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.prim(); @@ -162,8 +163,8 @@ TEST(PrimTest, test_4) { CXXGraph::DirectedWeightedEdge edge1(1, node1, node2, 1); CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); CXXGraph::Graph graph(edgeSet); CXXGraph::MstResult res = graph.prim(); @@ -172,7 +173,7 @@ TEST(PrimTest, test_4) { CXXGraph::UndirectedEdge edge3(3, node1, node2); CXXGraph::T_EdgeSet edgeSet1; - edgeSet1.insert(&edge3); + edgeSet1.insert(make_shared>(edge3)); CXXGraph::Graph graph1(edgeSet1); res = graph1.prim(); diff --git a/test/RWOutputTest.cpp b/test/RWOutputTest.cpp index 3a1f1d920..e17baa222 100644 --- a/test/RWOutputTest.cpp +++ b/test/RWOutputTest.cpp @@ -1,4 +1,6 @@ +#include #include +#include #include "CXXGraph.hpp" #include "gtest/gtest.h" @@ -66,9 +68,9 @@ TEST(RWOutputTest, test_1) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(); ASSERT_EQ(res, 0); @@ -90,9 +92,9 @@ TEST(RWOutputTest, test_2) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 4); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV); ASSERT_EQ(res, 0); @@ -114,9 +116,9 @@ TEST(RWOutputTest, test_3) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_3"); @@ -136,8 +138,8 @@ TEST(RWOutputTest, test_4) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::OUT_1, "test_4"); ASSERT_EQ(res, -1); @@ -153,8 +155,8 @@ TEST(RWOutputTest, test_5) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_5", false, true, true); @@ -178,8 +180,8 @@ TEST(RWOutputTest, test_6) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_6", false, false, true); @@ -201,8 +203,8 @@ TEST(RWOutputTest, test_7) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_7", false, true, false); @@ -227,9 +229,9 @@ TEST(RWOutputTest, test_8) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_8"); @@ -296,9 +298,9 @@ TEST(RWOutputTest, test_9) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_9", false, true); @@ -364,9 +366,9 @@ TEST(RWOutputTest, test_10) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_10", false, true, true); @@ -390,7 +392,7 @@ TEST(RWOutputTest, test_10) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 5); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); @@ -415,7 +417,7 @@ TEST(RWOutputTest, test_10) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 6); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); @@ -448,9 +450,9 @@ TEST(RWOutputTest, test_11) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(); ASSERT_EQ(res, 0); @@ -473,9 +475,9 @@ TEST(RWOutputTest, test_12) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 4); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV); ASSERT_EQ(res, 0); @@ -497,9 +499,9 @@ TEST(RWOutputTest, test_13) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_13"); @@ -519,8 +521,8 @@ TEST(RWOutputTest, test_14) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::OUT_1, "test_14"); ASSERT_EQ(res, -1); @@ -536,8 +538,8 @@ TEST(RWOutputTest, test_15) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_15", false, true, true); @@ -561,8 +563,8 @@ TEST(RWOutputTest, test_16) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_16", false, false, true); @@ -584,8 +586,8 @@ TEST(RWOutputTest, test_17) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_17", false, true, false); @@ -610,9 +612,9 @@ TEST(RWOutputTest, test_18) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_18"); @@ -679,9 +681,9 @@ TEST(RWOutputTest, test_19) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_19", false, true); @@ -747,9 +749,9 @@ TEST(RWOutputTest, test_20) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_20", false, true, true); @@ -773,7 +775,7 @@ TEST(RWOutputTest, test_20) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 5); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); @@ -798,7 +800,7 @@ TEST(RWOutputTest, test_20) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 6); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); @@ -826,8 +828,8 @@ TEST(RWOutputTest, test_21) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_21", true, true, false); @@ -855,9 +857,9 @@ TEST(RWOutputTest, test_22) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_22", true, true, true); @@ -886,7 +888,7 @@ TEST(RWOutputTest, test_22) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 5); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); @@ -911,7 +913,7 @@ TEST(RWOutputTest, test_22) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 6); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); @@ -942,8 +944,8 @@ TEST(RWOutputTest, test_23) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_CSV, ".", "test_23", true, false, false); @@ -966,8 +968,8 @@ TEST(RWOutputTest, test_24) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_24", true, true, false); @@ -995,9 +997,9 @@ TEST(RWOutputTest, test_25) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_25", true, true, true); @@ -1026,7 +1028,7 @@ TEST(RWOutputTest, test_25) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 5); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); @@ -1051,7 +1053,7 @@ TEST(RWOutputTest, test_25) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 6); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); @@ -1082,8 +1084,8 @@ TEST(RWOutputTest, test_26) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_26", true, false, false); @@ -1102,7 +1104,7 @@ TEST(RWOutputTest, test_26) { TEST(RWOutputTest, test_27) { CXXGraph::T_EdgeSet edgeSet; for (const auto &edge : edges) { - edgeSet.insert(edge.second); + edgeSet.insert(make_shared>(*(edge.second))); } CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", @@ -1131,7 +1133,7 @@ TEST(RWOutputTest, test_28) { CXXGraph::Node node2("2", 2); CXXGraph::DirectedWeightedEdge edge1(2, node1, node2, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); + edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); // force error by writing to a non-existent directory int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, @@ -1162,9 +1164,9 @@ TEST(RWOutputTest, test_29) { CXXGraph::DirectedEdge edge2(2, node2, node3); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); int res = graph.writeToFile(CXXGraph::InputOutputFormat::STANDARD_TSV, ".", "test_29", true, true, true); @@ -1193,7 +1195,7 @@ TEST(RWOutputTest, test_29) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 5); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); @@ -1218,7 +1220,7 @@ TEST(RWOutputTest, test_29) { ASSERT_TRUE(readEdgeIt->isWeighted().has_value() && readEdgeIt->isWeighted().value()); ASSERT_EQ( - (dynamic_cast(readEdgeIt))->getWeight(), + (dynamic_cast(readEdgeIt.get()))->getWeight(), 6); ASSERT_EQ(readEdgeIt->getNodePair().first->getUserId(), node1.getUserId()); diff --git a/test/TopologicalSortTest.cpp b/test/TopologicalSortTest.cpp index 6e289e036..92d35dabb 100644 --- a/test/TopologicalSortTest.cpp +++ b/test/TopologicalSortTest.cpp @@ -1,3 +1,4 @@ +#include #include "CXXGraph.hpp" #include "Utility/ConstString.hpp" #include "Utility/Typedef.hpp" @@ -23,9 +24,9 @@ TEST(TopologicalSortTest, test_1) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 2); CXXGraph::DirectedWeightedEdge edge3(3, node3, node1, 3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::TopoSortResult &&res = graph.topologicalSort(); @@ -47,10 +48,10 @@ TEST(TopologicalSortTest, test_2) { CXXGraph::UndirectedWeightedEdge edge3(3, node3, node4, 1); CXXGraph::UndirectedWeightedEdge edge4(3, node3, node5, 1); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); CXXGraph::TopoSortResult &&res = graph.topologicalSort(); @@ -79,14 +80,14 @@ TEST(TopologicalSortTest, test_3) { CXXGraph::DirectedWeightedEdge edge7(3, node8, node6, 6); CXXGraph::DirectedWeightedEdge edge8(3, node8, node7, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); - edgeSet.insert(&edge8); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); CXXGraph::Graph graph(edgeSet); CXXGraph::TopoSortResult &&res = graph.topologicalSort(); diff --git a/test/UndirectedEdgeTest.cpp b/test/UndirectedEdgeTest.cpp index 4fa074961..aaea103a3 100644 --- a/test/UndirectedEdgeTest.cpp +++ b/test/UndirectedEdgeTest.cpp @@ -24,7 +24,7 @@ TEST(UndirectedEdgeTest, Constructor_2) { std::pair *, const CXXGraph::Node *> pairNode( &node1, &node2); CXXGraph::UndirectedEdge edge(1, pairNode); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); } @@ -54,7 +54,7 @@ TEST(UndirectedEdgeTest, Constructor_5) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::UndirectedEdge edge(base_edge); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); @@ -77,7 +77,7 @@ TEST(UndirectedEdgeTest, Bool_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::UndirectedEdge edge2(2, pairNode); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); ASSERT_FALSE(edge2.isDirected().value()); @@ -100,7 +100,7 @@ TEST(UndirectedEdgeTest, String_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::UndirectedEdge edge2(2, pairNode); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); ASSERT_FALSE(edge2.isDirected().value()); @@ -114,7 +114,7 @@ TEST(UndirectedEdgeTest, Cast_1) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::UndirectedEdge edge = base_edge; - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); @@ -129,7 +129,7 @@ TEST(UndirectedEdgeTest, Cast_2) { CXXGraph::DirectedEdge base_edge(1, pairNode); CXXGraph::UndirectedEdge edge = base_edge; ASSERT_TRUE(base_edge.isDirected().value()); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); diff --git a/test/UndirectedWeightedEdgeTest.cpp b/test/UndirectedWeightedEdgeTest.cpp index 801c59fed..53eb8c716 100644 --- a/test/UndirectedWeightedEdgeTest.cpp +++ b/test/UndirectedWeightedEdgeTest.cpp @@ -25,7 +25,7 @@ TEST(UndirectedWeightedEdgeTest, Constructor_2) { std::pair *, const CXXGraph::Node *> pairNode( &node1, &node2); CXXGraph::UndirectedWeightedEdge edge(1, pairNode, 10); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_EQ(edge.getWeight(), 10); @@ -56,7 +56,7 @@ TEST(UndirectedWeightedEdgeTest, Constructor_5) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::UndirectedWeightedEdge edge(base_edge, 10); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); @@ -71,7 +71,7 @@ TEST(UndirectedWeightedEdgeTest, Constructor_6) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::UndirectedWeightedEdge edge(base_edge); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); @@ -86,7 +86,7 @@ TEST(UndirectedWeightedEdgeTest, Constructor_7) { &node1, &node2); CXXGraph::UndirectedEdge base_edge(1, pairNode); CXXGraph::UndirectedWeightedEdge edge(base_edge); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); @@ -110,7 +110,7 @@ TEST(UndirectedWeightedEdgeTest, Bool_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::UndirectedWeightedEdge edge2(2, pairNode, 7); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); ASSERT_FALSE(edge2.isDirected().value()); @@ -133,7 +133,7 @@ TEST(UndirectedWeightedEdgeTest, String_data) { std::pair *, const CXXGraph::Node *> pairNode( &node3, &node4); CXXGraph::UndirectedWeightedEdge edge2(2, pairNode, 5); - ASSERT_EQ(edge2.getNodePair(), pairNode); + /* ASSERT_EQ(edge2.getNodePair(), pairNode); */ ASSERT_EQ(*(edge2.getNodePair().first), node3); ASSERT_EQ(*(edge2.getNodePair().second), node4); ASSERT_FALSE(edge2.isDirected().value()); @@ -147,7 +147,7 @@ TEST(UndirectedWeightedEdgeTest, Cast_1) { &node1, &node2); CXXGraph::Edge base_edge(1, pairNode); CXXGraph::UndirectedWeightedEdge edge = base_edge; - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); @@ -163,7 +163,7 @@ TEST(UndirectedWeightedEdgeTest, Cast_2) { CXXGraph::UndirectedEdge base_edge(1, pairNode); CXXGraph::UndirectedWeightedEdge edge = base_edge; ASSERT_FALSE(base_edge.isDirected().value()); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); @@ -179,7 +179,7 @@ TEST(UndirectedWeightedEdgeTest, Cast_3) { CXXGraph::DirectedEdge base_edge(1, pairNode); CXXGraph::UndirectedWeightedEdge edge = base_edge; ASSERT_TRUE(base_edge.isDirected().value()); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); @@ -196,7 +196,7 @@ TEST(UndirectedWeightedEdgeTest, Cast_4) { CXXGraph::UndirectedWeightedEdge edge = base_edge; ASSERT_TRUE(base_edge.isDirected().value()); ASSERT_TRUE(base_edge.isWeighted().value()); - ASSERT_EQ(edge.getNodePair(), pairNode); + /* ASSERT_EQ(edge.getNodePair(), pairNode); */ ASSERT_EQ(*(edge.getNodePair().first), node1); ASSERT_EQ(*(edge.getNodePair().second), node2); ASSERT_FALSE(edge.isDirected().value()); diff --git a/test/UnionFindTest.cpp b/test/UnionFindTest.cpp index 3566f3b2c..5999cd5ad 100644 --- a/test/UnionFindTest.cpp +++ b/test/UnionFindTest.cpp @@ -34,15 +34,16 @@ TEST(UnionFindTest, setFindTest1) { // every element is a subset of itself std::unordered_map subset; + auto subset_ptr = make_shared>(subset); // {{0, 0}, {1, 0}, {2, 0}, {3, 0}}; CXXGraph::Subset set1{0, 0}, set2{1, 0}, set3{2, 0}, set4{3, 0}; subset = {{0, set1}, {1, set2}, {2, set3}, {3, set4}}; - auto res = graph.setFind(&subset, std::stoi(node0.getUserId())); + auto res = graph.setFind(subset_ptr, std::stoi(node0.getUserId())); ASSERT_EQ(res, 0); - res = graph.setFind(&subset, std::stoi(node1.getUserId())); + res = graph.setFind(subset_ptr, std::stoi(node1.getUserId())); ASSERT_EQ(res, 1); - res = graph.setFind(&subset, std::stoi(node2.getUserId())); + res = graph.setFind(subset_ptr, std::stoi(node2.getUserId())); ASSERT_EQ(res, 2); } @@ -54,19 +55,21 @@ TEST(UnionFindTest, setFindTest2) { // element 2 & 4 are subset of 0 // element 4 is subset of 1 std::unordered_map subset; + auto subset_ptr = make_shared>(subset); + CXXGraph::Subset set1{0, 0}, set2{0, 0}, set3{0, 0}, set4{1, 0}; subset = {{0, set1}, {1, set2}, {2, set3}, {3, set4}}; CXXGraph::UndirectedWeightedEdge edge1(0, node0, node1, 5); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); + edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); - auto res = graph.setFind(&subset, std::stoi(node0.getUserId())); + auto res = graph.setFind(subset_ptr, std::stoi(node0.getUserId())); ASSERT_EQ(res, 0); - res = graph.setFind(&subset, std::stoi(node3.getUserId())); + res = graph.setFind(subset_ptr, std::stoi(node3.getUserId())); ASSERT_EQ(res, 0); - res = graph.setFind(&subset, std::stoi(node2.getUserId())); + res = graph.setFind(subset_ptr, std::stoi(node2.getUserId())); ASSERT_EQ(res, 0); } @@ -77,6 +80,8 @@ TEST(UnionFindTest, setUnionTest3) { CXXGraph::Node node3("3", 3); // union of (node 1 & node3) should increase node0 rank by 1 std::unordered_map subset; + auto subset_ptr = make_shared>(subset); + CXXGraph::Subset set1{0, 0}, set2{0, 0}, set3{0, 0}, set4{1, 0}; subset = {{0, set1}, {1, set2}, {2, set3}, {3, set4}}; @@ -85,16 +90,16 @@ TEST(UnionFindTest, setUnionTest3) { // can be removed if Subset becomes a class of its own CXXGraph::UndirectedWeightedEdge edge1(0, node0, node1, 5); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); + edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); - graph.setUnion(&subset, std::stoi(node1.getUserId()), + graph.setUnion(subset_ptr, std::stoi(node1.getUserId()), std::stoi(node3.getUserId())); ASSERT_EQ(subset[0].rank, 1); ASSERT_EQ(subset[3].parent, 0); // rank shouldn't increase since both have the same parent - graph.setUnion(&subset, std::stoi(node2.getUserId()), + graph.setUnion(subset_ptr, std::stoi(node2.getUserId()), std::stoi(node3.getUserId())); ASSERT_EQ(subset[0].rank, 1); ASSERT_EQ(subset[3].parent, 0); @@ -106,20 +111,21 @@ TEST(UnionFindTest, containsCycle) { CXXGraph::Node node2("2", 2); CXXGraph::T_EdgeSet edgeSet; + auto edgeSet_ptr = make_shared>(edgeSet); CXXGraph::UndirectedWeightedEdge edge0(0, node0, node1, 5); - edgeSet.insert(&edge0); + edgeSet.insert(make_shared>(edge0)); CXXGraph::Graph graph(edgeSet); - bool containsCycle = graph.containsCycle(&edgeSet); + bool containsCycle = graph.containsCycle(edgeSet_ptr); ASSERT_EQ(containsCycle, false); CXXGraph::UndirectedWeightedEdge edge1(1, node1, node2, 10); - edgeSet.insert(&edge1); - containsCycle = graph.containsCycle(&edgeSet); + edgeSet.insert(make_shared>(edge1)); + containsCycle = graph.containsCycle(edgeSet_ptr); ASSERT_EQ(containsCycle, false); CXXGraph::UndirectedWeightedEdge edge2(2, node2, node0, 5); - edgeSet.insert(&edge2); - containsCycle = graph.containsCycle(&edgeSet); + edgeSet.insert(make_shared>(edge2)); + containsCycle = graph.containsCycle(edgeSet_ptr); ASSERT_EQ(containsCycle, true); } From 74805e50285e8e71d040d8306d9a2a2f0cbee7ec Mon Sep 17 00:00:00 2001 From: sbaldu Date: Fri, 26 May 2023 09:10:08 +0200 Subject: [PATCH 05/28] Add setter for nodes in Edge nodePair --- include/Edge/Edge.hpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/Edge/Edge.hpp b/include/Edge/Edge.hpp index 08457d32d..28ce4cdb1 100755 --- a/include/Edge/Edge.hpp +++ b/include/Edge/Edge.hpp @@ -57,6 +57,8 @@ class Edge { Edge(const unsigned long long id, const std::pair>, shared>> &nodepair); virtual ~Edge() = default; + void setFirstNode(shared> node); + void setSecondNode(shared> node); const unsigned long long &getId() const; const std::pair>, shared>> &getNodePair() const; virtual const std::optional isDirected() const; @@ -101,6 +103,18 @@ Edge::Edge(const unsigned long long id, this->id = id; } +template +void Edge::setFirstNode(shared> node) { + /* this->nodePair = std::make_pair(node, this->nodePair.second); */ + this->nodePair.first = node; +} + +template +void Edge::setSecondNode(shared> node) { + /* this->nodePair = std::make_pair(this->nodePair.first, node); */ + this->nodePair.second = node; +} + template const unsigned long long &Edge::getId() const { return id; From 12b1c22cd86afeb5db8bf3ad9dcdad9932787075 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Fri, 26 May 2023 09:12:22 +0200 Subject: [PATCH 06/28] Fix addition of edges and construction of adjacency matrix --- include/Graph/Graph.hpp | 410 ++++++++++++++++++++++++---------------- 1 file changed, 250 insertions(+), 160 deletions(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index adef7a629..1f4844992 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -74,10 +74,10 @@ namespace CXXGraph { template using unique = std::unique_ptr; template -using shared= std::shared_ptr; +using shared = std::shared_ptr; -using std::make_unique; using std::make_shared; +using std::make_unique; template using T_EdgeSet = std::unordered_set>>; @@ -595,16 +595,45 @@ class Graph { template Graph::Graph(const T_EdgeSet &edgeSet) { - for (const auto &edgeSetIt : edgeSet) { - /* + for (auto edgeSetIt : edgeSet) { + auto edgeIt = std::const_pointer_cast>(edgeSetIt); + auto nodeset = this->getNodeSet(); + // If the edge was already in the set, I don't add it again if (std::find_if(this->edgeSet.begin(), this->edgeSet.end(), - [edgeSetIt](const Edge *edge) { return (*edge == *edgeSetIt); }) == - this->edgeSet.end()) - { - this->edgeSet.insert(edgeSetIt); + [edgeIt](auto edge_inSet) { + return (edge_inSet->getNodePair().first->getUserId() == + edgeIt->getNodePair().first->getUserId() && + edge_inSet->getNodePair().second->getUserId() == + edgeIt->getNodePair().second->getUserId()); + }) != this->edgeSet.end()) { + continue; + } + + // Check that the nodes pointed by the pointers in 'edge' have not already + // been added in other edges + // + // We don't pointers pointing to different memory regions which represent + // the same nodes + shared> node1; + auto node1It = + std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { + return node_inSet->getUserId() == + edgeIt->getNodePair().first->getUserId(); + }); + if (node1It != nodeset.end()) { + edgeIt->setFirstNode(*node1It); + } + shared> node2; + auto node2It = + std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { + return node_inSet->getUserId() == + edgeIt->getNodePair().second->getUserId(); + }); + if (node2It != nodeset.end()) { + edgeIt->setSecondNode(*node2It); } - */ - this->edgeSet.insert(edgeSetIt); + + this->edgeSet.insert(edgeIt); } } @@ -616,54 +645,88 @@ const T_EdgeSet &Graph::getEdgeSet() const { template void Graph::setEdgeSet(const T_EdgeSet &edgeSet) { this->edgeSet.clear(); - for (const auto &edgeSetIt : edgeSet) { - /* + for (auto edgeSetIt : edgeSet) { + auto edgeIt = std::const_pointer_cast>(edgeSetIt); + auto nodeset = this->getNodeSet(); + // If the edge was already in the set, I don't add it again if (std::find_if(this->edgeSet.begin(), this->edgeSet.end(), - [edgeSetIt](const Edge *edge) { return (*edge == *edgeSetIt); }) == - this->edgeSet.end()) - { - this->edgeSet.insert(edgeSetIt); + [edgeIt](auto edge_inSet) { + return (edge_inSet->getNodePair().first->getUserId() == + edgeIt->getNodePair().first->getUserId() && + edge_inSet->getNodePair().second->getUserId() == + edgeIt->getNodePair().second->getUserId()); + }) != this->edgeSet.end()) { + continue; + } + + // Check that the nodes pointed by the pointers in 'edge' have not already + // been added in other edges + // + // We don't pointers pointing to different memory regions which represent + // the same nodes + shared> node1; + auto node1It = + std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { + return node_inSet->getUserId() == + edgeIt->getNodePair().first->getUserId(); + }); + if (node1It != nodeset.end()) { + edgeIt->setFirstNode(*node1It); + } + shared> node2; + auto node2It = + std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { + return node_inSet->getUserId() == + edgeIt->getNodePair().second->getUserId(); + }); + if (node2It != nodeset.end()) { + edgeIt->setSecondNode(*node2It); } - */ - this->edgeSet.insert(edgeSetIt); + + this->edgeSet.insert(edgeIt); } } template void Graph::addEdge(shared> edge) { + auto edgeIt = std::const_pointer_cast>(edge); + auto nodeset = this->getNodeSet(); // If the edge was already in the set, I don't add it again - if (std::find_if(edgeSet.begin(), edgeSet.end(), [edge](auto edge_inSet){ - return (edge_inSet->getNodePair().first->getUserId() == edge->getNodePair().first->getUserId() && - edge_inSet->getNodePair().second->getUserId() == edge->getNodePair().second->getUserId()); - }) != edgeSet.end()) { + if (std::find_if(this->edgeSet.begin(), this->edgeSet.end(), + [edgeIt](auto edge_inSet) { + return (edge_inSet->getNodePair().first->getUserId() == + edgeIt->getNodePair().first->getUserId() && + edge_inSet->getNodePair().second->getUserId() == + edgeIt->getNodePair().second->getUserId()); + }) != this->edgeSet.end()) { return; } - // Check that the nodes pointed by the pointers in 'edge' have not already been - // added in other edges + // Check that the nodes pointed by the pointers in 'edge' have not already + // been added in other edges // - // We don't pointers pointing to different memory regions which represent the - // same nodes + // We don't pointers pointing to different memory regions which represent + // the same nodes shared> node1; - auto edgeIt1 = std::find_if(edgeSet.begin(), edgeSet.end(), [edge](auto edge_inSet){ - return edge_inSet->getNodePair().first->getUserId() == edge->getNodePair().first->getUserId(); - }); - if (edgeIt1 == edgeSet.end()) { - node1 = edge->getNodePair().first; - } else { - node1 = (*edgeIt1)->getNodePair().first; + auto node1It = + std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { + return node_inSet->getUserId() == + edgeIt->getNodePair().first->getUserId(); + }); + if (node1It != nodeset.end()) { + edgeIt->setFirstNode(*node1It); } shared> node2; - auto edgeIt2 = std::find_if(edgeSet.begin(), edgeSet.end(), [edge](auto edge_inSet){ - return edge_inSet->getNodePair().second->getUserId() == edge->getNodePair().second->getUserId(); - }); - if (edgeIt2 == edgeSet.end()) { - node2 = edge->getNodePair().second; - } else { - node2 = (*edgeIt2)->getNodePair().second; + auto node2It = + std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { + return node_inSet->getUserId() == + edgeIt->getNodePair().second->getUserId(); + }); + if (node2It != nodeset.end()) { + edgeIt->setSecondNode(*node2It); } - shared> newEdge = make_shared>(edge->getId(), node1, node2); - edgeSet.insert(newEdge); + + this->edgeSet.insert(edgeIt); } template @@ -707,9 +770,9 @@ const std::set>> Graph::getNodeSet() const { template void Graph::setNodeData(const std::string &nodeUserId, T data) { auto nodeset = this->nodeSet(); - auto nodeIt = std::find_if(nodeset.begin(), nodeset.end(), [&nodeUserId](auto node){ - return node->getUserId() == nodeUserId; - }); + auto nodeIt = std::find_if( + nodeset.begin(), nodeset.end(), + [&nodeUserId](auto node) { return node->getUserId() == nodeUserId; }); (*nodeIt)->setData(std::move(data)); } @@ -737,8 +800,10 @@ template std::set>> Graph::nodeSet() { std::set>> nodeSet; for (auto &edgeSetIt : edgeSet) { - nodeSet.insert(std::const_pointer_cast>(edgeSetIt->getNodePair().first)); - nodeSet.insert(std::const_pointer_cast>(edgeSetIt->getNodePair().second)); + nodeSet.insert( + std::const_pointer_cast>(edgeSetIt->getNodePair().first)); + nodeSet.insert( + std::const_pointer_cast>(edgeSetIt->getNodePair().second)); } return nodeSet; @@ -784,7 +849,8 @@ int Graph::writeToDot(const std::string &workingDir, for (auto const &edgePtr : edgeSet) { std::string edgeLine = ""; if (edgePtr->isDirected().has_value() && edgePtr->isDirected().value()) { - auto directedPtr = std::static_pointer_cast>(edgePtr); + auto directedPtr = + std::static_pointer_cast>(edgePtr); edgeLine += '\t' + directedPtr->getFrom().getUserId() + ' '; edgeLine += directedLinkSymbol + ' '; edgeLine += directedPtr->getTo().getUserId(); @@ -795,10 +861,11 @@ int Graph::writeToDot(const std::string &workingDir, } if (edgePtr->isWeighted().has_value() && edgePtr->isWeighted().value()) { // Weights in dot files must be integers - edgeLine += " [weight=" + - std::to_string(static_cast( - dynamic_cast(edgePtr.get())->getWeight())) + - ']'; + edgeLine += + " [weight=" + + std::to_string(static_cast( + dynamic_cast(edgePtr.get())->getWeight())) + + ']'; } edgeLine += ";\n"; ofileGraph << edgeLine; @@ -1021,8 +1088,8 @@ void Graph::recreateGraph( if (edgeWeightMap.find(edgeIt.first) != edgeWeightMap.end()) { if (edgeDirectedMap.find(edgeIt.first) != edgeDirectedMap.end() && edgeDirectedMap.at(edgeIt.first)) { - auto edge = make_shared>(edgeIt.first, node1, node2, - edgeWeightMap.at(edgeIt.first)); + auto edge = make_shared>( + edgeIt.first, node1, node2, edgeWeightMap.at(edgeIt.first)); addEdge(edge); } else { auto edge = make_shared>( @@ -1121,9 +1188,9 @@ unsigned long long Graph::setFind( } template -void Graph::setUnion(shared> subsets, - const unsigned long long elem1, - const unsigned long long elem2) const { +void Graph::setUnion( + shared> subsets, + const unsigned long long elem1, const unsigned long long elem2) const { // if both sets have same parent // then there's nothing to be done if ((*subsets)[elem1].parent == (*subsets)[elem2].parent) return; @@ -1173,7 +1240,8 @@ const std::shared_ptr> Graph::getAdjMatrix() const { auto addElementToAdjMatrix = [&adj](shared> nodeFrom, shared> nodeTo, shared> edge) { - std::pair>, shared>> elem = {nodeTo, edge}; + std::pair>, shared>> elem = {nodeTo, + edge}; (*adj)[nodeFrom].push_back(std::move(elem)); }; for (const auto &edgeSetIt : edgeSet) { @@ -1182,19 +1250,16 @@ const std::shared_ptr> Graph::getAdjMatrix() const { shared> d_edge = std::static_pointer_cast>(edgeSetIt); addElementToAdjMatrix(d_edge->getNodePair().first, - d_edge->getNodePair().second, - d_edge); + d_edge->getNodePair().second, d_edge); } else if (edgeSetIt->isDirected().has_value() && !edgeSetIt->isDirected().value()) { shared> ud_edge = std::static_pointer_cast>(edgeSetIt); ; addElementToAdjMatrix(ud_edge->getNodePair().first, - ud_edge->getNodePair().second, - ud_edge); - addElementToAdjMatrix(ud_edge->getNodePair().first, - ud_edge->getNodePair().second, - ud_edge); + ud_edge->getNodePair().second, ud_edge); + addElementToAdjMatrix(ud_edge->getNodePair().second, + ud_edge->getNodePair().first, ud_edge); } else { // is a simple edge we cannot create adj matrix return adj; } @@ -1207,17 +1272,17 @@ const DijkstraResult Graph::dijkstra(const Node &source, const Node &target) const { DijkstraResult result; auto nodeSet = Graph::getNodeSet(); - auto source_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ - return node->getUserId() == source.getUserId(); - }); + auto source_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&source](auto node) { return node->getUserId() == source.getUserId(); }); if (source_node_it == nodeSet.end()) { // check if source node exist in the graph result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH; return result; } - auto target_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&target](auto node){ - return node->getUserId() == target.getUserId(); - }); + auto target_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&target](auto node) { return node->getUserId() == target.getUserId(); }); if (target_node_it == nodeSet.end()) { // check if target node exist in the graph result.errorMessage = ERR_TARGET_NODE_NOT_IN_GRAPH; @@ -1267,7 +1332,8 @@ const DijkstraResult Graph::dijkstra(const Node &source, if (elem.second->isDirected().has_value() && elem.second->isDirected().value()) { shared> dw_edge = - std::static_pointer_cast>(elem.second); + std::static_pointer_cast>( + elem.second); if (dw_edge->getWeight() < 0) { result.errorMessage = ERR_NEGATIVE_WEIGHTED_EDGE; return result; @@ -1278,7 +1344,8 @@ const DijkstraResult Graph::dijkstra(const Node &source, } else if (elem.second->isDirected().has_value() && !elem.second->isDirected().value()) { shared> udw_edge = - std::static_pointer_cast>(elem.second); + std::static_pointer_cast>( + elem.second); if (udw_edge->getWeight() < 0) { result.errorMessage = ERR_NEGATIVE_WEIGHTED_EDGE; return result; @@ -1318,17 +1385,17 @@ const BellmanFordResult Graph::bellmanford(const Node &source, result.errorMessage = ""; result.result = INF_DOUBLE; auto nodeSet = Graph::getNodeSet(); - auto source_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ - return node->getUserId() == source.getUserId(); - }); + auto source_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&source](auto node) { return node->getUserId() == source.getUserId(); }); if (source_node_it == nodeSet.end()) { // check if source node exist in the graph result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH; return result; } - auto target_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&target](auto node){ - return node->getUserId() == target.getUserId(); - }); + auto target_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&target](auto node) { return node->getUserId() == target.getUserId(); }); if (target_node_it == nodeSet.end()) { // check if target node exist in the graph result.errorMessage = ERR_TARGET_NODE_NOT_IN_GRAPH; @@ -1356,7 +1423,8 @@ const BellmanFordResult Graph::bellmanford(const Node &source, for (const auto &edge : edgeSet) { auto elem = edge->getNodePair(); if (edge->isWeighted().has_value() && edge->isWeighted().value()) { - auto edge_weight = (dynamic_cast(edge.get()))->getWeight(); + auto edge_weight = + (dynamic_cast(edge.get()))->getWeight(); if (dist[elem.first] + edge_weight < dist[elem.second]) dist[elem.second] = dist[elem.first] + edge_weight; } else { @@ -1386,7 +1454,8 @@ const BellmanFordResult Graph::bellmanford(const Node &source, auto edgeSet = Graph::getEdgeSet(); for (const auto &edge : edgeSet) { auto elem = edge->getNodePair(); - auto edge_weight = (dynamic_cast(edge.get()))->getWeight(); + auto edge_weight = + (dynamic_cast(edge.get()))->getWeight(); if (dist[elem.first] + edge_weight < dist[elem.second]) { result.success = true; result.negativeCycle = true; @@ -1435,7 +1504,8 @@ const FWResult Graph::floydWarshall() const { for (const auto &edge : edgeSet) { const auto &elem = edge->getNodePair(); if (edge->isWeighted().has_value() && edge->isWeighted().value()) { - auto edgeWeight = (dynamic_cast(edge.get()))->getWeight(); + auto edgeWeight = + (dynamic_cast(edge.get()))->getWeight(); auto key = std::make_pair(elem.first->getUserId(), elem.second->getUserId()); pairwise_dist[key] = edgeWeight; @@ -1545,7 +1615,8 @@ const MstResult Graph::prim() const { if (elem.second->isWeighted().has_value() && elem.second->isWeighted().value()) { shared> udw_edge = - std::static_pointer_cast>(elem.second); + std::static_pointer_cast>( + elem.second); if ((udw_edge->getWeight() < dist[elem.first]) && (std::find(doneNode.begin(), doneNode.end(), elem.first->getId()) == doneNode.end())) { @@ -1579,7 +1650,7 @@ const MstResult Graph::boruvka() const { const auto n = nodeSet.size(); // Use std map for storing n subsets. - shared> subsets; + auto subsets = make_shared>(); // Initially there are n different trees. // Finally there will be one tree that will be MST @@ -1691,7 +1762,7 @@ const MstResult Graph::kruskal() const { } } - shared> subset; + auto subset = make_shared>(); for (const auto &node : nodeSet) { Subset set{node->getId(), 0}; @@ -1722,17 +1793,17 @@ BestFirstSearchResult Graph::best_first_search( auto &nodeSet = Graph::getNodeSet(); using pq_type = std::pair>>; - auto source_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ - return node->getUserId() == source.getUserId(); - }); + auto source_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&source](auto node) { return node->getUserId() == source.getUserId(); }); if (source_node_it == nodeSet.end()) { result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH; return result; } - auto target_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&target](auto node){ - return node->getUserId() == target.getUserId(); - }); + auto target_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&target](auto node) { return node->getUserId() == target.getUserId(); }); if (target_node_it == nodeSet.end()) { result.errorMessage = ERR_TARGET_NODE_NOT_IN_GRAPH; return result; @@ -1758,7 +1829,8 @@ BestFirstSearchResult Graph::best_first_search( if (elem.second->isWeighted().has_value()) { if (elem.second->isDirected().has_value()) { shared> dw_edge = - std::static_pointer_cast>(elem.second); + std::static_pointer_cast>( + elem.second); if (std::find(visited.begin(), visited.end(), *(elem.first)) == visited.end()) { visited.push_back(*(elem.first)); @@ -1766,7 +1838,8 @@ BestFirstSearchResult Graph::best_first_search( } } else { shared> dw_edge = - std::static_pointer_cast>(elem.second); + std::static_pointer_cast>( + elem.second); if (std::find(visited.begin(), visited.end(), *(elem.first)) == visited.end()) { visited.push_back(*(elem.first)); @@ -1793,9 +1866,9 @@ const std::vector> Graph::breadth_first_search( std::vector> visited; auto &nodeSet = Graph::getNodeSet(); // check is exist node in the graph - auto start_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&start](auto node){ - return node->getUserId() == start.getUserId(); - }); + auto start_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&start](auto node) { return node->getUserId() == start.getUserId(); }); if (start_node_it == nodeSet.end()) { return visited; } @@ -1830,9 +1903,9 @@ const std::vector> Graph::concurrency_breadth_first_search( std::vector> bfs_result; // check is exist node in the graph auto &nodeSet = Graph::getNodeSet(); - auto start_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&start](auto node){ - return node->getUserId() == start.getUserId(); - }); + auto start_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&start](auto node) { return node->getUserId() == start.getUserId(); }); if (start_node_it == nodeSet.end()) { return bfs_result; } @@ -1886,8 +1959,9 @@ const std::vector> Graph::concurrency_breadth_first_search( return {start, end}; }; - auto submit_result = [&next_level_tracker, &next_tracker_mutex]( - std::vector>> &submission) -> void { + auto submit_result = + [&next_level_tracker, &next_tracker_mutex]( + std::vector>> &submission) -> void { std::lock_guard tracker_guard(next_tracker_mutex); next_level_tracker.insert(std::end(next_level_tracker), std::begin(submission), std::end(submission)); @@ -1986,15 +2060,15 @@ const std::vector> Graph::depth_first_search( std::vector> visited; auto nodeSet = Graph::getNodeSet(); // check is exist node in the graph - auto start_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&start](auto node){ - return node->getUserId() == start.getUserId(); - }); + auto start_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&start](auto node) { return node->getUserId() == start.getUserId(); }); if (start_node_it == nodeSet.end()) { return visited; } const auto adj = Graph::getAdjMatrix(); - std::function>, shared>, - std::vector> &)> + std::function>, + shared>, std::vector> &)> explore; explore = [&explore](const std::shared_ptr> adj, shared> node, @@ -2096,7 +2170,7 @@ bool Graph::isCyclicDirectedGraphDFS() const { template bool Graph::containsCycle(shared> edgeSet) const { - shared> subset; + auto subset = make_shared>(); // initialize the subset parent and rank values for (const auto &edge : *edgeSet) { auto &[first, second] = edge->getNodePair(); @@ -2231,7 +2305,8 @@ bool Graph::isConnectedGraph() const { visited[node->getId()] = false; } std::function>)> dfs_helper = - [this, &adjMatrix, &visited, &dfs_helper](shared> source) { + [this, &adjMatrix, &visited, + &dfs_helper](shared> source) { // mark the vertex visited visited[source->getId()] = true; @@ -2271,7 +2346,8 @@ bool Graph::isStronglyConnectedGraph() const { visited[node->getId()] = false; } std::function>)> dfs_helper = - [this, &adjMatrix, &visited, &dfs_helper](shared> source) { + [this, &adjMatrix, &visited, + &dfs_helper](shared> source) { // mark the vertex visited visited[source->getId()] = true; @@ -2424,7 +2500,8 @@ SCCResult Graph::kosaraju() const { std::stack>> st; std::function>)> dfs_helper = - [this, &adjMatrix, &visited, &dfs_helper, &st](shared> source) { + [this, &adjMatrix, &visited, &dfs_helper, + &st](shared> source) { // mark the vertex visited visited[source->getId()] = true; @@ -2451,7 +2528,8 @@ SCCResult Graph::kosaraju() const { auto addElementToAdjMatrix = [&rev](shared> nodeFrom, shared> nodeTo, shared> edge) { - std::pair> , shared>> elem = {nodeTo, edge}; + std::pair>, shared>> elem = {nodeTo, + edge}; rev[nodeFrom].push_back(std::move(elem)); }; for (const auto &edgeSetIt : edgeSet) { @@ -2459,30 +2537,30 @@ SCCResult Graph::kosaraju() const { std::static_pointer_cast>(edgeSetIt); // Add the reverse edge to the reverse adjacency matrix addElementToAdjMatrix(d_edge->getNodePair().first, - d_edge->getNodePair().second, - d_edge); + d_edge->getNodePair().second, d_edge); } visited.clear(); - std::function>, std::vector> &)> dfs_helper1 = - [this, &rev, &visited, &dfs_helper1](shared> source, - std::vector> &comp) { - // mark the vertex visited - visited[source->getId()] = true; - // Add the current vertex to the strongly connected - // component - comp.push_back(*source); - - // travel the neighbors - for (int i = 0; i < rev[source].size(); i++) { - shared> neighbor = rev[source].at(i).first; - if (visited[neighbor->getId()] == false) { - // make recursive call from neighbor - dfs_helper1(neighbor, comp); - } - } - }; + std::function>, std::vector> &)> + dfs_helper1 = + [this, &rev, &visited, &dfs_helper1](shared> source, + std::vector> &comp) { + // mark the vertex visited + visited[source->getId()] = true; + // Add the current vertex to the strongly connected + // component + comp.push_back(*source); + + // travel the neighbors + for (int i = 0; i < rev[source].size(); i++) { + shared> neighbor = rev[source].at(i).first; + if (visited[neighbor->getId()] == false) { + // make recursive call from neighbor + dfs_helper1(neighbor, comp); + } + } + }; while (st.size() != 0) { auto rem = st.top(); @@ -2507,9 +2585,9 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { const auto adj = getAdjMatrix(); auto nodeSet = getNodeSet(); - auto source_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ - return node->getUserId() == source.getUserId(); - }); + auto source_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&source](auto node) { return node->getUserId() == source.getUserId(); }); if (source_node_it == nodeSet.end()) { // check if source node exist in the graph result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH; @@ -2521,7 +2599,9 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { dist[i].first = distance of ith vertex from src vertex dits[i].second = vertex i in bucket number */ unsigned int V = nodeSet.size(); - std::unordered_map>, std::pair>>> dist; + std::unordered_map>, + std::pair>>> + dist; // Initialize all distances as infinite (INF) for (const auto &node : nodeSet) { @@ -2567,7 +2647,8 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { } else if (i.second->isDirected().has_value() && !i.second->isDirected().value()) { shared> udw_edge = - std::static_pointer_cast>(i.second); + std::static_pointer_cast>( + i.second); weight = (int)udw_edge->getWeight(); } else { // ERROR it shouldn't never returned ( does not exist a Node @@ -2582,13 +2663,17 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { } auto u_i = std::find_if( dist.begin(), dist.end(), - [u](std::pair>, std::pair>>> const - &it) { return (*u == *(it.first)); }); + [u](std::pair>, + std::pair>>> const &it) { + return (*u == *(it.first)); + }); auto v_i = std::find_if( dist.begin(), dist.end(), - [v](std::pair>, std::pair>>> const - &it) { return (*v == *(it.first)); }); + [v](std::pair>, + std::pair>>> const &it) { + return (*v == *(it.first)); + }); long du = u_i->second.first; long dv = v_i->second.first; @@ -2630,7 +2715,8 @@ double Graph::fordFulkersonMaxFlow(const Node &source, } double maxFlow = 0; std::unordered_map>, shared>> parent; - std::map>, std::map>, double>> weightMap; + std::map>, std::map>, double>> + weightMap; // build weight map auto edgeSet = this->getEdgeSet(); for (const auto &edge : edgeSet) { @@ -2649,14 +2735,15 @@ double Graph::fordFulkersonMaxFlow(const Node &source, // Constuct iterators for source and target nodes in nodeSet auto nodeSet = getNodeSet(); - auto source_node_ptr = *std::find_if(nodeSet.begin(), nodeSet.end(), [&source](auto node){ - return node->getUserId() == source.getUserId(); - }); - auto target_node_ptr = *std::find_if(nodeSet.begin(), nodeSet.end(), [&target](auto node){ - return node->getUserId() == target.getUserId(); - }); - - auto bfs_helper = [this, &source_node_ptr, &target_node_ptr, &parent, &weightMap]() -> bool { + auto source_node_ptr = *std::find_if( + nodeSet.begin(), nodeSet.end(), + [&source](auto node) { return node->getUserId() == source.getUserId(); }); + auto target_node_ptr = *std::find_if( + nodeSet.begin(), nodeSet.end(), + [&target](auto node) { return node->getUserId() == target.getUserId(); }); + + auto bfs_helper = [this, &source_node_ptr, &target_node_ptr, &parent, + &weightMap]() -> bool { std::unordered_map>, bool> visited; std::queue>> queue; queue.push(source_node_ptr); @@ -2701,9 +2788,9 @@ const std::vector> Graph::graph_slicing(const Node &start) const { auto nodeSet = Graph::getNodeSet(); // check if start node in the graph - auto start_node_it = std::find_if(nodeSet.begin(), nodeSet.end(), [&start](auto node){ - return node->getUserId() == start.getUserId(); - }); + auto start_node_it = std::find_if( + nodeSet.begin(), nodeSet.end(), + [&start](auto node) { return node->getUserId() == start.getUserId(); }); if (start_node_it == nodeSet.end()) { return result; } @@ -2994,9 +3081,9 @@ int Graph::writeToMTXFile(const std::string &workingDir, std::string header = "%%MatrixMarket graph"; // 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()); - }); + bool symmetric = !std::any_of(edgeSet.begin(), edgeSet.end(), [](auto edge) { + return (edge->isDirected().has_value() && edge->isDirected().value()); + }); // Write in the header whether the adj matrix is symmetric or not if (symmetric) { header += " symmetric\n"; @@ -3083,7 +3170,7 @@ int Graph::readFromMTXFile(const std::string &workingDir, // Since the matrix represents the adjacency matrix, it must be square if (n_rows != n_cols) { - return -1; + return -1; } // Read the content of each line @@ -3113,8 +3200,9 @@ int Graph::readFromMTXFile(const std::string &workingDir, } if (n_edges != edgeMap.size()) { - std::cout << "Error: The number of edges does not match the value provided in the size line.\n"; - return -1; + std::cout << "Error: The number of edges does not match the value provided " + "in the size line.\n"; + return -1; } iFile.close(); @@ -3154,7 +3242,8 @@ std::ostream &operator<<(std::ostream &os, const Graph &graph) { (*it)->isDirected().value()) && ((*it)->isWeighted().has_value() && (*it)->isWeighted().value())) { - os << std::static_pointer_cast>(*it) << "\n"; + os << std::static_pointer_cast>(*it) + << "\n"; } else if (((*it)->isDirected().has_value() && (*it)->isDirected().value()) && !((*it)->isWeighted().has_value() && @@ -3162,7 +3251,8 @@ std::ostream &operator<<(std::ostream &os, const Graph &graph) { os << std::static_pointer_cast>(*it) << "\n"; } else if (!(it->isDirected().has_value() && it->isDirected().value()) && (it->isWeighted().has_value() && it->isWeighted().value())) { - os << std::static_pointer_cast>(*it) << "\n"; + os << std::static_pointer_cast>(*it) + << "\n"; } else if (!(it->isDirected().has_value() && it->isDirected().value()) && !(it->isWeighted().has_value() && it->isWeighted().value())) { os << std::static_pointer_cast>(*it) << "\n"; From f84892465909f1be9e20b52ae8c0116ef33e3862 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Fri, 26 May 2023 16:42:07 +0200 Subject: [PATCH 07/28] Add overloads of algorithms which take raw pointers to mantain interface --- include/Graph/Graph.hpp | 115 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 2 deletions(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index 1f4844992..cfed52db0 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -221,6 +221,19 @@ class Graph { * @return parent node of elem * Note: No Thread Safe */ + virtual unsigned long long setFind( + std::unordered_map *, + const unsigned long long elem) const; + /** + * @brief This function finds the subset of given a nodeId + * Subset is stored in a map where keys are the hash-id of the node & values + * is the subset. + * @param shared pointer to subset query subset, we want to find target in this subset + * @param elem elem that we wish to find in the subset + * + * @return parent node of elem + * Note: No Thread Safe + */ virtual unsigned long long setFind( shared>, const unsigned long long elem) const; @@ -233,6 +246,18 @@ class Graph { * NOTE: Original subset is no longer available after union. * Note: No Thread Safe */ + virtual void setUnion(std::unordered_map *, + const unsigned long long set1, + const unsigned long long elem2) const; + /** + * @brief This function modifies the original subset array + * such that it the union of two sets a and b + * @param subset original subset is modified to obtain union of a & b + * @param a parent id of set1 + * @param b parent id of set2 + * NOTE: Original subset is no longer available after union. + * Note: No Thread Safe + */ virtual void setUnion(shared>, const unsigned long long set1, const unsigned long long elem2) const; @@ -386,6 +411,14 @@ class Graph { */ virtual bool isCyclicDirectedGraphBFS() const; + /** + * @brief + * This function checks if the given set of edges + * forms a cycle or not using union-find method. + * + * @return true if a cycle is detected, else false + */ + virtual bool containsCycle(const T_EdgeSet *) const; /** * @brief * This function checks if the given set of edges @@ -1173,6 +1206,21 @@ int Graph::decompressFile(const std::string &inputFile, } #endif +template +unsigned long long Graph::setFind( + std::unordered_map *subsets, + const unsigned long long nodeId) const { + auto subsets_ptr = make_shared>(*subsets); + // find root and make root as parent of i + // (path compression) + if ((*subsets)[nodeId].parent != nodeId) { + (*subsets)[nodeId].parent = + Graph::setFind(subsets_ptr, (*subsets)[nodeId].parent); + } + + return (*subsets)[nodeId].parent; +} + template unsigned long long Graph::setFind( shared> subsets, @@ -1187,6 +1235,37 @@ unsigned long long Graph::setFind( return (*subsets)[nodeId].parent; } +template +void Graph::setUnion( + std::unordered_map *subsets, + const unsigned long long elem1, const unsigned long long elem2) const { + /* auto subsets_ptr = make_shared>(*subsets); */ + // if both sets have same parent + // then there's nothing to be done + /* if ((*subsets_ptr)[elem1].parent == (*subsets_ptr)[elem2].parent) return; */ + /* auto elem1Parent = Graph::setFind(subsets_ptr, elem1); */ + /* auto elem2Parent = Graph::setFind(subsets_ptr, elem2); */ + /* if ((*subsets_ptr)[elem1Parent].rank < (*subsets_ptr)[elem2Parent].rank) */ + /* (*subsets_ptr)[elem1].parent = elem2Parent; */ + /* else if ((*subsets_ptr)[elem1Parent].rank > (*subsets_ptr)[elem2Parent].rank) */ + /* (*subsets_ptr)[elem2].parent = elem1Parent; */ + /* else { */ + /* (*subsets_ptr)[elem2].parent = elem1Parent; */ + /* (*subsets_ptr)[elem1Parent].rank++; */ + /* } */ + if ((*subsets)[elem1].parent == (*subsets)[elem2].parent) return; + auto elem1Parent = Graph::setFind(subsets, elem1); + auto elem2Parent = Graph::setFind(subsets, elem2); + if ((*subsets)[elem1Parent].rank < (*subsets)[elem2Parent].rank) + (*subsets)[elem1].parent = elem2Parent; + else if ((*subsets)[elem1Parent].rank > (*subsets)[elem2Parent].rank) + (*subsets)[elem2].parent = elem1Parent; + else { + (*subsets)[elem2].parent = elem1Parent; + (*subsets)[elem1Parent].rank++; + } +} + template void Graph::setUnion( shared> subsets, @@ -1210,11 +1289,14 @@ template std::shared_ptr>> Graph::eulerianPath() const { const auto nodeSet = Graph::getNodeSet(); const auto adj = Graph::getAdjMatrix(); + std::shared_ptr>> eulerPath = std::make_shared>>(); + std::vector>> currentPath; auto currentNode = *(nodeSet.begin()); currentPath.push_back(currentNode); + while (currentPath.size() > 0) { auto &edges = adj->at(currentNode); // we keep removing the edges that @@ -1272,6 +1354,7 @@ const DijkstraResult Graph::dijkstra(const Node &source, const Node &target) const { DijkstraResult result; auto nodeSet = Graph::getNodeSet(); + auto source_node_it = std::find_if( nodeSet.begin(), nodeSet.end(), [&source](auto node) { return node->getUserId() == source.getUserId(); }); @@ -1280,6 +1363,7 @@ const DijkstraResult Graph::dijkstra(const Node &source, result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH; return result; } + auto target_node_it = std::find_if( nodeSet.begin(), nodeSet.end(), [&target](auto node) { return node->getUserId() == target.getUserId(); }); @@ -2168,6 +2252,33 @@ bool Graph::isCyclicDirectedGraphDFS() const { return false; } +template +bool Graph::containsCycle(const T_EdgeSet *edgeSet) const { + auto edgeSet_ptr = make_shared>(*edgeSet); + auto subset = make_shared>(); + // initialize the subset parent and rank values + for (const auto &edge : *edgeSet_ptr) { + auto &[first, second] = edge->getNodePair(); + std::vector nodeId(2); + nodeId.push_back(first->getId()); + nodeId.push_back(second->getId()); + for (const auto &id : nodeId) { + auto nodeExists = [id](const auto &it) { + return (id == (it.second).parent); + }; + + if (std::find_if((*subset).begin(), (*subset).end(), nodeExists) == + (*subset).end()) { + Subset set; + set.parent = id; + set.rank = 0; + (*subset)[id] = set; + } + } + } + return Graph::containsCycle(edgeSet_ptr, subset); +} + template bool Graph::containsCycle(shared> edgeSet) const { auto subset = make_shared>(); @@ -2536,8 +2647,8 @@ SCCResult Graph::kosaraju() const { shared> d_edge = std::static_pointer_cast>(edgeSetIt); // Add the reverse edge to the reverse adjacency matrix - addElementToAdjMatrix(d_edge->getNodePair().first, - d_edge->getNodePair().second, d_edge); + addElementToAdjMatrix(d_edge->getNodePair().second, + d_edge->getNodePair().first, d_edge); } visited.clear(); From f2d2be9a257c2315796cfbabdabccaaaa61c734d Mon Sep 17 00:00:00 2001 From: sbaldu Date: Fri, 26 May 2023 16:43:19 +0200 Subject: [PATCH 08/28] Fix typos in tests --- test/BoruvkaTest.cpp | 2 +- test/DialTest.cpp | 2 +- test/DijkstraTest.cpp | 8 ++++---- test/FordFulkersonTest.cpp | 2 +- test/UnionFindTest.cpp | 26 +++++++++++--------------- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/test/BoruvkaTest.cpp b/test/BoruvkaTest.cpp index 113fbc74b..76ea91a7d 100644 --- a/test/BoruvkaTest.cpp +++ b/test/BoruvkaTest.cpp @@ -172,7 +172,7 @@ TEST(BoruvkaTest, test_4) { CXXGraph::UndirectedEdge edge3(3, node1, node2); CXXGraph::T_EdgeSet edgeSet1; - edgeSet.insert(make_shared>(edge3)); + edgeSet1.insert(make_shared>(edge3)); CXXGraph::Graph graph1(edgeSet1); res = graph1.boruvka(); diff --git a/test/DialTest.cpp b/test/DialTest.cpp index 27b0376e6..9afc45c0f 100644 --- a/test/DialTest.cpp +++ b/test/DialTest.cpp @@ -67,7 +67,7 @@ TEST(DialTest, test_3) { CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; edgeSet.insert(make_shared>(edge1)); - edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge2)); edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); auto res = graph.dial(node1, 6); diff --git a/test/DijkstraTest.cpp b/test/DijkstraTest.cpp index b9deab69f..d7bcc1c3e 100644 --- a/test/DijkstraTest.cpp +++ b/test/DijkstraTest.cpp @@ -24,8 +24,8 @@ TEST(DijkstraTest, correct_example_1) { CXXGraph::T_EdgeSet edgeSet; edgeSet.insert(make_shared>(edge1)); - edgeSet.insert(make_shared>(edge1)); - edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node1, node3); @@ -47,8 +47,8 @@ TEST(DijkstraTest, correct_example_2) { CXXGraph::T_EdgeSet edgeSet; edgeSet.insert(make_shared>(edge1)); - edgeSet.insert(make_shared>(edge1)); - edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); CXXGraph::DijkstraResult res = graph.dijkstra(node1, node3); diff --git a/test/FordFulkersonTest.cpp b/test/FordFulkersonTest.cpp index 389d1a572..6368d377d 100644 --- a/test/FordFulkersonTest.cpp +++ b/test/FordFulkersonTest.cpp @@ -59,7 +59,7 @@ TEST(FordFulkersonTest, test_2) { CXXGraph::DirectedWeightedEdge edge7(7, node4, node5, 4); CXXGraph::DirectedWeightedEdge edge8(8, node3, node5, 19); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge1)); edgeSet.insert(make_shared>(edge2)); edgeSet.insert(make_shared>(edge3)); edgeSet.insert(make_shared>(edge4)); diff --git a/test/UnionFindTest.cpp b/test/UnionFindTest.cpp index 5999cd5ad..e38acdf7e 100644 --- a/test/UnionFindTest.cpp +++ b/test/UnionFindTest.cpp @@ -34,16 +34,15 @@ TEST(UnionFindTest, setFindTest1) { // every element is a subset of itself std::unordered_map subset; - auto subset_ptr = make_shared>(subset); // {{0, 0}, {1, 0}, {2, 0}, {3, 0}}; CXXGraph::Subset set1{0, 0}, set2{1, 0}, set3{2, 0}, set4{3, 0}; subset = {{0, set1}, {1, set2}, {2, set3}, {3, set4}}; - auto res = graph.setFind(subset_ptr, std::stoi(node0.getUserId())); + auto res = graph.setFind(&subset, std::stoi(node0.getUserId())); ASSERT_EQ(res, 0); - res = graph.setFind(subset_ptr, std::stoi(node1.getUserId())); + res = graph.setFind(&subset, std::stoi(node1.getUserId())); ASSERT_EQ(res, 1); - res = graph.setFind(subset_ptr, std::stoi(node2.getUserId())); + res = graph.setFind(&subset, std::stoi(node2.getUserId())); ASSERT_EQ(res, 2); } @@ -55,7 +54,6 @@ TEST(UnionFindTest, setFindTest2) { // element 2 & 4 are subset of 0 // element 4 is subset of 1 std::unordered_map subset; - auto subset_ptr = make_shared>(subset); CXXGraph::Subset set1{0, 0}, set2{0, 0}, set3{0, 0}, set4{1, 0}; subset = {{0, set1}, {1, set2}, {2, set3}, {3, set4}}; @@ -65,11 +63,11 @@ TEST(UnionFindTest, setFindTest2) { edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); - auto res = graph.setFind(subset_ptr, std::stoi(node0.getUserId())); + auto res = graph.setFind(&subset, std::stoi(node0.getUserId())); ASSERT_EQ(res, 0); - res = graph.setFind(subset_ptr, std::stoi(node3.getUserId())); + res = graph.setFind(&subset, std::stoi(node3.getUserId())); ASSERT_EQ(res, 0); - res = graph.setFind(subset_ptr, std::stoi(node2.getUserId())); + res = graph.setFind(&subset, std::stoi(node2.getUserId())); ASSERT_EQ(res, 0); } @@ -80,7 +78,6 @@ TEST(UnionFindTest, setUnionTest3) { CXXGraph::Node node3("3", 3); // union of (node 1 & node3) should increase node0 rank by 1 std::unordered_map subset; - auto subset_ptr = make_shared>(subset); CXXGraph::Subset set1{0, 0}, set2{0, 0}, set3{0, 0}, set4{1, 0}; subset = {{0, set1}, {1, set2}, {2, set3}, {3, set4}}; @@ -93,13 +90,13 @@ TEST(UnionFindTest, setUnionTest3) { edgeSet.insert(make_shared>(edge1)); CXXGraph::Graph graph(edgeSet); - graph.setUnion(subset_ptr, std::stoi(node1.getUserId()), + graph.setUnion(&subset, std::stoi(node1.getUserId()), std::stoi(node3.getUserId())); ASSERT_EQ(subset[0].rank, 1); ASSERT_EQ(subset[3].parent, 0); // rank shouldn't increase since both have the same parent - graph.setUnion(subset_ptr, std::stoi(node2.getUserId()), + graph.setUnion(&subset, std::stoi(node2.getUserId()), std::stoi(node3.getUserId())); ASSERT_EQ(subset[0].rank, 1); ASSERT_EQ(subset[3].parent, 0); @@ -111,21 +108,20 @@ TEST(UnionFindTest, containsCycle) { CXXGraph::Node node2("2", 2); CXXGraph::T_EdgeSet edgeSet; - auto edgeSet_ptr = make_shared>(edgeSet); CXXGraph::UndirectedWeightedEdge edge0(0, node0, node1, 5); edgeSet.insert(make_shared>(edge0)); CXXGraph::Graph graph(edgeSet); - bool containsCycle = graph.containsCycle(edgeSet_ptr); + bool containsCycle = graph.containsCycle(&edgeSet); ASSERT_EQ(containsCycle, false); CXXGraph::UndirectedWeightedEdge edge1(1, node1, node2, 10); edgeSet.insert(make_shared>(edge1)); - containsCycle = graph.containsCycle(edgeSet_ptr); + containsCycle = graph.containsCycle(&edgeSet); ASSERT_EQ(containsCycle, false); CXXGraph::UndirectedWeightedEdge edge2(2, node2, node0, 5); edgeSet.insert(make_shared>(edge2)); - containsCycle = graph.containsCycle(edgeSet_ptr); + containsCycle = graph.containsCycle(&edgeSet); ASSERT_EQ(containsCycle, true); } From 177bb046628d3ef1f7dceef3827adeff2ce797f9 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Sat, 27 May 2023 00:01:49 +0200 Subject: [PATCH 09/28] Change dynamic_casts into dynamic_pointer_casts --- include/Graph/Graph.hpp | 20 +++++++++++-------- .../CoordinatedPartitionState.hpp | 2 +- include/Partitioning/Partitioner.hpp | 5 +++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index cfed52db0..98b1ff1b7 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -897,7 +897,7 @@ int Graph::writeToDot(const std::string &workingDir, edgeLine += " [weight=" + std::to_string(static_cast( - dynamic_cast(edgePtr.get())->getWeight())) + + std::dynamic_pointer_cast(edgePtr)->getWeight())) + ']'; } edgeLine += ";\n"; @@ -948,7 +948,7 @@ void Graph::writeGraphToStream(std::ostream &oGraph, std::ostream &oNodeFeat, oEdgeWeight << edge->getId() << sep << (edge->isWeighted().has_value() && edge->isWeighted().value() - ? (dynamic_cast(edge.get()))->getWeight() + ? (std::dynamic_pointer_cast(edge))->getWeight() : 0.0) << sep << (edge->isWeighted().has_value() && edge->isWeighted().value() ? 1 @@ -1294,7 +1294,11 @@ std::shared_ptr>> Graph::eulerianPath() const { std::make_shared>>(); std::vector>> currentPath; - auto currentNode = *(nodeSet.begin()); + // The starting node is the only node which has more outgoing than ingoing links + auto firstNodeIt = std::max_element(nodeSet.begin(), nodeSet.end(), [adj](auto n1, auto n2){ + return adj->at(n1).size() < adj->at(n2).size(); + }); + auto currentNode = *(firstNodeIt); currentPath.push_back(currentNode); while (currentPath.size() > 0) { @@ -1508,7 +1512,7 @@ const BellmanFordResult Graph::bellmanford(const Node &source, auto elem = edge->getNodePair(); if (edge->isWeighted().has_value() && edge->isWeighted().value()) { auto edge_weight = - (dynamic_cast(edge.get()))->getWeight(); + (std::dynamic_pointer_cast(edge))->getWeight(); if (dist[elem.first] + edge_weight < dist[elem.second]) dist[elem.second] = dist[elem.first] + edge_weight; } else { @@ -1539,7 +1543,7 @@ const BellmanFordResult Graph::bellmanford(const Node &source, for (const auto &edge : edgeSet) { auto elem = edge->getNodePair(); auto edge_weight = - (dynamic_cast(edge.get()))->getWeight(); + (std::dynamic_pointer_cast(edge))->getWeight(); if (dist[elem.first] + edge_weight < dist[elem.second]) { result.success = true; result.negativeCycle = true; @@ -1589,7 +1593,7 @@ const FWResult Graph::floydWarshall() const { const auto &elem = edge->getNodePair(); if (edge->isWeighted().has_value() && edge->isWeighted().value()) { auto edgeWeight = - (dynamic_cast(edge.get()))->getWeight(); + (std::dynamic_pointer_cast(edge))->getWeight(); auto key = std::make_pair(elem.first->getUserId(), elem.second->getUserId()); pairwise_dist[key] = edgeWeight; @@ -1747,7 +1751,7 @@ const MstResult Graph::boruvka() const { for (const auto &edge : edgeSet) { if (edge->isWeighted().has_value() && edge->isWeighted().value()) edgeWeight[edge->getId()] = - (dynamic_cast(edge.get()))->getWeight(); + (std::dynamic_pointer_cast(edge))->getWeight(); else { // No Weighted Edge result.errorMessage = ERR_NO_WEIGHTED_EDGE; @@ -1837,7 +1841,7 @@ const MstResult Graph::kruskal() const { sortedEdges; for (const auto &edge : edgeSet) { if (edge->isWeighted().has_value() && edge->isWeighted().value()) { - auto weight = (dynamic_cast(edge.get()))->getWeight(); + auto weight = (std::dynamic_pointer_cast(edge))->getWeight(); sortedEdges.push(std::make_pair(weight, edge)); } else { // No Weighted Edge diff --git a/include/Partitioning/CoordinatedPartitionState.hpp b/include/Partitioning/CoordinatedPartitionState.hpp index d37385c18..9462d47da 100755 --- a/include/Partitioning/CoordinatedPartitionState.hpp +++ b/include/Partitioning/CoordinatedPartitionState.hpp @@ -150,7 +150,7 @@ void CoordinatedPartitionState::incrementMachineWeight(const int m, std::lock_guard lock(*machines_weight_edges_mutex); double edge_weight = CXXGraph::NEGLIGIBLE_WEIGHT; if (e->isWeighted().has_value() && e->isWeighted().value()) { - edge_weight = (dynamic_cast(e.get()))->getWeight(); + edge_weight = (std::dynamic_pointer_cast(e))->getWeight(); } machines_weight_edges[m] = machines_weight_edges[m] + edge_weight; // double new_value = machines_weight_edges[m]; diff --git a/include/Partitioning/Partitioner.hpp b/include/Partitioning/Partitioner.hpp index 0b864fe8e..3f5520ee2 100755 --- a/include/Partitioning/Partitioner.hpp +++ b/include/Partitioning/Partitioner.hpp @@ -20,6 +20,7 @@ #ifndef __CXXGRAPH_PARTITIONING_PARTITIONER_H__ #define __CXXGRAPH_PARTITIONING_PARTITIONER_H__ +#include #pragma once #include @@ -82,7 +83,7 @@ Partitioner::Partitioner(shared> dataset, Globals &G) for (const auto &edge_it : *(this->dataset)) { weight_sum += (edge_it->isWeighted().has_value() && edge_it->isWeighted().value()) - ? dynamic_cast(edge_it.get())->getWeight() + ? std::dynamic_pointer_cast(edge_it)->getWeight() : CXXGraph::NEGLIGIBLE_WEIGHT; } double lambda = std::max(1.0, GLOBALS.param1); @@ -125,7 +126,7 @@ Partitioner::Partitioner(const Partitioner &other) { for (const auto &edge_it : *(this->dataset)) { weight_sum += (edge_it->isWeighted().has_value() && edge_it->isWeighted().value()) - ? dynamic_cast(edge_it.get())->getWeight() + ? std::dynamic_pointer_cast(edge_it)->getWeight() : CXXGraph::NEGLIGIBLE_WEIGHT; } double lambda = GLOBALS.param1; From 703dc8311b0238fa40e8a2397404b4d14d9e95f5 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Sat, 27 May 2023 00:02:41 +0200 Subject: [PATCH 10/28] Decrease the number of random edges and nodes in RW and partition tests --- test/PartitionTest.cpp | 14 +++++++------- test/RWOutputTest.cpp | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/PartitionTest.cpp b/test/PartitionTest.cpp index a1c18b181..409694124 100644 --- a/test/PartitionTest.cpp +++ b/test/PartitionTest.cpp @@ -13,8 +13,8 @@ using shared= std::shared_ptr; using std::make_unique; using std::make_shared; -static auto nodes = generateRandomNodes(10000, 2); -static auto edges = generateRandomEdges(10000, nodes); +static auto nodes = generateRandomNodes(1000, 2); +static auto edges = generateRandomEdges(1000, nodes); TEST(PartitionTest, test_1) { CXXGraph::Node node1("1", 1); @@ -82,7 +82,7 @@ TEST(PartitionTest, test_2) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, 1000); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -155,7 +155,7 @@ TEST(PartitionTest, test_4) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, 1000); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -228,7 +228,7 @@ TEST(PartitionTest, test_6) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, 1000); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -301,7 +301,7 @@ TEST(PartitionTest, test_8) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, 1000); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -323,7 +323,7 @@ TEST(PartitionTest, test_9) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, 1000); for (int i = 0; i < 4; ++i) { ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); } diff --git a/test/RWOutputTest.cpp b/test/RWOutputTest.cpp index e17baa222..0df6ebbe6 100644 --- a/test/RWOutputTest.cpp +++ b/test/RWOutputTest.cpp @@ -53,8 +53,8 @@ generateRandomEdges( return edges; } -static auto nodes = generateRandomNodes(10000, 2); -static auto edges = generateRandomEdges(10000, nodes); +static auto nodes = generateRandomNodes(1000, 2); +static auto edges = generateRandomEdges(1000, nodes); //************* CSV ***************// From 47e64fb382567fb39acb26487d3e32dc753c8d6f Mon Sep 17 00:00:00 2001 From: sbaldu Date: Tue, 30 May 2023 12:12:47 +0200 Subject: [PATCH 11/28] Add out/inOutEdges convenience methods for Graph --- include/Graph/Graph.hpp | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index dd36feb5c..0e8cbb1c5 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -199,6 +199,22 @@ class Graph { * corrispondent to the link Note: No Thread Safe */ virtual const std::shared_ptr> getAdjMatrix() const; + /** + * \brief + * Note: No Thread Safe + * + * @param Pointer to the node + * + */ + virtual const std::set *> outEdges(const Node *node) const; + /** + * \brief + * Note: No Thread Safe + * + * @param Pointer to the node + * + */ + virtual const std::set *> inOutEdges(const Node *node) const; /** * @brief This function finds the subset of given a nodeId * Subset is stored in a map where keys are the hash-id of the node & values @@ -1162,6 +1178,40 @@ const std::shared_ptr> Graph::getAdjMatrix() const { return adj; } +template +const std::set *> Graph::outEdges(const Node *node) const { + auto adj = getAdjMatrix(); + if (adj->find(node) == adj->end()) { + return std::set *>(); + } + auto nodeEdgePairs = adj->at(node); + + std::set *> outEdges; + for (auto pair : nodeEdgePairs) { + if (pair.second->isDirected().has_value() && pair.second->isDirected().value()) { + outEdges.insert(pair.first); + } + } + + return outEdges; +} + +template +const std::set *> Graph::inOutEdges(const Node *node) const { + auto adj = Graph::getAdjMatrix(); + if (adj->find(node) == adj->end()) { + return std::set *>(); + } + auto nodeEdgePairs = adj->at(node); + + std::set *> inOutEdges; + for (auto pair : nodeEdgePairs) { + inOutEdges.insert(pair.first); + } + + return inOutEdges; +} + template const DijkstraResult Graph::dijkstra(const Node &source, const Node &target) const { From cedbfbdb9a2ced9c6dfaae53134739e07d4f0be0 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Tue, 30 May 2023 12:13:30 +0200 Subject: [PATCH 12/28] Add tests for the out/inOutEdges methods --- test/GraphTest.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/test/GraphTest.cpp b/test/GraphTest.cpp index e28f8659c..782b8835d 100644 --- a/test/GraphTest.cpp +++ b/test/GraphTest.cpp @@ -1,3 +1,5 @@ +#include + #include "CXXGraph.hpp" #include "gtest/gtest.h" @@ -226,3 +228,100 @@ TEST(GraphTest, set_data) { ASSERT_EQ(nodeIt->getData(), data_values[nodeIt->getUserId()]); } } + +TEST(GraphTest, test_outEdges) { + CXXGraph::Node node1("1", 1); + CXXGraph::Node node2("2", 2); + CXXGraph::Node node3("3", 3); + CXXGraph::Node node4("4", 4); + CXXGraph::Node node5("5", 5); + CXXGraph::Node node6("6", 6); + CXXGraph::Node node7("7", 7); + CXXGraph::Node node8("8", 8); + CXXGraph::DirectedEdge edge1(1, node1, node2); + CXXGraph::DirectedEdge edge2(2, node1, node3); + CXXGraph::DirectedEdge edge3(3, node2, node4); + CXXGraph::DirectedEdge edge4(4, node2, node5); + CXXGraph::DirectedEdge edge5(5, node3, node4); + CXXGraph::DirectedEdge edge6(6, node3, node5); + CXXGraph::DirectedEdge edge7(7, node4, node6); + CXXGraph::DirectedEdge edge8(8, node4, node7); + CXXGraph::DirectedEdge edge9(7, node5, node6); + CXXGraph::DirectedEdge edge10(8, node5, node7); + CXXGraph::DirectedEdge edge11(8, node6, node8); + CXXGraph::DirectedEdge edge12(8, node7, node8); + CXXGraph::T_EdgeSet edgeSet; + edgeSet.insert(&edge1); + edgeSet.insert(&edge2); + edgeSet.insert(&edge3); + edgeSet.insert(&edge4); + edgeSet.insert(&edge5); + edgeSet.insert(&edge6); + edgeSet.insert(&edge7); + edgeSet.insert(&edge8); + edgeSet.insert(&edge9); + edgeSet.insert(&edge10); + edgeSet.insert(&edge11); + edgeSet.insert(&edge12); + CXXGraph::Graph graph(edgeSet); + + // Check node 1 + for (auto x : graph.outEdges(&node1)) { + ASSERT_TRUE(x == &node2 || x == &node3); + } + // Check node 2 + for (auto x : graph.outEdges(&node2)) { + ASSERT_TRUE(x == &node4 || x == &node5); + ASSERT_FALSE(x == &node1); + } + // Check node 5 + for (auto x : graph.outEdges(&node5)) { + ASSERT_TRUE(x == &node6 || x == &node7); + ASSERT_FALSE(x == &node2); + ASSERT_FALSE(x == &node3); + } + // Check that node 8 does not have any neighbors + ASSERT_EQ(graph.outEdges(&node8), std::set *>()); +} + +TEST(GraphTest, test_inOutEdges) { + CXXGraph::Node node1("1", 1); + CXXGraph::Node node2("2", 2); + CXXGraph::Node node3("3", 3); + CXXGraph::Node node4("4", 4); + CXXGraph::Node node5("5", 5); + CXXGraph::Node node6("6", 6); + CXXGraph::Node node7("7", 7); + CXXGraph::Node node8("8", 8); + CXXGraph::UndirectedEdge edge1(1, node1, node2); + CXXGraph::UndirectedEdge edge2(2, node1, node3); + CXXGraph::UndirectedEdge edge3(3, node2, node3); + CXXGraph::UndirectedEdge edge4(4, node2, node4); + CXXGraph::UndirectedEdge edge5(5, node4, node5); + CXXGraph::UndirectedEdge edge6(6, node4, node6); + CXXGraph::UndirectedEdge edge7(7, node6, node7); + CXXGraph::UndirectedEdge edge8(8, node6, node8); + CXXGraph::T_EdgeSet edgeSet; + edgeSet.insert(&edge1); + edgeSet.insert(&edge2); + edgeSet.insert(&edge3); + edgeSet.insert(&edge4); + edgeSet.insert(&edge5); + edgeSet.insert(&edge6); + edgeSet.insert(&edge7); + edgeSet.insert(&edge8); + CXXGraph::Graph graph(edgeSet); + + // Check node 1 + for (auto x : graph.inOutEdges(&node1)) { + ASSERT_TRUE(x == &node2 || x == &node3); + } + // Check node 4 + for (auto x : graph.inOutEdges(&node4)) { + ASSERT_TRUE(x == &node2 || x == &node3 || x == &node5 || x == &node6); + } + // Check node 7 + for (auto x : graph.inOutEdges(&node7)) { + ASSERT_TRUE(x == &node6); + } +} From f4f1e843254588012a8aaf10d21486bd06dfeee6 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Tue, 30 May 2023 20:34:35 +0200 Subject: [PATCH 13/28] Add overload of equality operators and hash functions for pointers of nodes --- include/Utility/PointerHash.hpp | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 include/Utility/PointerHash.hpp diff --git a/include/Utility/PointerHash.hpp b/include/Utility/PointerHash.hpp new file mode 100755 index 000000000..fdc3420b5 --- /dev/null +++ b/include/Utility/PointerHash.hpp @@ -0,0 +1,77 @@ +/***********************************************************/ +/*** ______ ____ ______ _ ***/ +/*** / ___\ \/ /\ \/ / ___|_ __ __ _ _ __ | |__ ***/ +/*** | | \ / \ / | _| '__/ _` | '_ \| '_ \ ***/ +/*** | |___ / \ / \ |_| | | | (_| | |_) | | | | ***/ +/*** \____/_/\_\/_/\_\____|_| \__,_| .__/|_| |_| ***/ +/*** |_| ***/ +/***********************************************************/ +/*** Header-Only C++ Library for Graph ***/ +/*** Representation and Algorithms ***/ +/***********************************************************/ +/*** Author: ZigRazor ***/ +/*** E-Mail: zigrazor@gmail.com ***/ +/***********************************************************/ +/*** Collaboration: ----------- ***/ +/***********************************************************/ +/*** License: AGPL v3.0 ***/ +/***********************************************************/ + +#ifndef __CXXGRAPH_POINTER_HASH__ +#define __CXXGRAPH_POINTER_HASH__ + +#pragma once + +#include +#include + +#include "../Edge/DirectedEdge.hpp" +#include "../Edge/DirectedWeightedEdge.hpp" +#include "../Edge/Edge.hpp" +#include "../Edge/UndirectedEdge.hpp" +#include "../Edge/UndirectedWeightedEdge.hpp" +#include "../Edge/Weighted.hpp" +#include "../Node/Node.hpp" + +namespace CXXGraph { +template +using shared = std::shared_ptr; + +// Redefine the hash functions and equality operators for shared pointers of nodes and edges +template +struct nodeHash { + size_t operator()(const shared>& node) const { + return node->getId(); + } + size_t operator()(const shared>& node) const { + return node->getId(); + } +}; + +template +struct edgeHash { + size_t operator()(const shared>& edge) const { + return (edge->getNodePair().first->getId()) ^ (edge->getNodePair().second->getId()); + } +}; + +template +bool operator==(shared> p1, shared> p2) { + return p1->getUserId() == p2->getUserId(); +} + +template +bool operator==(shared> p1, shared> p2) { + return p1->getUserId() == p2->getUserId(); +} + +template +bool operator==(shared> p1, shared> p2) { + return p1->getNodePair().first->getUserId() == + p2->getNodePair().first->getUserId() && + p2->getNodePair().second->getUserId() == + p2->getNodePair().second->getUserId(); +} +} // namespace CXXGraph + +#endif From 28ba46adc05f73da30ca5ff3aef389bea774f985 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Tue, 30 May 2023 20:47:03 +0200 Subject: [PATCH 14/28] Implement new hash functions for pointers of nodes and edges --- include/Graph/Graph.hpp | 149 ++++------------------------- include/Partitioning/Partition.hpp | 6 +- include/Utility/Typedef.hpp | 3 +- 3 files changed, 24 insertions(+), 134 deletions(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index 98b1ff1b7..b496880a3 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -80,7 +80,7 @@ using std::make_shared; using std::make_unique; template -using T_EdgeSet = std::unordered_set>>; +using T_EdgeSet = std::unordered_set>, edgeHash>; namespace Partitioning { template @@ -99,7 +99,7 @@ class Graph { T_EdgeSet edgeSet = {}; // Private non-const getter for the set of nodes - std::set>> nodeSet(); + std::unordered_set>, nodeHash> nodeSet(); std::optional> getExtenstionAndSeparator( InputOutputFormat format) const; @@ -176,7 +176,7 @@ class Graph { * @returns a list of Nodes of the graph * */ - virtual const std::set>> getNodeSet() const; + virtual const std::unordered_set>, nodeHash> getNodeSet() const; /** * \brief * Function that sets the data contained in a node @@ -628,44 +628,7 @@ class Graph { template Graph::Graph(const T_EdgeSet &edgeSet) { - for (auto edgeSetIt : edgeSet) { - auto edgeIt = std::const_pointer_cast>(edgeSetIt); - auto nodeset = this->getNodeSet(); - // If the edge was already in the set, I don't add it again - if (std::find_if(this->edgeSet.begin(), this->edgeSet.end(), - [edgeIt](auto edge_inSet) { - return (edge_inSet->getNodePair().first->getUserId() == - edgeIt->getNodePair().first->getUserId() && - edge_inSet->getNodePair().second->getUserId() == - edgeIt->getNodePair().second->getUserId()); - }) != this->edgeSet.end()) { - continue; - } - - // Check that the nodes pointed by the pointers in 'edge' have not already - // been added in other edges - // - // We don't pointers pointing to different memory regions which represent - // the same nodes - shared> node1; - auto node1It = - std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { - return node_inSet->getUserId() == - edgeIt->getNodePair().first->getUserId(); - }); - if (node1It != nodeset.end()) { - edgeIt->setFirstNode(*node1It); - } - shared> node2; - auto node2It = - std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { - return node_inSet->getUserId() == - edgeIt->getNodePair().second->getUserId(); - }); - if (node2It != nodeset.end()) { - edgeIt->setSecondNode(*node2It); - } - + for (auto edgeIt : edgeSet) { this->edgeSet.insert(edgeIt); } } @@ -678,88 +641,14 @@ const T_EdgeSet &Graph::getEdgeSet() const { template void Graph::setEdgeSet(const T_EdgeSet &edgeSet) { this->edgeSet.clear(); - for (auto edgeSetIt : edgeSet) { - auto edgeIt = std::const_pointer_cast>(edgeSetIt); - auto nodeset = this->getNodeSet(); - // If the edge was already in the set, I don't add it again - if (std::find_if(this->edgeSet.begin(), this->edgeSet.end(), - [edgeIt](auto edge_inSet) { - return (edge_inSet->getNodePair().first->getUserId() == - edgeIt->getNodePair().first->getUserId() && - edge_inSet->getNodePair().second->getUserId() == - edgeIt->getNodePair().second->getUserId()); - }) != this->edgeSet.end()) { - continue; - } - - // Check that the nodes pointed by the pointers in 'edge' have not already - // been added in other edges - // - // We don't pointers pointing to different memory regions which represent - // the same nodes - shared> node1; - auto node1It = - std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { - return node_inSet->getUserId() == - edgeIt->getNodePair().first->getUserId(); - }); - if (node1It != nodeset.end()) { - edgeIt->setFirstNode(*node1It); - } - shared> node2; - auto node2It = - std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { - return node_inSet->getUserId() == - edgeIt->getNodePair().second->getUserId(); - }); - if (node2It != nodeset.end()) { - edgeIt->setSecondNode(*node2It); - } - + for (auto edgeIt : edgeSet) { this->edgeSet.insert(edgeIt); } } template void Graph::addEdge(shared> edge) { - auto edgeIt = std::const_pointer_cast>(edge); - auto nodeset = this->getNodeSet(); - // If the edge was already in the set, I don't add it again - if (std::find_if(this->edgeSet.begin(), this->edgeSet.end(), - [edgeIt](auto edge_inSet) { - return (edge_inSet->getNodePair().first->getUserId() == - edgeIt->getNodePair().first->getUserId() && - edge_inSet->getNodePair().second->getUserId() == - edgeIt->getNodePair().second->getUserId()); - }) != this->edgeSet.end()) { - return; - } - - // Check that the nodes pointed by the pointers in 'edge' have not already - // been added in other edges - // - // We don't pointers pointing to different memory regions which represent - // the same nodes - shared> node1; - auto node1It = - std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { - return node_inSet->getUserId() == - edgeIt->getNodePair().first->getUserId(); - }); - if (node1It != nodeset.end()) { - edgeIt->setFirstNode(*node1It); - } - shared> node2; - auto node2It = - std::find_if(nodeset.begin(), nodeset.end(), [edgeIt](auto node_inSet) { - return node_inSet->getUserId() == - edgeIt->getNodePair().second->getUserId(); - }); - if (node2It != nodeset.end()) { - edgeIt->setSecondNode(*node2It); - } - - this->edgeSet.insert(edgeIt); + this->edgeSet.insert(edge); } template @@ -775,8 +664,8 @@ void Graph::removeEdge(const unsigned long long edgeId) { } template -const std::set>> Graph::getNodeSet() const { - std::set>> nodeSet; +const std::unordered_set>, nodeHash> Graph::getNodeSet() const { + std::unordered_set>, nodeHash> nodeSet; for (const auto &edgeSetIt : edgeSet) { nodeSet.insert(edgeSetIt->getNodePair().first); nodeSet.insert(edgeSetIt->getNodePair().second); @@ -830,8 +719,8 @@ const std::optional>> Graph::getEdge( } template -std::set>> Graph::nodeSet() { - std::set>> nodeSet; +std::unordered_set>, nodeHash> Graph::nodeSet() { + std::unordered_set>, nodeHash> nodeSet; for (auto &edgeSetIt : edgeSet) { nodeSet.insert( std::const_pointer_cast>(edgeSetIt->getNodePair().first)); @@ -1381,7 +1270,7 @@ const DijkstraResult Graph::dijkstra(const Node &source, int n = adj->size(); // setting all the distances initially to INF_DOUBLE - std::unordered_map>, double> dist; + std::unordered_map>, double, nodeHash> dist; for (const auto &node : nodeSet) { dist[node] = INF_DOUBLE; @@ -1490,7 +1379,7 @@ const BellmanFordResult Graph::bellmanford(const Node &source, return result; } // setting all the distances initially to INF_DOUBLE - std::unordered_map>, double> dist, currentDist; + std::unordered_map>, double, nodeHash> dist, currentDist; // n denotes the number of vertices in graph auto n = nodeSet.size(); for (const auto &elem : nodeSet) { @@ -1659,7 +1548,7 @@ const MstResult Graph::prim() const { const auto adj = Graph::getAdjMatrix(); // setting all the distances initially to INF_DOUBLE - std::unordered_map>, double> dist; + std::unordered_map>, double, nodeHash> dist; for (const auto &elem : (*adj)) { dist[elem.first] = INF_DOUBLE; } @@ -1998,7 +1887,7 @@ const std::vector> Graph::concurrency_breadth_first_search( return bfs_result; } - std::unordered_map>, int> node_to_index; + std::unordered_map>, int, nodeHash> node_to_index; for (const auto &node : nodeSet) { node_to_index[node] = node_to_index.size(); } @@ -2503,7 +2392,7 @@ TopoSortResult Graph::topologicalSort() const { } else { const auto &adjMatrix = getAdjMatrix(); const auto &nodeSet = getNodeSet(); - std::unordered_map>, bool> visited; + std::unordered_map>, bool, nodeHash> visited; std::function>)> postorder_helper = [&postorder_helper, &adjMatrix, &visited, @@ -2715,7 +2604,7 @@ const DialResult Graph::dial(const Node &source, int maxWeight) const { dits[i].second = vertex i in bucket number */ unsigned int V = nodeSet.size(); std::unordered_map>, - std::pair>>> + std::pair>>, nodeHash> dist; // Initialize all distances as infinite (INF) @@ -2829,8 +2718,8 @@ double Graph::fordFulkersonMaxFlow(const Node &source, return -1; } double maxFlow = 0; - std::unordered_map>, shared>> parent; - std::map>, std::map>, double>> + std::unordered_map>, shared>, nodeHash> parent; + std::unordered_map>, std::unordered_map>, double, nodeHash>, nodeHash> weightMap; // build weight map auto edgeSet = this->getEdgeSet(); @@ -2859,7 +2748,7 @@ double Graph::fordFulkersonMaxFlow(const Node &source, auto bfs_helper = [this, &source_node_ptr, &target_node_ptr, &parent, &weightMap]() -> bool { - std::unordered_map>, bool> visited; + std::unordered_map>, bool, nodeHash> visited; std::queue>> queue; queue.push(source_node_ptr); visited[source_node_ptr] = true; diff --git a/include/Partitioning/Partition.hpp b/include/Partitioning/Partition.hpp index 0750504e8..87a57af3c 100755 --- a/include/Partitioning/Partition.hpp +++ b/include/Partitioning/Partition.hpp @@ -43,7 +43,7 @@ template class Graph; template -using T_EdgeSet = std::unordered_set>>; +using T_EdgeSet = std::unordered_set>, edgeHash>; namespace Partitioning { template std::ostream &operator<<(std::ostream &o, const Partition &partition); @@ -290,10 +290,10 @@ unsigned int getNumberOfEdges(const PartitionMap &partitionMap) { template unsigned int getNumberOfNodes(const PartitionMap &partitionMap) { unsigned int numberOfNodes = 0; - std::set>> nodeSet; + std::unordered_set>, nodeHash> nodeSet; for (const auto &it : partitionMap) { - const std::set>> partitionNodeSet = it.second->getNodeSet(); + const std::unordered_set>, nodeHash> partitionNodeSet = it.second->getNodeSet(); for (const auto &it2 : partitionNodeSet) { // if (std::find_if(nodeSet.begin(), nodeSet.end(), [it2](const Node // *node) diff --git a/include/Utility/Typedef.hpp b/include/Utility/Typedef.hpp index 151c36306..e17ee1c9d 100755 --- a/include/Utility/Typedef.hpp +++ b/include/Utility/Typedef.hpp @@ -28,6 +28,7 @@ #include #include "ConstValue.hpp" +#include "PointerHash.hpp" namespace CXXGraph { // Smart pointers alias @@ -208,7 +209,7 @@ using BestFirstSearchResult = BestFirstSearchResult_struct; template using AdjacencyMatrix = std::unordered_map< - shared>, std::vector>, shared>>>>; + shared>, std::vector>, shared>>>, nodeHash>; template using PartitionMap = From 0d1d5100423d06bccb4dd4f8c3a059755ba1c376 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Tue, 30 May 2023 20:47:49 +0200 Subject: [PATCH 15/28] Increase number of nodes and edges in partition and RW tests --- test/PartitionTest.cpp | 14 +++++++------- test/RWOutputTest.cpp | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/PartitionTest.cpp b/test/PartitionTest.cpp index 409694124..a1c18b181 100644 --- a/test/PartitionTest.cpp +++ b/test/PartitionTest.cpp @@ -13,8 +13,8 @@ using shared= std::shared_ptr; using std::make_unique; using std::make_shared; -static auto nodes = generateRandomNodes(1000, 2); -static auto edges = generateRandomEdges(1000, nodes); +static auto nodes = generateRandomNodes(10000, 2); +static auto edges = generateRandomEdges(10000, nodes); TEST(PartitionTest, test_1) { CXXGraph::Node node1("1", 1); @@ -82,7 +82,7 @@ TEST(PartitionTest, test_2) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 1000); + ASSERT_EQ(totalEdgeInPartition, 10000); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -155,7 +155,7 @@ TEST(PartitionTest, test_4) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 1000); + ASSERT_EQ(totalEdgeInPartition, 10000); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -228,7 +228,7 @@ TEST(PartitionTest, test_6) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 1000); + ASSERT_EQ(totalEdgeInPartition, 10000); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -301,7 +301,7 @@ TEST(PartitionTest, test_8) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 1000); + ASSERT_EQ(totalEdgeInPartition, 10000); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -323,7 +323,7 @@ TEST(PartitionTest, test_9) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 1000); + ASSERT_EQ(totalEdgeInPartition, 10000); for (int i = 0; i < 4; ++i) { ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); } diff --git a/test/RWOutputTest.cpp b/test/RWOutputTest.cpp index 0df6ebbe6..e17baa222 100644 --- a/test/RWOutputTest.cpp +++ b/test/RWOutputTest.cpp @@ -53,8 +53,8 @@ generateRandomEdges( return edges; } -static auto nodes = generateRandomNodes(1000, 2); -static auto edges = generateRandomEdges(1000, nodes); +static auto nodes = generateRandomNodes(10000, 2); +static auto edges = generateRandomEdges(10000, nodes); //************* CSV ***************// From 5236a8739bd9e18099cd9a9b981666b85291d366 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 10:21:35 +0200 Subject: [PATCH 16/28] Fix typo --- include/Graph/Graph.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index 6ee36b2d8..0ca503773 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -189,7 +189,7 @@ class Graph { * @return True if the edge exists in the graph. */ virtual bool findEdge(shared> v1, shared> v2, - unsigned long long &id) const { + unsigned long long &id) const; /** * \brief * Function that return the Node Set of the Graph From a930216ddc3da9aab56525f2d28ea62854b30e8a Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 10:22:29 +0200 Subject: [PATCH 17/28] Use shared pointers in random node/edge generators --- test/PartitionTest.cpp | 20 ++++++++++---------- test/Utilities.hpp | 24 +++++++++++++++--------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/test/PartitionTest.cpp b/test/PartitionTest.cpp index a1c18b181..94c8e67e2 100644 --- a/test/PartitionTest.cpp +++ b/test/PartitionTest.cpp @@ -72,7 +72,7 @@ TEST(PartitionTest, test_1) { TEST(PartitionTest, test_2) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(make_shared>(*e.second)); + graph.addEdge(e.second); } auto partitionMap = graph.partitionGraph( CXXGraph::Partitioning::PartitionAlgorithm::HDRF_ALG, 4, 1, 0.001); @@ -82,7 +82,7 @@ TEST(PartitionTest, test_2) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, graph.getEdgeSet().size()); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -145,7 +145,7 @@ TEST(PartitionTest, test_3) { TEST(PartitionTest, test_4) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(make_shared>(*e.second)); + graph.addEdge(e.second); } auto partitionMap = graph.partitionGraph( CXXGraph::Partitioning::PartitionAlgorithm::EDGEBALANCED_VC_ALG, 4); @@ -155,7 +155,7 @@ TEST(PartitionTest, test_4) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, graph.getEdgeSet().size()); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -218,7 +218,7 @@ TEST(PartitionTest, test_5) { TEST(PartitionTest, test_6) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(make_shared>(*e.second)); + graph.addEdge(e.second); } auto partitionMap = graph.partitionGraph( CXXGraph::Partitioning::PartitionAlgorithm::GREEDY_VC_ALG, 4); @@ -228,7 +228,7 @@ TEST(PartitionTest, test_6) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, graph.getEdgeSet().size()); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -291,7 +291,7 @@ TEST(PartitionTest, test_7) { TEST(PartitionTest, test_8) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(make_shared>(*e.second)); + graph.addEdge(e.second); } auto partitionMap = graph.partitionGraph( CXXGraph::Partitioning::PartitionAlgorithm::EBV_ALG, 4, 1, 1); @@ -301,7 +301,7 @@ TEST(PartitionTest, test_8) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, graph.getEdgeSet().size()); for (int i = 0; i < 4; ++i) { // std::cout << *partitionMap.at(i) << std::endl; ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); @@ -311,7 +311,7 @@ TEST(PartitionTest, test_8) { TEST(PartitionTest, test_9) { CXXGraph::Graph graph; for (auto e : edges) { - graph.addEdge(make_shared>(*e.second)); + graph.addEdge(e.second); } auto partitionMap = graph.partitionGraph(CXXGraph::Partitioning::PartitionAlgorithm::WB_LIBRA, @@ -323,7 +323,7 @@ TEST(PartitionTest, test_9) { } // std::cout << "Total Edge in Partition: " << totalEdgeInPartition << // std::endl; - ASSERT_EQ(totalEdgeInPartition, 10000); + ASSERT_EQ(totalEdgeInPartition, graph.getEdgeSet().size()); for (int i = 0; i < 4; ++i) { ASSERT_EQ(partitionMap.at(i)->getPartitionId(), i); } diff --git a/test/Utilities.hpp b/test/Utilities.hpp index 6a71c91e3..788187897 100644 --- a/test/Utilities.hpp +++ b/test/Utilities.hpp @@ -2,36 +2,42 @@ #define __UTILITIES_H__ #include +#include #include #include "CXXGraph.hpp" +#include "../include/Utility/PointerHash.hpp" -static std::map *> generateRandomNodes( +template +using shared = std::shared_ptr; +using std::make_shared; + +static std::map>> generateRandomNodes( unsigned long numberOfNodes, int MaxValue) { thread_local static std::default_random_engine rand; thread_local static std::uniform_int_distribution distribution(0, RAND_MAX); - std::map *> nodes; + std::map>> nodes; unsigned int randSeed = (unsigned int)time(NULL); rand.seed(randSeed); for (auto index = 0; index < numberOfNodes; index++) { int randomNumber = (distribution(rand) % MaxValue) + 1; - CXXGraph::Node *newNode = - new CXXGraph::Node(std::to_string(index), randomNumber); + auto newNode = + make_shared>(std::to_string(index), randomNumber); nodes[index] = newNode; } return nodes; } -static std::map *> generateRandomEdges( +static std::map>> generateRandomEdges( unsigned long numberOfEdges, - std::map *> nodes) { + std::map>> nodes) { thread_local static std::default_random_engine rand; thread_local static std::uniform_int_distribution distribution(0, RAND_MAX); - std::map *> edges; + std::map>> edges; unsigned int randSeed = (unsigned int)time(NULL); rand.seed(randSeed); @@ -40,11 +46,11 @@ static std::map *> generateRandomEdges( for (auto index = 0; index < numberOfEdges; index++) { int randomNumber1 = (distribution(rand) % MaxValue); int randomNumber2 = (distribution(rand) % MaxValue); - CXXGraph::Edge *newEdge = new CXXGraph::Edge( + auto newEdge = make_shared>( index, *(nodes.at(randomNumber1)), *(nodes.at(randomNumber2))); edges[index] = newEdge; } return edges; } -#endif // __UTILITIES_H__ \ No newline at end of file +#endif // __UTILITIES_H__ From bbd06845bb78630384e54d4bbe296b84d4a8f7b4 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 10:35:07 +0200 Subject: [PATCH 18/28] Convert pointers in TransitiveReductionTest.cpp --- test/TransitiveReductionTest.cpp | 44 ++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/test/TransitiveReductionTest.cpp b/test/TransitiveReductionTest.cpp index 570d43acc..5d39e83e1 100644 --- a/test/TransitiveReductionTest.cpp +++ b/test/TransitiveReductionTest.cpp @@ -1,6 +1,12 @@ #include "CXXGraph.hpp" #include "gtest/gtest.h" +#include + +template +using shared = std::shared_ptr; +using std::make_shared; + TEST(TransitiveReductionTest, reduceEmpty) { CXXGraph::T_EdgeSet edgeSet; CXXGraph::Graph graph(edgeSet); @@ -27,13 +33,13 @@ TEST(TransitiveReductionTest, reduceReducedDAG) { CXXGraph::DirectedEdge edge7(7, node4, node7); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); - edgeSet.insert(&edge6); - edgeSet.insert(&edge7); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); CXXGraph::Graph graph(edgeSet); CXXGraph::Graph result = graph.transitiveReduction(); @@ -59,13 +65,13 @@ TEST(TransitiveReductionTest, reduceDAG) { CXXGraph::DirectedEdge edge7(7, node4, node7); CXXGraph::T_EdgeSet reducedEdgeSet; - reducedEdgeSet.insert(&edge1); - reducedEdgeSet.insert(&edge2); - reducedEdgeSet.insert(&edge3); - reducedEdgeSet.insert(&edge4); - reducedEdgeSet.insert(&edge5); - reducedEdgeSet.insert(&edge6); - reducedEdgeSet.insert(&edge7); + reducedEdgeSet.insert(make_shared>(edge1)); + reducedEdgeSet.insert(make_shared>(edge2)); + reducedEdgeSet.insert(make_shared>(edge3)); + reducedEdgeSet.insert(make_shared>(edge4)); + reducedEdgeSet.insert(make_shared>(edge5)); + reducedEdgeSet.insert(make_shared>(edge6)); + reducedEdgeSet.insert(make_shared>(edge7)); CXXGraph::Graph reducedGraph(reducedEdgeSet); // Add some more edges that can be reduced @@ -74,14 +80,14 @@ TEST(TransitiveReductionTest, reduceDAG) { CXXGraph::DirectedEdge edge9(9, node1, node4); CXXGraph::DirectedEdge edge10(10, node1, node7); CXXGraph::DirectedEdge edge11(11, node3, node6); - edgeSet.insert(&edge8); - edgeSet.insert(&edge9); - edgeSet.insert(&edge10); - edgeSet.insert(&edge11); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); CXXGraph::Graph graph(edgeSet); // Perform reduction CXXGraph::Graph result = graph.transitiveReduction(); ASSERT_EQ(reducedGraph.getEdgeSet(), result.getEdgeSet()); -} \ No newline at end of file +} From bb8372dc955361d8e8bde1d187cc2cc6cdc1fda4 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 10:50:47 +0200 Subject: [PATCH 19/28] Add overload of addEdge for raw pointers --- include/Graph/Graph.hpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index 0ca503773..f7a34522c 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -148,6 +148,17 @@ class Graph { * */ virtual void setEdgeSet(const T_EdgeSet &edgeSet); + /** + * \brief + * Function add an Edge to the Graph Edge Set + * First check if a pointer to a node with the same userId has + * already been added, and if not add it + * Note: No Thread Safe + * + * @param edge The Edge to insert + * + */ + virtual void addEdge(const Edge *edge); /** * \brief * Function add an Edge to the Graph Edge Set @@ -679,6 +690,12 @@ void Graph::setEdgeSet(const T_EdgeSet &edgeSet) { } } +template +void Graph::addEdge(const Edge *edge) { + auto edge_shared = make_shared>(edge); + this->edgeSet.insert(edge_shared); +} + template void Graph::addEdge(shared> edge) { this->edgeSet.insert(edge); From c94a1ddd2552e4bcd52c2390cf18cfeac1c1f80c Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 10:59:14 +0200 Subject: [PATCH 20/28] Fix typo --- include/Edge/Edge.hpp | 0 include/Graph/Graph.hpp | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 include/Edge/Edge.hpp diff --git a/include/Edge/Edge.hpp b/include/Edge/Edge.hpp old mode 100644 new mode 100755 diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index f7a34522c..42504d620 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -692,7 +692,7 @@ void Graph::setEdgeSet(const T_EdgeSet &edgeSet) { template void Graph::addEdge(const Edge *edge) { - auto edge_shared = make_shared>(edge); + auto edge_shared = make_shared>(*edge); this->edgeSet.insert(edge_shared); } From f5f229706f5c9cfb806296c0ed47fb6b494addd9 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 11:29:48 +0200 Subject: [PATCH 21/28] Fix edge insertion in FloydWarshall benchmark --- benchmark/FloydWarshall_BM.cpp | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/benchmark/FloydWarshall_BM.cpp b/benchmark/FloydWarshall_BM.cpp index 5d4e62e52..04b4ce475 100644 --- a/benchmark/FloydWarshall_BM.cpp +++ b/benchmark/FloydWarshall_BM.cpp @@ -1,4 +1,5 @@ #include +#include #include "CXXGraph.hpp" #include "Utilities.hpp" @@ -17,11 +18,11 @@ static void BM_FWDirected(benchmark::State &state) { CXXGraph::DirectedWeightedEdge edge5(3, node2, node3, 3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); CXXGraph::Graph graph(edgeSet); for (auto _ : state) { @@ -39,9 +40,9 @@ static void BM_FWNegCycle(benchmark::State &state) { CXXGraph::DirectedWeightedEdge edge2(2, node1, node2, 3); CXXGraph::DirectedWeightedEdge edge3(3, node2, node0, -7); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); for (auto _ : state) { CXXGraph::FWResult res = graph.floydWarshall(); @@ -59,9 +60,9 @@ static void BM_FWUndirectedWeighted(benchmark::State &state) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); for (auto _ : state) { CXXGraph::FWResult res = graph.floydWarshall(); @@ -79,9 +80,9 @@ static void BM_FWNoWeighted(benchmark::State &state) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); for (auto _ : state) { CXXGraph::FWResult res = graph.floydWarshall(); From dbe189e20f2760348ed20c688d3d04a5b1e06a9d Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 11:42:11 +0200 Subject: [PATCH 22/28] Fix typo in benchmark --- benchmark/FloydWarshall_BM.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/benchmark/FloydWarshall_BM.cpp b/benchmark/FloydWarshall_BM.cpp index 04b4ce475..93691d179 100644 --- a/benchmark/FloydWarshall_BM.cpp +++ b/benchmark/FloydWarshall_BM.cpp @@ -1,9 +1,12 @@ #include #include +#include #include "CXXGraph.hpp" #include "Utilities.hpp" +using std::make_shared; + // https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm#Example static void BM_FWDirected(benchmark::State &state) { CXXGraph::Node node1("1", 1); From ea732bccebd93c573b9a19453c9af329c85940bb Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 12:17:04 +0200 Subject: [PATCH 23/28] Remove all the calls to new in the tests --- test/BFSTest.cpp | 6 +++--- test/RWOutputTest.cpp | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/BFSTest.cpp b/test/BFSTest.cpp index be1ee27ba..e8a739576 100644 --- a/test/BFSTest.cpp +++ b/test/BFSTest.cpp @@ -295,11 +295,11 @@ TEST(BFSTest, test_13) { rand.seed(randSeed); int nodes_size = 60, edges_size = 2000; - std::vector *> nodes; + std::vector>> nodes; for (auto index = 0; index < nodes_size; index++) { int randomNumber = (distribution(rand) % nodes_size) + 1; - CXXGraph::Node *newNode = - new CXXGraph::Node(std::to_string(index), randomNumber); + auto newNode = + make_shared>(std::to_string(index), randomNumber); nodes.push_back(newNode); } diff --git a/test/RWOutputTest.cpp b/test/RWOutputTest.cpp index e17baa222..d205b9486 100644 --- a/test/RWOutputTest.cpp +++ b/test/RWOutputTest.cpp @@ -19,26 +19,26 @@ inline bool exists_test(const std::string &name) { return (stat(name.c_str(), &buffer) == 0); } -static std::unordered_map *> +static std::unordered_map>> generateRandomNodes(unsigned long numberOfNodes, int MaxValue) { - std::unordered_map *> nodes; + std::unordered_map>> nodes; srand(static_cast(time(NULL))); int randomNumber; for (auto index = 0; index < numberOfNodes; ++index) { // auto index = std::to_string(index); randomNumber = (rand() % MaxValue) + 1; - CXXGraph::Node *newNode = - new CXXGraph::Node(std::to_string(index), randomNumber); + auto newNode = + make_shared>(std::to_string(index), randomNumber); nodes[index] = newNode; } return nodes; } -static std::unordered_map *> +static std::unordered_map>> generateRandomEdges( unsigned long numberOfEdges, - std::unordered_map *> nodes) { - std::unordered_map *> edges; + std::unordered_map>> nodes) { + std::unordered_map>> edges; srand(static_cast(time(NULL))); int randomNumber1; int randomNumber2; @@ -46,7 +46,7 @@ generateRandomEdges( for (auto index = 0; index < numberOfEdges; ++index) { randomNumber1 = (rand() % MaxValue); randomNumber2 = (rand() % MaxValue); - CXXGraph::Edge *newEdge = new CXXGraph::Edge( + auto newEdge = make_shared>( index, *(nodes.at(randomNumber1)), *(nodes.at(randomNumber2))); edges[index] = newEdge; } From 57dc2a4a8a596501930b5e199af67e7ae42a288f Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 16:30:49 +0200 Subject: [PATCH 24/28] Fix typo in benchmark FloydWarshall Fix typo in benchmark FloydWarshall --- benchmark/FloydWarshall_BM.cpp | 28 ++++++++++++++-------------- include/Graph/Graph.hpp | 1 + 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/benchmark/FloydWarshall_BM.cpp b/benchmark/FloydWarshall_BM.cpp index 93691d179..73080db49 100644 --- a/benchmark/FloydWarshall_BM.cpp +++ b/benchmark/FloydWarshall_BM.cpp @@ -21,11 +21,11 @@ static void BM_FWDirected(benchmark::State &state) { CXXGraph::DirectedWeightedEdge edge5(3, node2, node3, 3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(make_shared>(edge1)); - edgeSet.insert(make_shared>(edge2)); - edgeSet.insert(make_shared>(edge3)); - edgeSet.insert(make_shared>(edge4)); - edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); CXXGraph::Graph graph(edgeSet); for (auto _ : state) { @@ -43,9 +43,9 @@ static void BM_FWNegCycle(benchmark::State &state) { CXXGraph::DirectedWeightedEdge edge2(2, node1, node2, 3); CXXGraph::DirectedWeightedEdge edge3(3, node2, node0, -7); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(make_shared>(edge1)); - edgeSet.insert(make_shared>(edge2)); - edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); for (auto _ : state) { CXXGraph::FWResult res = graph.floydWarshall(); @@ -63,9 +63,9 @@ static void BM_FWUndirectedWeighted(benchmark::State &state) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::UndirectedWeightedEdge edge3(3, node1, node3, 6); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(make_shared>(edge1)); - edgeSet.insert(make_shared>(edge2)); - edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); for (auto _ : state) { CXXGraph::FWResult res = graph.floydWarshall(); @@ -83,9 +83,9 @@ static void BM_FWNoWeighted(benchmark::State &state) { CXXGraph::DirectedWeightedEdge edge2(2, node2, node3, 1); CXXGraph::DirectedEdge edge3(3, node1, node3); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(make_shared>(edge1)); - edgeSet.insert(make_shared>(edge2)); - edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); CXXGraph::Graph graph(edgeSet); for (auto _ : state) { CXXGraph::FWResult res = graph.floydWarshall(); diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index 42504d620..a903a75ab 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -1993,6 +1993,7 @@ const std::vector> Graph::breadth_first_search( return visited; } + template const std::vector> Graph::concurrency_breadth_first_search( const Node &start, size_t num_threads) const { From b6a73fca3bfdda886958a44274fcd6819c84f4ef Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 17:13:43 +0200 Subject: [PATCH 25/28] Conversion of example codes --- examples/DialExample/dial_example.cpp | 15 +++++++++------ examples/DijkstraExample/dijkstra_example.cpp | 14 +++++++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/examples/DialExample/dial_example.cpp b/examples/DialExample/dial_example.cpp index 5374a975f..36b36503f 100644 --- a/examples/DialExample/dial_example.cpp +++ b/examples/DialExample/dial_example.cpp @@ -1,7 +1,10 @@ #include +#include #include +using std::make_shared; + typedef struct euclid_point { double x; double y; @@ -49,11 +52,11 @@ int main() { points_vector.at(node2.getData()))); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); - edgeSet.insert(&edge5); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); // Can print out the edges for debugging std::cout << edge1 << "\n"; @@ -68,4 +71,4 @@ int main() { std::cout << "Dial Result: " << res.minDistanceMap.at(node3.getId()) << ", " << res.success << "\n"; return 0; -} \ No newline at end of file +} diff --git a/examples/DijkstraExample/dijkstra_example.cpp b/examples/DijkstraExample/dijkstra_example.cpp index ac60a2bc6..29605be79 100644 --- a/examples/DijkstraExample/dijkstra_example.cpp +++ b/examples/DijkstraExample/dijkstra_example.cpp @@ -1,5 +1,9 @@ #include +#include + +using std::make_shared; + int main() { CXXGraph::Node node0("0", 0); CXXGraph::Node node1("1", 1); @@ -12,10 +16,10 @@ int main() { CXXGraph::UndirectedWeightedEdge edge4(4, node0, node3, 1.0); CXXGraph::T_EdgeSet edgeSet; - edgeSet.insert(&edge1); - edgeSet.insert(&edge2); - edgeSet.insert(&edge3); - edgeSet.insert(&edge4); + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); // Can print out the edges for debugging std::cout << edge1 << "\n"; @@ -29,4 +33,4 @@ int main() { std::cout << "Dijkstra Result: " << res.result << "\n"; return 0; -} \ No newline at end of file +} From 4e6ac48483b8d1af70b313d0d563b6fc9d9a5a4f Mon Sep 17 00:00:00 2001 From: sbaldu Date: Wed, 31 May 2023 20:15:11 +0200 Subject: [PATCH 26/28] Formatting --- include/Graph/Graph.hpp | 100 +++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 38 deletions(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index 835a5ed8d..6cc8b45f1 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -210,7 +210,8 @@ class Graph { * @returns a list of Nodes of the graph * */ - virtual const std::unordered_set>, nodeHash> getNodeSet() const; + virtual const std::unordered_set>, nodeHash> + getNodeSet() const; /** * \brief * Function that sets the data contained in a node @@ -252,7 +253,8 @@ class Graph { * @param Pointer to the node * */ - virtual const std::unordered_set>, nodeHash> outEdges(const Node *node) const; + virtual const std::unordered_set>, nodeHash> outEdges( + const Node *node) const; /** * \brief * Note: No Thread Safe @@ -260,7 +262,8 @@ class Graph { * @param Pointer to the node * */ - virtual const std::unordered_set>, nodeHash> outEdges(shared> node) const; + virtual const std::unordered_set>, nodeHash> outEdges( + shared> node) const; /** * \brief * Note: No Thread Safe @@ -268,7 +271,8 @@ class Graph { * @param Pointer to the node * */ - virtual const std::unordered_set>, nodeHash> inOutEdges(const Node *node) const; + virtual const std::unordered_set>, nodeHash> + inOutEdges(const Node *node) const; /** * \brief * Note: No Thread Safe @@ -276,7 +280,8 @@ class Graph { * @param Pointer to the node * */ - virtual const std::unordered_set>, nodeHash> inOutEdges(shared> node) const; + virtual const std::unordered_set>, nodeHash> + inOutEdges(shared> node) const; /** * @brief This function finds the subset of given a nodeId * Subset is stored in a map where keys are the hash-id of the node & values @@ -294,7 +299,8 @@ class Graph { * @brief This function finds the subset of given a nodeId * Subset is stored in a map where keys are the hash-id of the node & values * is the subset. - * @param shared pointer to subset query subset, we want to find target in this subset + * @param shared pointer to subset query subset, we want to find target in + * this subset * @param elem elem that we wish to find in the subset * * @return parent node of elem @@ -777,7 +783,8 @@ bool Graph::findEdge(shared> v1, shared> v2, } template -const std::unordered_set>, nodeHash> Graph::getNodeSet() const { +const std::unordered_set>, nodeHash> +Graph::getNodeSet() const { std::unordered_set>, nodeHash> nodeSet; for (const auto &edgeSetIt : edgeSet) { nodeSet.insert(edgeSetIt->getNodePair().first); @@ -896,11 +903,11 @@ int Graph::writeToDot(const std::string &workingDir, } if (edgePtr->isWeighted().has_value() && edgePtr->isWeighted().value()) { // Weights in dot files must be integers - edgeLine += - " [weight=" + - std::to_string(static_cast( - std::dynamic_pointer_cast(edgePtr)->getWeight())) + - ']'; + edgeLine += " [weight=" + + std::to_string(static_cast( + std::dynamic_pointer_cast(edgePtr) + ->getWeight())) + + ']'; } edgeLine += ";\n"; ofileGraph << edgeLine; @@ -950,7 +957,8 @@ void Graph::writeGraphToStream(std::ostream &oGraph, std::ostream &oNodeFeat, oEdgeWeight << edge->getId() << sep << (edge->isWeighted().has_value() && edge->isWeighted().value() - ? (std::dynamic_pointer_cast(edge))->getWeight() + ? (std::dynamic_pointer_cast(edge)) + ->getWeight() : 0.0) << sep << (edge->isWeighted().has_value() && edge->isWeighted().value() ? 1 @@ -1212,7 +1220,8 @@ template unsigned long long Graph::setFind( std::unordered_map *subsets, const unsigned long long nodeId) const { - auto subsets_ptr = make_shared>(*subsets); + auto subsets_ptr = + make_shared>(*subsets); // find root and make root as parent of i // (path compression) if ((*subsets)[nodeId].parent != nodeId) { @@ -1238,18 +1247,21 @@ unsigned long long Graph::setFind( } template -void Graph::setUnion( - std::unordered_map *subsets, - const unsigned long long elem1, const unsigned long long elem2) const { - /* auto subsets_ptr = make_shared>(*subsets); */ +void Graph::setUnion(std::unordered_map *subsets, + const unsigned long long elem1, + const unsigned long long elem2) const { + /* auto subsets_ptr = make_shared>(*subsets); */ // if both sets have same parent // then there's nothing to be done - /* if ((*subsets_ptr)[elem1].parent == (*subsets_ptr)[elem2].parent) return; */ + /* if ((*subsets_ptr)[elem1].parent == (*subsets_ptr)[elem2].parent) return; + */ /* auto elem1Parent = Graph::setFind(subsets_ptr, elem1); */ /* auto elem2Parent = Graph::setFind(subsets_ptr, elem2); */ /* if ((*subsets_ptr)[elem1Parent].rank < (*subsets_ptr)[elem2Parent].rank) */ /* (*subsets_ptr)[elem1].parent = elem2Parent; */ - /* else if ((*subsets_ptr)[elem1Parent].rank > (*subsets_ptr)[elem2Parent].rank) */ + /* else if ((*subsets_ptr)[elem1Parent].rank > + * (*subsets_ptr)[elem2Parent].rank) */ /* (*subsets_ptr)[elem2].parent = elem1Parent; */ /* else { */ /* (*subsets_ptr)[elem2].parent = elem1Parent; */ @@ -1363,50 +1375,55 @@ const std::shared_ptr> Graph::getAdjMatrix() const { } template -const std::unordered_set>, nodeHash> Graph::outEdges(const Node *node) const { +const std::unordered_set>, nodeHash> Graph::outEdges( + const Node *node) const { auto node_shared = make_shared>(*node); return outEdges(node_shared); } template -const std::unordered_set>, nodeHash> Graph::outEdges(shared> node) const { +const std::unordered_set>, nodeHash> Graph::outEdges( + shared> node) const { auto adj = getAdjMatrix(); if (adj->find(node) == adj->end()) { - return std::unordered_set>, nodeHash>(); + return std::unordered_set>, nodeHash>(); } auto nodeEdgePairs = adj->at(node); std::unordered_set>, nodeHash> outEdges; for (auto pair : nodeEdgePairs) { - if (pair.second->isDirected().has_value() && pair.second->isDirected().value()) { - outEdges.insert(pair.first); - } + if (pair.second->isDirected().has_value() && + pair.second->isDirected().value()) { + outEdges.insert(pair.first); + } } return outEdges; } template -const std::unordered_set>, nodeHash> Graph::inOutEdges(const Node *node) const { +const std::unordered_set>, nodeHash> +Graph::inOutEdges(const Node *node) const { auto node_shared = make_shared>(*node); - + return inOutEdges(node_shared); } template -const std::unordered_set>, nodeHash> Graph::inOutEdges(shared> node) const { +const std::unordered_set>, nodeHash> +Graph::inOutEdges(shared> node) const { auto adj = Graph::getAdjMatrix(); if (adj->find(node) == adj->end()) { - return std::unordered_set>, nodeHash>(); + return std::unordered_set>, nodeHash>(); } auto nodeEdgePairs = adj->at(node); - + std::unordered_set>, nodeHash> inOutEdges; for (auto pair : nodeEdgePairs) { - inOutEdges.insert(pair.first); + inOutEdges.insert(pair.first); } - + return inOutEdges; } @@ -1547,7 +1564,8 @@ const BellmanFordResult Graph::bellmanford(const Node &source, return result; } // setting all the distances initially to INF_DOUBLE - std::unordered_map>, double, nodeHash> dist, currentDist; + std::unordered_map>, double, nodeHash> dist, + currentDist; // n denotes the number of vertices in graph auto n = nodeSet.size(); for (const auto &elem : nodeSet) { @@ -1636,7 +1654,8 @@ const Graph Graph::transitiveReduction() const { Graph result(this->edgeSet); unsigned long long edgeId = 0; - std::unordered_set>, nodeHash> nodes = this->getNodeSet(); + std::unordered_set>, nodeHash> nodes = + this->getNodeSet(); for (auto x : nodes) { for (auto y : nodes) { if (this->findEdge(x, y, edgeId)) { @@ -1930,7 +1949,8 @@ const MstResult Graph::kruskal() const { sortedEdges; for (const auto &edge : edgeSet) { if (edge->isWeighted().has_value() && edge->isWeighted().value()) { - auto weight = (std::dynamic_pointer_cast(edge))->getWeight(); + auto weight = + (std::dynamic_pointer_cast(edge))->getWeight(); sortedEdges.push(std::make_pair(weight, edge)); } else { // No Weighted Edge @@ -2919,8 +2939,12 @@ double Graph::fordFulkersonMaxFlow(const Node &source, return -1; } double maxFlow = 0; - std::unordered_map>, shared>, nodeHash> parent; - std::unordered_map>, std::unordered_map>, double, nodeHash>, nodeHash> + std::unordered_map>, shared>, nodeHash> + parent; + std::unordered_map< + shared>, + std::unordered_map>, double, nodeHash>, + nodeHash> weightMap; // build weight map auto edgeSet = this->getEdgeSet(); From cc82485b6e4ffc01013326485f5c5576251aa5d5 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Thu, 1 Jun 2023 09:08:32 +0200 Subject: [PATCH 27/28] Add test for overloads of out/inOutEdges --- test/GraphTest.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/test/GraphTest.cpp b/test/GraphTest.cpp index b392647d2..8cd4ef367 100644 --- a/test/GraphTest.cpp +++ b/test/GraphTest.cpp @@ -301,6 +301,56 @@ TEST(GraphTest, test_outEdges) { ASSERT_EQ(graph.outEdges(&node8).size(), 0); } +// Test the overload that takes shared_ptr as input +TEST(GraphTest, test_outEdges_shared) { + CXXGraph::Node node1("1", 1); + CXXGraph::Node node2("2", 2); + CXXGraph::Node node3("3", 3); + CXXGraph::Node node4("4", 4); + CXXGraph::Node node5("5", 5); + CXXGraph::Node node6("6", 6); + CXXGraph::Node node7("7", 7); + CXXGraph::Node node8("8", 8); + CXXGraph::DirectedEdge edge1(1, node1, node2); + CXXGraph::DirectedEdge edge2(2, node1, node3); + CXXGraph::DirectedEdge edge3(3, node2, node4); + CXXGraph::DirectedEdge edge4(4, node2, node5); + CXXGraph::DirectedEdge edge5(5, node3, node4); + CXXGraph::DirectedEdge edge6(6, node3, node5); + CXXGraph::DirectedEdge edge7(7, node4, node6); + CXXGraph::DirectedEdge edge8(8, node4, node7); + CXXGraph::DirectedEdge edge9(7, node5, node6); + CXXGraph::DirectedEdge edge10(8, node5, node7); + CXXGraph::DirectedEdge edge11(8, node6, node8); + CXXGraph::DirectedEdge edge12(8, node7, node8); + CXXGraph::T_EdgeSet edgeSet; + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + edgeSet.insert(make_shared>(edge9)); + edgeSet.insert(make_shared>(edge10)); + edgeSet.insert(make_shared>(edge11)); + edgeSet.insert(make_shared>(edge12)); + CXXGraph::Graph graph(edgeSet); + + for (auto x : graph.outEdges(make_shared>(node1))) { + ASSERT_TRUE(x == make_shared>(node2) || + x == make_shared>(node3)); + } + + auto node2_shared = make_shared>(node2); + for (auto x : graph.outEdges(node2_shared)) { + ASSERT_TRUE(x == make_shared>(node4) || + x == make_shared>(node5)); + ASSERT_FALSE(x == make_shared>(node1)); + } +} + TEST(GraphTest, test_inOutEdges) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -346,3 +396,43 @@ TEST(GraphTest, test_inOutEdges) { ASSERT_TRUE(x == make_shared>(node6)); } } + +// Test the overload that takes shared_ptr as input +TEST(GraphTest, test_inOutEdges_shared) { + CXXGraph::Node node1("1", 1); + CXXGraph::Node node2("2", 2); + CXXGraph::Node node3("3", 3); + CXXGraph::Node node4("4", 4); + CXXGraph::Node node5("5", 5); + CXXGraph::Node node6("6", 6); + CXXGraph::Node node7("7", 7); + CXXGraph::Node node8("8", 8); + CXXGraph::UndirectedEdge edge1(1, node1, node2); + CXXGraph::UndirectedEdge edge2(2, node1, node3); + CXXGraph::UndirectedEdge edge3(3, node2, node3); + CXXGraph::UndirectedEdge edge4(4, node2, node4); + CXXGraph::UndirectedEdge edge5(5, node4, node5); + CXXGraph::UndirectedEdge edge6(6, node4, node6); + CXXGraph::UndirectedEdge edge7(7, node6, node7); + CXXGraph::UndirectedEdge edge8(8, node6, node8); + CXXGraph::T_EdgeSet edgeSet; + edgeSet.insert(make_shared>(edge1)); + edgeSet.insert(make_shared>(edge2)); + edgeSet.insert(make_shared>(edge3)); + edgeSet.insert(make_shared>(edge4)); + edgeSet.insert(make_shared>(edge5)); + edgeSet.insert(make_shared>(edge6)); + edgeSet.insert(make_shared>(edge7)); + edgeSet.insert(make_shared>(edge8)); + CXXGraph::Graph graph(edgeSet); + + for (auto x : graph.inOutEdges(make_shared>(node1))) { + ASSERT_TRUE(x == make_shared>(node2) || + x == make_shared>(node3)); + } + + auto node7_shared = make_shared>(node7); + for (auto x : graph.inOutEdges(node7_shared)) { + ASSERT_TRUE(x == make_shared>(node6)); + } +} From 7d897810d4331df3247a5b1e2d9c922f3f82abae Mon Sep 17 00:00:00 2001 From: sbaldu Date: Thu, 1 Jun 2023 10:49:09 +0200 Subject: [PATCH 28/28] Change name of out/inOutEdges methods to out/inOutNeighbors --- include/Graph/Graph.hpp | 45 +++++++++++++++++++++++------------------ test/GraphTest.cpp | 22 ++++++++++---------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index 6cc8b45f1..5981aa9bc 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -20,7 +20,6 @@ #ifndef __CXXGRAPH_GRAPH_H__ #define __CXXGRAPH_GRAPH_H__ -#include #pragma once #include @@ -61,6 +60,7 @@ #include "Partitioning/Utility/Globals.hpp" #include "Utility/ConstString.hpp" #include "Utility/ConstValue.hpp" +#include "Utility/PointerHash.hpp" #include "Utility/Reader.hpp" #include "Utility/ThreadSafe.hpp" #include "Utility/Typedef.hpp" @@ -247,41 +247,46 @@ class Graph { */ virtual const std::shared_ptr> getAdjMatrix() const; /** - * \brief + * \brief This function generates a set of nodes linked to the provided node in a + * directed graph * Note: No Thread Safe * * @param Pointer to the node * */ - virtual const std::unordered_set>, nodeHash> outEdges( + virtual const std::unordered_set>, nodeHash> outNeighbors( const Node *node) const; /** - * \brief + * \brief This function generates a set of nodes linked to the provided node in a + * directed graph * Note: No Thread Safe * * @param Pointer to the node * */ - virtual const std::unordered_set>, nodeHash> outEdges( + virtual const std::unordered_set>, nodeHash> outNeighbors( shared> node) const; /** - * \brief + * \brief This function generates a set of nodes linked to the provided node in + * any graph * Note: No Thread Safe * * @param Pointer to the node * */ virtual const std::unordered_set>, nodeHash> - inOutEdges(const Node *node) const; + inOutNeighbors(const Node *node) const; /** * \brief + * \brief This function generates a set of nodes linked to the provided node in + * any graph * Note: No Thread Safe * * @param Pointer to the node * */ virtual const std::unordered_set>, nodeHash> - inOutEdges(shared> node) const; + inOutNeighbors(shared> node) const; /** * @brief This function finds the subset of given a nodeId * Subset is stored in a map where keys are the hash-id of the node & values @@ -1375,15 +1380,15 @@ const std::shared_ptr> Graph::getAdjMatrix() const { } template -const std::unordered_set>, nodeHash> Graph::outEdges( +const std::unordered_set>, nodeHash> Graph::outNeighbors( const Node *node) const { auto node_shared = make_shared>(*node); - return outEdges(node_shared); + return outNeighbors(node_shared); } template -const std::unordered_set>, nodeHash> Graph::outEdges( +const std::unordered_set>, nodeHash> Graph::outNeighbors( shared> node) const { auto adj = getAdjMatrix(); if (adj->find(node) == adj->end()) { @@ -1391,40 +1396,40 @@ const std::unordered_set>, nodeHash> Graph::outEdges( } auto nodeEdgePairs = adj->at(node); - std::unordered_set>, nodeHash> outEdges; + std::unordered_set>, nodeHash> outNeighbors; for (auto pair : nodeEdgePairs) { if (pair.second->isDirected().has_value() && pair.second->isDirected().value()) { - outEdges.insert(pair.first); + outNeighbors.insert(pair.first); } } - return outEdges; + return outNeighbors; } template const std::unordered_set>, nodeHash> -Graph::inOutEdges(const Node *node) const { +Graph::inOutNeighbors(const Node *node) const { auto node_shared = make_shared>(*node); - return inOutEdges(node_shared); + return inOutNeighbors(node_shared); } template const std::unordered_set>, nodeHash> -Graph::inOutEdges(shared> node) const { +Graph::inOutNeighbors(shared> node) const { auto adj = Graph::getAdjMatrix(); if (adj->find(node) == adj->end()) { return std::unordered_set>, nodeHash>(); } auto nodeEdgePairs = adj->at(node); - std::unordered_set>, nodeHash> inOutEdges; + std::unordered_set>, nodeHash> inOutNeighbors; for (auto pair : nodeEdgePairs) { - inOutEdges.insert(pair.first); + inOutNeighbors.insert(pair.first); } - return inOutEdges; + return inOutNeighbors; } template diff --git a/test/GraphTest.cpp b/test/GraphTest.cpp index 8cd4ef367..f035a6d0b 100644 --- a/test/GraphTest.cpp +++ b/test/GraphTest.cpp @@ -280,25 +280,25 @@ TEST(GraphTest, test_outEdges) { CXXGraph::Graph graph(edgeSet); // Check node 1 - for (auto x : graph.outEdges(&node1)) { + for (auto x : graph.outNeighbors(&node1)) { ASSERT_TRUE(x == make_shared>(node2) || x == make_shared>(node3)); } // Check node 2 - for (auto x : graph.outEdges(&node2)) { + for (auto x : graph.outNeighbors(&node2)) { ASSERT_TRUE(x == make_shared>(node4) || x == make_shared>(node5)); ASSERT_FALSE(x == make_shared>(node1)); } // Check node 5 - for (auto x : graph.outEdges(&node5)) { + for (auto x : graph.outNeighbors(&node5)) { ASSERT_TRUE(x == make_shared>(node6) || x == make_shared>(node7)); ASSERT_FALSE(x == make_shared>(node2)); ASSERT_FALSE(x == make_shared>(node3)); } // Check that node 8 does not have any neighbors - ASSERT_EQ(graph.outEdges(&node8).size(), 0); + ASSERT_EQ(graph.outNeighbors(&node8).size(), 0); } // Test the overload that takes shared_ptr as input @@ -338,13 +338,13 @@ TEST(GraphTest, test_outEdges_shared) { edgeSet.insert(make_shared>(edge12)); CXXGraph::Graph graph(edgeSet); - for (auto x : graph.outEdges(make_shared>(node1))) { + for (auto x : graph.outNeighbors(make_shared>(node1))) { ASSERT_TRUE(x == make_shared>(node2) || x == make_shared>(node3)); } auto node2_shared = make_shared>(node2); - for (auto x : graph.outEdges(node2_shared)) { + for (auto x : graph.outNeighbors(node2_shared)) { ASSERT_TRUE(x == make_shared>(node4) || x == make_shared>(node5)); ASSERT_FALSE(x == make_shared>(node1)); @@ -380,19 +380,19 @@ TEST(GraphTest, test_inOutEdges) { CXXGraph::Graph graph(edgeSet); // Check node 1 - for (auto x : graph.inOutEdges(&node1)) { + for (auto x : graph.inOutNeighbors(&node1)) { ASSERT_TRUE(x == make_shared>(node2) || x == make_shared>(node3)); } // Check node 4 - for (auto x : graph.inOutEdges(&node4)) { + for (auto x : graph.inOutNeighbors(&node4)) { ASSERT_TRUE(x == make_shared>(node2) || x == make_shared>(node3) || x == make_shared>(node5) || x == make_shared>(node6)); } // Check node 7 - for (auto x : graph.inOutEdges(&node7)) { + for (auto x : graph.inOutNeighbors(&node7)) { ASSERT_TRUE(x == make_shared>(node6)); } } @@ -426,13 +426,13 @@ TEST(GraphTest, test_inOutEdges_shared) { edgeSet.insert(make_shared>(edge8)); CXXGraph::Graph graph(edgeSet); - for (auto x : graph.inOutEdges(make_shared>(node1))) { + for (auto x : graph.inOutNeighbors(make_shared>(node1))) { ASSERT_TRUE(x == make_shared>(node2) || x == make_shared>(node3)); } auto node7_shared = make_shared>(node7); - for (auto x : graph.inOutEdges(node7_shared)) { + for (auto x : graph.inOutNeighbors(node7_shared)) { ASSERT_TRUE(x == make_shared>(node6)); } }