std.algorithm.remove using SwapStrategy.unstable doesn't works

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Nov 16 10:03:05 PST 2010


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


More information about the Digitalmars-d mailing list