source: mainline/uspace/lib/cpp/include/__bits/functional/hash.hpp@ 8fd0675f

Last change on this file since 8fd0675f was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef LIBCPP_BITS_FUNCTIONAL_HASH
8#define LIBCPP_BITS_FUNCTIONAL_HASH
9
10#include <cstdlib>
11#include <cstdint>
12
13namespace std
14{
15 template<class>
16 struct is_arithmetic;
17
18 template<class>
19 struct is_pointer;
20
21 /**
22 * 20.9.13, hash function primary template:
23 */
24
25 namespace aux
26 {
27 template<class T>
28 union converter
29 {
30 T value;
31 uint64_t converted;
32 };
33
34 template<class T>
35 T hash_(uint64_t x) noexcept
36 {
37 /**
38 * Note: std::hash is used for indexing in
39 * unordered containers, not for cryptography.
40 * Because of this, we decided to simply convert
41 * the value to uin64_t, which will help us
42 * with testing (since in order to create
43 * a collision in a multiset or multimap
44 * we simply need 2 values that congruent
45 * by the size of the table.
46 */
47 return static_cast<T>(x);
48 }
49
50 template<class T>
51 size_t hash(T x) noexcept
52 {
53 static_assert(is_arithmetic<T>::value || is_pointer<T>::value,
54 "invalid type passed to aux::hash");
55
56 converter<T> conv;
57 conv.value = x;
58
59 return hash_<size_t>(conv.converted);
60 }
61 }
62
63 template<class T>
64 struct hash
65 { /* DUMMY BODY */ };
66
67 template<>
68 struct hash<bool>
69 {
70 size_t operator()(bool x) const noexcept
71 {
72 return aux::hash(x);
73 }
74
75 using argument_type = bool;
76 using result_type = size_t;
77 };
78
79 template<>
80 struct hash<char>
81 {
82 size_t operator()(char x) const noexcept
83 {
84 return aux::hash(x);
85 }
86
87 using argument_type = char;
88 using result_type = size_t;
89 };
90
91 template<>
92 struct hash<signed char>
93 {
94 size_t operator()(signed char x) const noexcept
95 {
96 return aux::hash(x);
97 }
98
99 using argument_type = signed char;
100 using result_type = size_t;
101 };
102
103 template<>
104 struct hash<unsigned char>
105 {
106 size_t operator()(unsigned char x) const noexcept
107 {
108 return aux::hash(x);
109 }
110
111 using argument_type = unsigned char;
112 using result_type = size_t;
113 };
114
115 template<>
116 struct hash<char16_t>
117 {
118 size_t operator()(char16_t x) const noexcept
119 {
120 return aux::hash(x);
121 }
122
123 using argument_type = char16_t;
124 using result_type = size_t;
125 };
126
127 template<>
128 struct hash<char32_t>
129 {
130 size_t operator()(char32_t x) const noexcept
131 {
132 return aux::hash(x);
133 }
134
135 using argument_type = char32_t;
136 using result_type = size_t;
137 };
138
139 template<>
140 struct hash<wchar_t>
141 {
142 size_t operator()(wchar_t x) const noexcept
143 {
144 return aux::hash(x);
145 }
146
147 using argument_type = wchar_t;
148 using result_type = size_t;
149 };
150
151 template<>
152 struct hash<short>
153 {
154 size_t operator()(short x) const noexcept
155 {
156 return aux::hash(x);
157 }
158
159 using argument_type = short;
160 using result_type = size_t;
161 };
162
163 template<>
164 struct hash<unsigned short>
165 {
166 size_t operator()(unsigned short x) const noexcept
167 {
168 return aux::hash(x);
169 }
170
171 using argument_type = unsigned short;
172 using result_type = size_t;
173 };
174
175 template<>
176 struct hash<int>
177 {
178 size_t operator()(int x) const noexcept
179 {
180 return aux::hash(x);
181 }
182
183 using argument_type = int;
184 using result_type = size_t;
185 };
186
187 template<>
188 struct hash<unsigned int>
189 {
190 size_t operator()(unsigned int x) const noexcept
191 {
192 return aux::hash(x);
193 }
194
195 using argument_type = unsigned int;
196 using result_type = size_t;
197 };
198
199 template<>
200 struct hash<long>
201 {
202 size_t operator()(long x) const noexcept
203 {
204 return aux::hash(x);
205 }
206
207 using argument_type = long;
208 using result_type = size_t;
209 };
210
211 template<>
212 struct hash<long long>
213 {
214 size_t operator()(long long x) const noexcept
215 {
216 return aux::hash(x);
217 }
218
219 using argument_type = long long;
220 using result_type = size_t;
221 };
222
223 template<>
224 struct hash<unsigned long>
225 {
226 size_t operator()(unsigned long x) const noexcept
227 {
228 return aux::hash(x);
229 }
230
231 using argument_type = unsigned long;
232 using result_type = size_t;
233 };
234
235 template<>
236 struct hash<unsigned long long>
237 {
238 size_t operator()(unsigned long long x) const noexcept
239 {
240 return aux::hash(x);
241 }
242
243 using argument_type = unsigned long long;
244 using result_type = size_t;
245 };
246
247 template<>
248 struct hash<float>
249 {
250 size_t operator()(float x) const noexcept
251 {
252 return aux::hash(x);
253 }
254
255 using argument_type = float;
256 using result_type = size_t;
257 };
258
259 template<>
260 struct hash<double>
261 {
262 size_t operator()(double x) const noexcept
263 {
264 return aux::hash(x);
265 }
266
267 using argument_type = double;
268 using result_type = size_t;
269 };
270
271 template<>
272 struct hash<long double>
273 {
274 size_t operator()(long double x) const noexcept
275 {
276 return aux::hash(x);
277 }
278
279 using argument_type = long double;
280 using result_type = size_t;
281 };
282
283 template<class T>
284 struct hash<T*>
285 {
286 size_t operator()(T* x) const noexcept
287 {
288 return aux::hash(x);
289 }
290
291 using argument_type = T*;
292 using result_type = size_t;
293 };
294}
295
296#endif
Note: See TracBrowser for help on using the repository browser.