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

Aleksandar Ružičić ruzicic.aleksandar at gmail.com
Tue Nov 16 11:29:55 PST 2010


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 at erdani.org> wrote:
> 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