From f7fa19a07967537d35c01190face0c32fddec88e Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Wed, 23 Sep 2015 16:31:12 +0200 Subject: [PATCH] Graph has equal and disjointUnion functions --- lib/graph/graph.hpp | 10 +++++++++ lib/graph/priority_queue.hpp | 7 +++++-- test/graph/fixture.hpp | 5 +++-- test/graph/test_graph.cpp | 31 ++++++++++++++++++++++++++++ test/graph/test_graph_algorithms.cpp | 2 +- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/lib/graph/graph.hpp b/lib/graph/graph.hpp index 68be930..e4e29f8 100644 --- a/lib/graph/graph.hpp +++ b/lib/graph/graph.hpp @@ -73,6 +73,7 @@ public: Graph(const std::vector& vertex_list); Graph(std::initializer_list edge_list); Graph(const std::vector& edge_list); + bool operator==(const Graph& o) const { return m_vertices == o.m_vertices; } void addVertex(const_reference data); void removeVertex(const_reference data); @@ -169,6 +170,15 @@ inline std::vector::Edge> edges(const Graph& g) return retval; } +template +inline Graph disjointUnion(const Graph& a, const Graph& b) { + Graph g(a); + for (const auto& e : edges(b)) + g.addEdge(e.source, e.destination); + + return g; +} + // Graph implementation diff --git a/lib/graph/priority_queue.hpp b/lib/graph/priority_queue.hpp index 9a5123a..2a2a58f 100644 --- a/lib/graph/priority_queue.hpp +++ b/lib/graph/priority_queue.hpp @@ -9,10 +9,13 @@ * The priority queue based Dijkstra (shortest path) algorith requires the * modifyKey functionality, which the std::priority_queue does not have. * - * @note modifyKey is very ineffective, since std::map is not a + * @note modifyKey is not very efficient, since std::map is not a * bidirectional map, looking up an element based on value is linear. * - * @todo replace std::map with a Fibonacci heap to improve performance. + * But not terribly that bad either: keylookup is fast thou the iteration + * over elements with same key is linear. + * + * @todo replace std::map with a Fibonacci or Buffer heap to improve performance. */ template < diff --git a/test/graph/fixture.hpp b/test/graph/fixture.hpp index 8a43fed..ebb0830 100644 --- a/test/graph/fixture.hpp +++ b/test/graph/fixture.hpp @@ -1,6 +1,8 @@ #ifndef GRAPH_TEST_FIXTURE_HPP #define GRAPH_TEST_FIXTURE_HPP +#include + #include inline std::size_t hash_f(float f) { return std::hash()(f); } @@ -16,10 +18,9 @@ struct float2 { }; inline bool operator ==(const float2& v1, const float2& v2) { return v1.x == v2.x && v1.y == v2.y; } +inline bool operator !=(const float2& v1, const float2& v2) { return !(v1 == v2); } inline float pow2(float f) { return f*f; } inline float dist(const float2& v1, const float2& v2) { return sqrt(pow2(v2.x - v1.x) + pow2(v2.y - v1.y)); } -// inline float dist(const float2& v1, const float2& v2) { return abs(v2.x - v1.x) + abs(v2.y - v1.y); } - namespace std { diff --git a/test/graph/test_graph.cpp b/test/graph/test_graph.cpp index f23d140..f4aa0eb 100644 --- a/test/graph/test_graph.cpp +++ b/test/graph/test_graph.cpp @@ -4,8 +4,39 @@ #include "fixture.hpp" +#include +#include + TEST_CASE( "Graph creation", "[graph][data_structure]" ) { + SECTION("rule of 3+2") { + REQUIRE ( std::is_default_constructible >::value == true); + REQUIRE ( std::is_copy_constructible >::value == true); + REQUIRE ( std::is_move_constructible >::value == true); + + Graph g1 = { 1, 2, 3 }; + Graph g2 = { 4, 5, 6 }; + Graph g3(g1); // cctor + REQUIRE ( g3 == g1 ); + + // catch does not likes templates in REQUIRE + constexpr bool is_assignable = std::is_assignable, Graph >::value; + REQUIRE ( is_assignable == true); + REQUIRE ( std::is_copy_assignable >::value == true); + REQUIRE ( std::is_move_assignable >::value == true); + + g3 = g2; // assign + REQUIRE ( g3 == g2 ); + + Graph g4 = g1; // cctor + Graph g5 = std::move(g1); // move ctor, g1 become invalid + REQUIRE ( g4 == g5 ); + + Graph g6 = g2; // cctor + g1 = std::move(g2); // move assign + REQUIRE ( g1 == g6 ); + } + SECTION("Initial state") { Graph g; REQUIRE( empty(g) == true ); diff --git a/test/graph/test_graph_algorithms.cpp b/test/graph/test_graph_algorithms.cpp index 4379bbd..163d328 100644 --- a/test/graph/test_graph_algorithms.cpp +++ b/test/graph/test_graph_algorithms.cpp @@ -103,7 +103,7 @@ TEST_CASE("Graph algorithms, small", "[graph][algorithm][dijkstra]" ) { TEST_CASE_METHOD(Fixture, "Graph algorithms, big graph", "[graph][algorithm][dijkstra][performance]" ) { - constexpr std::size_t number_of_rows = 300; + constexpr std::size_t number_of_rows = 1000; constexpr std::size_t number_of_columns = number_of_rows; Fixture::initOnce(number_of_rows, number_of_columns);