]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/functional_hash.h
1 // functional_hash.h header -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010 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/c++config.h>
39 /** @defgroup hashes Hashes
42 * Hashing functors taking a variable type and returning a @c std::size_t.
47 template<typename _Result
, typename _Arg
>
50 typedef _Result result_type
;
51 typedef _Arg argument_type
;
54 /// Primary class template hash.
55 template<typename _Tp
>
56 struct hash
: public __hash_base
<size_t, _Tp
>
59 operator()(_Tp __val
) const;
62 /// Partial specializations for pointer types.
63 template<typename _Tp
>
64 struct hash
<_Tp
*> : public __hash_base
<size_t, _Tp
*>
67 operator()(_Tp
* __p
) const
68 { return reinterpret_cast<size_t>(__p
); }
71 // Explicit specializations for integer types.
72 #define _Cxx_hashtable_define_trivial_hash(_Tp) \
75 hash<_Tp>::operator()(_Tp __val) const \
76 { return static_cast<size_t>(__val); }
78 /// Explicit specialization for bool.
79 _Cxx_hashtable_define_trivial_hash(bool);
81 /// Explicit specialization for char.
82 _Cxx_hashtable_define_trivial_hash(char);
84 /// Explicit specialization for signed char.
85 _Cxx_hashtable_define_trivial_hash(signed char);
87 /// Explicit specialization for unsigned char.
88 _Cxx_hashtable_define_trivial_hash(unsigned char);
90 /// Explicit specialization for wchar_t.
91 _Cxx_hashtable_define_trivial_hash(wchar_t);
93 /// Explicit specialization for char16_t.
94 _Cxx_hashtable_define_trivial_hash(char16_t
);
96 /// Explicit specialization for char32_t.
97 _Cxx_hashtable_define_trivial_hash(char32_t
);
99 /// Explicit specialization for short.
100 _Cxx_hashtable_define_trivial_hash(short);
102 /// Explicit specialization for int.
103 _Cxx_hashtable_define_trivial_hash(int);
105 /// Explicit specialization for long.
106 _Cxx_hashtable_define_trivial_hash(long);
108 /// Explicit specialization for long long.
109 _Cxx_hashtable_define_trivial_hash(long long);
111 /// Explicit specialization for unsigned short.
112 _Cxx_hashtable_define_trivial_hash(unsigned short);
114 /// Explicit specialization for unsigned int.
115 _Cxx_hashtable_define_trivial_hash(unsigned int);
117 /// Explicit specialization for unsigned long.
118 _Cxx_hashtable_define_trivial_hash(unsigned long);
120 /// Explicit specialization for unsigned long long.
121 _Cxx_hashtable_define_trivial_hash(unsigned long long);
123 #undef _Cxx_hashtable_define_trivial_hash
125 // Hash function implementation for the nontrivial specialization.
126 // All of them are based on a primitive that hashes a pointer to
127 // a byte array. The actual hash algorithm is not guaranteed to
128 // stay the same from release to release -- it may be updated or
129 // tuned to improve hash quality or speed.
131 _Hash_bytes(const void* __ptr
, size_t __len
, size_t __seed
);
133 // A similar hash primitive, using the FNV hash algorithm. This
134 // algorithm is guaranteed to stay the same from release to release.
135 // (although it might not produce the same values on different machines.)
137 _Fnv_hash_bytes(const void* __ptr
, size_t __len
, size_t __seed
);
142 hash(const void* __ptr
, size_t __clength
,
143 size_t __seed
= static_cast<size_t>(0xc70f6907UL
))
144 { return _Hash_bytes(__ptr
, __clength
, __seed
); }
146 template<typename _Tp
>
148 hash(const _Tp
& __val
)
149 { return hash(&__val
, sizeof(__val
)); }
151 template<typename _Tp
>
153 __hash_combine(const _Tp
& __val
, size_t __hash
)
154 { return hash(&__val
, sizeof(__val
), __hash
); }
157 struct _Fnv_hash_impl
160 hash(const void* __ptr
, size_t __clength
,
161 size_t __seed
= static_cast<size_t>(2166136261UL))
162 { return _Fnv_hash_bytes(__ptr
, __clength
, __seed
); }
164 template<typename _Tp
>
166 hash(const _Tp
& __val
)
167 { return hash(&__val
, sizeof(__val
)); }
169 template<typename _Tp
>
171 __hash_combine(const _Tp
& __val
, size_t __hash
)
172 { return hash(&__val
, sizeof(__val
), __hash
); }
175 /// Specialization for float.
178 hash
<float>::operator()(float __val
) const
180 // 0 and -0 both hash to zero.
181 return __val
!= 0.0f
? std::_Hash_impl::hash(__val
) : 0;
184 /// Specialization for double.
187 hash
<double>::operator()(double __val
) const
189 // 0 and -0 both hash to zero.
190 return __val
!= 0.0 ? std::_Hash_impl::hash(__val
) : 0;
193 /// Specialization for long double.
196 hash
<long double>::operator()(long double __val
) const;
201 #endif // _FUNCTIONAL_HASH_H