FlatlinerDOA.Rope
1.0.0
See the version list below for details.
dotnet add package FlatlinerDOA.Rope --version 1.0.0
NuGet\Install-Package FlatlinerDOA.Rope -Version 1.0.0
<PackageReference Include="FlatlinerDOA.Rope" Version="1.0.0" />
paket add FlatlinerDOA.Rope --version 1.0.0
#r "nuget: FlatlinerDOA.Rope, 1.0.0"
// Install FlatlinerDOA.Rope as a Cake Addin #addin nuget:?package=FlatlinerDOA.Rope&version=1.0.0 // Install FlatlinerDOA.Rope as a Cake Tool #tool nuget:?package=FlatlinerDOA.Rope&version=1.0.0
Rope
C# implementation of a Rope<T> immutable data structure. See the paper Ropes: an Alternative to Strings: h.-j. boehm, r. atkinson and m. plass
A Rope is an immutable sequence built using a b-tree style data structure that is useful for efficiently applying and storing edits, most commonly used with strings, but any list or sequence can be efficiently edited using the Rope data structure.
Where a b-tree has every node in the tree storing a single entry, a rope contains arrays of elements and only subdivides on the edits. The data structure then decides at edit time whether it is optimal to rebalance the tree using the following heuristic: A rope of depth n is considered balanced if its length is at least Fn+2.
Note: This implementation of Rope<T> has a hard-coded upper-bound depth of 46 added to the heuristic from the paper. As this seemed to be optimal for my workloads, your mileage may vary.
Example Usage
// Converting to a Rope<char> doesn't allocate any strings (simply points to the original memory).
Rope<char> text = "My favourite text".ToRope();
// With Rope<T>, splits don't allocate any new strings either.
IEnumerable<Rope<char>> words = text.Split(' ');
// Calling ToString() allocates a new string at the time of conversion.
Console.WriteLine(words.First().ToString());
// Warning: Concatenating a string to a Rope<char> converts to a string (allocating memory).
string text2 = text + " My second favourite text";
// Better: This makes a new rope out of the other two ropes, no string allocations.
Rope<char> text3 = text + " My second favourite text".ToRope();
Comparison with StringBuilder
A comparison could be drawn between a Rope and a StringBuilder as they use a very similar technique for efficient edits.
Feature | Rope<T> | StringBuilder |
---|---|---|
Efficient edits of strings (avoid double allocations) | ✅ | ✅ |
Supports lists of any type | ✅ | ❌ |
Immutable edits | ✅ | ❌ |
Thread safe | ✅ | ❌ |
Allocation free splitting | ✅ | ❌ |
Zero copy concatenation of two strings | ✅ | ❌ |
Allocation free insertion | ✅ | ❌ |
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.