can you tell me the usage of hashing what is hashing
Printable View
can you tell me the usage of hashing what is hashing
Hashing is when you take some bit of data and turn it into an integer suited for an array index :-)
Code:#include <iostream>
using namespace std;
const size_t H_SIZE = 100;
size_t myhash(char *string)
{
size_t hval;
for (hval = 0; *string != '\0'; string++)
{
hval = *string + 31 * hval;
}
return hval % H_SIZE;
}
int main()
{
cout<< (unsigned long)myhash("Have fun!") <<endl;
}
heres a great hash generating algo for those who might be interested
i stored a full english dictionary using this hash generating funtion and collision time was neglegable. =)Code:getHash(int& hash, const char* key)
{
unsigned long h = 0;
unsigned long g;
unsigned char ch;
while (*key != 0) {
ch = (unsigned char) *key++;
h = (h << 4) + ch;
if ((g = h & 0xf0000000L) != 0)
h = (h ^ (g >> 24)) ^ g;
}
hash = (int)h%CAPACITY; //CAPACITY is the size of your hash table
}
(btw: the algorithm is credited to some crazy math guy but i cant remember his name)