summaryrefslogtreecommitdiff
path: root/utils/hash.h
blob: 24d2b6ad4c9d8783f5282dd8aca7c2e36bf93d4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef CDEC_HASH_H
#define CDEC_HASH_H

#include <boost/functional/hash.hpp>

#include "murmur_hash3.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_SPARSEHASH
# include <sparsehash/dense_hash_map>
# include <sparsehash/dense_hash_set>
# include <sparsehash/sparse_hash_map>
# define SPARSE_HASH_MAP google::sparse_hash_map
# define HASH_MAP google::dense_hash_map
# define HASH_SET google::dense_hash_set
# define HASH_MAP_DELETED(h,deleted) do { (h).set_deleted_key(deleted); } while(0)
# define HASH_MAP_RESERVED(h,empty,deleted) do { (h).set_empty_key(empty); (h).set_deleted_key(deleted); } while(0)
# define HASH_MAP_EMPTY(h,empty) do { (h).set_empty_key(empty); } while(0)
#else
#ifndef HAVE_OLD_CPP
# include <unordered_map>
# include <unordered_set>
#else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
namespace std { using std::tr1::unordered_map; using std::tr1::unordered_set; }
#endif
# define SPARSE_HASH_MAP std::unordered_map
# define HASH_MAP std::unordered_map
# define HASH_SET std::unordered_set
# define HASH_MAP_DELETED(h,deleted)
# define HASH_MAP_RESERVED(h,empty,deleted)
# define HASH_MAP_EMPTY(h,empty)
#endif

#define BOOST_HASHED_MAP(k,v) HASH_MAP<k,v,boost::hash<k> >

namespace {
const unsigned GOLDEN_MEAN_FRACTION=2654435769U;
}

// assumes C is POD
template <class C>
struct murmur_hash {
  typedef size_t result_type;
  typedef C /*const&*/ argument_type;
  result_type operator()(argument_type const& c) const {
    return cdec::MurmurHash3_64((void*)&c, sizeof(c), GOLDEN_MEAN_FRACTION);
  }
};

// murmur_hash_array isn't std guaranteed safe (you need to use string::data())
template <>
struct murmur_hash<std::string> {
  typedef size_t result_type;
  typedef std::string /*const&*/ argument_type;
  result_type operator()(argument_type const& c) const {
    return cdec::MurmurHash3_64(c.data(), c.size(), GOLDEN_MEAN_FRACTION);
  }
};

// uses begin(),size() assuming contiguous layout and POD
template <class C>
struct murmur_hash_array
{
  typedef size_t result_type;
  typedef C /*const&*/ argument_type;
  result_type operator()(argument_type const& c) const {
    return cdec::MurmurHash3_64(&*c.begin(), c.size()*sizeof(*c.begin()), GOLDEN_MEAN_FRACTION);
  }
};


// adds default val to table if key wasn't found, returns ref to val
template <class H,class K>
typename H::mapped_type & get_default(H &ht,K const& k,typename H::mapped_type const& v) {
  return const_cast<typename H::mapped_type &>(ht.insert(typename H::value_type(k,v)).first->second);
}

// get_or_construct w/ no arg: just use ht[k]
template <class H,class K,class C0>
typename H::mapped_type & get_or_construct(H &ht,K const& k,C0 const& c0) {
  typedef typename H::mapped_type V;
  typedef typename H::value_type KV;
  typename H::iterator_type i=ht.find(k);
  if (i==ht.end()) {
    return const_cast<V &>(ht.insert(KV(k,V(c0))).first->second);
  } else {
    return i->second;
  }
}

// get_or_call (0 arg)
template <class H,class K,class F>
typename H::mapped_type & get_or_call(H &ht,K const& k,F const& f) {
  typedef typename H::mapped_type V;
  typedef typename H::value_type KV;
  typename H::iterator_type i=ht.find(k);
  if (i==ht.end()) {
    return const_cast<V &>(ht.insert(KV(k,f())).first->second);
  } else {
    return i->second;
  }
}

// the below could also return a ref to the mapped max/min.  they have the advantage of not falsely claiming an improvement when an equal value already existed.  otherwise you could just modify the get_default and if equal assume new.
template <class H,class K>
bool improve_mapped_max(H &ht,K const& k,typename H::mapped_type const& v) {
  std::pair<typename H::iterator,bool> inew=ht.insert(typename H::value_type(k,v));
  if (inew.second) return true;
  typedef typename H::mapped_type V;
  V &oldv=const_cast<V&>(inew.first->second);
  if (oldv<v) {
    oldv=v;
    return true;
  }
  return false;
}


// return true if there was no old value.  like ht[k]=v but lets you know whether it was a new addition
template <class H,class K>
bool put(H &ht,K const& k,typename H::mapped_type const& v) {
  std::pair<typename H::iterator,bool> inew=ht.insert(typename H::value_type(k,v));
  if (inew.second)
    return true;
  inew.first->second=v;
  return false;
}

// does not update old value (returns false) if one exists, otherwise add
template <class H,class K>
bool maybe_add(H &ht,K const& k,typename H::mapped_type const& v) {
  std::pair<typename H::iterator,bool> inew=ht.insert(typename H::value_type(k,v));
  return inew.second;
}

// ht[k] must not exist (yet)
template <class H,class K>
void add(H &ht,K const& k,typename H::mapped_type const& v) {
  maybe_add(ht,k,v);
}


template <class H,class K>
bool improve_mapped_min(H &ht,K const& k,typename H::mapped_type const& v) {
  std::pair<typename H::iterator,bool> inew=ht.insert(typename H::value_type(k,v));
  if (inew.second) return true;
  typedef typename H::mapped_type V;
  V &oldv=const_cast<V&>(inew.first->second);
  if (v<oldv) { // the only difference from above
    oldv=v;
    return true;
  }
  return false;
}

#endif