Objective : Create a hash map that takes 2 integer keys(pointers converted to integers using unsigned int casting, and yes this works) and maps it to a single value.
Attempted Solution: So i already have a hash map that takes a single key and maps it to value successfully. I now extended it to taking two keys using a “pairing function”. So i take the two keys , pair them using the Cantor pairing function and then hash this combined key .
Bottleneck: So the problem with two keys is that the cantor pairing function does a multiplication which causes integer overflow and hence “does not” give me unique outputs, as it is supposed to do mathematically.
Question:
- I see that a lot of hashing functions do multiplications. Is integer overflow a normal thing in hashing or is this bad?
- Im also thinking of doing an append of one key on the other into a new 64 bit integer. like aaaaaaaabbbbbbbb and then pass it on to the hash map. But I fear that this might cause abnormal numbers like NaN to come up due to the floating point representation, which could be bad.
- Any better ideas are welcome.
Please let me know if some parts are unclear.
Integer overflow is not that bad. True it can cause collisions, but its ok to have rare collisions for hashes meant for a hashmap.
Perhaps a bad idea. It can cause too many collisions.
If your inputs are
Nbits wide, then your output will have to be at least2Nbits wide. So to accommodateuintinputs, you need an output ofulongsize. If input is higher than that it will cause overflow. If output is smaller than that then there will be collisions/duplicates.But true there aren’t many functions which fit inside
2Nsize. You could tryOr this
both of which will be an unsigned long. These two are the most space efficient it can get.
See Mapping two integers to one, in a unique and deterministic way additionally.