tr1_impl/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
00031
00032
00033
00034
00035 namespace std
00036 {
00037 _GLIBCXX_BEGIN_NAMESPACE_TR1
00038
00039
00040
00041
00042 template<typename _Tp>
00043 struct hash : public std::unary_function<_Tp, size_t>
00044 {
00045 size_t
00046 operator()(_Tp __val) const;
00047 };
00048
00049
00050 template<typename _Tp>
00051 struct hash<_Tp*> : public std::unary_function<_Tp*, size_t>
00052 {
00053 size_t
00054 operator()(_Tp* __p) const
00055 { return reinterpret_cast<size_t>(__p); }
00056 };
00057
00058
00059 #define _TR1_hashtable_define_trivial_hash(_Tp) \
00060 template<> \
00061 inline size_t \
00062 hash<_Tp>::operator()(_Tp __val) const \
00063 { return static_cast<size_t>(__val); }
00064
00065 _TR1_hashtable_define_trivial_hash(bool);
00066 _TR1_hashtable_define_trivial_hash(char);
00067 _TR1_hashtable_define_trivial_hash(signed char);
00068 _TR1_hashtable_define_trivial_hash(unsigned char);
00069 _TR1_hashtable_define_trivial_hash(wchar_t);
00070 _TR1_hashtable_define_trivial_hash(short);
00071 _TR1_hashtable_define_trivial_hash(int);
00072 _TR1_hashtable_define_trivial_hash(long);
00073 _TR1_hashtable_define_trivial_hash(long long);
00074 _TR1_hashtable_define_trivial_hash(unsigned short);
00075 _TR1_hashtable_define_trivial_hash(unsigned int);
00076 _TR1_hashtable_define_trivial_hash(unsigned long);
00077 _TR1_hashtable_define_trivial_hash(unsigned long long);
00078
00079 #undef _TR1_hashtable_define_trivial_hash
00080
00081
00082
00083
00084
00085 template<size_t = sizeof(size_t)>
00086 struct _Fnv_hash
00087 {
00088 static size_t
00089 hash(const char* __first, size_t __length)
00090 {
00091 size_t __result = 0;
00092 for (; __length > 0; --__length)
00093 __result = (__result * 131) + *__first++;
00094 return __result;
00095 }
00096 };
00097
00098 template<>
00099 struct _Fnv_hash<4>
00100 {
00101 static size_t
00102 hash(const char* __first, size_t __length)
00103 {
00104 size_t __result = static_cast<size_t>(2166136261UL);
00105 for (; __length > 0; --__length)
00106 {
00107 __result ^= static_cast<size_t>(*__first++);
00108 __result *= static_cast<size_t>(16777619UL);
00109 }
00110 return __result;
00111 }
00112 };
00113
00114 template<>
00115 struct _Fnv_hash<8>
00116 {
00117 static size_t
00118 hash(const char* __first, size_t __length)
00119 {
00120 size_t __result =
00121 static_cast<size_t>(14695981039346656037ULL);
00122 for (; __length > 0; --__length)
00123 {
00124 __result ^= static_cast<size_t>(*__first++);
00125 __result *= static_cast<size_t>(1099511628211ULL);
00126 }
00127 return __result;
00128 }
00129 };
00130
00131
00132 template<>
00133 inline size_t
00134 hash<float>::operator()(float __val) const
00135 {
00136 size_t __result = 0;
00137
00138
00139 if (__val != 0.0f)
00140 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__val),
00141 sizeof(__val));
00142 return __result;
00143 };
00144
00145
00146 template<>
00147 inline size_t
00148 hash<double>::operator()(double __val) const
00149 {
00150 size_t __result = 0;
00151
00152
00153 if (__val != 0.0)
00154 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__val),
00155 sizeof(__val));
00156 return __result;
00157 };
00158
00159
00160 template<>
00161 size_t
00162 hash<long double>::operator()(long double __val) const;
00163
00164
00165 template<>
00166 size_t
00167 hash<string>::operator()(string) const;
00168
00169 template<>
00170 size_t
00171 hash<const string&>::operator()(const string&) const;
00172
00173 #ifdef _GLIBCXX_USE_WCHAR_T
00174 template<>
00175 size_t
00176 hash<wstring>::operator()(wstring) const;
00177
00178 template<>
00179 size_t
00180 hash<const wstring&>::operator()(const wstring&) const;
00181 #endif
00182
00183 _GLIBCXX_END_NAMESPACE_TR1
00184 }