Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zipper like data structure with more than one cursor

Tags:

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.

like image 338
Peter Jankuliak Avatar asked Aug 08 '10 23:08

Peter Jankuliak


1 Answers

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

like image 116
Don Stewart Avatar answered Oct 01 '22 18:10

Don Stewart