question about string hash function

Discussion in 'Programmer's Corner' started by moslem, Jul 10, 2010.

  1. moslem

    moslem Thread Starter New Member

    Joined:
    Dec 16, 2009
    20
    0
    Hello every one,
    i've a string hash function it's performace is already good but there is some things that happens inside it and idon't know why
    the hash function:
    unsigned int table::hash(const string &word)
    {
    int hash = 0;
    int n=word.length();
    for(int i=0;i<n;i++)
    hash = 31*hash+word;
    return hash % MAX_TABLE;
    }
    my questions about it
    why we multiply by 31 the hash every time is this for making weighting for the characters or any thing else?
    thanks inadvance.
     
    #1
  2. Harrington

    Harrington New Member

    Joined:
    Dec 19, 2009
    86
    3
    #2
    moslem likes this.
  3. Papabravo

    Papabravo AAC Fanatic!

    Joined:
    Feb 24, 2006
    7,902
    1,014
    #3
    moslem likes this.
Loading...