I have some problem where I need to define a one-to-one mapping from one variable to another. I am using a Dictionary like this:
mapping = Dict('I'=>1, 'V'=>5, 'X'=>10)
I also want to map it back, so at the moment I define a second Dict:
inverse_mapping = Dict(1=>'I', 5=>'V', 10=>'X')
Is there a better collection for this out there? something like a two way dictionary or maybe another kind of hash table?
You can use Bijections.jl. Here is an example of the usage.
First create an empty bijection mapping
InttoInt, and then add a couple pairs to the bijection:To find the value associated with a key, use the normal dictionary indexing syntax:
To find the key associated with a value, use function call syntax (note the parentheses instead of square brackets):
Bijections can also be iterated over like a dictionary:
Finally, you can see that
Bijectiondisallows adding a pair that would break the bijective map: