17 #ifndef immutable_hash_map_h
18 #define immutable_hash_map_h
26 template <
typename key_type,
typename value_type>
27 class immutable_hash_map {
29 std::vector<key_type> _keys;
30 std::vector<value_type> _values;
34 immutable_hash_map(key_type* keys, value_type* values,
int size) : _size(size) {
43 int table_size = 1 << int(ceil(log(size * 2) * (1 / M_LN2)));
44 _mask = table_size - 1;
46 _keys.resize(table_size);
47 _values.resize(table_size);
49 key_type* kp = keys, *end = kp + size;
50 value_type* vp = values;
58 const value_type& operator[](
const key_type& key)
const {
return _values[find(key)]; }
60 int size()
const {
return _size; }
63 int find(
const key_type& key)
const {
64 uint32_t
hash = intptr_t(key) ^ (intptr_t(key) >> 32);
66 hash = ~hash + (hash << 15);
67 hash = hash ^ (hash >> 12);
68 hash = hash + (hash << 2);
69 hash = hash ^ (hash >> 4);
71 hash = hash ^ (hash >> 16);
73 int pos = hash & _mask;
74 const key_type& k = _keys[pos];
75 if (k == key || !k)
return pos;
double hash(int n, double *args)