Dynamic arrays in D 1.0

Janice Caron caron800 at googlemail.com
Sun May 11 13:10:13 PDT 2008


On 11/05/2008, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
> Janice Caron wrote:

> > To erase the elements from index i to index j in place:
>
>  It can be done more efficiently using memmove to shift contents down. Also
> if you're dropping the tail end, you can just change .length. Cashew's erase
> routine (called "dropRange") does both.

OK, how about this, using std.algorithm.copy:

    array = copy(array[j..$], array[i..$]);
    array = array[0..$+i-j];

The copy is done in-place, like memmove(), but without the danger of
buffer overrun.
You still need the second line to change the length though.

You can even do it ONE LINE, but I have to confess, the code looks
pretty obfuscated.

    array = array[0..$-copy(array[j..$], array[i..$]).length];



More information about the Digitalmars-d mailing list