hashers.h
Go to the documentation of this file.00001 #ifndef Hasher_h_included
00002 #define Hasher_h_included
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 #include <string>
00032
00033 namespace cxxtls
00034 {
00035
00041
00042 template<class T>
00043 struct Hasher
00075
00076 {
00077 unsigned int operator() (T const &t) { return (unsigned int)(t); }
00078 };
00079
00080 template<>
00081 struct Hasher<std::string>
00085 {
00086 unsigned int operator() (std::string const &t)
00087 {
00088 unsigned int rv = t.size();
00089
00090 std::string::const_iterator scan = t.begin(),
00091 end = t.end();
00092
00093 while(scan != end)
00094 {
00095 rv <<= 1;
00096
00097 rv |= *scan++;
00098 }
00099
00100 return rv;
00101 }
00102 };
00103
00104
00105 template<>
00106 struct Hasher<float>
00108 {
00109 unsigned int operator() (float const &t)
00110 {
00111 unsigned int rv = 0;
00112
00113 char const *scan = (char const*)(&t);
00114 char const *end = scan + sizeof(t);
00115
00116 while(scan != end)
00117 {
00118 rv <<= 4;
00119
00120 rv |= *scan++;
00121 }
00122
00123 return rv;
00124 }
00125 };
00126
00127 template<>
00128 struct Hasher<double>
00130 {
00131 unsigned int operator() (double const &t)
00132 {
00133 unsigned int rv = 0;
00134
00135 char const *scan = (char const*)(&t);
00136 char const *end = scan + sizeof(t);
00137
00138 while(scan != end)
00139 {
00140 rv <<= 4;
00141
00142 rv |= *scan++;
00143 }
00144
00145 return rv;
00146 }
00147 };
00148
00149
00150 }
00151
00152 #endif