summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2017-01-02 17:33:01 +0100
committerEugen Wissner <belka@caraus.de>2017-01-02 17:33:01 +0100
commitb6413823cdf470e6b3dcf75ee59cfc97aff33825 (patch)
tree317ec09332dbb055c7babcd47c9b1c37edc6cd7c
parent48e355b87f672914a35c15ef83026a3ef26cee4c (diff)
downloadtanya-b6413823cdf470e6b3dcf75ee59cfc97aff33825.tar.gz
Add opEquals for all combinations of vector ranges
-rw-r--r--source/tanya/container/vector.d92
1 files changed, 91 insertions, 1 deletions
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