[Issue 8721] New: std.algorithm.remove problem

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Sep 24 17:12:19 PDT 2012


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

           Summary: std.algorithm.remove problem
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2012-09-24 17:13:09 PDT ---
This program works correctly, with output ABCEFG:


import std.stdio: writeln;
import std.algorithm: remove, SwapStrategy;
void main() {
    dchar[] data = "ABCDEFG"d.dup;
    data = remove!(SwapStrategy.stable)(data, 3);
    writeln(data);
}



But this shows a wrong output GBCDEF:

import std.stdio: writeln;
import std.algorithm: remove, SwapStrategy;
void main() {
    dchar[] data = "ABCDEFG"d.dup;
    data = remove!(SwapStrategy.unstable)(data, 3);
    writeln(data);
}


"unstable" means that the output order of the items is unspecified, but when I
give a single offset to remove from the array (that is by far the most common
use case for the remove() function, so maybe it's worth having a very efficient
overload for such case), I'd like it to just move the last item to the given
index position, producing: ABCGEF (and I'd this behavour to be written in the
docs of the remove() function, so programmers can rely on it).

Such semantics is fast and makes the remove() function more handy because most
times programmers use this strategy to manually remove one item from an array
when keeping the order is not important. So such semantics allows to use
remove() as drop-in replacement for that common manually written code.


This is an use case where this semantics is necessary, despite it's not needed
to keep the order of the items:


foreach_reverse (ref x; items)
    foreach_reverse (ref y; items)
        if (&x != &y && x.isIncluded(y)) {
            x = items[$ - 1];
            items.length--;
            break;
        }

-- 
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