From b6413823cdf470e6b3dcf75ee59cfc97aff33825 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Mon, 2 Jan 2017 17:33:01 +0100 Subject: [PATCH] Add opEquals for all combinations of vector ranges --- source/tanya/container/vector.d | 92 ++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/source/tanya/container/vector.d b/source/tanya/container/vector.d index a51a77a..ac26d43 100644 --- a/source/tanya/container/vector.d +++ b/source/tanya/container/vector.d @@ -161,8 +161,42 @@ private struct Range(E) return typeof(return)(begin + i, begin + j); } + bool opEquals()(Range that) const @trusted + { + if (length != that.length) + { + return false; + } + for (const(E)* i = begin; i != end; ++i) + { + if (*i != that.front) + { + return false; + } + that.popFront(); + } + return true; + } + static if (isMutable!E) { + bool opEquals(Range that) + { + if (length != that.length) + { + return false; + } + for (E* i = begin; i != end; ++i) + { + if (*i != that.front) + { + return false; + } + that.popFront(); + } + return true; + } + ref E opIndexAssign(ref E value, in size_t pos) @trusted in { @@ -803,7 +837,9 @@ struct Vector(T) /** * Comparison for equality. * - * Params: o = The vector to compare with. + * Params: + * R = Right hand side type. + * v = The vector to compare with. * * Returns: $(D_KEYWORD true) if the vectors are equal, $(D_KEYWORD false) * otherwise. @@ -843,6 +879,45 @@ struct Vector(T) return true; } + /// Ditto. + bool opEquals(Range!T v) @trusted + { + if (length_ != v.length) + { + return false; + } + const T* end = vector + length_; + for (T* v1 = vector; v1 != end; ++v1) + { + if (*v1 != v.front) + { + return false; + } + v.popFront(); + } + return true; + } + + /// Ditto. + bool opEquals(R)(Range!R v) const @trusted + if (is(Unqual!R == T)) + { + if (length_ != v.length) + { + return false; + } + const T* end = vector + length_; + for (const(T)* v1 = vector; v1 != end; ++v1) + { + if (*v1 != v.front) + { + return false; + } + v.popFront(); + } + return true; + } + /// unittest { @@ -1244,6 +1319,12 @@ unittest assert(r1 == r2); v1.insertBack(1, 2, 4); + assert(v1[] == v1); + assert(v2[] == v2); + assert(v2[] != v1); + assert(v1[] != v2); + assert(v1[] == v1[]); + assert(v2[] == v2[]); } @nogc unittest @@ -1251,14 +1332,23 @@ unittest auto v1 = Vector!ConstEqualsStruct(); auto v2 = Vector!ConstEqualsStruct(); assert(v1 == v2); + assert(v1[] == v2); + assert(v1 == v2[]); + assert(v1[] == v2[]); auto v3 = const Vector!ConstEqualsStruct(); auto v4 = const Vector!ConstEqualsStruct(); assert(v3 == v4); + assert(v3[] == v4); + assert(v3 == v4[]); + assert(v3[] == v4[]); auto v7 = Vector!MutableEqualsStruct(); auto v8 = Vector!MutableEqualsStruct(); assert(v7 == v8); + assert(v7[] == v8); + assert(v7 == v8[]); + assert(v7[] == v8[]); } @nogc unittest