PR debug/81307
[official-gcc.git] / libstdc++-v3 / include / bits / functional_hash.h
blobc0936843ac076c0372c59c0082f7f8ca515128bf
1 // functional_hash.h header -*- C++ -*-
3 // Copyright (C) 2007-2017 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.
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.
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.
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/>.
25 /** @file bits/functional_hash.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{functional}
30 #ifndef _FUNCTIONAL_HASH_H
31 #define _FUNCTIONAL_HASH_H 1
33 #pragma GCC system_header
35 #include <bits/hash_bytes.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 /** @defgroup hashes Hashes
42 * @ingroup functors
44 * Hashing functors taking a variable type and returning a @c std::size_t.
46 * @{
49 template<typename _Result, typename _Arg>
50 struct __hash_base
52 typedef _Result result_type _GLIBCXX17_DEPRECATED;
53 typedef _Arg argument_type _GLIBCXX17_DEPRECATED;
56 /// Primary class template hash.
57 template<typename _Tp>
58 struct hash;
60 template<typename _Tp, typename = void>
61 struct __poison_hash
63 static constexpr bool __enable_hash_call = false;
64 private:
65 // Private rather than deleted to be non-trivially-copyable.
66 __poison_hash(__poison_hash&&);
67 ~__poison_hash();
70 template<typename _Tp>
71 struct __poison_hash<_Tp, __void_t<decltype(hash<_Tp>()(declval<_Tp>()))>>
73 static constexpr bool __enable_hash_call = true;
76 // Helper struct for SFINAE-poisoning non-enum types.
77 template<typename _Tp, bool = is_enum<_Tp>::value>
78 struct __hash_enum
80 private:
81 // Private rather than deleted to be non-trivially-copyable.
82 __hash_enum(__hash_enum&&);
83 ~__hash_enum();
86 // Helper struct for hash with enum types.
87 template<typename _Tp>
88 struct __hash_enum<_Tp, true> : public __hash_base<size_t, _Tp>
90 size_t
91 operator()(_Tp __val) const noexcept
93 using __type = typename underlying_type<_Tp>::type;
94 return hash<__type>{}(static_cast<__type>(__val));
98 /// Primary class template hash, usable for enum types only.
99 // Use with non-enum types still SFINAES.
100 template<typename _Tp>
101 struct hash : __hash_enum<_Tp>
102 { };
104 /// Partial specializations for pointer types.
105 template<typename _Tp>
106 struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
108 size_t
109 operator()(_Tp* __p) const noexcept
110 { return reinterpret_cast<size_t>(__p); }
113 // Explicit specializations for integer types.
114 #define _Cxx_hashtable_define_trivial_hash(_Tp) \
115 template<> \
116 struct hash<_Tp> : public __hash_base<size_t, _Tp> \
118 size_t \
119 operator()(_Tp __val) const noexcept \
120 { return static_cast<size_t>(__val); } \
123 /// Explicit specialization for bool.
124 _Cxx_hashtable_define_trivial_hash(bool)
126 /// Explicit specialization for char.
127 _Cxx_hashtable_define_trivial_hash(char)
129 /// Explicit specialization for signed char.
130 _Cxx_hashtable_define_trivial_hash(signed char)
132 /// Explicit specialization for unsigned char.
133 _Cxx_hashtable_define_trivial_hash(unsigned char)
135 /// Explicit specialization for wchar_t.
136 _Cxx_hashtable_define_trivial_hash(wchar_t)
138 /// Explicit specialization for char16_t.
139 _Cxx_hashtable_define_trivial_hash(char16_t)
141 /// Explicit specialization for char32_t.
142 _Cxx_hashtable_define_trivial_hash(char32_t)
144 /// Explicit specialization for short.
145 _Cxx_hashtable_define_trivial_hash(short)
147 /// Explicit specialization for int.
148 _Cxx_hashtable_define_trivial_hash(int)
150 /// Explicit specialization for long.
151 _Cxx_hashtable_define_trivial_hash(long)
153 /// Explicit specialization for long long.
154 _Cxx_hashtable_define_trivial_hash(long long)
156 /// Explicit specialization for unsigned short.
157 _Cxx_hashtable_define_trivial_hash(unsigned short)
159 /// Explicit specialization for unsigned int.
160 _Cxx_hashtable_define_trivial_hash(unsigned int)
162 /// Explicit specialization for unsigned long.
163 _Cxx_hashtable_define_trivial_hash(unsigned long)
165 /// Explicit specialization for unsigned long long.
166 _Cxx_hashtable_define_trivial_hash(unsigned long long)
168 #ifdef __GLIBCXX_TYPE_INT_N_0
169 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0)
170 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0 unsigned)
171 #endif
172 #ifdef __GLIBCXX_TYPE_INT_N_1
173 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1)
174 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1 unsigned)
175 #endif
176 #ifdef __GLIBCXX_TYPE_INT_N_2
177 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2)
178 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2 unsigned)
179 #endif
180 #ifdef __GLIBCXX_TYPE_INT_N_3
181 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3)
182 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3 unsigned)
183 #endif
185 #undef _Cxx_hashtable_define_trivial_hash
187 struct _Hash_impl
189 static size_t
190 hash(const void* __ptr, size_t __clength,
191 size_t __seed = static_cast<size_t>(0xc70f6907UL))
192 { return _Hash_bytes(__ptr, __clength, __seed); }
194 template<typename _Tp>
195 static size_t
196 hash(const _Tp& __val)
197 { return hash(&__val, sizeof(__val)); }
199 template<typename _Tp>
200 static size_t
201 __hash_combine(const _Tp& __val, size_t __hash)
202 { return hash(&__val, sizeof(__val), __hash); }
205 // A hash function similar to FNV-1a (see PR59406 for how it differs).
206 struct _Fnv_hash_impl
208 static size_t
209 hash(const void* __ptr, size_t __clength,
210 size_t __seed = static_cast<size_t>(2166136261UL))
211 { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
213 template<typename _Tp>
214 static size_t
215 hash(const _Tp& __val)
216 { return hash(&__val, sizeof(__val)); }
218 template<typename _Tp>
219 static size_t
220 __hash_combine(const _Tp& __val, size_t __hash)
221 { return hash(&__val, sizeof(__val), __hash); }
224 /// Specialization for float.
225 template<>
226 struct hash<float> : public __hash_base<size_t, float>
228 size_t
229 operator()(float __val) const noexcept
231 // 0 and -0 both hash to zero.
232 return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
236 /// Specialization for double.
237 template<>
238 struct hash<double> : public __hash_base<size_t, double>
240 size_t
241 operator()(double __val) const noexcept
243 // 0 and -0 both hash to zero.
244 return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
248 /// Specialization for long double.
249 template<>
250 struct hash<long double>
251 : public __hash_base<size_t, long double>
253 _GLIBCXX_PURE size_t
254 operator()(long double __val) const noexcept;
257 // @} group hashes
259 // Hint about performance of hash functor. If not fast the hash-based
260 // containers will cache the hash code.
261 // Default behavior is to consider that hashers are fast unless specified
262 // otherwise.
263 template<typename _Hash>
264 struct __is_fast_hash : public std::true_type
265 { };
267 template<>
268 struct __is_fast_hash<hash<long double>> : public std::false_type
269 { };
271 _GLIBCXX_END_NAMESPACE_VERSION
272 } // namespace
274 #endif // _FUNCTIONAL_HASH_H