Bug 5224 – std.algorithm.remove!(SwapStrategy.unstable) doesn't work
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-11-16T11:52:00Z
Last change time
2013-07-30T05:50:41Z
Assigned to
andrei
Creator
ruzicic.aleksandar
Comments
Comment #0 by ruzicic.aleksandar — 2010-11-16T11:52:45Z
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);
}
-----------------------------------------------------------
Comment #1 by ruzicic.aleksandar — 2010-11-16T12:02:22Z
tested now also under DMD 2.050, same results.
Comment #2 by braddr — 2011-02-06T15:39:36Z
Mass migration of bugs marked as x86-64 to just x86. The platform run on isn't what's relevant, it's if the app is a 32 or 64 bit app.
Comment #3 by lovelydear — 2012-04-22T03:05:59Z
Although the unit tests pass, they seem insufficient.
This example actually comes from a comment in algorithm.d and yet it fails on 2.059:
int[] a = [ 0, 1, 2, 3 ];
assert(remove!(SwapStrategy.unstable)(a, 1) == [ 0, 3, 2 ]);
(returns [3, 1, 2])
Idem for SwapStrategy.semistable
Also, in one comment of algorithm.d, there is a typo. According to the explanation under the example, line 5914 of algorithm.d should be:
int[] a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
assert(remove(a, 1, tuple(3, 5), 9) == [ 0, 2, 5, 6, 7, 8, 10 ]);
^-- forgotten
"The tuple passes in a range closed to the left and open to
the right"
Comment #4 by lovelydear — 2012-04-22T03:10:47Z
*** Issue 6956 has been marked as a duplicate of this issue. ***