I need a lookup table that works in both directions for saving and loading a custom file.
Therefore I thought a dictionary that also used the values as keys might be handy.
Is there a good implementation for this?
My current attempt looks like this:
struct BiDict
key1
key2
end
function Base.getindex(bd::BiDict,::Val{1},ind)
bd.key2[findfirst(isequal(ind),bd.key1)]
end
function Base.getindex(bd::BiDict,::Val{2},ind)
bd.key1[findfirst(isequal(ind),bd.key2)]
end
It can be used to search in both directions using the first index, although I am not sure how to best avoid using Val
.
julia> lookup = BiDict([2,4,3,1],[:s,:j,:e,:t])
BiDict([2, 4, 3, 1], [:s, :j, :e, :t])
julia> lookup[Val(1),4]
:j
julia> lookup[Val(2),:j]
4