www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15421] New: The behaviours of the topNs differ with the bottom

https://issues.dlang.org/show_bug.cgi?id=15421

          Issue ID: 15421
           Summary: The behaviours of the topNs differ with the bottom
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: lt.infiltrator gmail.com

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

--
Dec 08 2015