The problem is not runtime but avoiding inconsistent states. Your mention of DataStructures.jl made me realize that this is a general problem indeed. It appears to be called a BiMap:
- Haskell: Data.Bimap
- Java: BiMap (Guava: Google Core Libraries for Java 19.0 API)
- R: bimap: Create a new 'bimap' in datastructures: Implementation of Core Data Structures
From the Haskell docs:
An implementation of bidirectional maps between values of two key types. A Bimap is essentially a bijection between subsets of its two argument types.
Each element of the left-hand type is associated with an element of the right-hand type, and vice-versa, such that the two mappings are inverses. Deleting an element will cause its twin to be deleted, and inserting a pair of elements will cause any overlapping bindings to be deleted.
There is a package created and last updated in 2015 at GitHub - bicycle1885/BiMaps.jl: bijective mapping.
I should probably make a PR at DataStructures.jl