Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 29 additions & 41 deletions std/algorithm/sorting.d
Original file line number Diff line number Diff line change
Expand Up @@ -3748,58 +3748,46 @@ void topNIndex(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
(Range r, RangeIndex index, SortOutput sorted = No.sortOutput)
if (isRandomAccessRange!Range &&
isRandomAccessRange!RangeIndex &&
hasAssignableElements!RangeIndex &&
isIntegral!(ElementType!(RangeIndex)))
hasAssignableElements!RangeIndex)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested I moved the specific ElementType checks and trigger a static assert error (and thus nice error message!) if an invalid type is passed by accident.

{
static assert(ss == SwapStrategy.unstable,
"Stable swap strategy not implemented yet.");

import std.container : BinaryHeap;
import std.exception : enforce;

import std.container.binaryheap : BinaryHeap;
if (index.empty) return;
enforce(ElementType!(RangeIndex).max >= index.length,
"Index type too small");
bool indirectLess(ElementType!(RangeIndex) a, ElementType!(RangeIndex) b)
{
return binaryFun!(less)(r[a], r[b]);
}
auto heap = BinaryHeap!(RangeIndex, indirectLess)(index, 0);
foreach (i; 0 .. r.length)
{
heap.conditionalInsert(cast(ElementType!RangeIndex) i);
}
if (sorted == Yes.sortOutput)
{
while (!heap.empty) heap.removeFront();
}
}

/// ditto
void topNIndex(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
Range, RangeIndex)
(Range r, RangeIndex index, SortOutput sorted = No.sortOutput)
if (isRandomAccessRange!Range &&
isRandomAccessRange!RangeIndex &&
hasAssignableElements!RangeIndex &&
is(ElementType!(RangeIndex) == ElementType!(Range)*))
{
static assert(ss == SwapStrategy.unstable,
"Stable swap strategy not implemented yet.");
static if (isIntegral!(ElementType!(RangeIndex)))
{
import std.exception : enforce;

import std.container : BinaryHeap;
enforce(ElementType!(RangeIndex).max >= index.length,
"Index type too small");
bool indirectLess(ElementType!(RangeIndex) a, ElementType!(RangeIndex) b)
{
return binaryFun!(less)(r[a], r[b]);
}
auto heap = BinaryHeap!(RangeIndex, indirectLess)(index, 0);
foreach (i; 0 .. r.length)
{
heap.conditionalInsert(cast(ElementType!RangeIndex) i);
}

if (index.empty) return;
static bool indirectLess(const ElementType!(RangeIndex) a,
const ElementType!(RangeIndex) b)
{
return binaryFun!less(*a, *b);
}
auto heap = BinaryHeap!(RangeIndex, indirectLess)(index, 0);
foreach (i; 0 .. r.length)
else static if (is(ElementType!(RangeIndex) == ElementType!(Range)*))
{
heap.conditionalInsert(&r[i]);
static bool indirectLess(const ElementType!(RangeIndex) a,
const ElementType!(RangeIndex) b)
{
return binaryFun!less(*a, *b);
}
auto heap = BinaryHeap!(RangeIndex, indirectLess)(index, 0);
foreach (i; 0 .. r.length)
{
heap.conditionalInsert(&r[i]);
}
}
else static assert(0, "Invalid ElementType");

if (sorted == Yes.sortOutput)
{
while (!heap.empty) heap.removeFront();
Expand Down