source: mainline/uspace/lib/cpp/include/__bits/functional/hash.hpp@ 5ab9df4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5ab9df4 was 7bbf91e, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: changed internal to bits to avoid include space pollusion, fixed old std::hel:: bugs in files that weren't touched since

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_BITS_FUNCTIONAL_HASH
30#define LIBCPP_BITS_FUNCTIONAL_HASH
31
32#include <cstdlib>
33#include <cstdint>
34
35namespace std
36{
37 template<class>
38 struct is_arithmetic;
39
40 template<class>
41 struct is_pointer;
42
43 /**
44 * 20.9.13, hash function primary template:
45 */
46
47 namespace aux
48 {
49 template<class T>
50 union converter
51 {
52 T value;
53 uint64_t converted;
54 };
55
56 template<class T>
57 T hash_(uint64_t x) noexcept
58 {
59 /**
60 * Note: std::hash is used for indexing in
61 * unordered containers, not for cryptography.
62 * Because of this, we decided to simply convert
63 * the value to uin64_t, which will help us
64 * with testing (since in order to create
65 * a collision in a multiset or multimap
66 * we simply need 2 values that congruent
67 * by the size of the table.
68 */
69 return static_cast<T>(x);
70 }
71
72 template<class T>
73 size_t hash(T x) noexcept
74 {
75 static_assert(is_arithmetic<T>::value || is_pointer<T>::value,
76 "invalid type passed to aux::hash");
77
78 converter<T> conv;
79 conv.value = x;
80
81 return hash_<size_t>(conv.converted);
82 }
83 }
84
85 template<class T>
86 struct hash
87 { /* DUMMY BODY */ };
88
89 template<>
90 struct hash<bool>
91 {
92 size_t operator()(bool x) const noexcept
93 {
94 return aux::hash(x);
95 }
96
97 using argument_type = bool;
98 using result_type = size_t;
99 };
100
101 template<>
102 struct hash<char>
103 {
104 size_t operator()(char x) const noexcept
105 {
106 return aux::hash(x);
107 }
108
109 using argument_type = char;
110 using result_type = size_t;
111 };
112
113 template<>
114 struct hash<signed char>
115 {
116 size_t operator()(signed char x) const noexcept
117 {
118 return aux::hash(x);
119 }
120
121 using argument_type = signed char;
122 using result_type = size_t;
123 };
124
125 template<>
126 struct hash<unsigned char>
127 {
128 size_t operator()(unsigned char x) const noexcept
129 {
130 return aux::hash(x);
131 }
132
133 using argument_type = unsigned char;
134 using result_type = size_t;
135 };
136
137 template<>
138 struct hash<char16_t>
139 {
140 size_t operator()(char16_t x) const noexcept
141 {
142 return aux::hash(x);
143 }
144
145 using argument_type = char16_t;
146 using result_type = size_t;
147 };
148
149 template<>
150 struct hash<char32_t>
151 {
152 size_t operator()(char32_t x) const noexcept
153 {
154 return aux::hash(x);
155 }
156
157 using argument_type = char32_t;
158 using result_type = size_t;
159 };
160
161 template<>
162 struct hash<wchar_t>
163 {
164 size_t operator()(wchar_t x) const noexcept
165 {
166 return aux::hash(x);
167 }
168
169 using argument_type = wchar_t;
170 using result_type = size_t;
171 };
172
173 template<>
174 struct hash<short>
175 {
176 size_t operator()(short x) const noexcept
177 {
178 return aux::hash(x);
179 }
180
181 using argument_type = short;
182 using result_type = size_t;
183 };
184
185 template<>
186 struct hash<unsigned short>
187 {
188 size_t operator()(unsigned short x) const noexcept
189 {
190 return aux::hash(x);
191 }
192
193 using argument_type = unsigned short;
194 using result_type = size_t;
195 };
196
197 template<>
198 struct hash<int>
199 {
200 size_t operator()(int x) const noexcept
201 {
202 return aux::hash(x);
203 }
204
205 using argument_type = int;
206 using result_type = size_t;
207 };
208
209 template<>
210 struct hash<unsigned int>
211 {
212 size_t operator()(unsigned int x) const noexcept
213 {
214 return aux::hash(x);
215 }
216
217 using argument_type = unsigned int;
218 using result_type = size_t;
219 };
220
221 template<>
222 struct hash<long>
223 {
224 size_t operator()(long x) const noexcept
225 {
226 return aux::hash(x);
227 }
228
229 using argument_type = long;
230 using result_type = size_t;
231 };
232
233 template<>
234 struct hash<long long>
235 {
236 size_t operator()(long long x) const noexcept
237 {
238 return aux::hash(x);
239 }
240
241 using argument_type = long long;
242 using result_type = size_t;
243 };
244
245 template<>
246 struct hash<unsigned long>
247 {
248 size_t operator()(unsigned long x) const noexcept
249 {
250 return aux::hash(x);
251 }
252
253 using argument_type = unsigned long;
254 using result_type = size_t;
255 };
256
257 template<>
258 struct hash<unsigned long long>
259 {
260 size_t operator()(unsigned long long x) const noexcept
261 {
262 return aux::hash(x);
263 }
264
265 using argument_type = unsigned long long;
266 using result_type = size_t;
267 };
268
269 template<>
270 struct hash<float>
271 {
272 size_t operator()(float x) const noexcept
273 {
274 return aux::hash(x);
275 }
276
277 using argument_type = float;
278 using result_type = size_t;
279 };
280
281 template<>
282 struct hash<double>
283 {
284 size_t operator()(double x) const noexcept
285 {
286 return aux::hash(x);
287 }
288
289 using argument_type = double;
290 using result_type = size_t;
291 };
292
293 template<>
294 struct hash<long double>
295 {
296 size_t operator()(long double x) const noexcept
297 {
298 return aux::hash(x);
299 }
300
301 using argument_type = long double;
302 using result_type = size_t;
303 };
304
305 template<class T>
306 struct hash<T*>
307 {
308 size_t operator()(T* x) const noexcept
309 {
310 return aux::hash(x);
311 }
312
313 using argument_type = T*;
314 using result_type = size_t;
315 };
316}
317
318#endif
Note: See TracBrowser for help on using the repository browser.