From 22ce8f2b3fc6b3fdd1615ba764ac84aa9927c289 Mon Sep 17 00:00:00 2001 From: Daniel Krebs Date: Tue, 30 Jan 2018 19:10:10 +0100 Subject: [PATCH] lib/graph: slightly change interface to allow for custom edges --- fpga/include/villas/directed_graph.hpp | 21 ++++++++++++++----- fpga/tests/graph.cpp | 29 +++++++++++++------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/fpga/include/villas/directed_graph.hpp b/fpga/include/villas/directed_graph.hpp index ca0f763a9..192673f76 100644 --- a/fpga/include/villas/directed_graph.hpp +++ b/fpga/include/villas/directed_graph.hpp @@ -106,20 +106,21 @@ public: return vertex->id; } - EdgeIdentifier addEdge(VertexIdentifier fromVertexId, + EdgeIdentifier addEdge(std::shared_ptr edge, + VertexIdentifier fromVertexId, VertexIdentifier toVertexId) { - std::shared_ptr edge(new EdgeType); + // allocate edge id edge->id = lastEdgeId++; - logger->debug("New edge {}: {} -> {}", edge->id, fromVertexId, toVertexId); - // connect it edge->from = fromVertexId; edge->to = toVertexId; + logger->debug("New edge {}: {} -> {}", edge->id, edge->from, edge->to); + // this is a directed graph, so only push edge to starting vertex - getVertex(fromVertexId)->edges.push_back(edge->id); + getVertex(edge->from)->edges.push_back(edge->id); // add new edge to graph edges[edge->id] = edge; @@ -127,6 +128,16 @@ public: return edge->id; } + + EdgeIdentifier addDefaultEdge(VertexIdentifier fromVertexId, + VertexIdentifier toVertexId) + { + // create a new edge + std::shared_ptr edge(new EdgeType); + + return addEdge(edge, fromVertexId, toVertexId); + } + void removeEdge(EdgeIdentifier edgeId) { auto edge = getEdge(edgeId); diff --git a/fpga/tests/graph.cpp b/fpga/tests/graph.cpp index 4eb25ac75..1de58e9b8 100644 --- a/fpga/tests/graph.cpp +++ b/fpga/tests/graph.cpp @@ -20,10 +20,10 @@ Test(graph, basic, .description = "DirectedGraph") auto v3id = g.addVertex(v3); cr_assert(g.getVertexCount() == 3); - g.addEdge(v1id, v2id); - g.addEdge(v3id, v2id); - g.addEdge(v1id, v3id); - g.addEdge(v2id, v1id); + g.addDefaultEdge(v1id, v2id); + g.addDefaultEdge(v3id, v2id); + g.addDefaultEdge(v1id, v3id); + g.addDefaultEdge(v2id, v1id); cr_assert(g.getEdgeCount() == 4); cr_assert(g.vertexGetEdges(v1id).size() == 2); cr_assert(g.vertexGetEdges(v2id).size() == 1); @@ -40,7 +40,8 @@ Test(graph, path, .description = "Find path") auto logger = loggerGetOrCreate("unittest:path"); logger->info("Testing path finding algorithm"); - villas::graph::DirectedGraph<> g("unittest:path"); + using Graph = villas::graph::DirectedGraph<>; + Graph g("unittest:path"); std::shared_ptr v1(new villas::graph::Vertex); std::shared_ptr v2(new villas::graph::Vertex); @@ -57,18 +58,18 @@ Test(graph, path, .description = "Find path") auto v5id = g.addVertex(v5); auto v6id = g.addVertex(v6); - g.addEdge(v1id, v2id); - g.addEdge(v2id, v3id); + g.addDefaultEdge(v1id, v2id); + g.addDefaultEdge(v2id, v3id); // create circular subgraph - g.addEdge(v4id, v5id); - g.addEdge(v5id, v4id); - g.addEdge(v5id, v6id); + g.addDefaultEdge(v4id, v5id); + g.addDefaultEdge(v5id, v4id); + g.addDefaultEdge(v5id, v6id); g.dump(); logger->info("Find simple path via two edges"); - std::list path1; + std::list path1; cr_assert(g.getPath(v1id, v3id, path1)); logger->info(" Path from {} to {} via:", v1id, v3id); @@ -77,19 +78,19 @@ Test(graph, path, .description = "Find path") } logger->info("Find path between two unconnected sub-graphs"); - std::list path2; + std::list path2; cr_assert(not g.getPath(v1id, v4id, path2)); logger->info(" no path found -> ok"); logger->info("Find non-existing path in circular sub-graph"); - std::list path3; + std::list path3; cr_assert(not g.getPath(v4id, v2id, path3)); logger->info(" no path found -> ok"); logger->info("Find path in circular graph"); - std::list path4; + std::list path4; cr_assert(g.getPath(v4id, v6id, path4)); logger->info(" Path from {} to {} via:", v4id, v6id);