From 6f18aec347ea79b425b184e729e1405165fa469c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20Nordl=C3=B6w?= Date: Mon, 24 Oct 2022 15:04:18 +0200 Subject: [PATCH] Use Phobos version of staticIsSorted that uses alias assign when compiling with compiler version => 2.098.0 Reduces number of template instances of staticIsSorted from log(items.length) to 1 --- source/mir/internal/meta.d | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/source/mir/internal/meta.d b/source/mir/internal/meta.d index da9b96e..2f6b90a 100644 --- a/source/mir/internal/meta.d +++ b/source/mir/internal/meta.d @@ -114,7 +114,7 @@ enum hasToHash(T) = __traits(hasMember, T, "toHash"); static if (__VERSION__ < 2094) enum isCopyable(S) = is(typeof({ S foo = S.init; S copy = foo; })); else - enum isCopyable(S) = __traits(isCopyable, S); + enum isCopyable(S) = __traits(isCopyable, S); enum isPOD(T) = __traits(isPOD, T); enum Sizeof(T) = T.sizeof; @@ -136,18 +136,31 @@ enum hasSemiMutableConstruction(T) = __traits(compiles, {static struct S { T a; static assert(hasInoutConstruction!S); } -template staticIsSorted(alias cmp, Seq...) +static if (__VERSION__ >= 2098) { + enum staticIsSorted(alias cmp, items...) = + { + static if (items.length > 1) + static foreach (i, item; items[1 .. $]) + static if (cmp!(items[i], item)) + if (__ctfe) return false; + return true; + }(); +} +else { - static if (Seq.length <= 1) - enum staticIsSorted = true; - else static if (Seq.length == 2) - enum staticIsSorted = cmp!(Seq[0], Seq[1]); - else + template staticIsSorted(alias cmp, Seq...) { - enum staticIsSorted = + static if (Seq.length <= 1) + enum staticIsSorted = true; + else static if (Seq.length == 2) + enum staticIsSorted = cmp!(Seq[0], Seq[1]); + else + { + enum staticIsSorted = cmp!(Seq[($ / 2) - 1], Seq[$ / 2]) && staticIsSorted!(cmp, Seq[0 .. $ / 2]) && staticIsSorted!(cmp, Seq[$ / 2 .. $]); + } } }