Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/tr1/functional_hash.h
1 // TR1 functional_hash.h header -*- C++ -*- 2 3 // Copyright (C) 2007-2025 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 #ifdef _GLIBCXX_SYSHDR 34 #pragma GCC system_header 35 #endif 36 37 namespace std _GLIBCXX_VISIBILITY(default) 38 { 39 _GLIBCXX_BEGIN_NAMESPACE_VERSION 40 41 namespace tr1 42 { 43 // Ignore warnings about std::unary_function and std::binary_function. 44 #pragma GCC diagnostic push 45 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 46 47 /// Class template hash. 48 // Declaration of default hash functor std::tr1::hash. The types for 49 // which std::tr1::hash<T> is well-defined is in clause 6.3.3. of the PDTR. 50 template<typename _Tp> 51 struct hash : public std::unary_function<_Tp, size_t> 52 { 53 size_t 54 operator()(_Tp __val) const; 55 }; 56 57 /// Partial specializations for pointer types. 58 template<typename _Tp> 59 struct hash<_Tp*> : public std::unary_function<_Tp*, size_t> 60 { 61 size_t 62 operator()(_Tp* __p) const 63 { return reinterpret_cast<size_t>(__p); } 64 }; 65 #pragma GCC diagnostic pop 66 67 /// Explicit specializations for integer types. 68 #define _TR1_hashtable_define_trivial_hash(_Tp) \ 69 template<> \ 70 inline size_t \ 71 hash<_Tp>::operator()(_Tp __val) const \ 72 { return static_cast<size_t>(__val); } 73 74 #pragma GCC diagnostic push 75 #pragma GCC diagnostic ignored "-Wlong-long" 76 _TR1_hashtable_define_trivial_hash(bool) 77 _TR1_hashtable_define_trivial_hash(char) 78 _TR1_hashtable_define_trivial_hash(signed char) 79 _TR1_hashtable_define_trivial_hash(unsigned char) 80 _TR1_hashtable_define_trivial_hash(wchar_t) 81 _TR1_hashtable_define_trivial_hash(short) 82 _TR1_hashtable_define_trivial_hash(int) 83 _TR1_hashtable_define_trivial_hash(long) 84 _TR1_hashtable_define_trivial_hash(long long) 85 _TR1_hashtable_define_trivial_hash(unsigned short) 86 _TR1_hashtable_define_trivial_hash(unsigned int) 87 _TR1_hashtable_define_trivial_hash(unsigned long) 88 _TR1_hashtable_define_trivial_hash(unsigned long long) 89 #pragma GCC diagnostic pop 90 91 #undef _TR1_hashtable_define_trivial_hash 92 93 // Fowler / Noll / Vo (FNV) Hash (type FNV-1a) 94 // (Used by the next specializations of std::tr1::hash.) 95 96 // N.B. These functions should work on unsigned char, otherwise they do not 97 // correctly implement the FNV-1a algorithm (see PR59406). 98 // The existing behaviour is retained for backwards compatibility. 99 100 /// Dummy generic implementation (for sizeof(size_t) != 4, 8). 101 template<size_t> 102 struct _Fnv_hash_base 103 { 104 template<typename _Tp> 105 static size_t 106 hash(const _Tp* __ptr, size_t __clength) 107 { 108 size_t __result = 0; 109 const char* __cptr = reinterpret_cast<const char*>(__ptr); 110 for (; __clength; --__clength) 111 __result = (__result * 131) + *__cptr++; 112 return __result; 113 } 114 }; 115 116 template<> 117 struct _Fnv_hash_base<4> 118 { 119 template<typename _Tp> 120 static size_t 121 hash(const _Tp* __ptr, size_t __clength) 122 { 123 size_t __result = static_cast<size_t>(2166136261UL); 124 const char* __cptr = reinterpret_cast<const char*>(__ptr); 125 for (; __clength; --__clength) 126 { 127 __result ^= static_cast<size_t>(*__cptr++); 128 __result *= static_cast<size_t>(16777619UL); 129 } 130 return __result; 131 } 132 }; 133 134 template<> 135 struct _Fnv_hash_base<8> 136 { 137 template<typename _Tp> 138 static size_t 139 hash(const _Tp* __ptr, size_t __clength) 140 { 141 #pragma GCC diagnostic push 142 #pragma GCC diagnostic ignored "-Wlong-long" 143 size_t __result 144 = static_cast<size_t>(14695981039346656037ULL); 145 const char* __cptr = reinterpret_cast<const char*>(__ptr); 146 for (; __clength; --__clength) 147 { 148 __result ^= static_cast<size_t>(*__cptr++); 149 __result *= static_cast<size_t>(1099511628211ULL); 150 } 151 return __result; 152 #pragma GCC diagnostic pop 153 } 154 }; 155 156 struct _Fnv_hash 157 : public _Fnv_hash_base<sizeof(size_t)> 158 { 159 using _Fnv_hash_base<sizeof(size_t)>::hash; 160 161 template<typename _Tp> 162 static size_t 163 hash(const _Tp& __val) 164 { return hash(&__val, sizeof(__val)); } 165 }; 166 167 /// Explicit specializations for float. 168 template<> 169 inline size_t 170 hash<float>::operator()(float __val) const 171 { 172 // 0 and -0 both hash to zero. 173 return __val != 0.0f ? std::tr1::_Fnv_hash::hash(__val) : 0; 174 } 175 176 /// Explicit specializations for double. 177 template<> 178 inline size_t 179 hash<double>::operator()(double __val) const 180 { 181 // 0 and -0 both hash to zero. 182 return __val != 0.0 ? std::tr1::_Fnv_hash::hash(__val) : 0; 183 } 184 185 /// Explicit specializations for long double. 186 template<> 187 _GLIBCXX_PURE size_t 188 hash<long double>::operator()(long double __val) const; 189 190 /// Explicit specialization of member operator for non-builtin types. 191 template<> 192 _GLIBCXX_PURE size_t 193 hash<string>::operator()(string) const; 194 195 template<> 196 _GLIBCXX_PURE size_t 197 hash<const string&>::operator()(const string&) const; 198 199 #ifdef _GLIBCXX_USE_WCHAR_T 200 template<> 201 _GLIBCXX_PURE size_t 202 hash<wstring>::operator()(wstring) const; 203 204 template<> 205 _GLIBCXX_PURE size_t 206 hash<const wstring&>::operator()(const wstring&) const; 207 #endif 208 } 209 210 _GLIBCXX_END_NAMESPACE_VERSION 211 } 212 213 #endif // _GLIBCXX_TR1_FUNCTIONAL_HASH_H