From 45760d997a636a0ab340a17edbf7a2323b2a8d07 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Fri, 12 May 2023 15:19:47 +0200 Subject: [PATCH 1/9] 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 f9afefa2db0fa8141033a9e43f5e2a56adc0a62c Mon Sep 17 00:00:00 2001 From: sbaldu Date: Sat, 13 May 2023 01:25:13 +0200 Subject: [PATCH 2/9] Add setter for data attribute and pass data with move in constructor --- include/Node/Node.hpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/include/Node/Node.hpp b/include/Node/Node.hpp index 29fe4b7b2..d3755cb6a 100755 --- a/include/Node/Node.hpp +++ b/include/Node/Node.hpp @@ -39,11 +39,12 @@ class Node { void setId(const std::string &); public: - Node(const std::string &, const T &data); + Node(const std::string &, T data); ~Node() = default; const std::size_t &getId() const; const std::string &getUserId() const; const T &getData() const; + void setData(T new_data); // operator bool operator==(const Node &b) const; bool operator<(const Node &b) const; @@ -51,11 +52,11 @@ class Node { }; template -Node::Node(const std::string &id, const T &data) { +Node::Node(const std::string& id, T data) { this->userId = id; // the userid is set as sha512 hash of the user provided id setId(id); - this->data = data; + this->data = std::move(data); } template @@ -99,6 +100,12 @@ const T &Node::getData() const { return data; } +template +void Node::setData(T new_data) { + data = std::move(new_data); +} + +// The data type T must have an overload of the equality operator template bool Node::operator==(const Node &b) const { return (this->id == b.id && this->data == b.data); @@ -110,6 +117,7 @@ bool Node::operator<(const Node &b) const { } // ostream overload +// The data type T must have an overload of the ostream operator template std::ostream &operator<<(std::ostream &os, const Node &node) { os << "Node: {\n" @@ -118,4 +126,4 @@ std::ostream &operator<<(std::ostream &os, const Node &node) { } } // namespace CXXGraph -#endif // __CXXGRAPH_NODE_H__ \ No newline at end of file +#endif // __CXXGRAPH_NODE_H__ From 941ae45288b8e66ec2dcf02dc4063b7d8eb450bb Mon Sep 17 00:00:00 2001 From: sbaldu Date: Sat, 13 May 2023 23:09:01 +0200 Subject: [PATCH 3/9] Use std::move when copying node data --- include/Graph/Graph.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index f6496d0eb..c2d35dd4f 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -909,7 +909,7 @@ void Graph::recreateGraph( // Create new Node T feat; if (nodeFeatMap.find(edgeIt.second.first) != nodeFeatMap.end()) { - feat = nodeFeatMap.at(edgeIt.second.first); + feat = std::move(nodeFeatMap.at(edgeIt.second.first)); } node1 = new Node(edgeIt.second.first, feat); nodeMap[edgeIt.second.first] = node1; @@ -920,7 +920,7 @@ void Graph::recreateGraph( // Create new Node T feat; if (nodeFeatMap.find(edgeIt.second.second) != nodeFeatMap.end()) { - feat = nodeFeatMap.at(edgeIt.second.second); + feat = std::move(nodeFeatMap.at(edgeIt.second.second)); } node2 = new Node(edgeIt.second.second, feat); nodeMap[edgeIt.second.second] = node2; From fe997477defa36147a0a30081e6eb427b3fedc78 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Sat, 13 May 2023 23:09:58 +0200 Subject: [PATCH 4/9] Add tests for edges with non-native node data types --- test/DirectedEdgeTest.cpp | 46 +++++++++++++++++++++++++++ test/DirectedWeightedEdgeTest.cpp | 46 +++++++++++++++++++++++++++ test/EdgeTest.cpp | 40 +++++++++++++++++++++++- test/UndirectedEdgeTest.cpp | 48 ++++++++++++++++++++++++++++- test/UndirectedWeightedEdgeTest.cpp | 46 +++++++++++++++++++++++++++ 5 files changed, 224 insertions(+), 2 deletions(-) diff --git a/test/DirectedEdgeTest.cpp b/test/DirectedEdgeTest.cpp index 8ae35ec6e..17e8855d2 100644 --- a/test/DirectedEdgeTest.cpp +++ b/test/DirectedEdgeTest.cpp @@ -52,6 +52,52 @@ TEST(DirectedEdgeTest, Constructor_5) { ASSERT_FALSE(edge.isWeighted().value()); } +TEST(DirectedEdgeTest, Bool_data) { + // First constructor + CXXGraph::Node node1("1", true); + CXXGraph::Node node2("2", false); + CXXGraph::DirectedEdge edge1(1, node1, node2); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + ASSERT_TRUE(edge1.isDirected().value()); + ASSERT_FALSE(edge1.isWeighted().value()); + + // Second constructor + CXXGraph::Node node3("3", true); + CXXGraph::Node node4("4", false); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::DirectedEdge edge2(2, pairNode); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); + ASSERT_TRUE(edge2.isDirected().value()); + ASSERT_FALSE(edge2.isWeighted().value()); +} + +TEST(DirectedEdgeTest, String_data) { + // First constructor + CXXGraph::Node node1("1", "On"); + CXXGraph::Node node2("2", "Off"); + CXXGraph::DirectedEdge edge1(1, node1, node2); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + ASSERT_TRUE(edge1.isDirected().value()); + ASSERT_FALSE(edge1.isWeighted().value()); + + // Second constructor + CXXGraph::Node node3("3", "On"); + CXXGraph::Node node4("4", "Off"); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::DirectedEdge edge2(2, pairNode); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); + ASSERT_TRUE(edge2.isDirected().value()); + ASSERT_FALSE(edge2.isWeighted().value()); +} + TEST(DirectedEdgeTest, Cast_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); diff --git a/test/DirectedWeightedEdgeTest.cpp b/test/DirectedWeightedEdgeTest.cpp index dff2ddd04..8eed0f700 100644 --- a/test/DirectedWeightedEdgeTest.cpp +++ b/test/DirectedWeightedEdgeTest.cpp @@ -85,6 +85,52 @@ TEST(DirectedWeightedEdgeTest, Constructor_7) { ASSERT_EQ(edge.getWeight(), 0.0); } +TEST(DirectedWeightedEdgeTest, Bool_data) { + // First constructor + CXXGraph::Node node1("1", true); + CXXGraph::Node node2("2", false); + CXXGraph::DirectedWeightedEdge edge1(1, node1, node2, 8); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + ASSERT_TRUE(edge1.isDirected().value()); + ASSERT_TRUE(edge1.isWeighted().value()); + + // Second constructor + CXXGraph::Node node3("3", true); + CXXGraph::Node node4("4", false); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::DirectedWeightedEdge edge2(2, pairNode, 2); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); + ASSERT_TRUE(edge2.isDirected().value()); + ASSERT_TRUE(edge2.isWeighted().value()); +} + +TEST(DirectedWeightedEdgeTest, String_data) { + // First constructor + CXXGraph::Node node1("1", "On"); + CXXGraph::Node node2("2", "Off"); + CXXGraph::DirectedWeightedEdge edge1(1, node1, node2, 4); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + ASSERT_TRUE(edge1.isDirected().value()); + ASSERT_TRUE(edge1.isWeighted().value()); + + // Second constructor + CXXGraph::Node node3("3", "On"); + CXXGraph::Node node4("4", "Off"); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::DirectedWeightedEdge edge2(2, pairNode, 6); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); + ASSERT_TRUE(edge2.isDirected().value()); + ASSERT_TRUE(edge2.isWeighted().value()); +} + TEST(DirectedWeightedEdgeTest, Cast_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); diff --git a/test/EdgeTest.cpp b/test/EdgeTest.cpp index 329491c64..fd4a6e823 100644 --- a/test/EdgeTest.cpp +++ b/test/EdgeTest.cpp @@ -28,6 +28,44 @@ TEST(EdgeTest, print_1) { std::cout << edge << std::endl; } +TEST(EdgeTest, Bool_data) { + // First constructor + CXXGraph::Node node1("1", true); + CXXGraph::Node node2("2", false); + CXXGraph::Edge edge1(1, node1, node2); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + + // Second constructor + CXXGraph::Node node3("3", true); + CXXGraph::Node node4("4", false); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::Edge edge2(2, pairNode); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); +} + +TEST(EdgeTest, String_data) { + // First constructor + CXXGraph::Node node1("1", "On"); + CXXGraph::Node node2("2", "Off"); + CXXGraph::Edge edge1(1, node1, node2); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + + // Second constructor + CXXGraph::Node node3("3", "On"); + CXXGraph::Node node4("4", "Off"); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::Edge edge2(2, pairNode); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); +} + TEST(EdgeTest, test) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -54,4 +92,4 @@ TEST(EdgeTest, test) { adj = graph.getAdjMatrix(); ASSERT_EQ(adj->size(), 1); std::cout << *adj << std::endl; -} \ No newline at end of file +} diff --git a/test/UndirectedEdgeTest.cpp b/test/UndirectedEdgeTest.cpp index 05e270972..6b73fcea0 100644 --- a/test/UndirectedEdgeTest.cpp +++ b/test/UndirectedEdgeTest.cpp @@ -52,6 +52,52 @@ TEST(UndirectedEdgeTest, Constructor_5) { ASSERT_FALSE(edge.isWeighted().value()); } +TEST(UndirectedEdgeTest, Bool_data) { + // First constructor + CXXGraph::Node node1("1", true); + CXXGraph::Node node2("2", false); + CXXGraph::UndirectedEdge edge1(1, node1, node2); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + ASSERT_FALSE(edge1.isDirected().value()); + ASSERT_FALSE(edge1.isWeighted().value()); + + // Second constructor + CXXGraph::Node node3("3", true); + CXXGraph::Node node4("4", false); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::UndirectedEdge edge2(2, pairNode); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); + ASSERT_FALSE(edge2.isDirected().value()); + ASSERT_FALSE(edge2.isWeighted().value()); +} + +TEST(UndirectedEdgeTest, String_data) { + // First constructor + CXXGraph::Node node1("1", "On"); + CXXGraph::Node node2("2", "Off"); + CXXGraph::UndirectedEdge edge1(1, node1, node2); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + ASSERT_FALSE(edge1.isDirected().value()); + ASSERT_FALSE(edge1.isWeighted().value()); + + // Second constructor + CXXGraph::Node node3("3", "On"); + CXXGraph::Node node4("4", "Off"); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::UndirectedEdge edge2(2, pairNode); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); + ASSERT_FALSE(edge2.isDirected().value()); + ASSERT_FALSE(edge2.isWeighted().value()); +} + TEST(UndirectedEdgeTest, Cast_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); @@ -87,4 +133,4 @@ TEST(UndirectedEdgeTest, print_1) { CXXGraph::UndirectedEdge edge(1, node1, node2); std::cout << "Test Print UndirectedEdge" << std::endl; std::cout << edge << std::endl; -} \ No newline at end of file +} diff --git a/test/UndirectedWeightedEdgeTest.cpp b/test/UndirectedWeightedEdgeTest.cpp index 95ce7ae82..2fd84e627 100644 --- a/test/UndirectedWeightedEdgeTest.cpp +++ b/test/UndirectedWeightedEdgeTest.cpp @@ -85,6 +85,52 @@ TEST(UndirectedWeightedEdgeTest, Constructor_7) { ASSERT_EQ(edge.getWeight(), 0.0); } +TEST(UndirectedWeightedEdgeTest, Bool_data) { + // First constructor + CXXGraph::Node node1("1", true); + CXXGraph::Node node2("2", false); + CXXGraph::UndirectedWeightedEdge edge1(1, node1, node2, 4); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + ASSERT_FALSE(edge1.isDirected().value()); + ASSERT_TRUE(edge1.isWeighted().value()); + + // Second constructor + CXXGraph::Node node3("3", true); + CXXGraph::Node node4("4", false); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::UndirectedWeightedEdge edge2(2, pairNode, 7); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); + ASSERT_FALSE(edge2.isDirected().value()); + ASSERT_TRUE(edge2.isWeighted().value()); +} + +TEST(UndirectedWeightedEdgeTest, String_data) { + // First constructor + CXXGraph::Node node1("1", "On"); + CXXGraph::Node node2("2", "Off"); + CXXGraph::UndirectedWeightedEdge edge1(1, node1, node2, 3); + ASSERT_EQ(*(edge1.getNodePair().first), node1); + ASSERT_EQ(*(edge1.getNodePair().second), node2); + ASSERT_FALSE(edge1.isDirected().value()); + ASSERT_TRUE(edge1.isWeighted().value()); + + // Second constructor + CXXGraph::Node node3("3", "On"); + CXXGraph::Node node4("4", "Off"); + std::pair *, const CXXGraph::Node *> pairNode( + &node3, &node4); + CXXGraph::UndirectedWeightedEdge edge2(2, pairNode, 5); + ASSERT_EQ(edge2.getNodePair(), pairNode); + ASSERT_EQ(*(edge2.getNodePair().first), node3); + ASSERT_EQ(*(edge2.getNodePair().second), node4); + ASSERT_FALSE(edge2.isDirected().value()); + ASSERT_TRUE(edge2.isWeighted().value()); +} + TEST(UndirectedWeightedEdgeTest, Cast_1) { CXXGraph::Node node1("1", 1); CXXGraph::Node node2("2", 2); From 7e90d213ffbf9508a17fe555523f9bb332723569 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Sun, 14 May 2023 21:08:37 +0200 Subject: [PATCH 5/9] Add methods for changing the data of nodes --- include/Graph/Graph.hpp | 51 +++++++++++++++++++++++++++++++++++++++++ include/Node/Node.hpp | 4 ++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index c2d35dd4f..c932aefb1 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -87,6 +87,10 @@ template class Graph { private: T_EdgeSet edgeSet = {}; + + // Private non-const getter for the set of nodes + std::set *> nodeSet(); + std::optional> getExtenstionAndSeparator( InputOutputFormat format) const; void writeGraphToStream(std::ostream &oGraph, std::ostream &oNodeFeat, @@ -161,6 +165,23 @@ class Graph { * */ virtual const std::set *> getNodeSet() const; + /** + * \brief + * Function that sets the data contained in a node + * + * @param nodeUserId The userId string of the node whose data is to be changes + * @param data The new value for the node data + * + */ + virtual void setNodeData(const std::string &nodeUserId, T data); + /** + * \brief + * Function that sets the data contained in every node of the graph + * + * @param dataMap Map of the userId of every node with its new data value + * + */ + virtual void setNodeData(std::map &dataMap); /** * \brief * Function that return an Edge with specific ID if Exist in the Graph @@ -641,6 +662,25 @@ const std::set *> Graph::getNodeSet() const { return nodeSet; } +template +void Graph::setNodeData(const std::string &nodeUserId, T data) { + for(auto &nodeSetIt : this->getNodeSet()) { + nodeIt = const_cast *>(nodeSetIt); + if (nodeIt->getUserId() == nodeUserId) { + nodeIt->setData(std::move(data)); + break; + } + } +} + +template +void Graph::setNodeData(std::map &dataMap) { + // Construct the set of all the nodes in the graph + for(auto &nodeSetIt : this->nodeSet()) { + nodeSetIt->setData(std::move(dataMap[nodeSetIt->getUserId()])); + } +} + template const std::optional *> Graph::getEdge( const unsigned long long edgeId) const { @@ -653,6 +693,17 @@ const std::optional *> Graph::getEdge( return std::nullopt; } +template +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)); + } + + return nodeSet; +} + template std::optional> Graph::getExtenstionAndSeparator( InputOutputFormat format) const { diff --git a/include/Node/Node.hpp b/include/Node/Node.hpp index d3755cb6a..4e28b76b0 100755 --- a/include/Node/Node.hpp +++ b/include/Node/Node.hpp @@ -44,7 +44,7 @@ class Node { const std::size_t &getId() const; const std::string &getUserId() const; const T &getData() const; - void setData(T new_data); + void setData(T&& new_data); // operator bool operator==(const Node &b) const; bool operator<(const Node &b) const; @@ -101,7 +101,7 @@ const T &Node::getData() const { } template -void Node::setData(T new_data) { +void Node::setData(T&& new_data) { data = std::move(new_data); } From bd665fa62f9f3c4723f63d895db89c44948034b1 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Sun, 14 May 2023 21:10:22 +0200 Subject: [PATCH 6/9] Add tests for new setNodeData methods --- test/GraphTest.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/test/GraphTest.cpp b/test/GraphTest.cpp index 5b9bf2e39..e28f8659c 100644 --- a/test/GraphTest.cpp +++ b/test/GraphTest.cpp @@ -179,4 +179,50 @@ TEST(GraphTest, adj_print_6) { CXXGraph::Graph graph(edgeSet); std::cout << "Test Print Adjacency Matrix" << std::endl; std::cout << *graph.getAdjMatrix() << std::endl; -} \ No newline at end of file +} + +TEST(GraphTest, set_data) { + // Create the graph + CXXGraph::Node node1("1", 1); + CXXGraph::Node node2("2", 2); + CXXGraph::Node node3("3", 3); + std::pair *, const CXXGraph::Node *> pairNode( + &node1, &node2); + CXXGraph::DirectedEdge edge1(1, pairNode); + 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); + CXXGraph::Graph graph(edgeSet); + + + std::map initial_values; + // Construct map with the initial values of the nodes data + for (const auto& nodeIt : graph.getNodeSet()) { + initial_values[nodeIt->getUserId()] = nodeIt->getData(); + } + // Change the data contained in the nodes singularly + std::map new_values; + for (const auto& nodeIt : graph.getNodeSet()) { + int r = std::rand(); + graph.setNodeData(nodeIt->getUserId(), r); + new_values[nodeIt->getUserId()] = r; + } + // Check the final values of the node data + for (const auto& nodeIt : graph.getNodeSet()) { + ASSERT_EQ(nodeIt->getData(), new_values[nodeIt->getUserId()]); + } + + // Now set the data of all the nodes at once + std::map data_values; + for (const auto& nodeIt : graph.getNodeSet()) { + int r = std::rand(); + data_values[nodeIt->getUserId()] = r; + } + graph.setNodeData(data_values); + for (const auto& nodeIt : graph.getNodeSet()) { + ASSERT_EQ(nodeIt->getData(), data_values[nodeIt->getUserId()]); + } +} From 3fe7ea51b2f1c381948bc7b9d8643f7778f1bbb1 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Mon, 15 May 2023 10:34:27 +0200 Subject: [PATCH 7/9] Fix in the first overload of setNodeData --- include/Graph/Graph.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index c932aefb1..f11bd8c70 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -664,10 +664,9 @@ const std::set *> Graph::getNodeSet() const { template void Graph::setNodeData(const std::string &nodeUserId, T data) { - for(auto &nodeSetIt : this->getNodeSet()) { - nodeIt = const_cast *>(nodeSetIt); - if (nodeIt->getUserId() == nodeUserId) { - nodeIt->setData(std::move(data)); + for(auto &nodeSetIt : this->nodeSet()) { + if (nodeSetIt->getUserId() == nodeUserId) { + nodeSetIt->setData(std::move(data)); break; } } From 5f343331a54fd365d0165c1ec39b01baa8670808 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Mon, 15 May 2023 10:55:03 +0200 Subject: [PATCH 8/9] Add move constructor for Node class --- include/Node/Node.hpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/include/Node/Node.hpp b/include/Node/Node.hpp index 4e28b76b0..a9578e401 100755 --- a/include/Node/Node.hpp +++ b/include/Node/Node.hpp @@ -39,7 +39,9 @@ class Node { void setId(const std::string &); public: - Node(const std::string &, T data); + Node(const std::string &, const T& data); + // Move constructor + Node(const std::string &, T&& data); ~Node() = default; const std::size_t &getId() const; const std::string &getUserId() const; @@ -52,7 +54,15 @@ class Node { }; template -Node::Node(const std::string& id, T data) { +Node::Node(const std::string& id, const T& data) { + this->userId = id; + // the userid is set as sha512 hash of the user provided id + setId(id); + this->data = data; +} + +template +Node::Node(const std::string& id, T&& data) { this->userId = id; // the userid is set as sha512 hash of the user provided id setId(id); From f8ceb79ee3fc9d5d792ec8ac33e5e47d69b9c146 Mon Sep 17 00:00:00 2001 From: sbaldu Date: Mon, 15 May 2023 17:31:01 +0200 Subject: [PATCH 9/9] Fix move constructor --- include/Node/Node.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/Node/Node.hpp b/include/Node/Node.hpp index a9578e401..e606e1142 100755 --- a/include/Node/Node.hpp +++ b/include/Node/Node.hpp @@ -41,7 +41,7 @@ class Node { public: Node(const std::string &, const T& data); // Move constructor - Node(const std::string &, T&& data); + Node(const std::string &, T&& data) noexcept; ~Node() = default; const std::size_t &getId() const; const std::string &getUserId() const; @@ -62,11 +62,11 @@ Node::Node(const std::string& id, const T& data) { } template -Node::Node(const std::string& id, T&& data) { +Node::Node(const std::string& id, T&& data) noexcept { this->userId = id; // the userid is set as sha512 hash of the user provided id setId(id); - this->data = std::move(data); + std::swap(this->data, data); } template @@ -112,7 +112,7 @@ const T &Node::getData() const { template void Node::setData(T&& new_data) { - data = std::move(new_data); + this->data = std::move(new_data); } // The data type T must have an overload of the equality operator