How to construct a Bijective map on Motoko?

I am trying to create a object to bound the Nat and Text variables in Motoko

For example,

let obj:[(Nat,Text)] = [(0,"a"),(1,"b")];
func foundTextbyNat(_input:Nat):Text;
func foundNatbyText(_input:Text):Nat;

the func foundTextbyNat(_input:Nat):Text; can be done by initializing a obj_map:HashMap.HashMap<Nat, Text> = HashMap.fromIter(obj.vals(), 0, Nat.equal, Hash.hash);, but how about the sec one? Any idea how to create a inversed HashMap? (Hardcode obj2:[(Text,Nat)] = [("a",0),("b",1)]; is NOT a solution since the obj must be dynamic variable)

Furthermore, any data structure we can use to directly create a bijective map? Given key Nat return associated Text and Given key Text return associated Nat.


Can you use a custom hash function that composes the key so that it is bit shifted so the original key and value are distinctly both able to hash to the same key value tuple? Ie get the key’s type max length, shift to concatenate the value, then switch on either to return the same collision yielding the key-value tuple?

Maybe this is a good reference: