| 1 |
// TR1 functional_hash.h header -*- C++ -*- |
| 2 |
|
| 3 |
// Copyright (C) 2007-2021 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// Under Section 7 of GPL version 3, you are granted additional |
| 17 |
// permissions described in the GCC Runtime Library Exception, version |
| 18 |
// 3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
// You should have received a copy of the GNU General Public License and |
| 21 |
// a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
// <http://www.gnu.org/licenses/>. |
| 24 |
|
| 25 |
/** @file tr1/functional_hash.h |
| 26 |
* This is an internal header file, included by other library headers. |
| 27 |
* Do not attempt to use it directly. @headername{tr1/functional} |
| 28 |
*/ |
| 29 |
|
| 30 |
#ifndef _GLIBCXX_TR1_FUNCTIONAL_HASH_H |
| 31 |
#define _GLIBCXX_TR1_FUNCTIONAL_HASH_H 1 |
| 32 |
|
| 33 |
#pragma GCC system_header |
| 34 |
|
| 35 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 36 |
{ |
| 37 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 38 |
|
| 39 |
namespace tr1 |
| 40 |
{ |
| 41 |
/// Class template hash. |
| 42 |
// Declaration of default hash functor std::tr1::hash. The types for |
| 43 |
// which std::tr1::hash<T> is well-defined is in clause 6.3.3. of the PDTR. |
| 44 |
template<typename _Tp> |
| 45 |
struct hash : public std::unary_function<_Tp, size_t> |
| 46 |
{ |
| 47 |
size_t |
| 48 |
operator()(_Tp __val) const; |
| 49 |
}; |
| 50 |
|
| 51 |
/// Partial specializations for pointer types. |
| 52 |
template<typename _Tp> |
| 53 |
struct hash<_Tp*> : public std::unary_function<_Tp*, size_t> |
| 54 |
{ |
| 55 |
size_t |
| 56 |
operator()(_Tp* __p) const |
| 57 |
{ return reinterpret_cast<size_t>(__p); } |
| 58 |
}; |
| 59 |
|
| 60 |
/// Explicit specializations for integer types. |
| 61 |
#define _TR1_hashtable_define_trivial_hash(_Tp) \ |
| 62 |
template<> \ |
| 63 |
inline size_t \ |
| 64 |
hash<_Tp>::operator()(_Tp __val) const \ |
| 65 |
{ return static_cast<size_t>(__val); } |
| 66 |
|
| 67 |
_TR1_hashtable_define_trivial_hash(bool); |
| 68 |
_TR1_hashtable_define_trivial_hash(char); |
| 69 |
_TR1_hashtable_define_trivial_hash(signed char); |
| 70 |
_TR1_hashtable_define_trivial_hash(unsigned char); |
| 71 |
_TR1_hashtable_define_trivial_hash(wchar_t); |
| 72 |
_TR1_hashtable_define_trivial_hash(short); |
| 73 |
_TR1_hashtable_define_trivial_hash(int); |
| 74 |
_TR1_hashtable_define_trivial_hash(long); |
| 75 |
_TR1_hashtable_define_trivial_hash(long long); |
| 76 |
_TR1_hashtable_define_trivial_hash(unsigned short); |
| 77 |
_TR1_hashtable_define_trivial_hash(unsigned int); |
| 78 |
_TR1_hashtable_define_trivial_hash(unsigned long); |
| 79 |
_TR1_hashtable_define_trivial_hash(unsigned long long); |
| 80 |
|
| 81 |
#undef _TR1_hashtable_define_trivial_hash |
| 82 |
|
| 83 |
// Fowler / Noll / Vo (FNV) Hash (type FNV-1a) |
| 84 |
// (Used by the next specializations of std::tr1::hash.) |
| 85 |
|
| 86 |
// N.B. These functions should work on unsigned char, otherwise they do not |
| 87 |
// correctly implement the FNV-1a algorithm (see PR59406). |
| 88 |
// The existing behaviour is retained for backwards compatibility. |
| 89 |
|
| 90 |
/// Dummy generic implementation (for sizeof(size_t) != 4, 8). |
| 91 |
template<size_t> |
| 92 |
struct _Fnv_hash_base |
| 93 |
{ |
| 94 |
template<typename _Tp> |
| 95 |
static size_t |
| 96 |
hash(const _Tp* __ptr, size_t __clength) |
| 97 |
{ |
| 98 |
size_t __result = 0; |
| 99 |
const char* __cptr = reinterpret_cast<const char*>(__ptr); |
| 100 |
for (; __clength; --__clength) |
| 101 |
__result = (__result * 131) + *__cptr++; |
| 102 |
return __result; |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
template<> |
| 107 |
struct _Fnv_hash_base<4> |
| 108 |
{ |
| 109 |
template<typename _Tp> |
| 110 |
static size_t |
| 111 |
hash(const _Tp* __ptr, size_t __clength) |
| 112 |
{ |
| 113 |
size_t __result = static_cast<size_t>(2166136261UL); |
| 114 |
const char* __cptr = reinterpret_cast<const char*>(__ptr); |
| 115 |
for (; __clength; --__clength) |
| 116 |
{ |
| 117 |
__result ^= static_cast<size_t>(*__cptr++); |
| 118 |
__result *= static_cast<size_t>(16777619UL); |
| 119 |
} |
| 120 |
return __result; |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
template<> |
| 125 |
struct _Fnv_hash_base<8> |
| 126 |
{ |
| 127 |
template<typename _Tp> |
| 128 |
static size_t |
| 129 |
hash(const _Tp* __ptr, size_t __clength) |
| 130 |
{ |
| 131 |
size_t __result |
| 132 |
= static_cast<size_t>(14695981039346656037ULL); |
| 133 |
const char* __cptr = reinterpret_cast<const char*>(__ptr); |
| 134 |
for (; __clength; --__clength) |
| 135 |
{ |
| 136 |
__result ^= static_cast<size_t>(*__cptr++); |
| 137 |
__result *= static_cast<size_t>(1099511628211ULL); |
| 138 |
} |
| 139 |
return __result; |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
struct _Fnv_hash |
| 144 |
: public _Fnv_hash_base<sizeof(size_t)> |
| 145 |
{ |
| 146 |
using _Fnv_hash_base<sizeof(size_t)>::hash; |
| 147 |
|
| 148 |
template<typename _Tp> |
| 149 |
static size_t |
| 150 |
hash(const _Tp& __val) |
| 151 |
{ return hash(&__val, sizeof(__val)); } |
| 152 |
}; |
| 153 |
|
| 154 |
/// Explicit specializations for float. |
| 155 |
template<> |
| 156 |
inline size_t |
| 157 |
hash<float>::operator()(float __val) const |
| 158 |
{ |
| 159 |
// 0 and -0 both hash to zero. |
| 160 |
return __val != 0.0f ? std::tr1::_Fnv_hash::hash(__val) : 0; |
| 161 |
} |
| 162 |
|
| 163 |
/// Explicit specializations for double. |
| 164 |
template<> |
| 165 |
inline size_t |
| 166 |
hash<double>::operator()(double __val) const |
| 167 |
{ |
| 168 |
// 0 and -0 both hash to zero. |
| 169 |
return __val != 0.0 ? std::tr1::_Fnv_hash::hash(__val) : 0; |
| 170 |
} |
| 171 |
|
| 172 |
/// Explicit specializations for long double. |
| 173 |
template<> |
| 174 |
_GLIBCXX_PURE size_t |
| 175 |
hash<long double>::operator()(long double __val) const; |
| 176 |
|
| 177 |
/// Explicit specialization of member operator for non-builtin types. |
| 178 |
template<> |
| 179 |
_GLIBCXX_PURE size_t |
| 180 |
hash<string>::operator()(string) const; |
| 181 |
|
| 182 |
template<> |
| 183 |
_GLIBCXX_PURE size_t |
| 184 |
hash<const string&>::operator()(const string&) const; |
| 185 |
|
| 186 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 187 |
template<> |
| 188 |
_GLIBCXX_PURE size_t |
| 189 |
hash<wstring>::operator()(wstring) const; |
| 190 |
|
| 191 |
template<> |
| 192 |
_GLIBCXX_PURE size_t |
| 193 |
hash<const wstring&>::operator()(const wstring&) const; |
| 194 |
#endif |
| 195 |
} |
| 196 |
|
| 197 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 198 |
} |
| 199 |
|
| 200 |
#endif // _GLIBCXX_TR1_FUNCTIONAL_HASH_H |