LIPH's C++ Codes
hash.h
Go to the documentation of this file.
1#ifndef LIPH_HASH_H_
2#define LIPH_HASH_H_
3
4#include <cstdint>
5
6namespace liph {
7
8inline unsigned bkdr_hash(const char *str) {
9 if (str == nullptr) return 0U;
10 unsigned hashval;
11 static const unsigned seed = 31;
12 for (hashval = 0; *str != '\0'; str++) {
13 hashval = hashval * seed + *str;
14 }
15 return hashval;
16}
17
18inline uint64_t bkdr_hash64(const char *str) {
19 if (str == nullptr) return 0U;
20 union {
21 uint32_t u32[2];
22 uint64_t u64;
23 };
24 static const uint32_t seeds[2] = {13, 131};
25 for (u32[0] = 0, u32[1] = 0; *str != '\0'; str++) {
26 u32[0] = u32[0] * seeds[0] + *str;
27 u32[1] = u32[1] * seeds[1] + *str;
28 }
29 return u64;
30}
31
32} // namespace liph
33
34#endif // LIPH_HASH_H_
Definition: algorithm.h:10
uint64_t bkdr_hash64(const char *str)
Definition: hash.h:18
unsigned bkdr_hash(const char *str)
Definition: hash.h:8