1*e4b17023SJohn Marino // TR1 functional_hash.h header -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4*e4b17023SJohn Marino //
5*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free
6*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
7*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
8*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
9*e4b17023SJohn Marino // any later version.
10*e4b17023SJohn Marino
11*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
12*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*e4b17023SJohn Marino // GNU General Public License for more details.
15*e4b17023SJohn Marino
16*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
17*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
18*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
19*e4b17023SJohn Marino
20*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
21*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
22*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
24*e4b17023SJohn Marino
25*e4b17023SJohn Marino /** @file tr1/functional_hash.h
26*e4b17023SJohn Marino * This is an internal header file, included by other library headers.
27*e4b17023SJohn Marino * Do not attempt to use it directly. @headername{tr1/functional}
28*e4b17023SJohn Marino */
29*e4b17023SJohn Marino
30*e4b17023SJohn Marino #ifndef _GLIBCXX_TR1_FUNCTIONAL_HASH_H
31*e4b17023SJohn Marino #define _GLIBCXX_TR1_FUNCTIONAL_HASH_H 1
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino #pragma GCC system_header
34*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)35*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
36*e4b17023SJohn Marino {
37*e4b17023SJohn Marino namespace tr1
38*e4b17023SJohn Marino {
39*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino /// Class template hash.
42*e4b17023SJohn Marino // Declaration of default hash functor std::tr1::hash. The types for
43*e4b17023SJohn Marino // which std::tr1::hash<T> is well-defined is in clause 6.3.3. of the PDTR.
44*e4b17023SJohn Marino template<typename _Tp>
45*e4b17023SJohn Marino struct hash : public std::unary_function<_Tp, size_t>
46*e4b17023SJohn Marino {
47*e4b17023SJohn Marino size_t
48*e4b17023SJohn Marino operator()(_Tp __val) const;
49*e4b17023SJohn Marino };
50*e4b17023SJohn Marino
51*e4b17023SJohn Marino /// Partial specializations for pointer types.
52*e4b17023SJohn Marino template<typename _Tp>
53*e4b17023SJohn Marino struct hash<_Tp*> : public std::unary_function<_Tp*, size_t>
54*e4b17023SJohn Marino {
55*e4b17023SJohn Marino size_t
56*e4b17023SJohn Marino operator()(_Tp* __p) const
57*e4b17023SJohn Marino { return reinterpret_cast<size_t>(__p); }
58*e4b17023SJohn Marino };
59*e4b17023SJohn Marino
60*e4b17023SJohn Marino /// Explicit specializations for integer types.
61*e4b17023SJohn Marino #define _TR1_hashtable_define_trivial_hash(_Tp) \
62*e4b17023SJohn Marino template<> \
63*e4b17023SJohn Marino inline size_t \
64*e4b17023SJohn Marino hash<_Tp>::operator()(_Tp __val) const \
65*e4b17023SJohn Marino { return static_cast<size_t>(__val); }
66*e4b17023SJohn Marino
67*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(bool);
68*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(char);
69*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(signed char);
70*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(unsigned char);
71*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(wchar_t);
72*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(short);
73*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(int);
74*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(long);
75*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(long long);
76*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(unsigned short);
77*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(unsigned int);
78*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(unsigned long);
79*e4b17023SJohn Marino _TR1_hashtable_define_trivial_hash(unsigned long long);
80*e4b17023SJohn Marino
81*e4b17023SJohn Marino #undef _TR1_hashtable_define_trivial_hash
82*e4b17023SJohn Marino
83*e4b17023SJohn Marino // Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
84*e4b17023SJohn Marino // (Used by the next specializations of std::tr1::hash.)
85*e4b17023SJohn Marino
86*e4b17023SJohn Marino /// Dummy generic implementation (for sizeof(size_t) != 4, 8).
87*e4b17023SJohn Marino template<size_t>
88*e4b17023SJohn Marino struct _Fnv_hash_base
89*e4b17023SJohn Marino {
90*e4b17023SJohn Marino template<typename _Tp>
91*e4b17023SJohn Marino static size_t
92*e4b17023SJohn Marino hash(const _Tp* __ptr, size_t __clength)
93*e4b17023SJohn Marino {
94*e4b17023SJohn Marino size_t __result = 0;
95*e4b17023SJohn Marino const char* __cptr = reinterpret_cast<const char*>(__ptr);
96*e4b17023SJohn Marino for (; __clength; --__clength)
97*e4b17023SJohn Marino __result = (__result * 131) + *__cptr++;
98*e4b17023SJohn Marino return __result;
99*e4b17023SJohn Marino }
100*e4b17023SJohn Marino };
101*e4b17023SJohn Marino
102*e4b17023SJohn Marino template<>
103*e4b17023SJohn Marino struct _Fnv_hash_base<4>
104*e4b17023SJohn Marino {
105*e4b17023SJohn Marino template<typename _Tp>
106*e4b17023SJohn Marino static size_t
107*e4b17023SJohn Marino hash(const _Tp* __ptr, size_t __clength)
108*e4b17023SJohn Marino {
109*e4b17023SJohn Marino size_t __result = static_cast<size_t>(2166136261UL);
110*e4b17023SJohn Marino const char* __cptr = reinterpret_cast<const char*>(__ptr);
111*e4b17023SJohn Marino for (; __clength; --__clength)
112*e4b17023SJohn Marino {
113*e4b17023SJohn Marino __result ^= static_cast<size_t>(*__cptr++);
114*e4b17023SJohn Marino __result *= static_cast<size_t>(16777619UL);
115*e4b17023SJohn Marino }
116*e4b17023SJohn Marino return __result;
117*e4b17023SJohn Marino }
118*e4b17023SJohn Marino };
119*e4b17023SJohn Marino
120*e4b17023SJohn Marino template<>
121*e4b17023SJohn Marino struct _Fnv_hash_base<8>
122*e4b17023SJohn Marino {
123*e4b17023SJohn Marino template<typename _Tp>
124*e4b17023SJohn Marino static size_t
125*e4b17023SJohn Marino hash(const _Tp* __ptr, size_t __clength)
126*e4b17023SJohn Marino {
127*e4b17023SJohn Marino size_t __result
128*e4b17023SJohn Marino = static_cast<size_t>(14695981039346656037ULL);
129*e4b17023SJohn Marino const char* __cptr = reinterpret_cast<const char*>(__ptr);
130*e4b17023SJohn Marino for (; __clength; --__clength)
131*e4b17023SJohn Marino {
132*e4b17023SJohn Marino __result ^= static_cast<size_t>(*__cptr++);
133*e4b17023SJohn Marino __result *= static_cast<size_t>(1099511628211ULL);
134*e4b17023SJohn Marino }
135*e4b17023SJohn Marino return __result;
136*e4b17023SJohn Marino }
137*e4b17023SJohn Marino };
138*e4b17023SJohn Marino
139*e4b17023SJohn Marino struct _Fnv_hash
140*e4b17023SJohn Marino : public _Fnv_hash_base<sizeof(size_t)>
141*e4b17023SJohn Marino {
142*e4b17023SJohn Marino using _Fnv_hash_base<sizeof(size_t)>::hash;
143*e4b17023SJohn Marino
144*e4b17023SJohn Marino template<typename _Tp>
145*e4b17023SJohn Marino static size_t
146*e4b17023SJohn Marino hash(const _Tp& __val)
147*e4b17023SJohn Marino { return hash(&__val, sizeof(__val)); }
148*e4b17023SJohn Marino };
149*e4b17023SJohn Marino
150*e4b17023SJohn Marino /// Explicit specializations for float.
151*e4b17023SJohn Marino template<>
152*e4b17023SJohn Marino inline size_t
153*e4b17023SJohn Marino hash<float>::operator()(float __val) const
154*e4b17023SJohn Marino {
155*e4b17023SJohn Marino // 0 and -0 both hash to zero.
156*e4b17023SJohn Marino return __val != 0.0f ? std::tr1::_Fnv_hash::hash(__val) : 0;
157*e4b17023SJohn Marino }
158*e4b17023SJohn Marino
159*e4b17023SJohn Marino /// Explicit specializations for double.
160*e4b17023SJohn Marino template<>
161*e4b17023SJohn Marino inline size_t
162*e4b17023SJohn Marino hash<double>::operator()(double __val) const
163*e4b17023SJohn Marino {
164*e4b17023SJohn Marino // 0 and -0 both hash to zero.
165*e4b17023SJohn Marino return __val != 0.0 ? std::tr1::_Fnv_hash::hash(__val) : 0;
166*e4b17023SJohn Marino }
167*e4b17023SJohn Marino
168*e4b17023SJohn Marino /// Explicit specializations for long double.
169*e4b17023SJohn Marino template<>
170*e4b17023SJohn Marino _GLIBCXX_PURE size_t
171*e4b17023SJohn Marino hash<long double>::operator()(long double __val) const;
172*e4b17023SJohn Marino
173*e4b17023SJohn Marino /// Explicit specialization of member operator for non-builtin types.
174*e4b17023SJohn Marino template<>
175*e4b17023SJohn Marino _GLIBCXX_PURE size_t
176*e4b17023SJohn Marino hash<string>::operator()(string) const;
177*e4b17023SJohn Marino
178*e4b17023SJohn Marino template<>
179*e4b17023SJohn Marino _GLIBCXX_PURE size_t
180*e4b17023SJohn Marino hash<const string&>::operator()(const string&) const;
181*e4b17023SJohn Marino
182*e4b17023SJohn Marino #ifdef _GLIBCXX_USE_WCHAR_T
183*e4b17023SJohn Marino template<>
184*e4b17023SJohn Marino _GLIBCXX_PURE size_t
185*e4b17023SJohn Marino hash<wstring>::operator()(wstring) const;
186*e4b17023SJohn Marino
187*e4b17023SJohn Marino template<>
188*e4b17023SJohn Marino _GLIBCXX_PURE size_t
189*e4b17023SJohn Marino hash<const wstring&>::operator()(const wstring&) const;
190*e4b17023SJohn Marino #endif
191*e4b17023SJohn Marino
192*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
193*e4b17023SJohn Marino }
194*e4b17023SJohn Marino }
195*e4b17023SJohn Marino
196*e4b17023SJohn Marino #endif // _GLIBCXX_TR1_FUNCTIONAL_HASH_H
197