2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / tr1_impl / functional_hash.h
blob49f2cb7384d89a1c0a65c9e00c2d79c57d3f3200
1 // TR1 functional -*- C++ -*-
3 // Copyright (C) 2007, 2008 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file tr1_impl/functional_hash.h
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
35 namespace std
37 _GLIBCXX_BEGIN_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 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
71 _TR1_hashtable_define_trivial_hash(char16_t);
72 _TR1_hashtable_define_trivial_hash(char32_t);
73 #endif
74 _TR1_hashtable_define_trivial_hash(short);
75 _TR1_hashtable_define_trivial_hash(int);
76 _TR1_hashtable_define_trivial_hash(long);
77 _TR1_hashtable_define_trivial_hash(long long);
78 _TR1_hashtable_define_trivial_hash(unsigned short);
79 _TR1_hashtable_define_trivial_hash(unsigned int);
80 _TR1_hashtable_define_trivial_hash(unsigned long);
81 _TR1_hashtable_define_trivial_hash(unsigned long long);
83 #undef _TR1_hashtable_define_trivial_hash
85 // Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
86 // (Used by the next specializations of std::tr1::hash.)
88 /// Dummy generic implementation (for sizeof(size_t) != 4, 8).
89 template<size_t = sizeof(size_t)>
90 struct _Fnv_hash
92 static size_t
93 hash(const char* __first, size_t __length)
95 size_t __result = 0;
96 for (; __length > 0; --__length)
97 __result = (__result * 131) + *__first++;
98 return __result;
102 template<>
103 struct _Fnv_hash<4>
105 static size_t
106 hash(const char* __first, size_t __length)
108 size_t __result = static_cast<size_t>(2166136261UL);
109 for (; __length > 0; --__length)
111 __result ^= static_cast<size_t>(*__first++);
112 __result *= static_cast<size_t>(16777619UL);
114 return __result;
118 template<>
119 struct _Fnv_hash<8>
121 static size_t
122 hash(const char* __first, size_t __length)
124 size_t __result =
125 static_cast<size_t>(14695981039346656037ULL);
126 for (; __length > 0; --__length)
128 __result ^= static_cast<size_t>(*__first++);
129 __result *= static_cast<size_t>(1099511628211ULL);
131 return __result;
135 /// Explicit specializations for float.
136 template<>
137 inline size_t
138 hash<float>::operator()(float __val) const
140 size_t __result = 0;
142 // 0 and -0 both hash to zero.
143 if (__val != 0.0f)
144 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__val),
145 sizeof(__val));
146 return __result;
149 /// Explicit specializations for double.
150 template<>
151 inline size_t
152 hash<double>::operator()(double __val) const
154 size_t __result = 0;
156 // 0 and -0 both hash to zero.
157 if (__val != 0.0)
158 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__val),
159 sizeof(__val));
160 return __result;
163 /// Explicit specializations for long double.
164 template<>
165 size_t
166 hash<long double>::operator()(long double __val) const;
168 /// Explicit specialization of member operator for non-builtin types.
169 template<>
170 size_t
171 hash<string>::operator()(string) const;
173 template<>
174 size_t
175 hash<const string&>::operator()(const string&) const;
177 #ifdef _GLIBCXX_USE_WCHAR_T
178 template<>
179 size_t
180 hash<wstring>::operator()(wstring) const;
182 template<>
183 size_t
184 hash<const wstring&>::operator()(const wstring&) const;
185 #endif
187 _GLIBCXX_END_NAMESPACE_TR1