Sierra Toolkit  Version of the Day
hash_rdestl.h
1 #ifndef RDESTL_HASH_H
2 #define RDESTL_HASH_H
3 
4 namespace rde
5 {
6 
7 typedef unsigned long hash_value_t;
8 
9 // Default implementations, just casts to hash_value.
10 template<typename T>
11 hash_value_t extract_int_key_value(const T& t)
12 {
13  return (hash_value_t)t;
14 }
15 
16 // Default implementation of hasher.
17 // Works for keys that can be converted to 32-bit integer
18 // with extract_int_key_value.
19 // Algorithm by Robert Jenkins.
20 // (see http://www.cris.com/~Ttwang/tech/inthash.htm for example).
21 template<typename T>
22 struct hash
23 {
24  hash_value_t operator()(const T& t) const
25  {
26  hash_value_t a = extract_int_key_value(t);
27  a = (a+0x7ed55d16) + (a<<12);
28  a = (a^0xc761c23c) ^ (a>>19);
29  a = (a+0x165667b1) + (a<<5);
30  a = (a+0xd3a2646c) ^ (a<<9);
31  a = (a+0xfd7046c5) + (a<<3);
32  a = (a^0xb55a4f09) ^ (a>>16);
33  return a;
34  }
35 };
36 
37 }
38 
39 #endif