Quadratic Probing Hashfunction C++

514 Views Asked by At

I am currently implementing a hashtable with quadratic probing in C++. I started of by implementing a rather simple hashfunction: Adding up the ASCII values of each letter of my key (=string). Since I am aware that this is not a good hashfunction at all I am now looking for a better one. I have been googeling for a while but all I seem to find are similar simple ones. Can someone suggest me a good hashfunction? Does it make sense to use the ASCII values for calculating the index? Or should I use the length of the word instead?

Also does it make sense to implement the collision handeling in a seperate function like I did, or should I rather do this step within the hashtfunction itself?

Thanks for your help!

int Hash::quadSond(int index, int i)
{
    int newIndex = index + (int)pow(i, 2); 
    return newIndex;
}


int Hash::hashFunction(std::string key) 
{
    int hash = 0;
    int index;
    int k = 1; 

    for (size_t i = 0; i < key.length(); i++) 
    {
        hash += (int)key[i]*5; 
    }
    
    index = hash % m_tableSize;  
    
    if (m_table[index] != nullptr) {
        while (m_table[index] != nullptr) { 
            int newIndex = quadSond(hash, k); 
            index = newIndex % m_tableSize;
            k++;
        }
    }
    
    return index;
}
0

There are 0 best solutions below