Kevin Goodsell said:
David said:
[...]
On Boost (
www.boost.org), there was a discussion about
a "bimap", which is a two-way map in which the value type
was actually a second key type.
Maybe I'm not thinking clearly (it's getting rather late), but it
seems like you could do this with a std::set where you insert
the two keys at the same time, and each with some kind of
reference to the other.
Wrapping it in a new class type would make it convenient to
use.
You could do that, but it's slightly slower and has other
undesirable properties. For instance, in the bimap, you
can query the map to see if a key does not exist in the
first set. How do you do that with your design? Maintaining
invariants is tricker with your design. Keys have to be
added and removed in pairs. Someone could make a
mistake and create non-paired keys that would corrupt
the map. Or maybe someone could accidentally create
just one key. At any rate, the wrapper would be fairly
elaborate, and it's not obvious to me that it would be any
better (in terms of simplicity or performance) than a custom
designed n-map.
Also, a bimap supports ordered keys, and yours does not
(at least not without a suitably complex definition of
"reference"). Consider the case where you wish to insert
both (a, b) and (b, c) into the map, but you wish each key
set to be unique. How do you do this with your design?
You can, but it gets more and more complicated.
Dave