Does Racket have a bidirectional hashmap?
That is, a hash map that can in constant time, be given a key and look up the value, or given the value and look up the key? I would be happy for an API that looks something like this:
#lang racket
(define my-map (bidirectional-hash '(key1 val1) '(key2 val2)))
(bidirectional-hash-ref my-map 'key 'key1) ; => val1
(bidirectional-hash-ref my-map 'val 'val2) ; => key2
Where the symbols key
and val
tell the hash map that it's being given a val, and looking for a key, or given a key, and looking for a val. In both cases, I want this to be done in constant O(1) time.
I know you can accomplish this by using two hash tables that are inverted from one another, but I would like a structure that is built into Racket (or an existing library).
(bidirectional-hash-ref my-map 'key 'key1)
, right? – Danille