7

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).

dimo414
  • 47,227
  • 18
  • 148
  • 244
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100

1 Answers1

0

As far as I know, there is nothing like that. But you can use one hash table and put (K,V) and (V,K) and use it normally.

KIM Taegyoon
  • 1,917
  • 21
  • 18