Re: Looking for a design pattern
On Aug 12, 1:02 am, Jerry Coffin <jerryvcof...@yahoo.com> wrote:
In article <62de703e-3c2b-42d9-abaa-d5b3845f3e21
@v36g2000yqv.googlegroups.com>, michael.dou...@free.fr says...
[ ... ]
That has the sake of simplicity but if you maintain a list
of modification (like rope or another list structure with
modifications stored in a scratch buffer) until next write
to file then insertion and deletion seems cheap to me.
I have never tried to write a text editor but this approach
appeals me more than gap buffer.
I can't quite understand why.
There's a quality rope class available on the internet. I don't
know of a readily available gap buffer class.
Offhand, I can only think of one operation (pasting a large
chunk of text) that it _might_ make faster (if you could just
link in the pasted block instead of copying it). In general,
it sounds to me like a lose-lose situation -- slower
operation, and more complex implementation.
Implementing a gap buffer isn't that trivial, either.
The only of those that's of any concern at all is moving
between marks. To move between marks, you copy all the text
between the marks from one side of the gap to the other. With
marks more than a few tens of megabytes apart, it's going to
be difficult to implement that in the 100 ms (or so) to be
perceived as "instant". A couple hundred ms isn't too
terrible, but much beyond that can start to bother the user.
Just out of curiousity, I ran a couple of tests on my machine,
with a buffer containing 10 million characters, over a total
buffer size of 12,5 million characters. I get the following
Move the cursor from the position 10 to 10 before the end, and
back, alternately, in a gap buffer:
11.2 ms per move with char
28,0 ms per move with wchar_t
Insert or delete at the position 10, alternatively ("flat"
6.9 ms per operation with char
16.8 ms per operation with wchar_t
Those aren't what I'd consider prohibitive times. And the
simplest implementation is clearly the flat buffer, which is in
practice probably faster than the gap buffer for the most
frequent operations, and acceptably fast for the least frequent.
(Admittedly, this is a machine which was installed only a few
weeks ago: A PC running under Ubuntu 9.04 (Linux 2.6), with
3.2GB real memory and four Intel Xeon processor cores at
The gap is always at the cursor, from the time the file is
And what do you do if there are several cursors? (A frequent
case, at least in the code I work on.)
In the end editing/word processing is interactive -- a (very)
soft real-time process. In most cases, what matters is fast
_enough_ response; faster doesn't hurt, but rarely matters
Agreed. And since just using an std::vector in the most obvious
manner seems fast enough, and is surely the simplest solution...
James Kanze (GABI Software) email:email@example.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34