c++ hash function for an int array

C++ TR1 contains a hash template function.

If you don't have that yet, you can use Boost Hash.

Idea for a handy helper:

#include <boost/functional/hash.hpp>

template <typename T, int N>
    static std::size_t hasharray(const T (&arr)[N])
{
     return boost::hash_range(arr, arr+N);
}

This would be (roughly?) equivalent to

 size_t seed = 0;
 for (const T* it=arr; it!=(arr+N); ++it)
     boost::hash_combine(seed, *it);
 return seed;

Don't forget to implement proper equality comparison operations if you're using this hash for lookup


Try to use lookup8 hash function. This function is VERY fast and good.

int key[100];
int key_size=10;
for (int i=0;i<key_size;i++) key[i]=i; //fill key with sample data
ub8 hash=hash((ub8*)key, sizeof(key[0])*key_size, 0);

UPD: Or use better function. - t1ha

Tags:

C++

Hash