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)
121 // Dummy generic implementation (for sizeof(size_t) != 4, 8).
123 struct _Fnv_hash_base
125 template<typename _Tp
>
127 hash(const _Tp
* __ptr
, size_t __clength
, size_t __hash
= 0)
129 const char* __cptr
= reinterpret_cast<const char*>(__ptr
);
130 for (; __clength
; --__clength
)
131 __hash
= (__hash
* 131) + *__cptr
++;
137 struct _Fnv_hash_base
<4>
139 template<typename _Tp
>
141 hash(const _Tp
* __ptr
, size_t __clength
,
142 size_t __hash
= static_cast<size_t>(2166136261UL))
144 const char* __cptr
= reinterpret_cast<const char*>(__ptr
);
145 for (; __clength
; --__clength
)
147 __hash
^= static_cast<size_t>(*__cptr
++);
148 __hash
*= static_cast<size_t>(16777619UL);
155 struct _Fnv_hash_base
<8>
157 template<typename _Tp
>
159 hash(const _Tp
* __ptr
, size_t __clength
,
160 size_t __hash
= static_cast<size_t>(14695981039346656037ULL))
162 const char* __cptr
= reinterpret_cast<const char*>(__ptr
);
163 for (; __clength
; --__clength
)
165 __hash
^= static_cast<size_t>(*__cptr
++);
166 __hash
*= static_cast<size_t>(1099511628211ULL);
173 : public _Fnv_hash_base
<sizeof(size_t)>
175 using _Fnv_hash_base
<sizeof(size_t)>::hash
;
177 template<typename _Tp
>
179 hash(const _Tp
& __val
)
180 { return hash(&__val
, sizeof(__val
)); }
182 template<typename _Tp
>
184 __hash_combine(const _Tp
& __val
, size_t __hash
)
185 { return hash(&__val
, sizeof(__val
), __hash
); }
188 /// Specialization for float.
191 hash
<float>::operator()(float __val
) const
193 // 0 and -0 both hash to zero.
194 return __val
!= 0.0f
? std::_Fnv_hash::hash(__val
) : 0;
197 /// Specialization for double.
200 hash
<double>::operator()(double __val
) const
202 // 0 and -0 both hash to zero.
203 return __val
!= 0.0 ? std::_Fnv_hash::hash(__val
) : 0;
206 /// Specialization for long double.
209 hash
<long double>::operator()(long double __val
) const;
214 #endif // _FUNCTIONAL_HASH_H