in reply to non-scalar hash key
#include <iostream> #include <ext/hash_map> #include <vector> using namespace std; /* * hash function, borrowed from * http://burtleburtle.net/bob/c/lookup3.c */ #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) #define mix(a,b,c) \ { \ a -= c; a ^= rot(c, 4); c += b; \ b -= a; b ^= rot(a, 6); a += c; \ c -= b; c ^= rot(b, 8); b += a; \ a -= c; a ^= rot(c,16); c += b; \ b -= a; b ^= rot(a,19); a += c; \ c -= b; c ^= rot(b, 4); b += a; \ } #define final(a,b,c) \ { \ c ^= b; c -= rot(b,14); \ a ^= c; a -= rot(c,11); \ b ^= a; b -= rot(a,25); \ c ^= b; c -= rot(b,16); \ a ^= c; a -= rot(c,4); \ b ^= a; b -= rot(a,14); \ c ^= b; c -= rot(b,24); \ } struct hashword { size_t operator()(const vector<uint32_t>* k) const { uint32_t a, b, c; size_t length = (*k).size(); uint32_t initval = 0; unsigned int ki = 0; /* Set up the internal state */ a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval; /*------------------------------ handle most of the key */ while (length > 3) { a += (*k)[ki++]; b += (*k)[ki++]; c += (*k)[ki++]; mix(a,b,c); length -= 3; } ki = (*k).size() - 1; /*-------------------------------- handle the last 3 uint32_t's */ switch (length) /* all the case statements fall through */ { case 3: c += (*k)[ki--]; case 2: b += (*k)[ki--]; case 1: a += (*k)[ki]; final(a,b,c) ; case 0: /* case 0: nothing left to add */ break; } /*-------------------------------------- report the result */ return c; } }; /* compare 2 vectors */ struct eqintarr { bool operator()(const vector<uint32_t>* s1, const vector<uint32_t>* +s2) const { return (*s1 == *s2); } }; int main() { __gnu_cxx::hash_map<const vector<uint32_t>*, int, hashword, eqintarr +> preds; uint32_t a[] = {1,2,3,4,5}; uint32_t b[] = {0,34567,3,5,256001}; vector<uint32_t> k1 (a, a + sizeof(a) / sizeof(uint32_t) ); vector<uint32_t> k2 (k1); vector<uint32_t> k3 (b, b + sizeof(b) / sizeof(uint32_t) ); vector<uint32_t> k4 (k3); preds[&k1] = 7; preds[&k3] = 100; cout << "k1 -> " << preds[&k2] << endl; cout << "k2 -> " << preds[&k4] << endl; }
|
|---|