Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data structure request: Generic Rope #151

Open
nloum opened this issue Nov 3, 2020 · 3 comments
Open

Data structure request: Generic Rope #151

nloum opened this issue Nov 3, 2020 · 3 comments
Labels
data structure request Suggest a new data structure for this project

Comments

@nloum
Copy link

nloum commented Nov 3, 2020

Is your data structure request related to a computational problem? Please describe.
I'm working on a Conflict-Free Replicated Data Type (CRDT) for strings, and I would like to use a rope-like structure like what is described here: http://archagon.net/blog/2018/03/24/data-laced-with-history/#representing-non-string-objects.

Describe the solution you'd like
The rope data type is specific to strings, but I would like a generic version of it where the item type is generic. Specifically, I'd like to have two generic Rope implementations, one where you can specify just the key and the other where you can specify the key and the value. These could be internally implemented by RedBlackTrees, to keep the tree balanced. (I don't think rope dictates a particular method of keeping the tree balanced.)

I would also like a virtual method for reindexing nodes that lets the user override the Rope class to index more than just the number of characters. This would let you skip not just a certain number of items when you want to iterate starting at an offset, but it could also let you skip a certain number of pages of text, or lines of text. That means there would have to be arbitrary properties available in the nodes, so I think that might have to be another generic parameter. Of course, there can be one class that only counts items and doesn't require a generic parameter for the per-node indexing properties.

Describe alternatives you've considered
I've tried to use an ordered dictionary, but the problem with that is you can't index the dictionary in a way that lets you easily skip an arbitrary number of items in the list, which is important in rope structures. E.g., if I had an ordered dictionary of chars and I wanted to display page 20 of the string (ropes are good for really long strings, they are often used in text editors), to get a list of the characters starting on page 20 I would have to iterate all the 19 pages prior to page 20.

With ropes, the internal structure is a binary tree, and each node in the tree has a field that records how many items there are on the left child of the node. This makes it easy to start iterating nodes at a certain offset without iterating all the nodes leading up to that offset.

Additional context
Some good information on ropes: https://medium.com/underrated-data-structures-and-algorithms/rope-data-structure-e623d7862137

@nloum nloum added the data structure request Suggest a new data structure for this project label Nov 3, 2020
@nloum
Copy link
Author

nloum commented Nov 3, 2020

I would be happy to implement this myself, but I wanted to open this issue first.

@github-actions
Copy link

github-actions bot commented Nov 3, 2020

Thanks for supporting the development of C# Algorithms with your first issue! We look forward to handling it.

@aalhour
Copy link
Owner

aalhour commented Dec 22, 2020

Hi @nloum, would be great to have this data structure, can you open a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data structure request Suggest a new data structure for this project
Projects
None yet
Development

No branches or pull requests

2 participants