EpcTools
An event based multi-threaded C++ development framework.
ehash.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2009-2019 Brian Waters
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __ehash_h_included
19 #define __ehash_h_included
20 
21 #include <limits>
22 #include <bits/hash_bytes.h>
23 
24 #include "ebase.h"
25 #include "estring.h"
26 #include "eerror.h"
27 
30 
32 class EHash
33 {
34 public:
38  static ULong getHash(EString &str);
43  static ULong getHash(cpChar val, ULong len);
48  static ULong getHash(cpUChar val, ULong len);
49 
50 private:
51  static ULong m_crcTable[256];
52 };
53 
56 
57 //
58 // ESipHash24 is adapted from the C reference implementation located at
59 // https://github.com/veorq/SipHash which is licensed to public domain by
60 // the [CC0 1.0 Universal] license.
61 //
62 
63 DECLARE_ERROR(ESipHash24Error_InvalidKeyLength);
64 
67 {
68 public:
70  class Key
71  {
72  public:
74  Key() : data_{0} {}
77  Key(const Key &k) { assign(k.data_,sizeof(k.data_)); }
81  Key(cpUChar k, const size_t len) { assign(k,len); }
82 
85  operator cpUChar() const { return data_; }
88  cpUChar data() const { return data_; }
89 
94  Key& assign(cpUChar k, const size_t len)
95  {
96  size_t i = 0;
97  for (; i<min(len,sizeof(data_)); i++)
98  data_[i] = k[i];
99  for( ; i < sizeof(data_); i++)
100  data_[i] = 0;
101  return *this;
102  }
103  private:
104  UChar data_[16];
105  };
106 
110  static Key& setKey(const Key& k) { return key_ = k; }
115  static Key& setKey(cpUChar val, const size_t len) { return key_.assign(val, len); }
118  static const Key& key() { return key_; }
119 
124  static ULong getHash32(EString &str, const Key &key=key_)
125  {
126  return getHash32(reinterpret_cast<cpUChar>(str.c_str()), str.length(), key);
127  }
133  static ULong getHash32(cpChar val, const size_t len, const Key &key=key_)
134  {
135  return getHash32(reinterpret_cast<cpUChar>(val), len, key);
136  }
142  static ULong getHash32(cpUChar in, const size_t inlen, const Key &key=key_);
150  static Bool getHash32(cpUChar in, const size_t inlen, pUChar out, const size_t outlen, const Key &key=key_);
156  static ULong getHash32(cShort val, const Key &key=key_) { return getHash32(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
157  static ULong getHash32(cUShort val, const Key &key=key_) { return getHash32(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
158  static ULong getHash32(cLong val, const Key &key=key_) { return getHash32(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
159  static ULong getHash32(cULong val, const Key &key=key_) { return getHash32(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
160  static ULong getHash32(cLongLong val, const Key &key=key_) { return getHash32(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
161  static ULong getHash32(cULongLong val, const Key &key=key_) { return getHash32(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
162  static ULong getHash32(cFloat val, const Key &key=key_) { return getHash32(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
163  static ULong getHash32(cDouble val, const Key &key=key_) { return getHash32(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
165 
170  static ULongLong getHash64(EString &str, const Key &key=key_)
171  {
172  return getHash64(reinterpret_cast<cpUChar>(str.c_str()), str.length(), key);
173  }
179  static ULongLong getHash64(cpChar val, const size_t len, const Key &key=key_)
180  {
181  return getHash64(reinterpret_cast<cpUChar>(val), len, key);
182  }
188  static ULongLong getHash64(cpUChar in, const size_t inlen, const Key &key=key_);
196  static Bool getHash64(cpUChar in, size_t inlen, pUChar out, const size_t outlen, const Key &key=key_);
201  static ULongLong getHash64(cShort val, const Key &key=key_) { return getHash64(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
202  static ULongLong getHash64(cUShort val, const Key &key=key_) { return getHash64(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
203  static ULongLong getHash64(cLong val, const Key &key=key_) { return getHash64(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
204  static ULongLong getHash64(cULong val, const Key &key=key_) { return getHash64(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
205  static ULongLong getHash64(cLongLong val, const Key &key=key_) { return getHash64(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
206  static ULongLong getHash64(cULongLong val, const Key &key=key_) { return getHash64(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
207  static ULongLong getHash64(cFloat val, const Key &key=key_) { return getHash64(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
208  static ULongLong getHash64(cDouble val, const Key &key=key_) { return getHash64(reinterpret_cast<cpUChar>(&val), sizeof(val), key); }
210 
217  static Bool getHash128(EString &str, pUChar out, const size_t outlen, const Key &key=key_)
218  {
219  return getHash128(reinterpret_cast<cpUChar>(str.c_str()), str.length(), out, outlen, key);
220  }
228  static Bool getHash128(cpChar val, size_t len, pUChar out, const size_t outlen, const Key &key=key_)
229  {
230  return getHash128(reinterpret_cast<cpUChar>(val), len, out, outlen, key);
231  }
239  static Bool getHash128(cpUChar in, size_t inlen, pUChar out, const size_t outlen, const Key &key=key_);
247  static Bool getHash128(cShort val, pUChar out, const size_t outlen, const Key &key=key_) { return getHash128(reinterpret_cast<cpUChar>(&val), sizeof(val), out, outlen, key); }
248  static Bool getHash128(cUShort val, pUChar out, const size_t outlen, const Key &key=key_) { return getHash128(reinterpret_cast<cpUChar>(&val), sizeof(val), out, outlen, key); }
249  static Bool getHash128(cLong val, pUChar out, const size_t outlen, const Key &key=key_) { return getHash128(reinterpret_cast<cpUChar>(&val), sizeof(val), out, outlen, key); }
250  static Bool getHash128(cULong val, pUChar out, const size_t outlen, const Key &key=key_) { return getHash128(reinterpret_cast<cpUChar>(&val), sizeof(val), out, outlen, key); }
251  static Bool getHash128(cLongLong val, pUChar out, const size_t outlen, const Key &key=key_) { return getHash128(reinterpret_cast<cpUChar>(&val), sizeof(val), out, outlen, key); }
252  static Bool getHash128(cULongLong val, pUChar out, const size_t outlen, const Key &key=key_) { return getHash128(reinterpret_cast<cpUChar>(&val), sizeof(val), out, outlen, key); }
253  static Bool getHash128(cFloat val, pUChar out, const size_t outlen, const Key &key=key_) { return getHash128(reinterpret_cast<cpUChar>(&val), sizeof(val), out, outlen, key); }
254  static Bool getHash128(cDouble val, pUChar out, const size_t outlen, const Key &key=key_) { return getHash128(reinterpret_cast<cpUChar>(&val), sizeof(val), out, outlen, key); }
256 
257 private:
258  static Bool getFullHash(cpUChar in, size_t inlen, cpUChar k, pUChar out, const size_t outlen);
259  static Bool getHalfHash(cpUChar in, size_t inlen, cpUChar k, pUChar out, const size_t outlen);
260 
261  static Key key_;
262 };
263 
266 {
267 public:
273  static size_t getHash(cChar val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
274  static size_t getHash(cUChar val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
275  static size_t getHash(cShort val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
276  static size_t getHash(cUShort val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
277  static size_t getHash(cLong val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
278  static size_t getHash(cULong val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
279  static size_t getHash(cLongLong val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
280  static size_t getHash(cULongLong val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
281  static size_t getHash(cFloat val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
282  static size_t getHash(cDouble val, size_t seed=0xc70f6907UL) { return _Hash_bytes(&val,sizeof(val),seed); }
283  static size_t getHash(const EString &val, size_t seed=0xc70f6907UL) { return _Hash_bytes(val.c_str(),val.size(),seed); }
285 
292  static size_t getHash(cpChar val, size_t len, size_t seed=0xc70f6907UL) { return _Hash_bytes(val,len,seed); }
293  static size_t getHash(cpUChar val, size_t len, size_t seed=0xc70f6907UL) { return _Hash_bytes(val,len,seed); }
295 
300  static size_t combine(size_t h1, size_t h2)
301  {
302  return rotateLeft(circularAdd(h1,h2), 19) ^ circularSubtract(h1,h2);
303  }
304 
305 private:
306  static size_t rotateLeft(const size_t val, const size_t bits)
307  {
308  return (size_t)((val << bits) | (val >> (64 - bits)));
309  }
310 
311  static size_t circularAdd(size_t val1, size_t val2)
312  {
313  size_t result = val1 + val2;
314  return result + (result < val1);
315  }
316 
317  static size_t circularSubtract(size_t val1, size_t val2)
318  {
319  size_t result = val1 - val2;
320  return result - (result > val1);
321  }
322 };
323 
324 #endif // #define __ehash_h_included
static size_t getHash(const EString &val, size_t seed=0xc70f6907UL)
Definition: ehash.h:283
Encapsulates and extends a std::string object.
static ULongLong getHash64(cLongLong val, const Key &key=key_)
Definition: ehash.h:205
static size_t getHash(cDouble val, size_t seed=0xc70f6907UL)
Definition: ehash.h:282
Macros for various standard C library functions and standard includes.
Represents a key to be used when calculating a hash value.
Definition: ehash.h:70
Key & assign(cpUChar k, const size_t len)
Assigns a new key value.
Definition: ehash.h:94
static ULongLong getHash64(cULong val, const Key &key=key_)
Definition: ehash.h:204
Calcuates a 64-bit murmur hash for the specified value.
Definition: ehash.h:265
static ULong getHash32(cFloat val, const Key &key=key_)
Definition: ehash.h:162
static Bool getHash128(cpChar val, size_t len, pUChar out, const size_t outlen, const Key &key=key_)
Calculates the 128-bit hash associated with the character buffer.
Definition: ehash.h:228
static ULong getHash32(cShort val, const Key &key=key_)
Returns the 32-bit hash associated with the unsigned character buffer.
Definition: ehash.h:156
static Bool getHash128(cDouble val, pUChar out, const size_t outlen, const Key &key=key_)
Definition: ehash.h:254
static size_t getHash(cpChar val, size_t len, size_t seed=0xc70f6907UL)
Calculates a 64-bit murmur hash for the value.
Definition: ehash.h:292
cpUChar data() const
Data extractor.
Definition: ehash.h:88
Calcuates a 32-bit hash value for the specified string or array of characters.
Definition: ehash.h:32
static ULongLong getHash64(cFloat val, const Key &key=key_)
Definition: ehash.h:207
static Key & setKey(cpUChar val, const size_t len)
Assigns a new default key value.
Definition: ehash.h:115
static const Key & key()
Retrieves the default key.
Definition: ehash.h:118
static ULong getHash32(EString &str, const Key &key=key_)
Returns the 32-bit hash associated with the string.
Definition: ehash.h:124
static Bool getHash128(cULong val, pUChar out, const size_t outlen, const Key &key=key_)
Definition: ehash.h:250
static size_t getHash(cpUChar val, size_t len, size_t seed=0xc70f6907UL)
Definition: ehash.h:293
static ULong getHash(EString &str)
Returns the 32-bit hash value for the specified string.
Definition: ehash.cpp:24
static ULongLong getHash64(cpChar val, const size_t len, const Key &key=key_)
Returns the 64-bit hash associated with the character buffer.
Definition: ehash.h:179
static ULongLong getHash64(cULongLong val, const Key &key=key_)
Definition: ehash.h:206
static size_t getHash(cLongLong val, size_t seed=0xc70f6907UL)
Definition: ehash.h:279
static ULong getHash32(cDouble val, const Key &key=key_)
Definition: ehash.h:163
static ULong getHash32(cpChar val, const size_t len, const Key &key=key_)
Returns the 32-bit hash associated with the character buffer.
Definition: ehash.h:133
Calculates a 32-bit, 64-bit or 128-bit hash value using the SipHash algorithm.
Definition: ehash.h:66
static ULong getHash32(cULong val, const Key &key=key_)
Definition: ehash.h:159
static size_t getHash(cULong val, size_t seed=0xc70f6907UL)
Definition: ehash.h:278
static Bool getHash128(cUShort val, pUChar out, const size_t outlen, const Key &key=key_)
Definition: ehash.h:248
Defines base class for exceptions and declaration helper macros.
static ULongLong getHash64(cUShort val, const Key &key=key_)
Definition: ehash.h:202
static size_t getHash(cULongLong val, size_t seed=0xc70f6907UL)
Definition: ehash.h:280
static ULongLong getHash64(EString &str, const Key &key=key_)
Returns the 64-bit hash associated with the string.
Definition: ehash.h:170
static size_t getHash(cShort val, size_t seed=0xc70f6907UL)
Definition: ehash.h:275
static Bool getHash128(EString &str, pUChar out, const size_t outlen, const Key &key=key_)
Calculates the 128-bit hash associated with the string.
Definition: ehash.h:217
static Bool getHash128(cFloat val, pUChar out, const size_t outlen, const Key &key=key_)
Definition: ehash.h:253
Key()
Class constructor.
Definition: ehash.h:74
static ULong getHash32(cLongLong val, const Key &key=key_)
Definition: ehash.h:160
DECLARE_ERROR(ESipHash24Error_InvalidKeyLength)
static ULongLong getHash64(cDouble val, const Key &key=key_)
Definition: ehash.h:208
static Bool getHash128(cShort val, pUChar out, const size_t outlen, const Key &key=key_)
Calculates the 128-bit hash associated with the unsigned character buffer.
Definition: ehash.h:247
static size_t combine(size_t h1, size_t h2)
Combines 2 64-bit hash values.
Definition: ehash.h:300
static size_t getHash(cLong val, size_t seed=0xc70f6907UL)
Definition: ehash.h:277
static ULongLong getHash64(cLong val, const Key &key=key_)
Definition: ehash.h:203
static Bool getHash128(cULongLong val, pUChar out, const size_t outlen, const Key &key=key_)
Definition: ehash.h:252
Key(cpUChar k, const size_t len)
Class constructor.
Definition: ehash.h:81
static Bool getHash128(cLongLong val, pUChar out, const size_t outlen, const Key &key=key_)
Definition: ehash.h:251
static Bool getHash128(cLong val, pUChar out, const size_t outlen, const Key &key=key_)
Definition: ehash.h:249
Key(const Key &k)
Copy constructor.
Definition: ehash.h:77
static Key & setKey(const Key &k)
Assigns a new default key value.
Definition: ehash.h:110
static size_t getHash(cChar val, size_t seed=0xc70f6907UL)
Calculates a 64-bit murmur hash for the value.
Definition: ehash.h:273
static ULong getHash32(cULongLong val, const Key &key=key_)
Definition: ehash.h:161
static ULong getHash32(cLong val, const Key &key=key_)
Definition: ehash.h:158
static ULongLong getHash64(cShort val, const Key &key=key_)
Returns the 64-bit hash associated with the unsigned character buffer.
Definition: ehash.h:201
static size_t getHash(cUChar val, size_t seed=0xc70f6907UL)
Definition: ehash.h:274
static size_t getHash(cFloat val, size_t seed=0xc70f6907UL)
Definition: ehash.h:281
static ULong getHash32(cUShort val, const Key &key=key_)
Definition: ehash.h:157
String class.
Definition: estring.h:31
static size_t getHash(cUShort val, size_t seed=0xc70f6907UL)
Definition: ehash.h:276