CrystalSpace

Public API Reference

csutil/hashcomputer.h
00001 /*
00002 Copyright (C) 2003 by Mat Sutcliffe <oktal@gmx.co.uk>
00003 2007 by Marten Svanfeldt
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public
00016 License along with this library; if not, write to the Free
00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_UTIL_HASHCOMPUTER_H__
00021 #define __CS_UTIL_HASHCOMPUTER_H__
00022 
00033 CS_CRYSTALSPACE_EXPORT
00034 unsigned int csHashCompute (char const*);
00035 
00042 CS_CRYSTALSPACE_EXPORT
00043 unsigned int csHashCompute (char const*, size_t length);
00044 
00049 template <class T>
00050 class csHashComputer
00051 {
00052 public:
00054   static uint ComputeHash (const T& key)
00055   {
00056     return key.GetHash();
00057   }
00058 };
00059 
00064 template <class T>
00065 class csHashComputerIntegral
00066 {
00067 public:
00069   static uint ComputeHash (const T& key)
00070   {
00071     return (uintptr_t)key;
00072   }
00073 };
00074 
00076 
00079 template<class T>
00080 class csHashComputer<T*> : public csHashComputerIntegral<T*> {};
00081 
00082 template<>
00083 class csHashComputer<char> : public csHashComputerIntegral<char> {};
00084 template<>
00085 class csHashComputer<unsigned char> : public csHashComputerIntegral<unsigned char> {};
00086 
00087 template<>
00088 class csHashComputer<short> : public csHashComputerIntegral<short> {};
00089 template<>
00090 class csHashComputer<unsigned short> : public csHashComputerIntegral<unsigned short> {};
00091 
00092 template<>
00093 class csHashComputer<int> : public csHashComputerIntegral<int> {}; 
00094 template<>
00095 class csHashComputer<unsigned int> : 
00096   public csHashComputerIntegral<unsigned int> {}; 
00097 
00098 template<>
00099 class csHashComputer<long> : public csHashComputerIntegral<long> {}; 
00100 template<>
00101 class csHashComputer<unsigned long> : 
00102   public csHashComputerIntegral<unsigned long> {}; 
00103 
00104 #if (CS_LONG_SIZE < 8)    
00105 template<>
00106 class csHashComputer<longlong> : 
00107   public csHashComputerIntegral<longlong> {}; 
00108 template<>
00109 class csHashComputer<ulonglong> : 
00110   public csHashComputerIntegral<ulonglong> {}; 
00111 #endif
00112 
00113 template<>
00114 class csHashComputer<float>
00115 {
00116 public:
00118   static uint ComputeHash (float key)
00119   {
00120     union
00121     {
00122       float f;
00123       uint u;
00124     } float2uint;
00125     float2uint.f = key;
00126     return float2uint.u;
00127   }
00128 };
00129 template<>
00130 class csHashComputer<double>
00131 {
00132 public:
00134   static uint ComputeHash (double key)
00135   {
00136     union
00137     {
00138       double f;
00139       uint u;
00140     } double2uint;
00141     double2uint.f = key;
00142     return double2uint.u;
00143   }
00144 };
00145 
00146 
00156 template <class T>
00157 class csHashComputerString
00158 {
00159 public:
00160   static uint ComputeHash (const T& key)
00161   {
00162     return csHashCompute ((const char*)key);
00163   }
00164 };
00165 
00169 template<>
00170 class csHashComputer<const char*> : public csHashComputerString<const char*> {};
00171 
00181 template <class T>
00182 class csHashComputerStruct
00183 {
00184 public:
00185   static uint ComputeHash (const T& key)
00186   {
00187     return csHashCompute ((char*)&key, sizeof (T));
00188   }
00189 };
00190 
00192 
00193 namespace CS
00194 {
00195   namespace Utility
00196   {
00197 
00198     class HashFoldingFNV1
00199     {
00200     public:
00201       static uint32 FoldHash (uint32 input)
00202       {
00203         static const uint32 fnvprime = 0x1000193;
00204         static const uint32 fnvoffset = 0x811C9DC5;
00205 
00206         uint32 fold = fnvoffset;
00207         for (size_t i = 0; i < 4; ++i)
00208         {
00209           fold = fold ^ (input & 0xFF);
00210           fold *= fnvprime;
00211           input >>= 8;
00212         }
00213         
00214         return fold;
00215       }
00216     };
00217 
00218     class HashFoldingNone
00219     {
00220     public:
00221       static uint32 FoldHash (uint32 input)
00222       {
00223         return input;
00224       }
00225     };
00226 
00227   }
00228 }
00229 
00232 #endif

Generated for Crystal Space 2.0 by doxygen 1.7.6.1