Skip to content

Instantly share code, notes, and snippets.

@ChiHsiang
Last active February 28, 2017 11:25
Show Gist options
  • Select an option

  • Save ChiHsiang/74d589f528d54c67552c922051afbaa2 to your computer and use it in GitHub Desktop.

Select an option

Save ChiHsiang/74d589f528d54c67552c922051afbaa2 to your computer and use it in GitHub Desktop.
Hash function
// BKDR Hash Function
version-1
unsigned int BKDRHash(char *str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
=============================================================================================================
version-2
unsigned int BKDRHash(char* str, unsigned int len)
{
unsigned int seed = 131; /* 31 131 1313 13131 131313 etc.. */
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = (hash * seed) + (*str);
}
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment