www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.algorithm.remove using SwapStrategy.unstable doesn't works

reply =?UTF-8?B?QWxla3NhbmRhciBSdcW+acSNacSH?= <ruzicic.aleksandar gmail.com> writes:
I'm trying to use remove() from std.algorithm to remove item from an
array and when I tried to use SwapStrategy.unstable (as I don't need
to maintain order of items in array and I want to optimize where ever
I can) I came to surprising results. In the example below one would
expect that item at index 2 (number 3 in example) will be removed but
instead item at index 0 is removed from array.

Is this a bug or I'm missing a point?

-----------------------------------------------------

import std.stdio, std.algorithm;

void main(string[] args) {

        auto a = [1,2,3,4];

	foreach (e; a) writef("%s ", e); writefln("(%s)", a.length); // 1 2 3 4 (4)

	auto i = a.indexOf(3);

	writefln("%s", i);	// 2

	if (i > -1) {
		a = remove!(SwapStrategy.unstable)(a, i);
	}

	foreach (e; a) writef("%s ", e); writefln("(%s)", a.length); // 4 2 3 (3) !?!
}

-----------------------------------------------------

thx in advance,
Aleksandar
Nov 16 2010
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 11/16/10 4:24 AM, Aleksandar Ružičić wrote:
 I'm trying to use remove() from std.algorithm to remove item from an
 array and when I tried to use SwapStrategy.unstable (as I don't need
 to maintain order of items in array and I want to optimize where ever
 I can) I came to surprising results. In the example below one would
 expect that item at index 2 (number 3 in example) will be removed but
 instead item at index 0 is removed from array.

 Is this a bug or I'm missing a point?

 -----------------------------------------------------

 import std.stdio, std.algorithm;

 void main(string[] args) {

          auto a = [1,2,3,4];

 	foreach (e; a) writef("%s ", e); writefln("(%s)", a.length); // 1 2 3 4 (4)

 	auto i = a.indexOf(3);

 	writefln("%s", i);	// 2

 	if (i>  -1) {
 		a = remove!(SwapStrategy.unstable)(a, i);
 	}

 	foreach (e; a) writef("%s ", e); writefln("(%s)", a.length); // 4 2 3 (3) !?!
 }

 -----------------------------------------------------

 thx in advance,
 Aleksandar

Looks like a bug. Could you please file it to bugzilla? Thanks, Andrei
Nov 16 2010
prev sibling next sibling parent =?UTF-8?B?QWxla3NhbmRhciBSdcW+acSNacSH?= <ruzicic.aleksandar gmail.com> writes:
No problem, just to isolate the code and to confirm it's not something
to other parts of my code (but I'm pretty sure it's not).

On Tue, Nov 16, 2010 at 7:03 PM, Andrei Alexandrescu
<SeeWebsiteForEmail erdani.org> wrote:
 On 11/16/10 4:24 AM, Aleksandar Ru=C5=BEi=C4=8Di=C4=87 wrote:
 I'm trying to use remove() from std.algorithm to remove item from an
 array and when I tried to use SwapStrategy.unstable (as I don't need
 to maintain order of items in array and I want to optimize where ever
 I can) I came to surprising results. In the example below one would
 expect that item at index 2 (number 3 in example) will be removed but
 instead item at index 0 is removed from array.

 Is this a bug or I'm missing a point?

 -----------------------------------------------------

 import std.stdio, std.algorithm;

 void main(string[] args) {

 =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto a =3D [1,2,3,4];

 =C2=A0 =C2=A0 =C2=A0 =C2=A0foreach (e; a) writef("%s ", e); writefln("(%=


 3 4 (4)

 =C2=A0 =C2=A0 =C2=A0 =C2=A0auto i =3D a.indexOf(3);

 =C2=A0 =C2=A0 =C2=A0 =C2=A0writefln("%s", i); =C2=A0 =C2=A0 =C2=A0// 2

 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (i> =C2=A0-1) {
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0a =3D remove!(Swa=


 =C2=A0 =C2=A0 =C2=A0 =C2=A0}

 =C2=A0 =C2=A0 =C2=A0 =C2=A0foreach (e; a) writef("%s ", e); writefln("(%=


 3 (3) !?!
 }

 -----------------------------------------------------

 thx in advance,
 Aleksandar

Looks like a bug. Could you please file it to bugzilla? Thanks, Andrei

Nov 16 2010
prev sibling parent =?UTF-8?B?QWxla3NhbmRhciBSdcW+acSNacSH?= <ruzicic.aleksandar gmail.com> writes:
filed as Issue #5224 - http://d.puremagic.com/issues/show_bug.cgi?id=3D5224

regards,
Aleksandar

2010/11/16 Aleksandar Ru=C5=BEi=C4=8Di=C4=87 <ruzicic.aleksandar gmail.com>=
:
 No problem, just to isolate the code and to confirm it's not something
 to other parts of my code (but I'm pretty sure it's not).

 On Tue, Nov 16, 2010 at 7:03 PM, Andrei Alexandrescu
 <SeeWebsiteForEmail erdani.org> wrote:
 On 11/16/10 4:24 AM, Aleksandar Ru=C5=BEi=C4=8Di=C4=87 wrote:
 I'm trying to use remove() from std.algorithm to remove item from an
 array and when I tried to use SwapStrategy.unstable (as I don't need
 to maintain order of items in array and I want to optimize where ever
 I can) I came to surprising results. In the example below one would
 expect that item at index 2 (number 3 in example) will be removed but
 instead item at index 0 is removed from array.

 Is this a bug or I'm missing a point?

 -----------------------------------------------------

 import std.stdio, std.algorithm;

 void main(string[] args) {

 =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto a =3D [1,2,3,4];

 =C2=A0 =C2=A0 =C2=A0 =C2=A0foreach (e; a) writef("%s ", e); writefln("(=



 3 4 (4)

 =C2=A0 =C2=A0 =C2=A0 =C2=A0auto i =3D a.indexOf(3);

 =C2=A0 =C2=A0 =C2=A0 =C2=A0writefln("%s", i); =C2=A0 =C2=A0 =C2=A0// 2

 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (i> =C2=A0-1) {
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0a =3D remove!(Sw=



 =C2=A0 =C2=A0 =C2=A0 =C2=A0}

 =C2=A0 =C2=A0 =C2=A0 =C2=A0foreach (e; a) writef("%s ", e); writefln("(=



 3 (3) !?!
 }

 -----------------------------------------------------

 thx in advance,
 Aleksandar

Looks like a bug. Could you please file it to bugzilla? Thanks, Andrei


Nov 16 2010