mirror of
https://github.com/dlang/phobos.git
synced 2025-05-11 06:28:28 +03:00
Bug fix: swap must happen
This commit is contained in:
parent
06cb165a47
commit
19288618af
2 changed files with 33 additions and 18 deletions
|
@ -2206,24 +2206,23 @@ auto topN(alias less = "a < b",
|
|||
auto right = r.partition!(a => binaryFun!less(a, r.back), ss);
|
||||
assert(right.length >= 1);
|
||||
pivot = r.length - right.length;
|
||||
if (pivot < nth)
|
||||
{
|
||||
++pivot;
|
||||
r = r[pivot .. $];
|
||||
nth -= pivot;
|
||||
}
|
||||
else if (pivot > nth)
|
||||
if (pivot > nth)
|
||||
{
|
||||
// We don't care to swap the pivot back, won't be visited anymore
|
||||
assert(pivot < r.length);
|
||||
r = r[0 .. pivot];
|
||||
continue;
|
||||
}
|
||||
else
|
||||
// Swap the pivot to where it should be
|
||||
swap(right.front, r.back);
|
||||
if (pivot == nth)
|
||||
{
|
||||
// Happy case!
|
||||
// Swap the pivot to where it should be
|
||||
swap(right.front, r.back);
|
||||
// Found Waldo!
|
||||
break;
|
||||
}
|
||||
++pivot; // skip the pivot
|
||||
r = r[pivot .. $];
|
||||
nth -= pivot;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue