TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_hash.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4#include <string_view>
5
6namespace tachyon
7{
10
11 namespace Hash
12 {
14 constexpr uint64_t Basis = 14695981039346656037ULL;
15 constexpr uint64_t Prime = 1099511628211ULL;
16
18 constexpr uint64_t CalculateConst(std::string_view str, uint64_t hash = Basis)
19 {
20 for (char c : str)
21 {
22 hash ^= static_cast<uint64_t>(c);
23 hash *= Prime;
24 }
25 return hash;
26 }
27
29 inline uint64_t Calculate(std::string_view str)
30 {
31 uint64_t hash = Basis;
32 for (size_t i = 0; i < str.size(); ++i)
33 {
34 hash ^= static_cast<uint64_t>(str[i]);
35 hash *= Prime;
36 }
37 return hash;
38 }
39
41 inline uint64_t CalculateBytes(const void* data, size_t len)
42 {
43 const uint8_t* ptr = static_cast<const uint8_t*>(data);
44 uint64_t hash = Basis;
45 for (size_t i = 0; i < len; ++i)
46 {
47 hash ^= static_cast<uint64_t>(ptr[i]);
48 hash *= Prime;
49 }
50 return hash;
51 }
52
55 template <typename T,
56 typename = std::enable_if_t<std::is_trivially_copyable_v<std::decay_t<T>> &&
57 !std::is_pointer_v<std::decay_t<T>> &&
58 !std::is_same_v<std::decay_t<T>, std::string_view>>>
59 inline uint64_t Calculate(const T& value)
60 {
61 return CalculateBytes(&value, sizeof(T));
62 }
63
65 inline constexpr uint64_t Combine(uint64_t a, uint64_t b)
66 {
67 return a ^ (b + 0x9E3779B97F4A7C15ULL + (a << 6) + (a >> 2));
68 }
69
71 template<typename... Args>
72 inline constexpr uint64_t Combine(const Args&... args)
73 {
74 uint64_t hash = Basis;
75 ((hash = Combine(hash, Calculate(args))), ...);
76 return hash;
77 }
78 }
79
80}
Definition gui_hash.h:12
constexpr uint64_t Basis
FNV-1a 64-bit offset basis (Prime is the FNV-1a prime).
Definition gui_hash.h:14
uint64_t CalculateBytes(const void *data, size_t len)
FNV-1a hash of len bytes at data.
Definition gui_hash.h:41
uint64_t Calculate(std::string_view str)
FNV-1a hash of str; same result as CalculateConst.
Definition gui_hash.h:29
constexpr uint64_t Prime
Definition gui_hash.h:15
constexpr uint64_t CalculateConst(std::string_view str, uint64_t hash=Basis)
FNV-1a hash of str, usable at compile time; hash continues an earlier hash.
Definition gui_hash.h:18
constexpr uint64_t Combine(uint64_t a, uint64_t b)
Order-dependent combine; repeated values don't cancel out.
Definition gui_hash.h:65
Definition gui_logging.h:24