Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
hash.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_HASH__
30 #define __ETL_HASH__
31 
32 #include <stdint.h>
33 
34 // The default hash calculation.
35 #include "fnv_1.h"
36 
37 #include "type_traits.h"
38 
41 
42 namespace etl
43 {
44  namespace __private_hash__
45  {
46  //*************************************************************************
48  //*************************************************************************
49  template <typename T>
50  typename enable_if<sizeof(T) == sizeof(uint32_t), size_t>::type
51  generic_hash(uint8_t* begin, uint8_t* end)
52  {
53  return fnv_1a_32<>(begin, end);
54  }
55 
56  //*************************************************************************
58  //*************************************************************************
59  template <typename T>
60  typename enable_if<sizeof(T) == sizeof(uint64_t), size_t>::type
61  generic_hash(uint8_t* begin, uint8_t* end)
62  {
63  return fnv_1a_64<>(begin, end);
64  }
65  }
66 
67  //***************************************************************************
70  //***************************************************************************
71  template <typename T> struct hash;
72 
73  //***************************************************************************
76  //***************************************************************************
77  template <>
78  struct hash <bool>
79  {
80  size_t operator ()(bool v) const
81  {
82  return static_cast<size_t>(v);
83  }
84  };
85 
86  //***************************************************************************
89  //***************************************************************************
90  template <>
91  struct hash<char>
92  {
93  size_t operator ()(char v) const
94  {
95  return static_cast<size_t>(v);
96  }
97  };
98 
99  //***************************************************************************
102  //***************************************************************************
103  template<> struct
105  {
106  size_t operator ()(signed char v) const
107  {
108  return static_cast<size_t>(v);
109  }
110  };
111 
112  //***************************************************************************
115  //***************************************************************************
116  template<>
117  struct hash<unsigned char>
118  {
119  size_t operator ()(unsigned char v) const
120  {
121  return static_cast<size_t>(v);
122  }
123  };
124 
125  //***************************************************************************
128  //***************************************************************************
129  template<>
130  struct hash<wchar_t>
131  {
132  size_t operator ()(wchar_t v) const
133  {
134  return static_cast<size_t>(v);
135  }
136  };
137 
138  //***************************************************************************
141  //***************************************************************************
142  template<>
143  struct hash<short>
144  {
145  size_t operator ()(short v) const
146  {
147  return static_cast<size_t>(v);
148  }
149  };
150 
151  //***************************************************************************
154  //***************************************************************************
155  template<>
156  struct hash<unsigned short>
157  {
158  size_t operator ()(unsigned short v) const
159  {
160  return static_cast<size_t>(v);
161  }
162  };
163 
164  //***************************************************************************
167  //***************************************************************************
168  template<>
169  struct hash<int>
170  {
171  size_t operator ()(int v) const
172  {
173  return static_cast<size_t>(v);
174  }
175  };
176 
177  //***************************************************************************
180  //***************************************************************************
181  template<>
182  struct hash<unsigned int>
183  {
184  size_t operator ()(unsigned int v) const
185  {
186  return static_cast<size_t>(v);
187  }
188  };
189 
190  //***************************************************************************
193  //***************************************************************************
194  template<>
195  struct hash<long>
196  {
197  size_t operator ()(long v) const
198  {
199  if (sizeof(size_t) == sizeof(v))
200  {
201  return static_cast<size_t>(v);
202  }
203  else
204  {
205  uint8_t* p = reinterpret_cast<uint8_t*>(&v);
206 
207  return __private_hash__::generic_hash<size_t>(p, p + sizeof(v));
208  }
209  }
210  };
211 
212  //***************************************************************************
215  //***************************************************************************
216  template<>
217  struct hash<long long>
218  {
219  size_t operator ()(long long v) const
220  {
221  if (sizeof(size_t) == sizeof(v))
222  {
223  return static_cast<size_t>(v);
224  }
225  else
226  {
227  uint8_t* p = reinterpret_cast<uint8_t*>(&v);
228 
229  return __private_hash__::generic_hash<size_t>(p, p + sizeof(v));
230  }
231  }
232  };
233 
234  //***************************************************************************
237  //***************************************************************************
238  template<>
239  struct hash<unsigned long>
240  {
241  size_t operator ()(unsigned long v) const
242  {
243  if (sizeof(size_t) == sizeof(v))
244  {
245  return static_cast<size_t>(v);
246  }
247  else
248  {
249  uint8_t* p = reinterpret_cast<uint8_t*>(&v);
250 
251  return __private_hash__::generic_hash<size_t>(p, p + sizeof(v));
252  }
253  }
254  };
255 
256  //***************************************************************************
259  //***************************************************************************
260  template<>
261  struct hash<unsigned long long>
262  {
263  size_t operator ()(unsigned long long v) const
264  {
265  if (sizeof(size_t) == sizeof(v))
266  {
267  return static_cast<size_t>(v);
268  }
269  else
270  {
271  uint8_t* p = reinterpret_cast<uint8_t*>(&v);
272 
273  return __private_hash__::generic_hash<size_t>(p, p + sizeof(v));
274  }
275  }
276  };
277 
278  //***************************************************************************
281  //***************************************************************************
282  template<>
283  struct hash<float>
284  {
285  size_t operator ()(float v) const
286  {
287  if (sizeof(size_t) == sizeof(v))
288  {
289  return *reinterpret_cast<size_t*>(&v);
290  }
291  else
292  {
293  uint8_t* p = reinterpret_cast<uint8_t*>(&v);
294 
295  return __private_hash__::generic_hash<size_t>(p, p + sizeof(v));
296  }
297  }
298  };
299 
300  //***************************************************************************
303  //***************************************************************************
304  template<>
305  struct hash<double>
306  {
307  size_t operator ()(double v) const
308  {
309  if (sizeof(size_t) == sizeof(v))
310  {
311  return *reinterpret_cast<size_t*>(&v);
312  }
313  else
314  {
315  uint8_t* p = reinterpret_cast<uint8_t*>(&v);
316 
317  return __private_hash__::generic_hash<size_t>(p, p + sizeof(v));
318  }
319  }
320  };
321 
322  //***************************************************************************
325  //***************************************************************************
326  template<>
327  struct hash<long double>
328  {
329  size_t operator ()(long double v) const
330  {
331  if (sizeof(size_t) == sizeof(v))
332  {
333  return *reinterpret_cast<size_t*>(&v);
334  }
335  else
336  {
337  uint8_t* p = reinterpret_cast<uint8_t*>(&v);
338 
339  return __private_hash__::generic_hash<size_t>(p, p + sizeof(v));
340  }
341  }
342  };
343 
344  //***************************************************************************
347  //***************************************************************************
348  template <typename T>
349  struct hash<T*>
350  {
351  size_t operator ()(T* v) const
352  {
353  if (sizeof(size_t) == sizeof(v))
354  {
355  return reinterpret_cast<size_t>(v);
356  }
357  else
358  {
359  uint8_t* p = reinterpret_cast<uint8_t*>(&v);
360 
361  return __private_hash__::generic_hash<size_t>(p, p + sizeof(v));
362  }
363  }
364  };
365 }
366 
367 #endif
Definition: hash.h:103
Definition: hash.h:71
Definition: algorithm.h:43
TContainer::iterator end(TContainer &container)
Definition: container.h:95
TContainer::iterator begin(TContainer &container)
Definition: container.h:45