From b0dc7b59e570849e55894ae99a9d162247ccc329 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Mon, 17 Sep 2018 19:17:39 +0200 Subject: [PATCH] Add predicate support for algorithm.comparison.equal --- source/tanya/algorithm/comparison.d | 17 ++++++++++------- source/tanya/container/array.d | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/source/tanya/algorithm/comparison.d b/source/tanya/algorithm/comparison.d index bf2ac34..88f6860 100644 --- a/source/tanya/algorithm/comparison.d +++ b/source/tanya/algorithm/comparison.d @@ -278,16 +278,19 @@ if (isForwardRange!Range && isOrderingComparable!(ElementType!Range)) * If the ranges have different lengths, they aren't equal. * * Params: - * R1 = First range type. - * R2 = Second range type. - * r1 = First range. - * r2 = Second range. + * pred = Predicate used to compare individual element pairs. + * R1 = First range type. + * R2 = Second range type. + * r1 = First range. + * r2 = Second range. * * Returns: $(D_KEYWORD true) if both ranges are equal, $(D_KEYWORD false) * otherwise. */ -bool equal(R1, R2)(R1 r1, R2 r2) -if (allSatisfy!(isInputRange, R1, R2) && is(typeof(r1.front == r2.front))) +bool equal(alias pred = (auto ref a, auto ref b) => a == b, R1, R2) + (R1 r1, R2 r2) +if (allSatisfy!(isInputRange, R1, R2) + && is(typeof(pred(r1.front, r2.front)) == bool)) { static if (isDynamicArray!R1 && 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()) { - if (r1.front != r2.front) + if (!pred(r1.front, r2.front)) { return false; } diff --git a/source/tanya/container/array.d b/source/tanya/container/array.d index 59cc3fb..78e4dd5 100644 --- a/source/tanya/container/array.d +++ b/source/tanya/container/array.d @@ -1554,14 +1554,14 @@ struct Array(T) { struct MutableEqualsStruct { - int opEquals(typeof(this) that) @nogc nothrow pure @safe + bool opEquals(typeof(this) that) @nogc nothrow pure @safe { return true; } } 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; }