Graph has equal and disjointUnion functions

master
dmatetelki 9 years ago
parent 3348cebc9e
commit f7fa19a079

@ -73,6 +73,7 @@ public:
Graph(const std::vector<V>& vertex_list); Graph(const std::vector<V>& vertex_list);
Graph(std::initializer_list<Edge> edge_list); Graph(std::initializer_list<Edge> edge_list);
Graph(const std::vector<Edge>& edge_list); Graph(const std::vector<Edge>& edge_list);
bool operator==(const Graph& o) const { return m_vertices == o.m_vertices; }
void addVertex(const_reference data); void addVertex(const_reference data);
void removeVertex(const_reference data); void removeVertex(const_reference data);
@ -169,6 +170,15 @@ inline std::vector<typename Graph<V>::Edge> edges(const Graph<V>& g)
return retval; return retval;
} }
template <typename V>
inline Graph<V> disjointUnion(const Graph<V>& a, const Graph<V>& b) {
Graph<V> g(a);
for (const auto& e : edges(b))
g.addEdge(e.source, e.destination);
return g;
}
// Graph implementation // Graph implementation

@ -9,10 +9,13 @@
* The priority queue based Dijkstra (shortest path) algorith requires the * The priority queue based Dijkstra (shortest path) algorith requires the
* modifyKey functionality, which the std::priority_queue does not have. * 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. * 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 < template <

@ -1,6 +1,8 @@
#ifndef GRAPH_TEST_FIXTURE_HPP #ifndef GRAPH_TEST_FIXTURE_HPP
#define GRAPH_TEST_FIXTURE_HPP #define GRAPH_TEST_FIXTURE_HPP
#include <graph/graph.hpp>
#include <cmath> #include <cmath>
inline std::size_t hash_f(float f) { return std::hash<float>()(f); } inline std::size_t hash_f(float f) { return std::hash<float>()(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.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 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 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 { namespace std {

@ -4,8 +4,39 @@
#include "fixture.hpp" #include "fixture.hpp"
#include <map>
#include <type_traits>
TEST_CASE( "Graph creation", "[graph][data_structure]" ) { TEST_CASE( "Graph creation", "[graph][data_structure]" ) {
SECTION("rule of 3+2") {
REQUIRE ( std::is_default_constructible<Graph<int> >::value == true);
REQUIRE ( std::is_copy_constructible<Graph<int> >::value == true);
REQUIRE ( std::is_move_constructible<Graph<int> >::value == true);
Graph<int> g1 = { 1, 2, 3 };
Graph<int> g2 = { 4, 5, 6 };
Graph<int> g3(g1); // cctor
REQUIRE ( g3 == g1 );
// catch does not likes templates in REQUIRE
constexpr bool is_assignable = std::is_assignable<Graph<int>, Graph<int> >::value;
REQUIRE ( is_assignable == true);
REQUIRE ( std::is_copy_assignable<Graph<int> >::value == true);
REQUIRE ( std::is_move_assignable<Graph<int> >::value == true);
g3 = g2; // assign
REQUIRE ( g3 == g2 );
Graph<int> g4 = g1; // cctor
Graph<int> g5 = std::move(g1); // move ctor, g1 become invalid
REQUIRE ( g4 == g5 );
Graph<int> g6 = g2; // cctor
g1 = std::move(g2); // move assign
REQUIRE ( g1 == g6 );
}
SECTION("Initial state") { SECTION("Initial state") {
Graph<int> g; Graph<int> g;
REQUIRE( empty(g) == true ); REQUIRE( empty(g) == true );

@ -103,7 +103,7 @@ TEST_CASE("Graph algorithms, small", "[graph][algorithm][dijkstra]" ) {
TEST_CASE_METHOD(Fixture<float2>, "Graph algorithms, big graph", "[graph][algorithm][dijkstra][performance]" ) { TEST_CASE_METHOD(Fixture<float2>, "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; constexpr std::size_t number_of_columns = number_of_rows;
Fixture<float2>::initOnce(number_of_rows, number_of_columns); Fixture<float2>::initOnce(number_of_rows, number_of_columns);

Loading…
Cancel
Save