Is there a bidirectional multimap persistent data structure?

谁说胖子不能爱 提交于 2019-12-01 04:09:51

The simplest way is to use a pair of unidirectional maps. It has some cost, but you won't get much better (you could get a bit better using dedicated binary trees, but you have a huge complexity cost to pay if you have to implement it yourself). In essence, lookups will be just as fast, but addition and deletion will be twice as slow. Which isn't so bad for a logarithmic operation. Another advantage of this technique is that you can use specialized maps types for the key or value type if you have one available. You won't get as much flexibility with a specific generalist data structure.

A different solution is to use a quadtree (instead of considering a NxN relation as a pair of 1xN and Nx1 relations, you see it as a set of elements in the cartesian product (Key*Value) of your types, that is, a spatial plane), but it's not clear to me that the time and memory costs are better than with two maps. I suppose it needs to be tested.

Finally, I there is a mind-blowing non-regular recursive data structure to do that, but I can't find a reference for it in english.

Edit: I just quickly pasted an adapted version of the original code for this mysterious data structure.

Proof by construction: the bimap package for Haskell.

A Bimap is essentially a bijection between subsets of its two argument types

And how is it implemented?

data Bimap a b = MkBimap !(M.Map a b) !(M.Map b a)

As a pair of unidirectional maps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!