diff --git a/include/graph_t.hpp b/include/graph_t.hpp index e0196fe8cd459e1fb86d52954866d413464d8f5e..0f50eddb2a40a713b1b1d8c97393109b718257bb 100644 --- a/include/graph_t.hpp +++ b/include/graph_t.hpp @@ -2,6 +2,7 @@ #define __GRAPH_T_HPP__ #include <exception> +#include <memory> #include <map> #include <vector> #include <string> @@ -44,13 +45,13 @@ template<class ND, class ED> class Graph { struct Node { int node_id; - ND data; + std::shared_ptr<ND> data; }; struct Edge { int edge_id; - ED data; int source_id; int dest_id; + std::shared_ptr<ED> data; }; /* data structures */ @@ -87,7 +88,7 @@ public: inline int add_node(const ND &m) { Node node; node.node_id = id_counter; - node.data = m; + node.data = std::make_shared<ND>(m); nodes[id_counter] = node; dests[id_counter] = std::vector<int>(); return id_counter++; @@ -110,7 +111,8 @@ public: Edge edge; edge.edge_id = edge_counter; - edge.data = m; + edge.data = std::make_shared<ED>(m); + *edge.data = m; edge.source_id = source_id; edge.dest_id = dest_id; edges[edge_counter] = edge; @@ -138,14 +140,14 @@ public: return -1; } - inline ND get_node_data(int node_id) const { + inline std::shared_ptr<ND> get_node_data(int node_id) const { if (!node_exist(node_id)) { throw NodeNotFound(node_id); } return nodes.at(node_id).data; } - inline ED get_edge_data(int edge_id) const { + inline std::shared_ptr<ED> get_edge_data(int edge_id) const { if (edges.find(edge_id) == edges.end()) { throw EdgeNotFound(edge_id); } @@ -269,7 +271,7 @@ public: int edge_id = find_edge(current_node, dest); if (edge_id == -1) continue; // If no edge, skip - int weight = f(edges.at(edge_id).data); + int weight = f(*edges.at(edge_id).data); int distance = current_distance + weight; // If a shorter path to the successor has been found, update it diff --git a/test/test_gnt b/test/test_gnt index f2cc8adbf87dca21b2425ed47927aef0bb300076..8718ac9df67514b3a9dba05abe8e99c413997174 100755 Binary files a/test/test_gnt and b/test/test_gnt differ diff --git a/test/test_gnt.cpp b/test/test_gnt.cpp index 02dc8302e27a9404a6d52507bf65c4398f91d63b..0bb33457289c8c6095496fa81389a924931483c7 100644 --- a/test/test_gnt.cpp +++ b/test/test_gnt.cpp @@ -7,31 +7,31 @@ using namespace std; -SCENARIO("Graph properties", "[graph nt]") +SCENARIO("Graph properties", "[graph t]") { GIVEN("A graph with some items") { Graph<string, string> g; - int a = g.add_node("A"); - int b = g.add_node("B"); - int c = g.add_node("C"); - int d = g.add_node("D"); - int ab = g.add_edge("msg-ab", a,b); - int ac = g.add_edge("msg-ac", a,c); - int bd = g.add_edge("msg-bd", b,d); - int cd = g.add_edge("msg-cd", c,d); - - REQUIRE(g.get_node_data(a) == "A"); - REQUIRE(g.get_node_data(b) == "B"); - REQUIRE(g.get_node_data(c) == "C"); - REQUIRE(g.get_node_data(d) == "D"); + auto a = g.add_node("A"); + auto b = g.add_node("B"); + auto c = g.add_node("C"); + auto d = g.add_node("D"); + auto ab = g.add_edge("msg-ab", a,b); + auto ac = g.add_edge("msg-ac", a,c); + auto bd = g.add_edge("msg-bd", b,d); + auto cd = g.add_edge("msg-cd", c,d); + + REQUIRE(*g.get_node_data(a) == "A"); + REQUIRE(*g.get_node_data(b) == "B"); + REQUIRE(*g.get_node_data(c) == "C"); + REQUIRE(*g.get_node_data(d) == "D"); REQUIRE(ab == 0); REQUIRE(ac == 1); - REQUIRE(g.get_edge_data(ab) == "msg-ab"); - REQUIRE(g.get_edge_data(ac) == "msg-ac"); - REQUIRE(g.get_edge_data(cd) == "msg-cd"); - REQUIRE(g.get_edge_data(bd) == "msg-bd"); + REQUIRE(*g.get_edge_data(ab) == "msg-ab"); + REQUIRE(*g.get_edge_data(ac) == "msg-ac"); + REQUIRE(*g.get_edge_data(cd) == "msg-cd"); + REQUIRE(*g.get_edge_data(bd) == "msg-bd"); vector<int> succ_a = {b, c}; REQUIRE(g.get_successors(a) == succ_a); @@ -57,8 +57,7 @@ SCENARIO("Graph properties", "[graph nt]") } } - -SCENARIO("Computing paths", "[graph nt]") +SCENARIO("Computing paths", "[graph t]") { GIVEN("A graph with some elements and no loops") { Graph<string, string> g; @@ -83,7 +82,7 @@ SCENARIO("Computing paths", "[graph nt]") } } -SCENARIO("Shortest paths", "[graph nt]") +SCENARIO("Shortest paths", "[graph t]") { GIVEN("A graph with some elements and no loops") { Graph<string, string> g; @@ -114,7 +113,7 @@ SCENARIO("Shortest paths", "[graph nt]") using EdgeProps = EdgeData<RouteLength>; -SCENARIO("Decorators", "[graph nt]") +SCENARIO("Decorators", "[graph t]") { GIVEN("A graph with some items") { @@ -137,18 +136,18 @@ SCENARIO("Decorators", "[graph nt]") int bd = g.add_edge(_bd, b,d); int cd = g.add_edge(_cd, c,d); - REQUIRE(g.get_node_data(a) == "A"); - REQUIRE(g.get_node_data(b) == "B"); - REQUIRE(g.get_node_data(c) == "C"); - REQUIRE(g.get_node_data(d) == "D"); + REQUIRE(*g.get_node_data(a) == "A"); + REQUIRE(*g.get_node_data(b) == "B"); + REQUIRE(*g.get_node_data(c) == "C"); + REQUIRE(*g.get_node_data(d) == "D"); REQUIRE(ab == 0); REQUIRE(ac == 1); - EdgeProps __ab = g.get_edge_data(ab); - EdgeProps __ac = g.get_edge_data(ac); - EdgeProps __cd = g.get_edge_data(cd); - EdgeProps __bd = g.get_edge_data(bd); + EdgeProps __ab = *g.get_edge_data(ab); + EdgeProps __ac = *g.get_edge_data(ac); + EdgeProps __cd = *g.get_edge_data(cd); + EdgeProps __bd = *g.get_edge_data(bd); REQUIRE(__ab.get_string() == "msg-ab"); REQUIRE(__ac.get_string() == "msg-ac"); REQUIRE(__cd.get_string() == "msg-cd");