0

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;
}
xaemy
  • 11
  • 3
  • 2
    The hash function usually has one job: hash the data into a number deterministically. If you're looking into a hashtable and doing some probing, that's not right, that code should be elsewhere. And how do you know it's bad? Have you measured it? –  Mar 25 '21 at 18:17
  • I recommend changing the parameter to `const std::string& key`; you don't want to create a copy just to calculate the index. Also why not just use [the implementation that's already available](https://en.cppreference.com/w/cpp/string/basic_string/hash)? (you may need to add some logic to convert from `size_t` to `int`)... – fabian Mar 25 '21 at 18:28
  • How would you measure it? What makes a hash function bad (or good)? Feed it some representative data and count the collisions. If you change the function, you can then compare if it's an improvement. –  Mar 25 '21 at 18:47

0 Answers0