I have a hash table implementation that contains features like insert, remove, find, exists.
Using this hash table, I wanted to implement a hash map.
So, I meant to create a simple pair class that simply contained the key and value. It would be overloaded operator=to only account for key equality. Also, the hash function would receive the input object of the pair and return the hash, again taking into account only the key part.
Thus, I would have a hashtable<pair>hashmap inside, which in essence would be something like hashtable<key>, because it would take into account only the key part and simply carry values over the element.
But then there were problems.
For example, I wanted to implement an exists function that would check if a pair with the specified key exists in hashmap. Thus, it will take the key and check if the key is inside the card. This will be implemented using a hash table. But now it hashtable::existsaccepts the input type of the pair.
So now I had these alternatives that I don't like.
Create a paired object with a key and leave the value element uninitialized
Passing a key object to a pair object (for example, reinterpret_cast (& key)), since the hash table functions will only use the key part of the pair in this situation.
The first creates an unnecessary copy. And the second address of the key may not coincide with the paired address of the object. Although I believe that I can know the key address exactly, given that I can calculate
(&pair.key) - (&pair)
, , .
?