-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHashing.h
84 lines (66 loc) · 2.49 KB
/
Hashing.h
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
/*
This file is part of the Util library.
Copyright (C) 2007-2013 Benjamin Eikel <[email protected]>
Copyright (C) 2007-2012 Claudius Jähn <[email protected]>
Copyright (C) 2007-2012 Ralf Petring <[email protected]>
Copyright (C) 2020 Sascha Brandt <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef UTIL_HASHING_H_
#define UTIL_HASHING_H_
#include <cstddef>
#include <cstdint>
#include <string>
namespace Util {
//! @addtogroup util_helper
//! @{
//! @name Hashing
//! @{
UTILAPI uint32_t calcHash(const uint8_t * ptr,uint64_t size);
UTILAPI std::string md5(const std::string& str);
template <class T>
inline uint64_t hash_combine(uint64_t& seed, const T& v) {
static std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
inline uint64_t hash_param(uint64_t &seed) { return seed; }
template <typename T>
inline uint64_t hash_param(uint64_t &seed, const T &value) {
hash_combine(seed, value);
return seed;
}
template <typename T, typename... Args>
inline uint64_t hash_param(uint64_t& seed, const T& first_arg, const Args& ...args) {
hash_combine(seed, first_arg);
hash_param(seed, args...);
return seed;
}
template <typename T>
inline uint64_t hash(const T& arg) {
static std::hash<T> hasher;
return hasher(arg);
}
constexpr uint32_t val_32_const = 0x811c9dc5;
constexpr uint32_t prime_32_const = 0x1000193;
constexpr uint64_t val_64_const = 0xcbf29ce484222325;
constexpr uint64_t prime_64_const = 0x100000001b3;
/** FNV1a c++11 constexpr compile time hash functions, 32 bit
* str should be a null terminated string literal, value should be left out
* e.g hash32("example")
* code license: public domain or equivalent
* post: https://notes.underscorediscovery.com/constexpr-fnv1a/
*/
inline constexpr uint32_t hash32(const char* const str, const uint32_t value = val_32_const) noexcept {
return (str[0] == '\0') ? value : hash32(&str[1], (value ^ uint32_t(str[0])) * prime_32_const);
}
//! FNV1a c++11 constexpr compile time hash functions, 64 bit @see{hash32()}
inline constexpr uint64_t hash64(const char* const str, const uint64_t value = val_64_const) noexcept {
return (str[0] == '\0') ? value : hash64(&str[1], (value ^ uint64_t(str[0])) * prime_64_const);
}
//! @}
//! @}
} /* Util */
#endif /* end of include guard: UTIL_HASHING_H_ */