test fixture float2 has a ctor time computed hash member for performance

master
dmatetelki 10 years ago
parent d81550a1b7
commit 834c3239ff

@ -3,11 +3,16 @@
#include <cmath> #include <cmath>
inline std::size_t hash_f(float f) { return std::hash<float>()(f); }
inline std::size_t hash_2f(float f1, float f2) { return hash_f(f1) ^ (hash_f(f2) << 1); }
struct float2 { struct float2 {
float x, y; typedef float value_type;
value_type x, y;
std::size_t h;
inline float2() : x(0.0), y(0.0) {} constexpr float2() : x(0.0), y(0.0), h(0) {}
inline float2(float f1, float f2) : x(f1), y(f2) {} float2(value_type f1, value_type f2) : x(f1), y(f2), h(hash_2f(f1, f2)) {}
}; };
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; }
@ -15,16 +20,13 @@ 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); } // inline float dist(const float2& v1, const float2& v2) { return abs(v2.x - v1.x) + abs(v2.y - v1.y); }
namespace std { namespace std {
template <> template <>
struct hash<float2> struct hash<float2>
{ {
std::size_t operator()(const float2& f2) const std::size_t operator()(const float2& f2) const { return f2.h; }
{
std::size_t h1 = std::hash<float>()(f2.x);
std::size_t h2 = std::hash<float>()(f2.y);
return h1 ^ (h2 << 1);
}
}; };
class distanceOf2float2s : public std::function<float(float2, float2)> class distanceOf2float2s : public std::function<float(float2, float2)>

@ -555,6 +555,7 @@ TEST_CASE_METHOD(Fixture<float2>, "Graph performance", "[graph][data_structure][
SECTION("Adding edges") { SECTION("Adding edges") {
const std::vector<typename Graph<float2>::Edge> edges = Fixture<float2>::getEdges(); const std::vector<typename Graph<float2>::Edge> edges = Fixture<float2>::getEdges();
Graph<float2> g(edges); Graph<float2> g(edges);
REQUIRE( numberOfEdges(g) == number_of_edges );
} }
SECTION("teardown") { SECTION("teardown") {

@ -99,12 +99,10 @@ TEST_CASE("Graph algorithms, small", "[graph][algorithm][dijkstra]" ) {
} }
TEST_CASE_METHOD(Fixture<float2>, "Graph algorithms, big graph", "[graph][algorithm][dijkstra]" ) { TEST_CASE_METHOD(Fixture<float2>, "Graph algorithms, big graph", "[graph][algorithm][dijkstra][performance]" ) {
constexpr std::size_t number_of_rows = 100; constexpr std::size_t number_of_rows = 300;
constexpr std::size_t number_of_columns = number_of_rows; constexpr std::size_t number_of_columns = number_of_rows;
constexpr std::size_t number_of_vertices = number_of_rows * number_of_columns;
constexpr std::size_t number_of_edges = numberOfEdges(number_of_rows, number_of_columns);
Fixture<float2>::initOnce(number_of_rows, number_of_columns); Fixture<float2>::initOnce(number_of_rows, number_of_columns);

Loading…
Cancel
Save