From b3f4ea572ee4818d696f9e45460f15f9fef6e4e3 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 1 Jan 2017 22:01:07 +0100 Subject: [PATCH] Vector: Use opEquals if defined to compare items --- .travis.yml | 2 +- README.md | 2 +- source/tanya/container/vector.d | 71 ++++++++++++++++++++++++++++----- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 69766e5..24018bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ os: language: d d: - - dmd-2.072.1 + - dmd-2.072.2 - dmd-2.071.2 env: diff --git a/README.md b/README.md index 4167c9c..a1c7215 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ helper functions). ### Supported compilers -* dmd 2.072.1 +* dmd 2.072.2 * dmd 2.071.2 ### Current status diff --git a/source/tanya/container/vector.d b/source/tanya/container/vector.d index 69eb310..7fc9e84 100644 --- a/source/tanya/container/vector.d +++ b/source/tanya/container/vector.d @@ -21,12 +21,25 @@ import tanya.memory; version (unittest) { - struct TestA + struct SWithDtor { + bool opEquals(ref SWithDtor that) + { + return true; + } + ~this() @nogc { } } + + struct SConstEquals + { + bool opEquals(in ref SConstEquals that) const + { + return true; + } + } } // Defines the container's primary range. @@ -387,7 +400,7 @@ struct Vector(T) /** * Destroys this $(D_PSYMBOL Vector). */ - ~this() + ~this() @trusted { static if (hasElaborateDestructor!T) { @@ -404,7 +417,7 @@ struct Vector(T) private unittest { - auto v = Vector!TestA(); // Destructor can destroy empty vectors. + auto v = Vector!SWithDtor(); // Destructor can destroy empty vectors. } /** @@ -810,21 +823,44 @@ struct Vector(T) /** * Comparison for equality. * - * Params: - * o = The vector to compare with. + * Params: o = The vector to compare with. * * Returns: $(D_KEYWORD true) if the vectors are equal, $(D_KEYWORD false) * otherwise. */ - bool opEquals(typeof(this) v) const + bool opEquals()(auto ref typeof(this) v) @trusted { - return opEquals(v); + if (length_ != v.length_) + { + return false; + } + const T* end = vector + length_; + for (T* v1 = vector, v2 = v.vector; v1 != end; ++v1, ++v2) + { + if (*v1 != *v2) + { + return false; + } + } + return true; } /// Ditto. - bool opEquals(ref typeof(this) v) const @trusted + bool opEquals()(in auto ref typeof(this) v) const @trusted { - return vector[0 .. length_] == v.vector[0 .. v.length_]; + if (length_ != v.length_) + { + return false; + } + const T* end = vector + length_; + for (const(T)* v1 = vector, v2 = v.vector; v1 != end; ++v1, ++v2) + { + if (*v1 != *v2) + { + return false; + } + } + return true; } /// @@ -847,6 +883,21 @@ struct Vector(T) assert(v1 == v2); } + private unittest + { + auto v1 = Vector!SConstEquals(); + auto v2 = Vector!SConstEquals(); + assert(v1 == v2); + + auto v3 = const Vector!SConstEquals(); + auto v4 = const Vector!SConstEquals(); + assert(v3 == v4); + + auto v7 = Vector!SWithDtor(); + auto v8 = Vector!SWithDtor(); + assert(v7 == v8); + } + /** * $(D_KEYWORD foreach) iteration. * @@ -1192,7 +1243,7 @@ private @nogc unittest auto a = Vector!A(); // Test that structs can be members of the vector. - static assert(is(typeof(Vector!TestA()))); + static assert(is(typeof(Vector!SWithDtor()))); } private @nogc unittest