Static Constructors

Denis Koroskin 2korden at gmail.com
Sat Oct 4 07:40:54 PDT 2008


On Sat, 04 Oct 2008 16:34:56 +0400, Saaa <empty at needmail.com> wrote:

>
>>>
>>>> deleting is even more annoying.
>>>>
>>> You don't have to delete anything.  This is what the GC is for.
>>>
>>
>> delete could be "remove element 42 from an array of 54 resulting an an
>> array of 53"
>>
>>  data = data[0..42] ~ data[43..$];
>> or
>>  data[42..$-1] = data[43..$].dup; // in-place sortof
>>  data.length = data.length-1;
>>
>
> Yes, like that :)
> What is the difference between those two? I mean, what do you mean by
> in-place.
>
>

First one allocates new memory block.
Second one attempts to erase an element in-place. Dupping, however,  
allocates new memory, too.
The best solution would be as follows:

void eraseNth(ref T[] data, int n) {
     data[n] = data[$-1];
     data.length = data.length - 1;
}


More information about the Digitalmars-d-learn mailing list