[Issue 5224] New: std.algorithm.remove!(SwapStrategy.unstable) doesn't work

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 16 11:53:59 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=5224

           Summary: std.algorithm.remove!(SwapStrategy.unstable) doesn't
                    work
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: ruzicic.aleksandar at gmail.com


--- Comment #0 from Aleksandar Ruzicic <ruzicic.aleksandar at gmail.com> 2010-11-16 20:52:45 CET ---
Example code below shows that there is a bug in remove() using unstable
SwapStrategy - it removes wrong element from an array.
Using default swap strategy works as expected.
-----------------------------------------------------------
module test;

import std.algorithm;

import std.stdio, std.algorithm;

void main(string[] args) {

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

    print("initial", a);

    auto i = a.indexOf(3);

    writefln("indexOf(3) = %s", i);

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

    /*
        Tested with DMD v2.049 under Windows 7:
        > dmd -run test.d
        initial: 1 2 3 4 (length = 4)
        indexOf(3) = 2
        remove!(SwapStrategy.unstable): 4 2 3 (length = 3)

                it removed element at index 0 rather than element at index 2
     */
}

void print(string name, int[] array) {
    writef("%s: ", name);
    foreach (elem; array) {
        writef("%s ", elem);
    }
    writefln("(length = %s)", array.length);
}
-----------------------------------------------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list