TPIE

2362a60
hash.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2010, 2011, 2012 The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 #ifndef __TPIE_HASH_H__
20 #define __TPIE_HASH_H__
21 
22 #include <tpie/util.h>
23 #include <cstring>
24 
25 namespace tpie {
26 
27 namespace hash_bits {
28 
29 extern size_t hash_codes[sizeof(size_t)][256];
30 
31 } // namespace hash_bits
32 
33 void init_hash();
34 
40 template <typename T>
41 struct hash {
42 private:
43  size_t m_matrix[sizeof(size_t)][256];
44 public:
48  inline size_t operator()(const T & e) const {
49  size_t key = e;
50  size_t result = 0;
51  for(size_t i = 0; i < sizeof(size_t); ++i) {
52  // use the least significant byte as key in the matrix
53  result ^= hash_bits::hash_codes[i][key & 0xFF];
54  key >>= 8;
55  }
56 
57  return result;
58  }
59 };
60 
66 template <typename T1, typename T2>
67 struct hash<std::pair<T1,T2> > {
68  hash<T1> h1;
69  hash<T2> h2;
70 
75  inline size_t operator()(const std::pair<T1,T2> & e) const {
76  return h1(e.first) + h2(e.second) * 99181;
77  }
78 };
79 
83 template <>
84 struct hash<const char *> {
89  inline size_t operator()(const char * s) const {
90  uint32_t r = 1;
91  for(int i=0; s[i]; i++){
92  r = r*13+s[i]*7;
93  }
94  return r;
95  }
96 };
97 
101 template <>
102 struct hash<std::string> {
108  inline size_t operator()(const std::string & s) const {
109  return h(s.c_str());
110  }
111 };
112 
113 
114 // Predeclare reflect
115 template <typename R, typename T, typename ... TT>
116 bool reflect(R & r, T && v, TT && ... vs);
117 
118 struct BufferedHash {
119 public:
120  BufferedHash(size_t seed=0);
121  BufferedHash(BufferedHash && o) {copy(o);}
122  BufferedHash(const BufferedHash & o) {copy(o);}
123  BufferedHash & operator=(BufferedHash && o) {copy(o); return *this;}
124  BufferedHash & operator=(const BufferedHash & o) {copy(o); return *this;}
125 
126  void add(const char * data, size_t length) {
127  const char * istart = data;
128  const char * iend = data + length;
129  while (iend - istart > stop - cur) {
130  memcpy(cur, istart, stop - cur);
131  istart += stop - cur;
132  cur = stop;
133  flush();
134  }
135  memcpy(cur, istart, iend - istart);
136  cur += (iend - istart);
137  }
138 
139  template <typename T>
140  void add(const T & v) {
141  add(reinterpret_cast<const char *>(&v), sizeof(v));
142  }
143 
144  uint32_t finalize();
145 private:
146  void flush();
147  void copy(const BufferedHash & o);
148 
149  char buff[1024];
150  char * stop;
151  char * cur;
152  uint32_t value;
153 };
154 
155 template <typename H, typename T>
156 inline void ghash(H & h, const T & t);
157 
161 template <typename H>
163 public:
164  static constexpr bool write = false;
165  static constexpr bool arithmetic = true;
166  static constexpr bool string = true;
167  static constexpr bool trivialSerializable = false;
168 
169  HashReflector(H & h): h(h) {}
170 
171  void begin(const char *) {}
172  void end() {}
173  void beginArray(size_t x) {h.add(x);}
174  void endArray() {}
175  void name(const char *) {}
176  void beginStaticArray(size_t) {}
177  void endStaticArray() {}
178 
179  template <typename T>
180  bool operator()(const T & v) {
181  h.add(v);
182  return true;
183  }
184 
185  bool operator()(const std::string & v) {
186  h.add(v.size());
187  h.add(v.c_str(), v.size());
188  return true;
189  }
190 
191  template <typename T>
192  bool apply(const T & v) {
193  ghash(h, v);
194  return true;
195  }
196 private:
197  H & h;
198 };
199 
203 template <typename H, typename T>
204 inline void ghash(H & h, const T & t) {
205  HashReflector<H> r(h);
206  reflect(r, t);
207 }
208 
209 } // namespace tpie
210 
211 #endif //__TPIE_HASH_H__
size_t operator()(const char *s) const
Calculate string hash.
Definition: hash.h:89
size_t operator()(const std::string &s) const
Calculate string hash by using std::string::c_str().
Definition: hash.h:108
Buffer based hash reflector.
Definition: hash.h:162
Default tabulation-hashing function for integral (size_t-castable) types.
Definition: hash.h:41
Default hashing function for C-style strings.
Definition: hash.h:84
Miscellaneous utility functions.
size_t operator()(const std::pair< T1, T2 > &e) const
Calculate std::pair hash.
Definition: hash.h:75
size_t operator()(const T &e) const
Calculate integer hash using tabulation hashing.
Definition: hash.h:48
void ghash(H &h, const T &t)
Hash t using reflection.
Definition: hash.h:204