Zipper like data structure with more than one cursor

巧了我就是萌 提交于 2019-11-29 00:39:43

问题


The Zipper data structure is great when one wants to traverse a tree and keep the current position, but what data structure one should use if they want to track more then one position?

Let me explain with examples:

  • Someone on the #haskell channel has told me that zippers are used in yi editor to represent the cursor position. This is great, but what if you want to have two cursors. Like if you want to represent a selection, you need to know the beginning and the end of the selection.
  • In the Minotaur example on wikibooks, they use Zipper to represent Minotaur's position inside the labyrinth. If I wanted to add enemy into the labyrinth, representing their position with a Zipper would make as much sense.
  • Last one is actualy from my mini project where it all started: As part of learning Haskell I'm trying to visualize a tree structure using cairo and gth2hs. This has gone well so far but now I would like to select one or more of the nodes and be able to e.g. move them around. Because there can be more then one of the selected nodes I can't just use the Zipper as defined in text books.

There is a trivial (naive?) solution, similar to the one they had used in early versions of XMonad which involves finite maps as explained here.

That is, e.g. in case of my example project, I would store the selected nodes in an indexed map and replace their representation in the main structure with the indices. But this solution has plenty of disadvantages. Like the ones explained in the link above, or say, again in case of my example, unselecting all the nodes would require searching the whole tree.


回答1:


Oleg's work on "concurrent" zippers via delimited continuations is the main reference.




回答2:


See this paper . I seem to recall reading somewhere that the second derivative has two holes, which is probably what you want.



来源:https://stackoverflow.com/questions/3436559/zipper-like-data-structure-with-more-than-one-cursor

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