Is there something like BidiMap?
1 answer
There is no such thing in the language or library (AFAIK), but it's easy enough to implement: just combine the two cards in struct
and make sure they stay in sync. The only problem is that they are difficult to write in general terms, but this can be done with interface{}
:
type BidirMap struct {
left, right map[interface{}]interface{}
}
func (m *BidirMap) Insert(key, val interface{}) {
if _, inleft := left[key]; inleft {
delete(left, key)
}
if _, inright := right[val]; inright {
delete(right, val)
}
m.left[key] = val
m.right[val] = key
}
and etc.
+5
source to share