libstdc++
functional_hash.h
Go to the documentation of this file.
1 // functional_hash.h header -*- C++ -*-
2 
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.
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 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}
28  */
29 
30 #ifndef _FUNCTIONAL_HASH_H
31 #define _FUNCTIONAL_HASH_H 1
32 
33 #pragma GCC system_header
34 
35 #include <bits/hash_bytes.h>
36 
37 namespace std _GLIBCXX_VISIBILITY(default)
38 {
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 
41  /** @defgroup hashes Hashes
42  * @ingroup functors
43  *
44  * Hashing functors taking a variable type and returning a @c std::size_t.
45  *
46  * @{
47  */
48 
49  template<typename _Result, typename _Arg>
50  struct __hash_base
51  {
52  typedef _Result result_type;
53  typedef _Arg argument_type;
54  };
55 
56  /// Primary class template hash.
57  template<typename _Tp>
58  struct hash;
59 
60  template<typename _Tp, typename = void>
61  struct __poison_hash
62  {
63  private:
64  // Private rather than deleted to be non-trivially-copyable.
65  __poison_hash(__poison_hash&&);
66  ~__poison_hash();
67  };
68 
69  template<typename _Tp>
70  struct __poison_hash<_Tp, __void_t<decltype(hash<_Tp>()(declval<_Tp>()))>>
71  {
72  };
73 
74  // Helper struct for SFINAE-poisoning non-enum types.
75  template<typename _Tp, bool = is_enum<_Tp>::value>
76  struct __hash_enum
77  {
78  private:
79  // Private rather than deleted to be non-trivially-copyable.
80  __hash_enum(__hash_enum&&);
81  ~__hash_enum();
82  };
83 
84  // Helper struct for hash with enum types.
85  template<typename _Tp>
86  struct __hash_enum<_Tp, true> : public __hash_base<size_t, _Tp>
87  {
88  size_t
89  operator()(_Tp __val) const noexcept
90  {
91  using __type = typename underlying_type<_Tp>::type;
92  return hash<__type>{}(static_cast<__type>(__val));
93  }
94  };
95 
96  /// Primary class template hash, usable for enum types only.
97  // Use with non-enum types still SFINAES.
98  template<typename _Tp>
99  struct hash : __hash_enum<_Tp>
100  { };
101 
102  /// Partial specializations for pointer types.
103  template<typename _Tp>
104  struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
105  {
106  size_t
107  operator()(_Tp* __p) const noexcept
108  { return reinterpret_cast<size_t>(__p); }
109  };
110 
111  // Explicit specializations for integer types.
112 #define _Cxx_hashtable_define_trivial_hash(_Tp) \
113  template<> \
114  struct hash<_Tp> : public __hash_base<size_t, _Tp> \
115  { \
116  size_t \
117  operator()(_Tp __val) const noexcept \
118  { return static_cast<size_t>(__val); } \
119  };
120 
121  /// Explicit specialization for bool.
122  _Cxx_hashtable_define_trivial_hash(bool)
123 
124  /// Explicit specialization for char.
125  _Cxx_hashtable_define_trivial_hash(char)
126 
127  /// Explicit specialization for signed char.
128  _Cxx_hashtable_define_trivial_hash(signed char)
129 
130  /// Explicit specialization for unsigned char.
131  _Cxx_hashtable_define_trivial_hash(unsigned char)
132 
133  /// Explicit specialization for wchar_t.
134  _Cxx_hashtable_define_trivial_hash(wchar_t)
135 
136  /// Explicit specialization for char16_t.
137  _Cxx_hashtable_define_trivial_hash(char16_t)
138 
139  /// Explicit specialization for char32_t.
140  _Cxx_hashtable_define_trivial_hash(char32_t)
141 
142  /// Explicit specialization for short.
143  _Cxx_hashtable_define_trivial_hash(short)
144 
145  /// Explicit specialization for int.
146  _Cxx_hashtable_define_trivial_hash(int)
147 
148  /// Explicit specialization for long.
149  _Cxx_hashtable_define_trivial_hash(long)
150 
151  /// Explicit specialization for long long.
152  _Cxx_hashtable_define_trivial_hash(long long)
153 
154  /// Explicit specialization for unsigned short.
155  _Cxx_hashtable_define_trivial_hash(unsigned short)
156 
157  /// Explicit specialization for unsigned int.
158  _Cxx_hashtable_define_trivial_hash(unsigned int)
159 
160  /// Explicit specialization for unsigned long.
161  _Cxx_hashtable_define_trivial_hash(unsigned long)
162 
163  /// Explicit specialization for unsigned long long.
164  _Cxx_hashtable_define_trivial_hash(unsigned long long)
165 
166 #ifdef __GLIBCXX_TYPE_INT_N_0
167  _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0)
168  _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0 unsigned)
169 #endif
170 #ifdef __GLIBCXX_TYPE_INT_N_1
171  _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1)
172  _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1 unsigned)
173 #endif
174 #ifdef __GLIBCXX_TYPE_INT_N_2
175  _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2)
176  _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2 unsigned)
177 #endif
178 #ifdef __GLIBCXX_TYPE_INT_N_3
179  _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3)
180  _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3 unsigned)
181 #endif
182 
183 #undef _Cxx_hashtable_define_trivial_hash
184 
185  struct _Hash_impl
186  {
187  static size_t
188  hash(const void* __ptr, size_t __clength,
189  size_t __seed = static_cast<size_t>(0xc70f6907UL))
190  { return _Hash_bytes(__ptr, __clength, __seed); }
191 
192  template<typename _Tp>
193  static size_t
194  hash(const _Tp& __val)
195  { return hash(&__val, sizeof(__val)); }
196 
197  template<typename _Tp>
198  static size_t
199  __hash_combine(const _Tp& __val, size_t __hash)
200  { return hash(&__val, sizeof(__val), __hash); }
201  };
202 
203  // A hash function similar to FNV-1a (see PR59406 for how it differs).
204  struct _Fnv_hash_impl
205  {
206  static size_t
207  hash(const void* __ptr, size_t __clength,
208  size_t __seed = static_cast<size_t>(2166136261UL))
209  { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
210 
211  template<typename _Tp>
212  static size_t
213  hash(const _Tp& __val)
214  { return hash(&__val, sizeof(__val)); }
215 
216  template<typename _Tp>
217  static size_t
218  __hash_combine(const _Tp& __val, size_t __hash)
219  { return hash(&__val, sizeof(__val), __hash); }
220  };
221 
222  /// Specialization for float.
223  template<>
224  struct hash<float> : public __hash_base<size_t, float>
225  {
226  size_t
227  operator()(float __val) const noexcept
228  {
229  // 0 and -0 both hash to zero.
230  return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
231  }
232  };
233 
234  /// Specialization for double.
235  template<>
236  struct hash<double> : public __hash_base<size_t, double>
237  {
238  size_t
239  operator()(double __val) const noexcept
240  {
241  // 0 and -0 both hash to zero.
242  return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
243  }
244  };
245 
246  /// Specialization for long double.
247  template<>
248  struct hash<long double>
249  : public __hash_base<size_t, long double>
250  {
251  _GLIBCXX_PURE size_t
252  operator()(long double __val) const noexcept;
253  };
254 
255  // @} group hashes
256 
257  // Hint about performance of hash functor. If not fast the hash-based
258  // containers will cache the hash code.
259  // Default behavior is to consider that hashers are fast unless specified
260  // otherwise.
261  template<typename _Hash>
262  struct __is_fast_hash : public std::true_type
263  { };
264 
265  template<>
266  struct __is_fast_hash<hash<long double>> : public std::false_type
267  { };
268 
269 _GLIBCXX_END_NAMESPACE_VERSION
270 } // namespace
271 
272 #endif // _FUNCTIONAL_HASH_H
Primary class template hash.
Definition: system_error:141
ISO C++ entities toplevel namespace is std.
integral_constant
Definition: type_traits:69