Bug 8721 – std.algorithm.remove problem

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-09-24T17:13:00Z
Last change time
2014-02-26T15:41:37Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2012-09-24T17:13:09Z
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; }
Comment #1 by msoucy — 2014-02-26T15:41:37Z
This appears to have been fixed as of 2.064