Add predicate support for algorithm.comparison.equal

This commit is contained in:
Eugen Wissner 2018-09-17 19:17:39 +02:00
parent eb796e0ddf
commit b0dc7b59e5
2 changed files with 12 additions and 9 deletions

View File

@ -278,16 +278,19 @@ if (isForwardRange!Range && isOrderingComparable!(ElementType!Range))
* If the ranges have different lengths, they aren't equal. * If the ranges have different lengths, they aren't equal.
* *
* Params: * Params:
* R1 = First range type. * pred = Predicate used to compare individual element pairs.
* R2 = Second range type. * R1 = First range type.
* r1 = First range. * R2 = Second range type.
* r2 = Second range. * r1 = First range.
* r2 = Second range.
* *
* Returns: $(D_KEYWORD true) if both ranges are equal, $(D_KEYWORD false) * Returns: $(D_KEYWORD true) if both ranges are equal, $(D_KEYWORD false)
* otherwise. * otherwise.
*/ */
bool equal(R1, R2)(R1 r1, R2 r2) bool equal(alias pred = (auto ref a, auto ref b) => a == b, R1, R2)
if (allSatisfy!(isInputRange, R1, R2) && is(typeof(r1.front == r2.front))) (R1 r1, R2 r2)
if (allSatisfy!(isInputRange, R1, R2)
&& is(typeof(pred(r1.front, r2.front)) == bool))
{ {
static if (isDynamicArray!R1 static if (isDynamicArray!R1
&& is(R1 == R2) && is(R1 == R2)
@ -306,7 +309,7 @@ if (allSatisfy!(isInputRange, R1, R2) && is(typeof(r1.front == r2.front)))
} }
for (; !r1.empty && !r2.empty; r1.popFront(), r2.popFront()) for (; !r1.empty && !r2.empty; r1.popFront(), r2.popFront())
{ {
if (r1.front != r2.front) if (!pred(r1.front, r2.front))
{ {
return false; return false;
} }

View File

@ -1554,14 +1554,14 @@ struct Array(T)
{ {
struct MutableEqualsStruct struct MutableEqualsStruct
{ {
int opEquals(typeof(this) that) @nogc nothrow pure @safe bool opEquals(typeof(this) that) @nogc nothrow pure @safe
{ {
return true; return true;
} }
} }
struct ConstEqualsStruct struct ConstEqualsStruct
{ {
int opEquals(const typeof(this) that) const @nogc nothrow pure @safe bool opEquals(const typeof(this) that) const @nogc nothrow pure @safe
{ {
return true; return true;
} }