'ElMo-Knock'

general_hash_functions.cpp

Go to the documentation of this file.
00001 #include "general_hash_functions.h"
00002 
00003 unsigned int RSHash(const std::string& str)
00004 {
00005    unsigned int b    = 378551;
00006    unsigned int a    = 63689;
00007    unsigned int hash = 0;
00008 
00009    for(std::size_t i = 0; i < str.length(); i++)
00010    {
00011       hash = hash * a + str[i];
00012       a    = a * b;
00013    }
00014 
00015    return hash;
00016 }
00017 /* End Of RS Hash Function */
00018 
00019 
00020 unsigned int JSHash(const std::string& str)
00021 {
00022    unsigned int hash = 1315423911;
00023 
00024    for(std::size_t i = 0; i < str.length(); i++)
00025    {
00026       hash ^= ((hash << 5) + str[i] + (hash >> 2));
00027    }
00028 
00029    return hash;
00030 }
00031 /* End Of JS Hash Function */
00032 
00033 
00034 unsigned int PJWHash(const std::string& str)
00035 {
00036    unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
00037    unsigned int ThreeQuarters     = (unsigned int)((BitsInUnsignedInt  * 3) / 4);
00038    unsigned int OneEighth         = (unsigned int)(BitsInUnsignedInt / 8);
00039    unsigned int HighBits          = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
00040    unsigned int hash              = 0;
00041    unsigned int test              = 0;
00042 
00043    for(std::size_t i = 0; i < str.length(); i++)
00044    {
00045       hash = (hash << OneEighth) + str[i];
00046 
00047       if((test = hash & HighBits)  != 0)
00048       {
00049          hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
00050       }
00051    }
00052 
00053    return hash;
00054 }
00055 /* End Of  P. J. Weinberger Hash Function */
00056 
00057 
00058 unsigned int ELFHash(const std::string& str)
00059 {
00060    unsigned int hash = 0;
00061    unsigned int x    = 0;
00062 
00063    for(std::size_t i = 0; i < str.length(); i++)
00064    {
00065       hash = (hash << 4) + str[i];
00066       if((x = hash & 0xF0000000L) != 0)
00067       {
00068          hash ^= (x >> 24);
00069       }
00070       hash &= ~x;
00071    }
00072 
00073    return hash;
00074 }
00075 /* End Of ELF Hash Function */
00076 
00077 
00078 unsigned int BKDRHash(const std::string& str)
00079 {
00080    unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
00081    unsigned int hash = 0;
00082 
00083    for(std::size_t i = 0; i < str.length(); i++)
00084    {
00085       hash = (hash * seed) + str[i];
00086    }
00087 
00088    return hash;
00089 }
00090 /* End Of BKDR Hash Function */
00091 
00092 
00093 unsigned int SDBMHash(const std::string& str)
00094 {
00095    unsigned int hash = 0;
00096 
00097    for(std::size_t i = 0; i < str.length(); i++)
00098    {
00099       hash = str[i] + (hash << 6) + (hash << 16) - hash;
00100    }
00101 
00102    return hash;
00103 }
00104 /* End Of SDBM Hash Function */
00105 
00106 
00107 unsigned int DJBHash(const std::string& str)
00108 {
00109    unsigned int hash = 5381;
00110 
00111    for(std::size_t i = 0; i < str.length(); i++)
00112    {
00113       hash = ((hash << 5) + hash) + str[i];
00114    }
00115 
00116    return hash;
00117 }
00118 /* End Of DJB Hash Function */
00119 
00120 
00121 unsigned int DEKHash(const std::string& str)
00122 {
00123    unsigned int hash = static_cast<unsigned int>(str.length());
00124 
00125    for(std::size_t i = 0; i < str.length(); i++)
00126    {
00127       hash = ((hash << 5) ^ (hash >> 27)) ^ str[i];
00128    }
00129 
00130    return hash;
00131 }
00132 /* End Of DEK Hash Function */
00133 
00134 
00135 unsigned int BPHash(const std::string& str)
00136 {
00137    unsigned int hash = 0;
00138    for(std::size_t i = 0; i < str.length(); i++)
00139    {
00140       hash = hash << 7 ^ str[i];
00141    }
00142 
00143    return hash;
00144 }
00145 /* End Of BP Hash Function */
00146 
00147 
00148 unsigned int FNVHash(const std::string& str)
00149 {
00150    const unsigned int fnv_prime = 0x811C9DC5;
00151    unsigned int hash = 0;
00152    for(std::size_t i = 0; i < str.length(); i++)
00153    {
00154       hash *= fnv_prime;
00155       hash ^= str[i];
00156    }
00157 
00158    return hash;
00159 }
00160 /* End Of FNV Hash Function */
00161 
00162 
00163 unsigned int APHash(const std::string& str)
00164 {
00165    unsigned int hash = 0xAAAAAAAA;
00166 
00167    for(std::size_t i = 0; i < str.length(); i++)
00168    {
00169       hash ^= ((i & 1) == 0) ? (  (hash <<  7) ^ str[i] * (hash >> 3)) :
00170                                (~((hash << 11) + (str[i] ^ (hash >> 5))));
00171    }
00172 
00173    return hash;
00174 }
00175 /* End Of AP Hash Function */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines