开发者

Data structure for when key and value are equally "important"

So, this is probably a stupid question, but I have a mapping of unique IDs to unique values. Sometimes I want the value for a certain ID, sometimes I want to know the ID of a certain value. I search more than I modify the collection. I'm wondering if there's a special datastructure that makes sense here, or if I should 开发者_如何学Pythonjust maintain two copies of the collection (which is never super large)--one keyed by ID, and one keyed by value.

Thanks!


Maintaining two copies of the collection is the canonical solution.

Note that the two directions can use different collection types if appropriate (e.g. hash tables with different hash functions, a hash table and a balanced tree, etc.).


Something like Google's BiMap.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜