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 * You should not attempt to use it directly.
30 #ifndef _FUNCTIONAL_HASH_H
31 #define _FUNCTIONAL_HASH_H 1
33 #pragma GCC system_header
36 #include <bits/stl_function.h>
40 /** @defgroup hashes Hashes
43 * Hashing functors taking a variable type and returning a @c std::size_t.
48 /// Primary class template hash.
49 template<typename _Tp
>
50 struct hash
: public std::unary_function
<_Tp
, size_t>
53 operator()(_Tp __val
) const;
56 /// Partial specializations for pointer types.
57 template<typename _Tp
>
58 struct hash
<_Tp
*> : public std::unary_function
<_Tp
*, size_t>
61 operator()(_Tp
* __p
) const
62 { return reinterpret_cast<size_t>(__p
); }
65 // Explicit specializations for integer types.
66 #define _Cxx_hashtable_define_trivial_hash(_Tp) \
69 hash<_Tp>::operator()(_Tp __val) const \
70 { return static_cast<size_t>(__val); }
72 /// Explicit specialization for bool.
73 _Cxx_hashtable_define_trivial_hash(bool);
75 /// Explicit specialization for char.
76 _Cxx_hashtable_define_trivial_hash(char);
78 /// Explicit specialization for signed char.
79 _Cxx_hashtable_define_trivial_hash(signed char);
81 /// Explicit specialization for unsigned char.
82 _Cxx_hashtable_define_trivial_hash(unsigned char);
84 /// Explicit specialization for wchar_t.
85 _Cxx_hashtable_define_trivial_hash(wchar_t);
87 /// Explicit specialization for char16_t.
88 _Cxx_hashtable_define_trivial_hash(char16_t
);
90 /// Explicit specialization for char32_t.
91 _Cxx_hashtable_define_trivial_hash(char32_t
);
93 /// Explicit specialization for short.
94 _Cxx_hashtable_define_trivial_hash(short);
96 /// Explicit specialization for int.
97 _Cxx_hashtable_define_trivial_hash(int);
99 /// Explicit specialization for long.
100 _Cxx_hashtable_define_trivial_hash(long);
102 /// Explicit specialization for long long.
103 _Cxx_hashtable_define_trivial_hash(long long);
105 /// Explicit specialization for unsigned short.
106 _Cxx_hashtable_define_trivial_hash(unsigned short);
108 /// Explicit specialization for unsigned int.
109 _Cxx_hashtable_define_trivial_hash(unsigned int);
111 /// Explicit specialization for unsigned long.
112 _Cxx_hashtable_define_trivial_hash(unsigned long);
114 /// Explicit specialization for unsigned long long.
115 _Cxx_hashtable_define_trivial_hash(unsigned long long);
117 #undef _Cxx_hashtable_define_trivial_hash
119 // Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
120 // (Used by the next specializations of std::hash.)
122 template<size_t = sizeof(size_t)>
125 // Dummy generic implementation (for sizeof(size_t) != 4, 8).
130 hash(const char* __first
, size_t __length
)
133 for (; __length
> 0; --__length
)
134 __result
= (__result
* 131) + *__first
++;
143 hash(const char* __first
, size_t __length
)
145 size_t __result
= static_cast<size_t>(2166136261UL);
146 for (; __length
> 0; --__length
)
148 __result
^= static_cast<size_t>(*__first
++);
149 __result
*= static_cast<size_t>(16777619UL);
159 hash(const char* __first
, size_t __length
)
162 static_cast<size_t>(14695981039346656037ULL);
163 for (; __length
> 0; --__length
)
165 __result
^= static_cast<size_t>(*__first
++);
166 __result
*= static_cast<size_t>(1099511628211ULL);
172 /// Specialization for float.
175 hash
<float>::operator()(float __val
) const
179 // 0 and -0 both hash to zero.
181 __result
= _Fnv_hash
<>::hash(reinterpret_cast<const char*>(&__val
),
186 /// Specialization for double.
189 hash
<double>::operator()(double __val
) const
193 // 0 and -0 both hash to zero.
195 __result
= _Fnv_hash
<>::hash(reinterpret_cast<const char*>(&__val
),
200 /// Specialization for long double.
203 hash
<long double>::operator()(long double __val
) const;
208 #endif // _FUNCTIONAL_HASH_H