Decrease template instantiation in std.algorithm.searching : all & any

Using LDC with optimizations enabled the end compiled result is the same.
This commit is contained in:
Nathan Sashihara 2019-04-15 01:34:30 -04:00
parent 3fcbc9bcc7
commit a4d4e03a5e

View file

@ -124,9 +124,10 @@ template all(alias pred = "a")
{ {
static assert(is(typeof(unaryFun!pred(range.front))), static assert(is(typeof(unaryFun!pred(range.front))),
"`" ~ pred.stringof[1..$-1] ~ "` isn't a unary predicate function for range.front"); "`" ~ pred.stringof[1..$-1] ~ "` isn't a unary predicate function for range.front");
import std.functional : not; foreach (ref e; range)
if (!unaryFun!pred(e))
return find!(not!(unaryFun!pred))(range).empty; return false;
return true;
} }
} }
@ -171,7 +172,10 @@ template any(alias pred = "a")
bool any(Range)(Range range) bool any(Range)(Range range)
if (isInputRange!Range && is(typeof(unaryFun!pred(range.front)))) if (isInputRange!Range && is(typeof(unaryFun!pred(range.front))))
{ {
return !find!pred(range).empty; foreach (ref e; range)
if (unaryFun!pred(e))
return true;
return false;
} }
} }