Boost::Multiindex vs string indexed boost::unordered_map
-
20-01-2021 - |
Question
I need a container of unique elements to be accessed with a triplet of int, and each int can be over 1.000.000.000.
(Only few of these elements will be actually filled, and actually these elements are boost::unordered_map themselves).
Is it faster to have a multiindex array like boost::multiindex (or maybe something else I don't know) or just a boost::unordered_map with a composed string as a key ?
Solution
Multi-index isn't what you want, you seem to want a single index whose type is a triple. (Unless you actually do want three independent indexes; if I misunderstood, leave a comment.)
Don't use strings, heavens no. Just use the triple as a key:
typedef std::tuple<int, int, int> key_type;
If you use an std::map<key_type, T>
, you get logarithmic lookup, which may be sufficient, and I think you don't even have to do any more work (not sure if lexicographic comparison is defined by default for tuples).
If you want to use an std::unordered_map<key_type, T>
(or the boost version), you have to define a hash function. Boost already has one for tuples, I think, but C++11 doesn't; but it's very easy to implement yourself based on hash_combine()
which you can just crop out off the Boost code.