Make XORFastHash great again

This commit is contained in:
Daniel J. Hofmann 2016-01-21 15:56:08 +01:00
parent 0f9eec887f
commit 2c0547bb0e

View File

@ -1,7 +1,13 @@
#ifndef XOR_FAST_HASH_HPP #ifndef XOR_FAST_HASH_HPP
#define XOR_FAST_HASH_HPP #define XOR_FAST_HASH_HPP
#include <boost/assert.hpp>
#include <array>
#include <algorithm> #include <algorithm>
#include <iterator>
#include <numeric>
#include <random>
#include <vector> #include <vector>
namespace osrm namespace osrm
@ -29,27 +35,29 @@ namespace util
*/ */
class XORFastHash class XORFastHash
{ // 65k entries { // 65k entries
std::vector<unsigned short> table1; std::array<unsigned short, (2 << 16)> table1;
std::vector<unsigned short> table2; std::array<unsigned short, (2 << 16)> table2;
public: public:
XORFastHash() XORFastHash()
{ {
table1.resize(2 << 16); std::mt19937 generator; // impl. defined but deterministic default seed
table2.resize(2 << 16);
for (unsigned i = 0; i < (2 << 16); ++i) std::iota(begin(table1), end(table1), 0u);
{ std::shuffle(begin(table1), end(table1), generator);
table1[i] = static_cast<unsigned short>(i);
table2[i] = static_cast<unsigned short>(i); std::iota(begin(table2), end(table2), 0u);
} std::shuffle(begin(table2), end(table2), generator);
std::random_shuffle(table1.begin(), table1.end());
std::random_shuffle(table2.begin(), table2.end());
} }
inline unsigned short operator()(const unsigned originalValue) const inline unsigned short operator()(const unsigned originalValue) const
{ {
unsigned short lsb = ((originalValue)&0xffff); unsigned short lsb = ((originalValue)&0xffff);
unsigned short msb = (((originalValue) >> 16) & 0xffff); unsigned short msb = (((originalValue) >> 16) & 0xffff);
BOOST_ASSERT(lsb < table1.size());
BOOST_ASSERT(msb < table2.size());
return table1[lsb] ^ table2[msb]; return table1[lsb] ^ table2[msb];
} }
}; };