Best data structure for two way mapping
Asked Answered
B

1

12

I want a data structure that maps from key to object and vice-versa(unlike HashMaps that map only in a single direction.) An idea could be to store the HashMap within itself for reverse look-up, but it will be an inefficient approach.

What would be the best implementation for two-way mapping?

Binomial answered 20/2, 2014 at 16:29 Comment(2)
Take a look at BiMapKkt
#13315405Buehler
B
11

Simplest idea: wrapper class which contains 2 maps, second with swapped keys/values. You will keep O(1) complexity and will use only slightly more memory since you will (probably) keep there reference to object.

Bounty answered 20/2, 2014 at 16:37 Comment(6)
I mentioned this idea in question itself, but I guess there might be better implementations in terms of efficiency.Binomial
Why do you think it will be inefficient? You will keep O(1) complexity.Bounty
@TanaySoni Start simple and only look for inefficiencies when you really need to. Do you have any metrics that would signal this approach as a potential bottleneck in your system?Rodent
I'm trying to use mapping for pixels in a image processing applications. Considering the large data set( a typical image has millions of pixels), I am looking for a rather efficient approach.Binomial
@ProblemFactory: Is BitMap implemented the same way?Binomial
I believe you meant BiMap, then "probably" yes, you can download source and check it by yourself.Bounty

© 2022 - 2024 — McMap. All rights reserved.