Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Rope (data structure)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Data structure for storing strings}} [[File:Vector Rope example.svg|right|x200px|thumb|A simple rope built on the string of "Hello_my_name_is_Simon".]] In [[computer programming]], a '''rope''', or '''cord''', is a [[data structure]] composed of smaller [[String (computer science)|strings]] that is used to efficiently store and manipulate longer strings or entire texts. For example, a [[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.<ref name="Boehm"> {{cite journal | last = Boehm | first = Hans-J |author2=Atkinson, Russ |author3=Plass, Michael | title = Ropes: an Alternative to Strings | journal = Software: Practice and Experience | volume = 25 | issue = 12 | pages = 1315β1330 | publisher = John Wiley & Sons, Inc. | location = New York, NY, USA | date = December 1995 | url = https://www.cs.tufts.edu/comp/150FP/archive/hans-boehm/ropes.pdf | doi = 10.1002/spe.4380251203 | archive-url = https://web.archive.org/web/20200308005351/https://citeseer.ist.psu.edu/viewdoc/download?doi=10.1.1.14.9450&rep=rep1&type=pdf | archive-date = 2020-03-08 | url-status = live }}</ref> ==Description== A rope is a type of [[binary tree]] where each leaf (end node) holds a string of manageable size and length (also known as a ''weight''), and each node further up the tree holds the sum of the lengths of all the leaves in its left [[subtree]]. A node with two children thus divides the whole string into two parts: the left subtree stores the first part of the string, the right subtree stores the second part of the string, and a node's weight is the length of the first part. For rope operations, the strings stored in nodes are assumed to be constant [[immutable object]]s in the typical nondestructive case, allowing for some [[copy-on-write]] behavior. Leaf nodes are usually implemented as [[string (computer science)|basic fixed-length strings]] with a [[reference counting|reference count]] attached for deallocation when no longer needed, although other [[Garbage collection (computer science)|garbage collection]] methods can be used as well. ==Operations== In the following definitions, ''N'' is the length of the rope, that is, the weight of the root node. === Collect leaves === : ''Definition:'' Create a stack ''S'' and a list ''L''. Traverse down the left-most spine of the tree until you reach a leaf l', adding each node ''n'' to ''S''. Add l' to ''L''. The parent of l' (''p'') is at the top of the stack. Repeat the procedure for p's right subtree. <syntaxhighlight lang="java"> final class InOrderRopeIterator implements Iterator<RopeLike> { private final Deque<RopeLike> stack; InOrderRopeIterator(@NonNull RopeLike root) { stack = new ArrayDeque<>(); var c = root; while (c != null) { stack.push(c); c = c.getLeft(); } } @Override public boolean hasNext() { return stack.size() > 0; } @Override public RopeLike next() { val result = stack.pop(); if (!stack.isEmpty()) { var parent = stack.pop(); var right = parent.getRight(); if (right != null) { stack.push(right); var cleft = right.getLeft(); while (cleft != null) { stack.push(cleft); cleft = cleft.getLeft(); } } } return result; } } </syntaxhighlight> === Rebalance === : ''Definition:'' Collect the set of leaves ''L'' and rebuild the tree from the bottom-up. <syntaxhighlight lang="java"> static boolean isBalanced(RopeLike r) { val depth = r.depth(); if (depth >= FIBONACCI_SEQUENCE.length - 2) { return false; } return FIBONACCI_SEQUENCE[depth + 2] <= r.weight(); } static RopeLike rebalance(RopeLike r) { if (!isBalanced(r)) { val leaves = Ropes.collectLeaves(r); return merge(leaves, 0, leaves.size()); } return r; } static RopeLike merge(List<RopeLike> leaves) { return merge(leaves, 0, leaves.size()); } static RopeLike merge(List<RopeLike> leaves, int start, int end) { int range = end - start; if (range == 1) { return leaves.get(start); } if (range == 2) { return new RopeLikeTree(leaves.get(start), leaves.get(start + 1)); } int mid = start + (range / 2); return new RopeLikeTree(merge(leaves, start, mid), merge(leaves, mid, end)); } </syntaxhighlight> === Insert === : ''Definition:'' <code>Insert(i, Sβ)</code>: insert the string ''Sβ'' beginning at position ''i'' in the string ''s'', to form a new string {{math|''C''<sub>1</sub>, ..., ''C<sub>i</sub>'', ''S''', ''C''<sub>''i'' + 1</sub>, ..., ''C<sub>m</sub>''}}. : ''Time complexity:'' {{tmath|O(\log N)}}. This operation can be done by a <code>Split()</code> and two <code>Concat()</code> operations. The cost is the sum of the three. <syntaxhighlight lang="java"> public Rope insert(int idx, CharSequence sequence) { if (idx == 0) { return prepend(sequence); } if (idx == length()) { return append(sequence); } val lhs = base.split(idx); return new Rope(Ropes.concat(lhs.fst.append(sequence), lhs.snd)); } </syntaxhighlight> === Index === [[File:Vector Rope index.svg|right|x200px|thumb|Figure 2.1: Example of index lookup on a rope.]] : ''Definition:'' <code>Index(i)</code>: return the character at position ''i'' : ''Time complexity:'' {{tmath|O(\log N)}} To retrieve the ''i''-th character, we begin a [[Recursion|recursive]] search from the root node: <syntaxhighlight lang="java"> @Override public int indexOf(char ch, int startIndex) { if (startIndex > weight) { return right.indexOf(ch, startIndex - weight); } return left.indexOf(ch, startIndex); } </syntaxhighlight> For example, to find the character at {{code|1=i=10}} in Figure 2.1 shown on the right, start at the root node (A), find that 22 is greater than 10 and there is a left child, so go to the left child (B). 9 is less than 10, so subtract 9 from 10 (leaving {{code|1=i=1}}) and go to the right child (D). Then because 6 is greater than 1 and there's a left child, go to the left child (G). 2 is greater than 1 and there's a left child, so go to the left child again (J). Finally 2 is greater than 1 but there is no left child, so the character at index 1 of the short string "na" (ie "n") is the answer. (1-based index) === Concat === [[File:Vector Rope concat.svg|right|x200px|thumb|Figure 2.2: Concatenating two child ropes into a single rope.]] : ''Definition:'' <code>Concat(S1, S2)</code>: concatenate two ropes, ''S''<sub>1</sub> and ''S''<sub>2</sub>, into a single rope. : ''Time complexity:'' {{tmath|O(1)}} (or {{tmath|O(\log N)}} time to compute the root weight) A concatenation can be performed simply by creating a new root node with {{mono|1=left = S1}} and {{mono|1=right = S2}}, which is constant time. The weight of the parent node is set to the length of the left child ''S''<sub>1</sub>, which would take {{tmath|O(\log N)}} time, if the tree is balanced. As most rope operations require balanced trees, the tree may need to be re-balanced after concatenation. === Split === [[File:Vector Rope split.svg|right|x600px|thumb|Figure 2.3: Splitting a rope in half.]] : ''Definition:'' <code>Split (i, S)</code>: split the string ''S'' into two new strings ''S''<sub>1</sub> and ''S''<sub>2</sub>, {{math|1=''S''<sub>1</sub> = ''C''<sub>1</sub>, ..., ''C<sub>i</sub>''}} and {{math|1=''S''<sub>2</sub> = ''C''<sub>''i'' + 1</sub>, ..., ''C<sub>m</sub>''}}. : ''Time complexity:'' {{tmath|O(\log N)}} There are two cases that must be dealt with: # The split point is at the end of a string (i.e. after the last character of a leaf node) # The split point is in the middle of a string. The second case reduces to the first by splitting the string at the split point to create two new leaf nodes, then creating a new node that is the parent of the two component strings. For example, to split the 22-character rope pictured in Figure 2.3 into two equal component ropes of length 11, query the 12th character to locate the node ''K'' at the bottom level. Remove the link between ''K'' and ''G''. Go to the parent of ''G'' and subtract the weight of ''K'' from the weight of ''D''. Travel up the tree and remove any right links to subtrees covering characters past position 11, subtracting the weight of ''K'' from their parent nodes (only node ''D'' and ''A'', in this case). Finally, build up the newly orphaned nodes ''K'' and ''H'' by concatenating them together and creating a new parent ''P'' with weight equal to the length of the left node ''K''. As most rope operations require balanced trees, the tree may need to be re-balanced after splitting. <syntaxhighlight lang="java"> public Pair<RopeLike, RopeLike> split(int index) { if (index < weight) { val split = left.split(index); return Pair.of(rebalance(split.fst), rebalance(new RopeLikeTree(split.snd, right))); } else if (index > weight) { val split = right.split(index - weight); return Pair.of(rebalance(new RopeLikeTree(left, split.fst)), rebalance(split.snd)); } else { return Pair.of(left, right); } } </syntaxhighlight> === Delete === : ''Definition:'' <code>Delete(i, j)</code>: delete the substring {{math|''C<sub>i</sub>'', β¦, ''C''<sub>''i'' + ''j'' β 1</sub>}}, from ''s'' to form a new string {{math|''C''<sub>1</sub>, β¦, ''C''<sub>''i'' β 1</sub>, ''C''<sub>''i'' + ''j''</sub>, β¦, ''C<sub>m</sub>''}}. : ''Time complexity:'' {{tmath|O(\log N)}}. This operation can be done by two <code>Split()</code> and one <code>Concat()</code> operation. First, split the rope in three, divided by ''i''-th and ''i+j''-th character respectively, which extracts the string to delete in a separate node. Then concatenate the other two nodes. <syntaxhighlight lang="java"> @Override public RopeLike delete(int start, int length) { val lhs = split(start); val rhs = split(start + length); return rebalance(new RopeLikeTree(lhs.fst, rhs.snd)); } </syntaxhighlight> === Report === : ''Definition:'' <code>Report(i, j)</code>: output the string {{math|''C<sub>i</sub>'', β¦, ''C''<sub>''i'' + ''j'' β 1</sub>}}. : ''Time complexity:'' {{tmath|O(j + \log N)}} To report the string {{math|''C<sub>i</sub>'', β¦, ''C''<sub>''i'' + ''j'' β 1</sub>}}, find the node ''u'' that contains ''C<sub>i</sub>'' and {{code|1=weight(u) >= j}}, and then traverse ''T'' starting at node ''u''. Output {{math|''C<sub>i</sub>'', β¦, ''C''<sub>''i'' + ''j'' β 1</sub>}} by doing an [[in-order traversal]] of ''T'' starting at node ''u''. ==Comparison with monolithic arrays== {| class="wikitable floatright" |+ Complexity{{citation needed|date=October 2010}} ! Operation !! Rope !! String |- align="center" | Index<ref name="Boehm" /> || {{bad|O(log n)}} || {{yes|O(1)}} |- align="center" | Split<ref name="Boehm" /> || {{bad|O(log n)}} || {{yes|O(1)}} |- align="center" | Concatenate || {{yes|O(1) amortized, O(log n) worst case}}{{Citation needed|date=June 2022|reason=earlier this article claims "A concatenation can be performed simply by ... which is constant time.", or O(1).}} || {{bad|O(n)}} |- align="center" | Iterate over each character<ref name="Boehm" /> | {{okay|O(n)}} || {{okay|O(n)}} |- align="center" | Insert<ref name=":0" />{{Failed verification|date=September 2023}} || {{yes|O(log n)}} || {{bad|O(n)}} |- align="center" | Append<ref name=":0">{{Cite web|url=https://www.sgi.com/tech/stl/ropeimpl.html|title=Rope Implementation Overview|website=www.sgi.com|access-date=2017-03-01 |archive-url=https://web.archive.org/web/20171219030153/https://www.sgi.com/tech/stl/ropeimpl.html |archive-date=2017-12-19 }}</ref>{{Failed verification|date=September 2023}} || {{yes|O(1) amortized, O(log n) worst case}} || {{bad|O(1) amortized, O(n) worst case}} |- align="center" | Delete || {{yes|O(log n)}} || {{bad|O(n)}} |- align="center" | Report || {{bad|O(j + log n)}} || {{yes|O(j)}} |- align="center" | Build || {{okay|O(n)}} || {{okay|O(n)}} |} Advantages: * Ropes enable much faster insertion and deletion of text than monolithic string arrays, on which operations have time complexity O(n). * Ropes do not require O(n) extra memory when operated upon (arrays need that for copying operations). * Ropes do not require large contiguous memory spaces. * If only nondestructive versions of operations are used, rope is a [[persistent data structure]]. For the text editing program example, this leads to an easy support for multiple [[undo]] levels. Disadvantages: * Greater overall space use when not being operated on, mainly to store parent nodes. There is a trade-off between how much of the total memory is such overhead and how long pieces of data are being processed as strings. The strings in example figures above are unrealistically short for modern architectures. The overhead is always O(n), but the constant can be made arbitrarily small. * Increase in time to manage the extra storage * Increased complexity of source code; greater risk of bugs This table compares the ''algorithmic'' traits of string and rope implementations, not their ''raw speed''. Array-based strings have smaller overhead, so (for example) concatenation and split operations are faster on small datasets. However, when array-based strings are used for longer strings, time complexity and memory use for inserting and deleting characters becomes unacceptably large. In contrast, a rope data structure has stable performance regardless of data size. Further, the space complexity for ropes and arrays are both O(n). In summary, ropes are preferable when the data is large and modified often. ==See also== * The [[Cedar (programming language)|Cedar]] programming environment, which used ropes "almost since its inception"<ref name="Boehm"/> * The [[Enfilade (Xanadu)|Model T enfilade]], a similar data structure from the early 1970s * [[Gap buffer]], a data structure commonly used in text editors that allows efficient insertion and deletion operations clustered near the same location * [[Piece table]], another data structure commonly used in text editors ==References== {{one source|date = September 2011}} {{Reflist}} ==External links== {{Commons category}} *[https://github.com/abseil/abseil-cpp/blob/master/absl/strings/cord.h "absl::Cord" implementation of ropes within The Abseil library] *[https://github.com/ivmai/bdwgc/ "C cords" implementation of ropes within the Boehm Garbage Collector library] *[https://web.archive.org/web/20121225183151/http://www.sgi.com/tech/stl/Rope.html SGI C++ specification for ropes] (supported by STLPort and [https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a00223.html libstdc++]) *[https://github.com/thyer/Ropes Ropes] for [[C Sharp (programming language)|C#]] *[https://github.com/Ramarren/ropes ropes] for [[Common Lisp]] *[http://ahmadsoft.org/ropes/ Ropes for Java] *[https://github.com/sunshower-io/sunshower-arcus/tree/master/arcus-lang/src/main/java/io/sunshower/lang/primitives String-Like Ropes for Java] *[https://github.com/component/rope Ropes for JavaScript] *[https://github.com/KenDickey/Limbo-Ropes Ropes] for [[Limbo (programming language)|Limbo]] *[https://nim-lang.org/docs/ropes.html ropes] for [[Nim (programming language)|Nim]] *[https://github.com/Chris00/ocaml-rope Ropes] for [[OCaml]] *[http://sourceforge.net/projects/pyropes/files/?source=navbar pyropes] for [[Python (programming language)|Python]] *[https://github.com/KenDickey/Cuis-Smalltalk-Ropes Ropes] for [[Smalltalk]] *[https://github.com/fweez/SwiftRope SwiftRope] for [[Swift (programming language)|Swift]] *[https://docs.rs/ropey/ "Ropey"] for [[Rust (programming language)|Rust]] *[https://pub.dev/packages/rope/ Rope] for Dart *[https://zed.dev/blog/zed-decoded-rope-sumtree/ Rope & SumTree] in Zed Editor {{Strings |state=collapsed}} {{DEFAULTSORT:Rope Data Structure}} [[Category:Binary trees]] [[Category:String data structures]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Bad
(
edit
)
Template:Citation needed
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:Commons category
(
edit
)
Template:Failed verification
(
edit
)
Template:Math
(
edit
)
Template:Mono
(
edit
)
Template:Okay
(
edit
)
Template:One source
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Strings
(
edit
)
Template:Tmath
(
edit
)
Template:Yes
(
edit
)