From 59508c4949fb56b6f5d88853d3c189c2e81edd9c Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Sat, 18 Feb 2017 03:47:25 +0100 Subject: [PATCH] [Static if] replace overload constraints with static if (sorting.d) --- std/algorithm/sorting.d | 70 +++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/std/algorithm/sorting.d b/std/algorithm/sorting.d index 384a9c00ec8..3c38cddd15b 100644 --- a/std/algorithm/sorting.d +++ b/std/algorithm/sorting.d @@ -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) { 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();