Merged revision 156805 into branch.
[official-gcc.git] / libstdc++-v3 / include / tr1 / functional_hash.h
blobd6849300f4c23557759f0bcdbb88a3c33aaf7ae8
1 // TR1 functional_hash.h header -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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 tr1/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 _GLIBCXX_TR1_FUNCTIONAL_HASH_H
31 #define _GLIBCXX_TR1_FUNCTIONAL_HASH_H 1
33 #pragma GCC system_header
35 namespace std
37 namespace tr1
39 /// Class template hash.
40 // Declaration of default hash functor std::tr1::hash. The types for
41 // which std::tr1::hash<T> is well-defined is in clause 6.3.3. of the PDTR.
42 template<typename _Tp>
43 struct hash : public std::unary_function<_Tp, size_t>
45 size_t
46 operator()(_Tp __val) const;
49 /// Partial specializations for pointer types.
50 template<typename _Tp>
51 struct hash<_Tp*> : public std::unary_function<_Tp*, size_t>
53 size_t
54 operator()(_Tp* __p) const
55 { return reinterpret_cast<size_t>(__p); }
58 /// Explicit specializations for integer types.
59 #define _TR1_hashtable_define_trivial_hash(_Tp) \
60 template<> \
61 inline size_t \
62 hash<_Tp>::operator()(_Tp __val) const \
63 { return static_cast<size_t>(__val); }
65 _TR1_hashtable_define_trivial_hash(bool);
66 _TR1_hashtable_define_trivial_hash(char);
67 _TR1_hashtable_define_trivial_hash(signed char);
68 _TR1_hashtable_define_trivial_hash(unsigned char);
69 _TR1_hashtable_define_trivial_hash(wchar_t);
70 _TR1_hashtable_define_trivial_hash(short);
71 _TR1_hashtable_define_trivial_hash(int);
72 _TR1_hashtable_define_trivial_hash(long);
73 _TR1_hashtable_define_trivial_hash(long long);
74 _TR1_hashtable_define_trivial_hash(unsigned short);
75 _TR1_hashtable_define_trivial_hash(unsigned int);
76 _TR1_hashtable_define_trivial_hash(unsigned long);
77 _TR1_hashtable_define_trivial_hash(unsigned long long);
79 #undef _TR1_hashtable_define_trivial_hash
81 // Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
82 // (Used by the next specializations of std::tr1::hash.)
84 /// Dummy generic implementation (for sizeof(size_t) != 4, 8).
85 template<size_t = sizeof(size_t)>
86 struct _Fnv_hash
88 static size_t
89 hash(const char* __first, size_t __length)
91 size_t __result = 0;
92 for (; __length > 0; --__length)
93 __result = (__result * 131) + *__first++;
94 return __result;
98 template<>
99 struct _Fnv_hash<4>
101 static size_t
102 hash(const char* __first, size_t __length)
104 size_t __result = static_cast<size_t>(2166136261UL);
105 for (; __length > 0; --__length)
107 __result ^= static_cast<size_t>(*__first++);
108 __result *= static_cast<size_t>(16777619UL);
110 return __result;
114 template<>
115 struct _Fnv_hash<8>
117 static size_t
118 hash(const char* __first, size_t __length)
120 size_t __result =
121 static_cast<size_t>(14695981039346656037ULL);
122 for (; __length > 0; --__length)
124 __result ^= static_cast<size_t>(*__first++);
125 __result *= static_cast<size_t>(1099511628211ULL);
127 return __result;
131 /// Explicit specializations for float.
132 template<>
133 inline size_t
134 hash<float>::operator()(float __val) const
136 size_t __result = 0;
138 // 0 and -0 both hash to zero.
139 if (__val != 0.0f)
140 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__val),
141 sizeof(__val));
142 return __result;
145 /// Explicit specializations for double.
146 template<>
147 inline size_t
148 hash<double>::operator()(double __val) const
150 size_t __result = 0;
152 // 0 and -0 both hash to zero.
153 if (__val != 0.0)
154 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__val),
155 sizeof(__val));
156 return __result;
159 /// Explicit specializations for long double.
160 template<>
161 _GLIBCXX_PURE size_t
162 hash<long double>::operator()(long double __val) const;
164 /// Explicit specialization of member operator for non-builtin types.
165 template<>
166 _GLIBCXX_PURE size_t
167 hash<string>::operator()(string) const;
169 template<>
170 _GLIBCXX_PURE size_t
171 hash<const string&>::operator()(const string&) const;
173 #ifdef _GLIBCXX_USE_WCHAR_T
174 template<>
175 _GLIBCXX_PURE size_t
176 hash<wstring>::operator()(wstring) const;
178 template<>
179 _GLIBCXX_PURE size_t
180 hash<const wstring&>::operator()(const wstring&) const;
181 #endif
185 #endif // _GLIBCXX_TR1_FUNCTIONAL_HASH_H