libstdc++
functional_hash.h
Go to the documentation of this file.
1 // functional_hash.h header -*- C++ -*-
2 
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 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 : public __hash_base<size_t, _Tp>
59  {
60  static_assert(sizeof(_Tp) < 0,
61  "std::hash is not specialized for this type");
62  size_t operator()(const _Tp&) const noexcept;
63  };
64 
65  /// Partial specializations for pointer types.
66  template<typename _Tp>
67  struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
68  {
69  size_t
70  operator()(_Tp* __p) const noexcept
71  { return reinterpret_cast<size_t>(__p); }
72  };
73 
74  // Explicit specializations for integer types.
75 #define _Cxx_hashtable_define_trivial_hash(_Tp) \
76  template<> \
77  struct hash<_Tp> : public __hash_base<size_t, _Tp> \
78  { \
79  size_t \
80  operator()(_Tp __val) const noexcept \
81  { return static_cast<size_t>(__val); } \
82  };
83 
84  /// Explicit specialization for bool.
85  _Cxx_hashtable_define_trivial_hash(bool)
86 
87  /// Explicit specialization for char.
88  _Cxx_hashtable_define_trivial_hash(char)
89 
90  /// Explicit specialization for signed char.
91  _Cxx_hashtable_define_trivial_hash(signed char)
92 
93  /// Explicit specialization for unsigned char.
94  _Cxx_hashtable_define_trivial_hash(unsigned char)
95 
96  /// Explicit specialization for wchar_t.
97  _Cxx_hashtable_define_trivial_hash(wchar_t)
98 
99  /// Explicit specialization for char16_t.
100  _Cxx_hashtable_define_trivial_hash(char16_t)
101 
102  /// Explicit specialization for char32_t.
103  _Cxx_hashtable_define_trivial_hash(char32_t)
104 
105  /// Explicit specialization for short.
106  _Cxx_hashtable_define_trivial_hash(short)
107 
108  /// Explicit specialization for int.
109  _Cxx_hashtable_define_trivial_hash(int)
110 
111  /// Explicit specialization for long.
112  _Cxx_hashtable_define_trivial_hash(long)
113 
114  /// Explicit specialization for long long.
115  _Cxx_hashtable_define_trivial_hash(long long)
116 
117  /// Explicit specialization for unsigned short.
118  _Cxx_hashtable_define_trivial_hash(unsigned short)
119 
120  /// Explicit specialization for unsigned int.
121  _Cxx_hashtable_define_trivial_hash(unsigned int)
122 
123  /// Explicit specialization for unsigned long.
124  _Cxx_hashtable_define_trivial_hash(unsigned long)
125 
126  /// Explicit specialization for unsigned long long.
127  _Cxx_hashtable_define_trivial_hash(unsigned long long)
128 
129 #undef _Cxx_hashtable_define_trivial_hash
130 
131  struct _Hash_impl
132  {
133  static size_t
134  hash(const void* __ptr, size_t __clength,
135  size_t __seed = static_cast<size_t>(0xc70f6907UL))
136  { return _Hash_bytes(__ptr, __clength, __seed); }
137 
138  template<typename _Tp>
139  static size_t
140  hash(const _Tp& __val)
141  { return hash(&__val, sizeof(__val)); }
142 
143  template<typename _Tp>
144  static size_t
145  __hash_combine(const _Tp& __val, size_t __hash)
146  { return hash(&__val, sizeof(__val), __hash); }
147  };
148 
149  struct _Fnv_hash_impl
150  {
151  static size_t
152  hash(const void* __ptr, size_t __clength,
153  size_t __seed = static_cast<size_t>(2166136261UL))
154  { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
155 
156  template<typename _Tp>
157  static size_t
158  hash(const _Tp& __val)
159  { return hash(&__val, sizeof(__val)); }
160 
161  template<typename _Tp>
162  static size_t
163  __hash_combine(const _Tp& __val, size_t __hash)
164  { return hash(&__val, sizeof(__val), __hash); }
165  };
166 
167  /// Specialization for float.
168  template<>
169  struct hash<float> : public __hash_base<size_t, float>
170  {
171  size_t
172  operator()(float __val) const noexcept
173  {
174  // 0 and -0 both hash to zero.
175  return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
176  }
177  };
178 
179  /// Specialization for double.
180  template<>
181  struct hash<double> : public __hash_base<size_t, double>
182  {
183  size_t
184  operator()(double __val) const noexcept
185  {
186  // 0 and -0 both hash to zero.
187  return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
188  }
189  };
190 
191  /// Specialization for long double.
192  template<>
193  struct hash<long double>
194  : public __hash_base<size_t, long double>
195  {
196  _GLIBCXX_PURE size_t
197  operator()(long double __val) const noexcept;
198  };
199 
200  // @} group hashes
201 
202 _GLIBCXX_END_NAMESPACE_VERSION
203 } // namespace
204 
205 #endif // _FUNCTIONAL_HASH_H
Primary class template hash.
Definition: system_error:112