functional_hash.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _FUNCTIONAL_HASH_H
00031 #define _FUNCTIONAL_HASH_H 1
00032
00033 #pragma GCC system_header
00034
00035 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00036 # include <c++0x_warning.h>
00037 #endif
00038
00039 #include <string>
00040 #include <system_error>
00041
00042 namespace std
00043 {
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 template<typename _Tp>
00054 struct hash;
00055
00056
00057 template<typename _Tp>
00058 struct hash<_Tp*> : public std::unary_function<_Tp*, size_t>
00059 {
00060 size_t
00061 operator()(_Tp* __p) const
00062 { return reinterpret_cast<size_t>(__p); }
00063 };
00064
00065
00066 #define _Cxx_hashtable_define_trivial_hash(_Tp) \
00067 template<> \
00068 struct hash<_Tp> : public std::unary_function<_Tp, size_t> \
00069 { \
00070 size_t \
00071 operator()(_Tp __val) const \
00072 { return static_cast<size_t>(__val); } \
00073 };
00074
00075
00076 _Cxx_hashtable_define_trivial_hash(bool);
00077
00078
00079 _Cxx_hashtable_define_trivial_hash(char);
00080
00081
00082 _Cxx_hashtable_define_trivial_hash(signed char);
00083
00084
00085 _Cxx_hashtable_define_trivial_hash(unsigned char);
00086
00087
00088 _Cxx_hashtable_define_trivial_hash(wchar_t);
00089
00090 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
00091
00092 _Cxx_hashtable_define_trivial_hash(char16_t);
00093
00094
00095 _Cxx_hashtable_define_trivial_hash(char32_t);
00096 #endif
00097
00098
00099 _Cxx_hashtable_define_trivial_hash(short);
00100
00101
00102 _Cxx_hashtable_define_trivial_hash(int);
00103
00104
00105 _Cxx_hashtable_define_trivial_hash(long);
00106
00107
00108 _Cxx_hashtable_define_trivial_hash(long long);
00109
00110
00111 _Cxx_hashtable_define_trivial_hash(unsigned short);
00112
00113
00114 _Cxx_hashtable_define_trivial_hash(unsigned int);
00115
00116
00117 _Cxx_hashtable_define_trivial_hash(unsigned long);
00118
00119
00120 _Cxx_hashtable_define_trivial_hash(unsigned long long);
00121
00122 #undef _Cxx_hashtable_define_trivial_hash
00123
00124
00125
00126
00127
00128 template<size_t = sizeof(size_t)>
00129 struct _Fnv_hash
00130 {
00131 static size_t
00132 hash(const char* __first, size_t __length)
00133 {
00134 size_t __result = 0;
00135 for (; __length > 0; --__length)
00136 __result = (__result * 131) + *__first++;
00137 return __result;
00138 }
00139 };
00140
00141 template<>
00142 struct _Fnv_hash<4>
00143 {
00144 static size_t
00145 hash(const char* __first, size_t __length)
00146 {
00147 size_t __result = static_cast<size_t>(2166136261UL);
00148 for (; __length > 0; --__length)
00149 {
00150 __result ^= static_cast<size_t>(*__first++);
00151 __result *= static_cast<size_t>(16777619UL);
00152 }
00153 return __result;
00154 }
00155 };
00156
00157 template<>
00158 struct _Fnv_hash<8>
00159 {
00160 static size_t
00161 hash(const char* __first, size_t __length)
00162 {
00163 size_t __result =
00164 static_cast<size_t>(14695981039346656037ULL);
00165 for (; __length > 0; --__length)
00166 {
00167 __result ^= static_cast<size_t>(*__first++);
00168 __result *= static_cast<size_t>(1099511628211ULL);
00169 }
00170 return __result;
00171 }
00172 };
00173
00174
00175 template<>
00176 struct hash<float>
00177 : public std::unary_function<float, size_t>
00178 {
00179 size_t
00180 operator()(float __val) const
00181 {
00182 size_t __result = 0;
00183
00184
00185 if (__val != 0.0f)
00186 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__val),
00187 sizeof(__val));
00188 return __result;
00189 }
00190 };
00191
00192
00193 template<>
00194 struct hash<double>
00195 : public std::unary_function<double, size_t>
00196 {
00197 size_t
00198 operator()(double __val) const
00199 {
00200 size_t __result = 0;
00201
00202
00203 if (__val != 0.0)
00204 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__val),
00205 sizeof(__val));
00206 return __result;
00207 }
00208 };
00209
00210
00211 template<>
00212 struct hash<long double>
00213 : public std::unary_function<long double, size_t>
00214 {
00215 size_t
00216 operator()(long double __val) const
00217 {
00218 size_t __result = 0;
00219
00220 int __exponent;
00221 __val = __builtin_frexpl(__val, &__exponent);
00222 __val = __val < 0.0l ? -(__val + 0.5l) : __val;
00223
00224 const long double __mult =
00225 __gnu_cxx::__numeric_traits<size_t>::__max + 1.0l;
00226 __val *= __mult;
00227
00228
00229
00230 const size_t __hibits = (size_t)__val;
00231 __val = (__val - (long double)__hibits) * __mult;
00232
00233 const size_t __coeff =
00234 __gnu_cxx::__numeric_traits<size_t>::__max / __LDBL_MAX_EXP__;
00235
00236 __result = __hibits + (size_t)__val + __coeff * __exponent;
00237
00238 return __result;
00239 }
00240 };
00241
00242
00243 template<>
00244 struct hash<string>
00245 : public std::unary_function<string, size_t>
00246 {
00247 size_t
00248 operator()(const string& __s) const
00249 { return _Fnv_hash<>::hash(__s.data(), __s.length()); }
00250 };
00251
00252 #ifdef _GLIBCXX_USE_WCHAR_T
00253
00254 template<>
00255 struct hash<wstring>
00256 : public std::unary_function<wstring, size_t>
00257 {
00258 size_t
00259 operator()(const wstring& __s) const
00260 {
00261 const char* __p = reinterpret_cast<const char*>(__s.data());
00262 return _Fnv_hash<>::hash(__p, __s.length() * sizeof(wchar_t));
00263 }
00264 };
00265 #endif
00266
00267 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
00268
00269 template<>
00270 struct hash<u16string>
00271 : public std::unary_function<u16string, size_t>
00272 {
00273 size_t
00274 operator()(const u16string& __s) const
00275 {
00276 const char* __p = reinterpret_cast<const char*>(__s.data());
00277 return _Fnv_hash<>::hash(__p, __s.length() * sizeof(char16_t));
00278 }
00279 };
00280
00281
00282 template<>
00283 struct hash<u32string>
00284 : public std::unary_function<u32string, size_t>
00285 {
00286 size_t
00287 operator()(const u32string& __s) const
00288 {
00289 const char* __p = reinterpret_cast<const char*>(__s.data());
00290 return _Fnv_hash<>::hash(__p, __s.length() * sizeof(char32_t));
00291 }
00292 };
00293 #endif
00294
00295
00296 template<>
00297 struct hash<error_code>
00298 : public std::unary_function<error_code, size_t>
00299 {
00300 size_t
00301 operator()(const error_code& __e) const
00302 {
00303 const char* __p = reinterpret_cast<const char*>(&__e);
00304 return _Fnv_hash<>::hash(__p, sizeof(__e));
00305 }
00306 };
00307
00308 }
00309
00310 #endif // _FUNCTIONAL_HASH_H