]> git.tdb.fi Git - libs/core.git/blob - source/core/hash.h
Add move semantics to Variant
[libs/core.git] / source / core / hash.h
1 #ifndef MSP_CORE_HASH_H_
2 #define MSP_CORE_HASH_H_
3
4 #include <cstdint>
5 #include <string>
6
7 namespace Msp {
8
9 /**
10 Computes a 32-bit Fowler-Noll-Vo (FNV-1a) hash.  The number of bits can be
11 limited to less than 32, in which case XOR-folding is used to reduce the hash
12 size.
13 */
14 std::uint32_t hash32(const void *, unsigned, unsigned = 32);
15
16 /**
17 Convenience function to compute a 32-bit hash of a string.
18 */
19 inline unsigned hash32(const std::string &s, unsigned b = 32)
20 { return hash32(s.data(), s.size(), b); }
21
22 /**
23 Computes a 64-bit Fowler-Noll-Vo (FNV-1a) hash.  Note that even if bits is
24 limited to 32 or less, this does not produce the same result as hash32.
25 */
26 std::uint64_t hash64(const void *, unsigned, unsigned = 64);
27
28 /**
29 Convenience function to compute a 64-bit hash of a string.
30 */
31 inline std::uint64_t hash64(const std::string &s, unsigned b = 64)
32 { return hash64(s.data(), s.size(), b); }
33
34 inline std::uint32_t fold32(std::uint64_t hash)
35 { return hash^(hash>>32); }
36
37 inline std::uint16_t fold16(std::uint64_t hash)
38 { return hash^(hash>>16)^(hash>>32)^(hash>>48); }
39
40 } // namespace Msp
41
42 #endif