Skip to content

Commit

Permalink
Use Phobos version of staticIsSorted that uses alias assign when comp…
Browse files Browse the repository at this point in the history
…iling with compiler version => 2.098.0

Reduces number of template instances of staticIsSorted from log(items.length) to 1
  • Loading branch information
nordlow committed Oct 24, 2022
1 parent 4fdf97d commit 6f18aec
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions source/mir/internal/meta.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 .. $]);
}
}
}

Expand Down

0 comments on commit 6f18aec

Please sign in to comment.