hashers.h

Go to the documentation of this file.
00001 #ifndef Hasher_h_included
00002 #define Hasher_h_included
00003 
00004 //
00005 // Copyright 2002, Lowell Boggs Jr.
00006 //
00007 // This file or directory, containing source code for a computer program,
00008 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00009 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00010 // source file without charge or obligation so long as you agree to
00011 // the following:
00012 //
00013 //  1.  You must indemnify Lowell Boggs against any and all financial
00014 //      obligations caused by its use, misuse, function, or malfunction.
00015 //      Further, you acknowledge that there is no warranty of any kind,
00016 //      whatsoever.
00017 //
00018 //  2.  You agree not to attempt to patent any portion of this original
00019 //      work -- though you may attempt to patent your own extensions to
00020 //      it if you so choose.
00021 //
00022 //  3.  You keep this copyright notice with the file and all copies
00023 //      of the file and do not change it anyway except language translation.
00024 //
00025 // You are responsible for enforcing your own compliance with these
00026 // conditions and may not use this source file if you cannot agree to the
00027 // above terms and conditions.
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 } // namespace cxxtls
00151 
00152 #endif
Generated on Wed Feb 29 22:50:04 2012 for CXXUtilities by  doxygen 1.6.3