I have ID values of the type unsigned int. I need to map an Id to a pointer in constant time.
Key Distribution:
ID will have a value in the range of 0 to uint_max. Most of keys will be clustered into a single group, but there will be outliers.
Implementation:
-
I thought about using the C++ ext hash_map stuff, but I’ve heard their performance isn’t too great when keys have a huge potential range.
-
I’ve also thought of using some form of chained lookup (equivalent to recursively subdividing the range into C chucks). If there are no keys in a range, that range will point to NULL.
N = Key Range
Level 0 (divided into C = 16, so 16 pieces) = [0, N/16), [N/16, 2*(N/16)), …
Level 1 (divided into C = 16, so 16 * 16 pieces) = …
Does anyone else have ideas on how this mapping can be more efficiently implemented?
Update:
By constant, I just meant each key lookup is not significantly influenced by the # of values in the item. I did not mean it had to be a single op.
Use a hash map (
unordered_map). This gives ~O(1) look-up times. You “heard” it was bad, but did you try it, test it, and determine it to be a problem? If not, use a hash map.After your code gets close to completion, profile it and determine if the look-up times are the main cause of slowness in your program. Chances are, it won’t be.