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.
Oleg's work on "concurrent" zippers via delimited continuations is the main reference.
See this paper . I seem to recall reading somewhere that the second derivative has two holes, which is probably what you want.
Related
Let use as example standard Binary Search Tree, its nodes having an encapsulated value and pointers to the left and right children and its parent. Its primary methods are addNode(), removeNode() and searchNode().
I want to understand how to make my data structure safe. I however do not know much about thread-safety and want to ask if what I am thinking is correct or not.
My first problem is one of decision: what I want to happen when I have concurrent methods? Let us say I am using my BST to manage a map in a game: the position of the character of a player is represented by an array of doubles, I store the player's positions in my BST in lexicographic order. One player destroyed a target (and his thread want to remove it from my map), but another player is initiating an attack and needs a list of all possible targets inside a certain distance from him, including the target just removed (and his thread want to execute a search that will include the target). How I manage the situation? What about other situations?
My second problem, (I think) my biggest, is one of actuation: how I make happen what I want to happen. I read about things like atomic-operations, linearity, obstruction-free, lock-free, wait-free. I never found out in a single place a simple definition for each, the advantages of that strategy, the disadvantages. What are those things? What am I doing if I am locking the nodes, or if i am locking the pointers to other nodes, or the values encapsulates in the node?
Am I understanding correctly the problematics of thread-safety? Do you have suggestions on problems I should think about, or on written papers and pdfs I should read in order to better understand the problems and their many solutions?
I want to create a tree in Fortran (90) like the one in this picture:
The idea is that I would then be able to follow a path through the tree starting from the root in the following way. At each node perform a check with the value stored there: passing the check move to the left-most child, not passing or having reached a leaf-node move the highest node that the traversal hasn't been to yet. Here is an example of a possible traversal (green indicating passing the test and red not passing):
Importantly, not every node is reached (the ones in black) which is the point of the procedure actually.
So, I think I need is a subroutine that would insert nodes in the tree, in order to build it and another that would allow me to follow paths of the kind described above.
My question is, is this possible? Does this data structure have a name?
Granted that I have virtually no experience with building data structures of this kind, Google has not been much help. An example code would be great but I would be happy to just be referred to some reading where I could learn this.
Recently I was asked this question in an interview. The exact question was
What data structures will you use to implement a text editor. Size of editor can be changed and you also need to save the styling information for all the text like italic, bold etc ?
At that point of time, I tried to convince him using many different approaches like stack, Doubly Linked list and all.
From that point of time,This question is bugging me.
It looks like they'd like to know if you were aware of the flyweight pattern and how to use it correctly.
A text editor is a common example while describing that pattern.
Maybe your interviewer was a lover of the GOF book. :-)
In addition to the previous answers, I would like to add that in order to get to the data structures, you need first to know your design - otherwise the options will be too broad selected.
As example let's assume that you'll need an editing functionality. Here the State and Memento design patterns will be a good fit. Very suitable structure will be the Cord, since it's
composed of smaller strings that is used for efficiently storing and manipulating a very long string.
In our case the text editing program
may use a rope to represent the text being edited, so that operations such as insertion, deletion, and random access can be done efficiently.
An open-ended question like this is designed more to see if you can think cogently about making a design that hangs together well, rather than having one, specific answer.
One specialized answer to the question is to use DOM/XML ("Document Object Model"). Markup "languages" are intended to solve this exact problem. You could store the data for the editor in a DOM. One of the advantages of using a DOM is that there are libraries like Xerces that have extensive support for building and managing DOMs, so a lot of your work is done for you. It is possible the interviewer intended this to be the ideal answer.
A more general answer is that any nested sequence structure can be used. The text can be seen as a sequence of strings. Each elment of the sequence, like rows in a database, can have multiple attributes (font type, font size, italic, bold, strikethrough, etc). Nesting (hierarchy) is useful because the document might have structure such as chapters, sections, paragraphs. For example, if a paragraph has its own styling (indent), then it may need to have its own level. So you have something like this:
Document
Chapter
Paragraph
Text
To implement this, you would use a tree and each node of the tree would have multiple attributes. You would require different kinds of nodes (Chapter nodes, Paragraph nodes, etc). So, for example, a document like a paper would have multiple Section nodes and a Notes node inside a Document node, but a book-like document might have Chapter nodes inside a document node. The advantage of this approach is that it is more specific and hand-tailored to the problem than using a DOM, which is a more flexible approach.
You could also combine the two approaches, using a DOM as your base structure and the hierarchical structure described above as your DOM implementation.
(Note: in the future you should post questions like this to https://softwareengineering.stackexchange.com/)
What data structure does Clojure use to implement its vector type?
I ask because they have some interesting complexity properties. It is cheap (O(log32(N))) to index in to them, and you can get a new copy with any item changed cheaply.
This would lead me to think that it is based on (really wide) tree, but that wouldn't explain why it is cheap to add to one end, but not the other. You also can't cheaply insert or delete elements in the middle of a vector.
Yes, they are wide trees. http://blog.higher-order.net/2009/02/01/understanding-clojures-persistentvector-implementation.html and http://hypirion.com/musings/understanding-persistent-vector-pt-1 are two article series describing in more detail how they work.
I have a bunch of nodes that may have dependencies, for example A, B, C, with connections A <- B, and B <-C. I would like to lay them out in a list (listview/treeview in a gui), and draw a nice diagram showing the relations in one column. I am thinking of something like what some git tools give you.
(See this thread for more examples).
I managed to sketch my own algorithm for this, but I'm not sure I got all corner cases. This seems to be a Solved Problem, so I thought I'd ask here for any standard algorithm. My requirements are:
Lines can leave and arrive at each row.
Lines may pass by a row.
The rows have a natural order. For now, the dependencies only go in one direction (later lines may depend on previous ones), but I'd like to drop that requirement if possible. (My made up algorithm doesn't allow that.)
I don't need the lines to merge as in the above image. If several lines arrive at or leave from the same point, they may merge for cosmetic reasons. I don't want to merge a passing with an arrival though, as in line 3 in above image. (So there would be two passings, and one end point there.)
Again, for cosmetic reasons, the algorithm may "compactify" the tree, and bend the lines to save space. But it would also suffice if it only had straight lines.
I want to start with the list of dependencies, and get out instructions what to draw in each cell to create the tree.
Any references / code examples for such an algorithm? Of course there's the source of various git clients, but they do things slightly differently to what I'm looking for (I have no merges).