1 // functional_hash.h header -*- C++ -*-
3 // Copyright (C) 2007-2015 Free Software Foundation, Inc.
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)
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
44 * Hashing functors taking a variable type and returning a @c std::size_t.
49 template<typename _Result
, typename _Arg
>
52 typedef _Result result_type
;
53 typedef _Arg argument_type
;
56 /// Primary class template hash.
57 template<typename _Tp
>
60 /// Partial specializations for pointer types.
61 template<typename _Tp
>
62 struct hash
<_Tp
*> : public __hash_base
<size_t, _Tp
*>
65 operator()(_Tp
* __p
) const noexcept
66 { return reinterpret_cast<size_t>(__p
); }
69 // Explicit specializations for integer types.
70 #define _Cxx_hashtable_define_trivial_hash(_Tp) \
72 struct hash<_Tp> : public __hash_base<size_t, _Tp> \
75 operator()(_Tp __val) const noexcept \
76 { return static_cast<size_t>(__val); } \
79 /// Explicit specialization for bool.
80 _Cxx_hashtable_define_trivial_hash(bool)
82 /// Explicit specialization for char.
83 _Cxx_hashtable_define_trivial_hash(char)
85 /// Explicit specialization for signed char.
86 _Cxx_hashtable_define_trivial_hash(signed char)
88 /// Explicit specialization for unsigned char.
89 _Cxx_hashtable_define_trivial_hash(unsigned char)
91 /// Explicit specialization for wchar_t.
92 _Cxx_hashtable_define_trivial_hash(wchar_t)
94 /// Explicit specialization for char16_t.
95 _Cxx_hashtable_define_trivial_hash(char16_t
)
97 /// Explicit specialization for char32_t.
98 _Cxx_hashtable_define_trivial_hash(char32_t
)
100 /// Explicit specialization for short.
101 _Cxx_hashtable_define_trivial_hash(short)
103 /// Explicit specialization for int.
104 _Cxx_hashtable_define_trivial_hash(int)
106 /// Explicit specialization for long.
107 _Cxx_hashtable_define_trivial_hash(long)
109 /// Explicit specialization for long long.
110 _Cxx_hashtable_define_trivial_hash(long long)
112 /// Explicit specialization for unsigned short.
113 _Cxx_hashtable_define_trivial_hash(unsigned short)
115 /// Explicit specialization for unsigned int.
116 _Cxx_hashtable_define_trivial_hash(unsigned int)
118 /// Explicit specialization for unsigned long.
119 _Cxx_hashtable_define_trivial_hash(unsigned long)
121 /// Explicit specialization for unsigned long long.
122 _Cxx_hashtable_define_trivial_hash(unsigned long long)
124 #ifdef __GLIBCXX_TYPE_INT_N_0
125 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0
)
126 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0
unsigned)
128 #ifdef __GLIBCXX_TYPE_INT_N_1
129 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1
)
130 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1
unsigned)
132 #ifdef __GLIBCXX_TYPE_INT_N_2
133 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2
)
134 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2
unsigned)
136 #ifdef __GLIBCXX_TYPE_INT_N_3
137 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3
)
138 _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3
unsigned)
141 #undef _Cxx_hashtable_define_trivial_hash
146 hash(const void* __ptr
, size_t __clength
,
147 size_t __seed
= static_cast<size_t>(0xc70f6907UL
))
148 { return _Hash_bytes(__ptr
, __clength
, __seed
); }
150 template<typename _Tp
>
152 hash(const _Tp
& __val
)
153 { return hash(&__val
, sizeof(__val
)); }
155 template<typename _Tp
>
157 __hash_combine(const _Tp
& __val
, size_t __hash
)
158 { return hash(&__val
, sizeof(__val
), __hash
); }
161 struct _Fnv_hash_impl
164 hash(const void* __ptr
, size_t __clength
,
165 size_t __seed
= static_cast<size_t>(2166136261UL))
166 { return _Fnv_hash_bytes(__ptr
, __clength
, __seed
); }
168 template<typename _Tp
>
170 hash(const _Tp
& __val
)
171 { return hash(&__val
, sizeof(__val
)); }
173 template<typename _Tp
>
175 __hash_combine(const _Tp
& __val
, size_t __hash
)
176 { return hash(&__val
, sizeof(__val
), __hash
); }
179 /// Specialization for float.
181 struct hash
<float> : public __hash_base
<size_t, float>
184 operator()(float __val
) const noexcept
186 // 0 and -0 both hash to zero.
187 return __val
!= 0.0f
? std::_Hash_impl::hash(__val
) : 0;
191 /// Specialization for double.
193 struct hash
<double> : public __hash_base
<size_t, double>
196 operator()(double __val
) const noexcept
198 // 0 and -0 both hash to zero.
199 return __val
!= 0.0 ? std::_Hash_impl::hash(__val
) : 0;
203 /// Specialization for long double.
205 struct hash
<long double>
206 : public __hash_base
<size_t, long double>
209 operator()(long double __val
) const noexcept
;
214 // Hint about performance of hash functor. If not fast the hash based
215 // containers will cache the hash code.
216 // Default behavior is to consider that hasher are fast unless specified
218 template<typename _Hash
>
219 struct __is_fast_hash
: public std::true_type
223 struct __is_fast_hash
<hash
<long double>> : public std::false_type
226 _GLIBCXX_END_NAMESPACE_VERSION
229 #endif // _FUNCTIONAL_HASH_H