Static Constructors

BCS ao at pathlink.com
Sat Oct 4 13:48:05 PDT 2008


Reply to Denis,

> 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;
> }

The added speed from array copy optimizations might overcome the cost of 
alocing. OTOH use memmove and you will do even better.




More information about the Digitalmars-d-learn mailing list