I have a TMap whose key is ultimately an FName. I have a scheme where I want to encode my name hash into a GUID, extract the hash later, then use it to look up in a map.
In other words, given:
uint32 hash = GetTypeHash(...); // ultimately a raw hash of MyKeyType outputted from elsewhere
Find the corresponding element in the map
MyValueType* value = map.FindByHash(hash);
The problem is, the TMap::FindByHash signature also takes in a ComparableKey argument, and I am struggling to provide that. What exactly is the ComparableKey? I thought it might the map’s key funcs, and attempted this:
But why do I need these mental gymnastics to find an element by its raw hash? If the container knows how to hash my key type, what is the ComparableKey all about, and do I really need to implement a custom struct like the blog post shows?
As far as i can see, hash is uint32, which isn’t a lot, especially considering possibility of collisions. Hence, there are several keys may match the single hash. So, for FindByHash you have to provide key you searching for in addition to hash you passing.
Here is a small example on how FindByHash can be used:
Oh, if that is the case, then it is completely counter-productive - i.e you would be passing the key to FindByHash, when at that point, you might as well index the map or perform a find directly with the key.
TestMap.FindByHash(Hash, Key); // Pointless
TestMap.Find(Key); // Just do this instead
This begs a follow up question: what would be an application of FindByHash?
Looking up by key value only, will call the hash function.
If you already have the hash value available, calling FindByHash() saves having to calculate it again.
If you know that your elements have unique hash values, you can make the key of your TMap be uint32, and make the hash function be identity.