October 05, 2008
On Sat, 4 Oct 2008 14:02:43 -0400, "Steven Schveighoffer" <schveiguy@yahoo.com> wrote:

>
>"Saaa" <empty@needmail.com> wrote in message news:gc8314$24e0$1@digitalmars.com...
>>
>>>
>>> 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;
>>> }
>>
>> Yay, thats how I do it :)
>
>If you want the memory removed as soon as possible, you should zero out the last element, otherwise, the GC will still think the element is being pointed to:
>
>data[n] = data[$-1];
>data[$-1] = null;
>data.length = data.length - 1;
>
>-Steve
>

The following code outputs [0 4 2 3], is this a bug in D2?

int n = 1;
int[] data = [0,1,2,3,4].dup;
writeln(data); // [0 1 2 3 4]
data[n] = data[$-1];
data[$-1] = 0;
data.length = data.length - 1;
writeln(data); // [0 4 2 3]


Gide
October 05, 2008
On Sun, 05 Oct 2008 21:26:58 +0400, Gide Nwawudu <gide@btinternet.com> wrote:

> On Sat, 4 Oct 2008 14:02:43 -0400, "Steven Schveighoffer"
> <schveiguy@yahoo.com> wrote:
>
>>
>> "Saaa" <empty@needmail.com> wrote in message
>> news:gc8314$24e0$1@digitalmars.com...
>>>
>>>>
>>>> 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;
>>>> }
>>>
>>> Yay, thats how I do it :)
>>
>> If you want the memory removed as soon as possible, you should zero out the
>> last element, otherwise, the GC will still think the element is being
>> pointed to:
>>
>> data[n] = data[$-1];
>> data[$-1] = null;
>> data.length = data.length - 1;
>>
>> -Steve
>>
>
> The following code outputs [0 4 2 3], is this a bug in D2?
>
> int n = 1;
> int[] data = [0,1,2,3,4].dup;
> writeln(data); // [0 1 2 3 4]
> data[n] = data[$-1];
> data[$-1] = 0;
> data.length = data.length - 1;
> writeln(data); // [0 4 2 3]
>
>
> Gide

Errr... And what is an expected output?
I believe the actual output is correct.
October 05, 2008
On Sun, Oct 5, 2008 at 1:26 PM, Gide Nwawudu <gide@btinternet.com> wrote:

> The following code outputs [0 4 2 3], is this a bug in D2?
>
> int n = 1;
> int[] data = [0,1,2,3,4].dup;
> writeln(data); // [0 1 2 3 4]
> data[n] = data[$-1];

now data is [0 4 2 3 4]

> data[$-1] = 0;

now data is [0 4 2 3 0]

> data.length = data.length - 1;

now data is [0 4 2 3]

> writeln(data); // [0 4 2 3]

And that's right.

What's the issue?
October 05, 2008
On Sun, 5 Oct 2008 15:31:38 -0400, "Jarrett Billingsley" <jarrett.billingsley@gmail.com> wrote:

>On Sun, Oct 5, 2008 at 1:26 PM, Gide Nwawudu <gide@btinternet.com> wrote:
>
>> The following code outputs [0 4 2 3], is this a bug in D2?
>>
>> int n = 1;
>> int[] data = [0,1,2,3,4].dup;
>> writeln(data); // [0 1 2 3 4]
>> data[n] = data[$-1];
>
>now data is [0 4 2 3 4]
>
>> data[$-1] = 0;
>
>now data is [0 4 2 3 0]
>
>> data.length = data.length - 1;
>
>now data is [0 4 2 3]
>
>> writeln(data); // [0 4 2 3]
>
>And that's right.
>
>What's the issue?

My main issue was with eraseNth re-ordering elements, but if moving the end element over the nth is standard, then I suppose it's ok. I would have expected a 'stable' version, i.e. [0 2 3 4] as the result.

Gide
October 05, 2008
On Sun, Oct 5, 2008 at 5:32 PM, Gide Nwawudu <gide@btinternet.com> wrote:
> On Sun, 5 Oct 2008 15:31:38 -0400, "Jarrett Billingsley" <jarrett.billingsley@gmail.com> wrote:
>
>>On Sun, Oct 5, 2008 at 1:26 PM, Gide Nwawudu <gide@btinternet.com> wrote:
>>
>>> The following code outputs [0 4 2 3], is this a bug in D2?
>>>
>>> int n = 1;
>>> int[] data = [0,1,2,3,4].dup;
>>> writeln(data); // [0 1 2 3 4]
>>> data[n] = data[$-1];
>>
>>now data is [0 4 2 3 4]
>>
>>> data[$-1] = 0;
>>
>>now data is [0 4 2 3 0]
>>
>>> data.length = data.length - 1;
>>
>>now data is [0 4 2 3]
>>
>>> writeln(data); // [0 4 2 3]
>>
>>And that's right.
>>
>>What's the issue?
>
> My main issue was with eraseNth re-ordering elements, but if moving the end element over the nth is standard, then I suppose it's ok. I would have expected a 'stable' version, i.e. [0 2 3 4] as the result.
>
> Gide

It's the faster method is all.  If you want a stable remove, you can either copy everything after the removed element down a slot, or you can create a new array with "a[0 .. n] ~ a[n + 1 .. $]".  The former requires less memory and less strain on the GC, and unless you have a really fancy compiler that will autoparallelize the copy from the old array into the new, the second solution will be no faster at copying the data.
1 2 3 4
Next ›   Last »