[Static if] replace overload constraints with static if (sorting.d)

This commit is contained in:
Sebastian Wilzbach 2017-02-18 03:47:25 +01:00
parent 78acf07136
commit 59508c4949

View file

@ -3748,58 +3748,46 @@ void topNIndex(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
(Range r, RangeIndex index, SortOutput sorted = No.sortOutput) (Range r, RangeIndex index, SortOutput sorted = No.sortOutput)
if (isRandomAccessRange!Range && if (isRandomAccessRange!Range &&
isRandomAccessRange!RangeIndex && isRandomAccessRange!RangeIndex &&
hasAssignableElements!RangeIndex && hasAssignableElements!RangeIndex)
isIntegral!(ElementType!(RangeIndex)))
{ {
static assert(ss == SwapStrategy.unstable, static assert(ss == SwapStrategy.unstable,
"Stable swap strategy not implemented yet."); "Stable swap strategy not implemented yet.");
import std.container : BinaryHeap; import std.container.binaryheap : BinaryHeap;
import std.exception : enforce;
if (index.empty) return; 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 static if (isIntegral!(ElementType!(RangeIndex)))
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.");
import std.container : BinaryHeap;
if (index.empty) return;
static bool indirectLess(const ElementType!(RangeIndex) a,
const ElementType!(RangeIndex) b)
{ {
return binaryFun!less(*a, *b); import std.exception : enforce;
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);
}
} }
auto heap = BinaryHeap!(RangeIndex, indirectLess)(index, 0); else static if (is(ElementType!(RangeIndex) == ElementType!(Range)*))
foreach (i; 0 .. r.length)
{ {
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) if (sorted == Yes.sortOutput)
{ {
while (!heap.empty) heap.removeFront(); while (!heap.empty) heap.removeFront();