From 5160549dc9469bd5f09e647feabeb7dbebc8243f Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Tue, 12 May 2015 11:40:32 +0200 Subject: [PATCH] Graph all locals are auto typed. --- lib/graph/graph.hpp | 43 ++++++++++---- test/graph/test_graph.hpp | 120 ++++++++++++-------------------------- 2 files changed, 69 insertions(+), 94 deletions(-) diff --git a/lib/graph/graph.hpp b/lib/graph/graph.hpp index 6d2f75b..01d730c 100644 --- a/lib/graph/graph.hpp +++ b/lib/graph/graph.hpp @@ -61,8 +61,11 @@ public: Graph() : m_vertices() {} Graph(const Graph& o) : m_vertices(o.m_vertices) {} + Graph(Graph&& o) : m_vertices(std::move(o.m_vertices)) {} Graph(std::initializer_list vertex_list); + Graph(const std::vector& vertex_list); Graph(std::initializer_list edge_list); + Graph(const std::vector& edge_list); Graph& operator=(Graph o) { swap(o); return *this; } void swap(Graph& o) { std::swap(m_vertices, o.m_vertices); } @@ -164,7 +167,15 @@ template inline Graph::Graph(std::initializer_list vertex_list) : Graph() { - for(const V& v : vertex_list) + for(const auto& v : vertex_list) + addVertex(v); +} + +template +inline Graph::Graph(const std::vector& vertex_list) + : Graph() +{ + for(const auto& v : vertex_list) addVertex(v); } @@ -172,7 +183,15 @@ template inline Graph::Graph(std::initializer_list edge_list) : Graph() { - for (const Edge& e : edge_list ) + for (const auto& e : edge_list ) + addEdge(e.source, e.destination); +} + +template +inline Graph::Graph(const std::vector& edge_list) + : Graph() +{ + for (const auto& e : edge_list ) addEdge(e.source, e.destination); } @@ -198,10 +217,10 @@ inline void Graph::modifyVertex(const_reference old_data, const_reference new if (old_data == new_data) return; - std::vector neighbours = neighboursOf(old_data); + auto neighbours = neighboursOf(old_data); for (auto &v : neighbours) { - std::vector& n_v = nonConstNeighboursOf(v); - typename std::vector::iterator n_it = std::find(n_v.begin(), n_v.end(), old_data); + auto& n = nonConstNeighboursOf(v); + auto n_it = std::find(n.begin(), n.end(), old_data); *n_it = new_data; } const auto number_of_removed_elements = m_vertices.erase(old_data); @@ -225,8 +244,8 @@ inline void Graph::addEdge(const_reference source, const_reference destinatio addVertex(source); addVertex(destination); - v_iterator source_it = m_vertices.find(source); - v_iterator destination_it = m_vertices.find(destination); + auto source_it = m_vertices.find(source); + auto destination_it = m_vertices.find(destination); source_it->second.push_back(destination); destination_it->second.push_back(source); @@ -236,7 +255,7 @@ template inline void Graph::setEdges(const_reference source, const std::vector& destinations) { addVertex(source); - v_iterator source_it = m_vertices.find(source); + auto source_it = m_vertices.find(source); source_it->second.clear(); source_it->second = destinations; @@ -245,11 +264,11 @@ inline void Graph::setEdges(const_reference source, const std::vector inline void Graph::removeEdge(const_reference source, const_reference destination) { - v_iterator source_it = m_vertices.find(source); + auto source_it = m_vertices.find(source); if (source_it == m_vertices.end()) return; - v_iterator destination_it = m_vertices.find(destination); + auto destination_it = m_vertices.find(destination); if (destination_it == m_vertices.end()) return; @@ -270,7 +289,7 @@ inline std::vector::value_type> Graph::vertices() const template inline std::vector Graph::neighboursOf(const_reference data) const { - v_const_iterator vertex_it = m_vertices.find(data); + auto vertex_it = m_vertices.find(data); if (vertex_it == m_vertices.end()) return std::vector(); else @@ -280,7 +299,7 @@ inline std::vector Graph::neighboursOf(const_reference data) const template inline std::vector& Graph::nonConstNeighboursOf(const_reference data) { - v_iterator vertex_it = m_vertices.find(data); + auto vertex_it = m_vertices.find(data); return vertex_it->second; } diff --git a/test/graph/test_graph.hpp b/test/graph/test_graph.hpp index c39da43..08adff2 100644 --- a/test/graph/test_graph.hpp +++ b/test/graph/test_graph.hpp @@ -2,10 +2,6 @@ #include "../catch.hpp" -#include -#include -#include - TEST_CASE( "Graph creation", "[graph][data_structure]" ) { SECTION("Initial state") { @@ -154,6 +150,15 @@ TEST_CASE( "Graph adding vertices", "[graph][data_structure]" ) { REQUIRE( contains(g, 2) == true ); } + SECTION("Modify vertex to the same") { + Graph g; + g.addVertex(1); + REQUIRE( contains(g, 1) == true ); + + g.modifyVertex(1, 1); + REQUIRE( contains(g, 1) == true ); + } + SECTION("get array of vertices") { Graph g; g.addVertex(1); @@ -215,6 +220,21 @@ TEST_CASE( "Graph adding edges", "[graph][data_structure]" ) { REQUIRE( numberOfEdges(g) == 0 ); } + SECTION("Adding some edges, one edge mentioned from both directions") { + Graph g = { {1, 2}, {2, 1} }; + REQUIRE( numberOfVertices(g) == 2 ); + REQUIRE( numberOfEdges(g) == 1*2 ); + } + + SECTION("Adding some edges, one edge mentioned from both directions v2") { + Graph g = { {1, 2}, {1, 3}, {1, 4}, + {2, 1}, {2, 3}, {2, 4}, + {3, 1}, {3, 2}, {3, 4}, + {4, 1}, {4, 2}, {4, 3} }; + REQUIRE( numberOfVertices(g) == 4 ); + REQUIRE( numberOfEdges(g) == 4*3 ); + } + SECTION("Adding some edges then clear") { Graph g = { {1, 2}, {1, 3}, {3, 4} }; g.clear(); @@ -228,6 +248,20 @@ TEST_CASE( "Graph adding edges", "[graph][data_structure]" ) { REQUIRE( numberOfEdges(g) == 3*2 ); } + SECTION("Removing edge with unexisting source") { + Graph g = { {1, 2}, {1, 3}, {3, 4} }; + REQUIRE( numberOfEdges(g) == 3*2 ); + g.removeEdge(5, 4); + REQUIRE( numberOfEdges(g) == 3*2 ); + } + + SECTION("Removing edge with unexisting destination") { + Graph g = { {1, 2}, {1, 3}, {3, 4} }; + REQUIRE( numberOfEdges(g) == 3*2 ); + g.removeEdge(1, 5); + REQUIRE( numberOfEdges(g) == 3*2 ); + } + SECTION("connected vertices") { Graph g = { {1, 2}, {1, 3}, {3, 4} }; REQUIRE( connected(g, 1, 2) == true ); @@ -489,81 +523,3 @@ TEST_CASE( "Graph adding/removing with more data", "[graph][data_structure]" ) { } } - - -struct float2 { - float x, y; - - inline float2() : x(0.0), y(0.0) {} - inline float2(float f1, float f2) : x(f1), y(f2) {} -}; - -inline bool operator ==(const float2& v1, const float2& v2) { return v1.x == v2.x && v1.y == v2.y; } - -namespace std { - template <> - struct hash - { - std::size_t operator()(const float2& f2) const - { - std::size_t h1 = std::hash()(f2.x); - std::size_t h2 = std::hash()(f2.y); - return h1 ^ (h2 << 1); - } - }; -} - -int numberOfEdges(int N, int M) { - return (N-2)*(M-2)*8 // inside vertices have 8 - + 4*3 // corners have 3 - + (N-2)*2*5 + (M-2)*2*5; // sides has 5 -} - -// TEST_CASE( "Graph performance", "[graph][performance]" ) { -// -// SECTION("Adding 1000x1000 vertices") { -// -// const std::size_t N = 1000; -// const std::size_t M = 1000; -// Graph g; -// for (std::size_t i = 0; i < N; ++i) -// for (std::size_t j = 0; j < M; ++j) -// g.addVertex(float2(i, j)); -// -// REQUIRE( numberOfVertices(g) == N*M ); -// } -// -// SECTION("Adding 1000x1000 vertices and MANY edges") { -// -// /** Creating a big (N rows and M columns) grid. -// * Every vertex is connexted to it's 8 neighbours. -// * -// * +-+-+-+ -// * |x+x+x| -// * +x+x+x+ -// * |x+x+x| -// * +-+-+-+ -// */ -// -// const std::size_t N = 1000; -// const std::size_t M = 1000; -// Graph g; -// -// // inside -// for (std::size_t i = 1; i < N-1; ++i) -// for (std::size_t j = 1; j < M-1; ++j) { -// g.addEdge(float2(i, j), float2(i-1, j-1)); -// g.addEdge(float2(i, j), float2(i-1, j)); -// g.addEdge(float2(i, j), float2(i-1, j+1)); -// g.addEdge(float2(i, j), float2(i, j-1)); -// g.addEdge(float2(i, j), float2(i, j+1)); -// g.addEdge(float2(i, j), float2(i+1, j-1)); -// g.addEdge(float2(i, j), float2(i+1, j)); -// g.addEdge(float2(i, j), float2(i+1, j+1)); -// } -// -// REQUIRE( numberOfVertices(g) == N*M ); -// // REQUIRE( numberOfEdges(g) == numberOfEdges(N, M)*2 ); -// } -// -// } \ No newline at end of file