From 2c0547bb0e6b99b792d72bf41fd647bf130e6059 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Thu, 21 Jan 2016 15:56:08 +0100 Subject: [PATCH] Make XORFastHash great again --- include/util/xor_fast_hash.hpp | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/include/util/xor_fast_hash.hpp b/include/util/xor_fast_hash.hpp index 5b7a103bb..54f9d67d9 100644 --- a/include/util/xor_fast_hash.hpp +++ b/include/util/xor_fast_hash.hpp @@ -1,7 +1,13 @@ #ifndef XOR_FAST_HASH_HPP #define XOR_FAST_HASH_HPP +#include + +#include #include +#include +#include +#include #include namespace osrm @@ -29,27 +35,29 @@ namespace util */ class XORFastHash { // 65k entries - std::vector table1; - std::vector table2; + std::array table1; + std::array table2; public: XORFastHash() { - table1.resize(2 << 16); - table2.resize(2 << 16); - for (unsigned i = 0; i < (2 << 16); ++i) - { - table1[i] = static_cast(i); - table2[i] = static_cast(i); - } - std::random_shuffle(table1.begin(), table1.end()); - std::random_shuffle(table2.begin(), table2.end()); + std::mt19937 generator; // impl. defined but deterministic default seed + + std::iota(begin(table1), end(table1), 0u); + std::shuffle(begin(table1), end(table1), generator); + + std::iota(begin(table2), end(table2), 0u); + std::shuffle(begin(table2), end(table2), generator); } inline unsigned short operator()(const unsigned originalValue) const { unsigned short lsb = ((originalValue)&0xffff); unsigned short msb = (((originalValue) >> 16) & 0xffff); + + BOOST_ASSERT(lsb < table1.size()); + BOOST_ASSERT(msb < table2.size()); + return table1[lsb] ^ table2[msb]; } };