Ropes (concatenation trees) for strings in D ?

Messenger via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 14 02:16:52 PDT 2014


On Thursday, 14 August 2014 at 05:57:18 UTC, Carl Sturtivant 
wrote:
>
> This is the idea I mean.
> http://citeseer.ist.psu.edu/viewdoc/download?doi=10.1.1.14.9450&rep=rep1&type=pdf
> Here's a C++ implementation supported I think by gcc.
> http://www.sgi.com/tech/stl/Rope.html
>
> Is there a D implementation of a rope out there somewhere?
>
> How is unicode handled? Just by using dchar?

Hmm. Appender?
> http://dlang.org/phobos/std_array.html#.Appender

     import std.array;
     import std.range;

     enum arrayLength = 1_000_000;

     void main() {
         auto writer = appender!string;
         auto xes = new char[](arrayLength);  // dynamic on heap

         xes[] = 'x';
         assert(xes[0] == 'x');
         assert(xes[100] == 'x');
         assert(xes[1000] == 'x');

         writer.reserve((2 * arrayLength) + 3);  // optional
         writer.put(xes);  // or writer ~= xes;
         writer.put("abc");
         writer.put(xes);

         // normal slicing op
         assert(writer.data[arrayLength..(arrayLength+3)] == 
"abc");

         // .array allocates a new array but not sure how else to 
do it
         
assert(writer.data.retro.array[arrayLength..(arrayLength+3)] == 
"cba");
     }


More information about the Digitalmars-d-learn mailing list