]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/functional_hash.h
re PR libstdc++/52931 (std::hash shouldn't be defined for unknown types)
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / functional_hash.h
1 // functional_hash.h header -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /** @file bits/functional_hash.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{functional}
29 */
30
31 #ifndef _FUNCTIONAL_HASH_H
32 #define _FUNCTIONAL_HASH_H 1
33
34 #pragma GCC system_header
35
36 #include <bits/hash_bytes.h>
37
38 namespace std _GLIBCXX_VISIBILITY(default)
39 {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41
42 /** @defgroup hashes Hashes
43 * @ingroup functors
44 *
45 * Hashing functors taking a variable type and returning a @c std::size_t.
46 *
47 * @{
48 */
49
50 template<typename _Result, typename _Arg>
51 struct __hash_base
52 {
53 typedef _Result result_type;
54 typedef _Arg argument_type;
55 };
56
57 /// Primary class template hash.
58 template<typename _Tp>
59 struct hash;
60
61 /// Partial specializations for pointer types.
62 template<typename _Tp>
63 struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
64 {
65 size_t
66 operator()(_Tp* __p) const noexcept
67 { return reinterpret_cast<size_t>(__p); }
68 };
69
70 // Explicit specializations for integer types.
71 #define _Cxx_hashtable_define_trivial_hash(_Tp) \
72 template<> \
73 struct hash<_Tp> : public __hash_base<size_t, _Tp> \
74 { \
75 size_t \
76 operator()(_Tp __val) const noexcept \
77 { return static_cast<size_t>(__val); } \
78 };
79
80 /// Explicit specialization for bool.
81 _Cxx_hashtable_define_trivial_hash(bool)
82
83 /// Explicit specialization for char.
84 _Cxx_hashtable_define_trivial_hash(char)
85
86 /// Explicit specialization for signed char.
87 _Cxx_hashtable_define_trivial_hash(signed char)
88
89 /// Explicit specialization for unsigned char.
90 _Cxx_hashtable_define_trivial_hash(unsigned char)
91
92 /// Explicit specialization for wchar_t.
93 _Cxx_hashtable_define_trivial_hash(wchar_t)
94
95 /// Explicit specialization for char16_t.
96 _Cxx_hashtable_define_trivial_hash(char16_t)
97
98 /// Explicit specialization for char32_t.
99 _Cxx_hashtable_define_trivial_hash(char32_t)
100
101 /// Explicit specialization for short.
102 _Cxx_hashtable_define_trivial_hash(short)
103
104 /// Explicit specialization for int.
105 _Cxx_hashtable_define_trivial_hash(int)
106
107 /// Explicit specialization for long.
108 _Cxx_hashtable_define_trivial_hash(long)
109
110 /// Explicit specialization for long long.
111 _Cxx_hashtable_define_trivial_hash(long long)
112
113 /// Explicit specialization for unsigned short.
114 _Cxx_hashtable_define_trivial_hash(unsigned short)
115
116 /// Explicit specialization for unsigned int.
117 _Cxx_hashtable_define_trivial_hash(unsigned int)
118
119 /// Explicit specialization for unsigned long.
120 _Cxx_hashtable_define_trivial_hash(unsigned long)
121
122 /// Explicit specialization for unsigned long long.
123 _Cxx_hashtable_define_trivial_hash(unsigned long long)
124
125 #undef _Cxx_hashtable_define_trivial_hash
126
127 struct _Hash_impl
128 {
129 static size_t
130 hash(const void* __ptr, size_t __clength,
131 size_t __seed = static_cast<size_t>(0xc70f6907UL))
132 { return _Hash_bytes(__ptr, __clength, __seed); }
133
134 template<typename _Tp>
135 static size_t
136 hash(const _Tp& __val)
137 { return hash(&__val, sizeof(__val)); }
138
139 template<typename _Tp>
140 static size_t
141 __hash_combine(const _Tp& __val, size_t __hash)
142 { return hash(&__val, sizeof(__val), __hash); }
143 };
144
145 struct _Fnv_hash_impl
146 {
147 static size_t
148 hash(const void* __ptr, size_t __clength,
149 size_t __seed = static_cast<size_t>(2166136261UL))
150 { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
151
152 template<typename _Tp>
153 static size_t
154 hash(const _Tp& __val)
155 { return hash(&__val, sizeof(__val)); }
156
157 template<typename _Tp>
158 static size_t
159 __hash_combine(const _Tp& __val, size_t __hash)
160 { return hash(&__val, sizeof(__val), __hash); }
161 };
162
163 /// Specialization for float.
164 template<>
165 struct hash<float> : public __hash_base<size_t, float>
166 {
167 size_t
168 operator()(float __val) const noexcept
169 {
170 // 0 and -0 both hash to zero.
171 return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
172 }
173 };
174
175 /// Specialization for double.
176 template<>
177 struct hash<double> : public __hash_base<size_t, double>
178 {
179 size_t
180 operator()(double __val) const noexcept
181 {
182 // 0 and -0 both hash to zero.
183 return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
184 }
185 };
186
187 /// Specialization for long double.
188 template<>
189 struct hash<long double>
190 : public __hash_base<size_t, long double>
191 {
192 _GLIBCXX_PURE size_t
193 operator()(long double __val) const noexcept;
194 };
195
196 // @} group hashes
197
198 _GLIBCXX_END_NAMESPACE_VERSION
199 } // namespace
200
201 #endif // _FUNCTIONAL_HASH_H