Mercurial > repos > rliterman > csp2
comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/hash.h @ 69:33d812a61356
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 17:55:14 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:0e9998148a16 | 69:33d812a61356 |
---|---|
1 // Copyright (c) 2018 Kenton Varda and contributors | |
2 // Licensed under the MIT License: | |
3 // | |
4 // Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 // of this software and associated documentation files (the "Software"), to deal | |
6 // in the Software without restriction, including without limitation the rights | |
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 // copies of the Software, and to permit persons to whom the Software is | |
9 // furnished to do so, subject to the following conditions: | |
10 // | |
11 // The above copyright notice and this permission notice shall be included in | |
12 // all copies or substantial portions of the Software. | |
13 // | |
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 // THE SOFTWARE. | |
21 | |
22 #pragma once | |
23 | |
24 #include "string.h" | |
25 | |
26 KJ_BEGIN_HEADER | |
27 | |
28 namespace kj { | |
29 namespace _ { // private | |
30 | |
31 struct HashCoder { | |
32 // This is a dummy type with only one instance: HASHCODER (below). To make an arbitrary type | |
33 // hashable, define `operator*(HashCoder, T)` to return any other type that is already hashable. | |
34 // Be sure to declare the operator in the same namespace as `T` **or** in the global scope. | |
35 // You can use the KJ_HASHCODE() macro as syntax sugar for this. | |
36 // | |
37 // A more usual way to accomplish what we're doing here would be to require that you define | |
38 // a function like `hashCode(T)` and then rely on argument-dependent lookup. However, this has | |
39 // the problem that it pollutes other people's namespaces and even the global namespace. For | |
40 // example, some other project may already have functions called `hashCode` which do something | |
41 // different. Declaring `operator*` with `HashCoder` as the left operand cannot conflict with | |
42 // anything. | |
43 | |
44 uint operator*(ArrayPtr<const byte> s) const; | |
45 inline uint operator*(ArrayPtr<byte> s) const { return operator*(s.asConst()); } | |
46 | |
47 inline uint operator*(ArrayPtr<const char> s) const { return operator*(s.asBytes()); } | |
48 inline uint operator*(ArrayPtr<char> s) const { return operator*(s.asBytes()); } | |
49 inline uint operator*(const Array<const char>& s) const { return operator*(s.asBytes()); } | |
50 inline uint operator*(const Array<char>& s) const { return operator*(s.asBytes()); } | |
51 inline uint operator*(const String& s) const { return operator*(s.asBytes()); } | |
52 inline uint operator*(const StringPtr& s) const { return operator*(s.asBytes()); } | |
53 inline uint operator*(const ConstString& s) const { return operator*(s.asBytes()); } | |
54 | |
55 inline uint operator*(decltype(nullptr)) const { return 0; } | |
56 inline uint operator*(bool b) const { return b; } | |
57 inline uint operator*(char i) const { return i; } | |
58 inline uint operator*(signed char i) const { return i; } | |
59 inline uint operator*(unsigned char i) const { return i; } | |
60 inline uint operator*(signed short i) const { return i; } | |
61 inline uint operator*(unsigned short i) const { return i; } | |
62 inline uint operator*(signed int i) const { return i; } | |
63 inline uint operator*(unsigned int i) const { return i; } | |
64 | |
65 inline uint operator*(signed long i) const { | |
66 if (sizeof(i) == sizeof(uint)) { | |
67 return operator*(static_cast<uint>(i)); | |
68 } else { | |
69 return operator*(static_cast<unsigned long long>(i)); | |
70 } | |
71 } | |
72 inline uint operator*(unsigned long i) const { | |
73 if (sizeof(i) == sizeof(uint)) { | |
74 return operator*(static_cast<uint>(i)); | |
75 } else { | |
76 return operator*(static_cast<unsigned long long>(i)); | |
77 } | |
78 } | |
79 inline uint operator*(signed long long i) const { | |
80 return operator*(static_cast<unsigned long long>(i)); | |
81 } | |
82 inline uint operator*(unsigned long long i) const { | |
83 // Mix 64 bits to 32 bits in such a way that if our input values differ primarily in the upper | |
84 // 32 bits, we still get good diffusion. (I.e. we cannot just truncate!) | |
85 // | |
86 // 49123 is an arbitrarily-chosen prime that is vaguely close to 2^16. | |
87 // | |
88 // TODO(perf): I just made this up. Is it OK? | |
89 return static_cast<uint>(i) + static_cast<uint>(i >> 32) * 49123; | |
90 } | |
91 | |
92 template <typename T> | |
93 uint operator*(T* ptr) const { | |
94 static_assert(!isSameType<Decay<T>, char>(), "Wrap in StringPtr if you want to hash string " | |
95 "contents. If you want to hash the pointer, cast to void*"); | |
96 if (sizeof(ptr) == sizeof(uint)) { | |
97 // TODO(cleanup): In C++17, make the if() above be `if constexpr ()`, then change this to | |
98 // reinterpret_cast<uint>(ptr). | |
99 return reinterpret_cast<unsigned long long>(ptr); | |
100 } else { | |
101 return operator*(reinterpret_cast<unsigned long long>(ptr)); | |
102 } | |
103 } | |
104 | |
105 template <typename T, typename = decltype(instance<const HashCoder&>() * instance<const T&>())> | |
106 uint operator*(ArrayPtr<T> arr) const; | |
107 template <typename T, typename = decltype(instance<const HashCoder&>() * instance<const T&>())> | |
108 uint operator*(const Array<T>& arr) const; | |
109 template <typename T, typename = EnableIf<__is_enum(T)>> | |
110 inline uint operator*(T e) const; | |
111 | |
112 template <typename T, typename Result = decltype(instance<T>().hashCode())> | |
113 inline Result operator*(T&& value) const { return kj::fwd<T>(value).hashCode(); } | |
114 }; | |
115 static KJ_CONSTEXPR(const) HashCoder HASHCODER = HashCoder(); | |
116 | |
117 } // namespace _ (private) | |
118 | |
119 #define KJ_HASHCODE(...) operator*(::kj::_::HashCoder, __VA_ARGS__) | |
120 // Defines a hash function for a custom type. Example: | |
121 // | |
122 // class Foo {...}; | |
123 // inline uint KJ_HASHCODE(const Foo& foo) { return kj::hashCode(foo.x, foo.y); } | |
124 // | |
125 // This allows Foo to be passed to hashCode(). | |
126 // | |
127 // The function should be declared either in the same namespace as the target type or in the global | |
128 // namespace. It can return any type which itself is hashable -- that value will be hashed in turn | |
129 // until a `uint` comes out. | |
130 | |
131 inline uint hashCode(uint value) { return value; } | |
132 template <typename T> | |
133 inline uint hashCode(T&& value) { return hashCode(_::HASHCODER * kj::fwd<T>(value)); } | |
134 template <typename T, size_t N> | |
135 inline uint hashCode(T (&arr)[N]) { | |
136 static_assert(!isSameType<Decay<T>, char>(), "Wrap in StringPtr if you want to hash string " | |
137 "contents. If you want to hash the pointer, cast to void*"); | |
138 static_assert(isSameType<Decay<T>, char>(), "Wrap in ArrayPtr if you want to hash a C array. " | |
139 "If you want to hash the pointer, cast to void*"); | |
140 return 0; | |
141 } | |
142 template <typename... T> | |
143 inline uint hashCode(T&&... values) { | |
144 uint hashes[] = { hashCode(kj::fwd<T>(values))... }; | |
145 return hashCode(kj::ArrayPtr<uint>(hashes).asBytes()); | |
146 } | |
147 // kj::hashCode() is a universal hashing function, like kj::str() is a universal stringification | |
148 // function. Throw stuff in, get a hash code. | |
149 // | |
150 // Hash codes may differ between different processes, even running exactly the same code. | |
151 // | |
152 // NOT SUITABLE FOR CRYPTOGRAPHY. This is for hash tables, not crypto. | |
153 | |
154 // ======================================================================================= | |
155 // inline implementation details | |
156 | |
157 namespace _ { // private | |
158 | |
159 template <typename T, typename> | |
160 inline uint HashCoder::operator*(ArrayPtr<T> arr) const { | |
161 // Hash each array element to create a string of hashes, then murmur2 over those. | |
162 // | |
163 // TODO(perf): Choose a more-modern hash. (See hash.c++.) | |
164 | |
165 constexpr uint m = 0x5bd1e995; | |
166 constexpr uint r = 24; | |
167 uint h = arr.size() * sizeof(uint); | |
168 | |
169 for (auto& e: arr) { | |
170 uint k = kj::hashCode(e); | |
171 k *= m; | |
172 k ^= k >> r; | |
173 k *= m; | |
174 h *= m; | |
175 h ^= k; | |
176 } | |
177 | |
178 h ^= h >> 13; | |
179 h *= m; | |
180 h ^= h >> 15; | |
181 return h; | |
182 } | |
183 template <typename T, typename> | |
184 inline uint HashCoder::operator*(const Array<T>& arr) const { | |
185 return operator*(arr.asPtr()); | |
186 } | |
187 | |
188 template <typename T, typename> | |
189 inline uint HashCoder::operator*(T e) const { | |
190 return operator*(static_cast<__underlying_type(T)>(e)); | |
191 } | |
192 | |
193 } // namespace _ (private) | |
194 } // namespace kj | |
195 | |
196 KJ_END_HEADER |