Comment #0 by lt.infiltrator — 2015-12-08T11:56:19Z
============
import std.algorithm;
void main() {
auto a = [ 9, 8, 0, 3, 5, 25, 43, 4, 2, 0, 7 ];
auto b = [ 9, 8, 0, 3, 5, 25, 43, 4, 2, 0, 7 ];
topN(a, 4);
topN(b[0 .. 4], b[4 .. $]);
sort(a[0 .. 4]); sort(a[4 .. $]);
sort(b[0 .. 4]); sort(b[4 .. $]);
assert(a[0 .. 4] == b[0 .. 4]);
assert(a[4 .. $] == b[4 .. $]); // This one fails
assert(a == b); // As would this one obviously
}
============
Basically, topN(Range, index) rearranges *all* of the elements such that all those on the left are smaller (or whatever the predicate specifies) than those on the right, while still keeping all the elements. However, topN(Range, Range) leaves the second range untouched such that you end up with with some elements disappearing and others being duplicated.
Comment #1 by lt.infiltrator — 2015-12-08T12:08:07Z