So I need to write a chaining hash map. I found some examples on the internet and have a pretty clear idea on how to go about it. However the biggest problem I am having is understanding the different hash function. I have a large list of id and they are 7 digits numbers so I think the best method would be to do something along lines of % 12 or something similiar with the modulo. So I am trying to understand this code and write something similiar but in terms of my function

Code:

unsigned djb_hash ( const void *key, int len )
{
  unsigned const char *p = key;
  unsigned h = 0;
  int i;
 
  for ( i = 0; i < len; i++ )
    h = 33 * h ^ p[i];
 
  return h;
}

but I am just confused on what is going on in the for loop and what is it doing. Can anyone explain this in more detail and help me on the right path to writing the modulo hash function?

Like i understand it multiplies it by 33 and raises h by the p but i dont get why its h = 33 * h^p; i dont get the point of the end result. So if I am trying to do % 12 i should do something like h = p % 12; right? but is key the placement of where the node is?

Here is the link to the full code http://www.daniweb.com/software-development/c/threads/104887

unsigned djb_hash ( const void *key, int len )
{
  unsigned const char *p = key;
  unsigned h = 0;
  int i;
 
  for ( i = 0; i < len; i++ )
    h = 33 * h ^ p[i];
 
  return h;
}

Like i understand it multiplies it by 33 and raises h by the p

First things first: C doesn't have a "power" operator. '^' is exclusive OR.

This article has been dead for over six months. Start a new discussion instead.