Text editing [Was: Re: #line decoder]
Sergey Gromov
snake.scaly at gmail.com
Fri Sep 26 18:21:49 PDT 2008
In article <MPG.2347c0e378c6cc6a98970c at news.digitalmars.com>,
snake.scaly at gmail.com says...
> module rope;
> class Rope(T)
> {
> typeof(this) opCatAssign(T el)
> {
> if (pool >= current.length)
> {
> chunks ~= current;
> current = new T[current.length * 2];
> pool = 0;
> }
> current[pool++] = el;
> return this;
> }
> T[] get()
> {
> T[] result;
> foreach (c; chunks)
> result ~= c;
> return result ~ current[0 .. pool];
> }
> this()
> {
> current = new T[16];
> pool = 0;
> }
> private
> {
> T[][] chunks;
> T[] current;
> size_t pool;
> }
> }
The simpler, the better: use Array instead of Rope and get 2.36s:
module array;
class Array(T)
{
this()
{
data = new T[16];
}
typeof(this) opCatAssign(T el)
{
if (pos == data.length)
data.length = data.length * 2;
data[pos++] = el;
return this;
}
T[] get()
{
return data[0 .. pos];
}
private
{
T[] data;
size_t pos;
}
}
More information about the Digitalmars-d-announce
mailing list