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