Bidirectional HashMap in Racket
Asked Answered
A

1

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

Alphitomancy answered 18/2, 2016 at 2:35 Comment(1)
You probably mean (bidirectional-hash-ref my-map 'key 'key1), right?Danille
O
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.

Outline answered 5/2, 2020 at 4:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.