From eec2a935d8cdcae32efce08f8e0023c69b2c38f7 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 27 Sep 2024 03:18:51 +0200 Subject: [PATCH] Fix vector copy constructor --- .github/workflows/pushes.yml | 4 +- source/numem/collections/vector.d | 68 ++++++++++++++----------------- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/.github/workflows/pushes.yml b/.github/workflows/pushes.yml index 1291fc8..b05a11f 100644 --- a/.github/workflows/pushes.yml +++ b/.github/workflows/pushes.yml @@ -35,8 +35,8 @@ jobs: # Build and run tests, as defined by `unittest` configuration # In this mode, `mainSourceFile` is excluded and `version (unittest)` are included # See https://dub.pm/package-format-json.html#configurations - dub test + dub test --config=unittest # Ditto, in release mode. # Sometimes D packages break in release mode, so this is important to test. - dub test --build=release \ No newline at end of file + dub test --config=unittest --build=release \ No newline at end of file diff --git a/source/numem/collections/vector.d b/source/numem/collections/vector.d index 6c4ae50..9ccadca 100644 --- a/source/numem/collections/vector.d +++ b/source/numem/collections/vector.d @@ -26,6 +26,8 @@ enum isSomeWeakVector(T) = is(T : VectorImpl!(U, false), U); struct VectorImpl(T, bool ownsMemory=false) { @nogc: private: + alias selfType = VectorImpl!(T, ownsMemory); + enum VECTOR_ALIGN = 32; T* memory = null; @@ -112,49 +114,41 @@ public: this._memcpy(this.memory, data.ptr, data.length); } - static if (!isCopyable!T && __traits(hasMember, T, "moveTo")) { - - /** - Moves non-copyable members of one vector to another - */ - @trusted - this(ref vector!T rhs) { - if (rhs.memory) { - this.resize_(rhs.size_); - foreach(i; 0..rhs.size_) { - rhs.memory[i].moveTo(this.memory[i]); - } + /** + Makes a copy of a vector - // Clear memory. - rhs.resize(0); - rhs.shrinkToFit(); - } + Allows weak_vector <-> vector copies. + */ + @trusted + this(T)(ref T rhs) if(!is(T == selfType) && isSomeVector!T) { + if (rhs.memory) { + this.resize_(rhs.size_); + this._memcpy(this.memory, rhs.memory, rhs.size_); } - } else { + } - /** - Makes a copy of a vector - */ - @trusted - this(ref vector!T rhs) { - if (rhs.memory) { - this.resize_(rhs.size_); - this._memcpy(this.memory, rhs.memory, rhs.size_); - } + /** + Makes a copy of a vector + */ + @trusted + this(ref selfType rhs) { + if (rhs.memory) { + this.resize_(rhs.size_); + this._memcpy(this.memory, rhs.memory, rhs.size_); } + } - /** - Makes a copy of a vector - */ - @trusted - this(ref return scope inout(vector!T) rhs) inout { - if (rhs.memory) { - auto self = (cast(vector!T)this); - auto other = (cast(vector!T)rhs); + /** + Makes a copy of a vector + */ + @trusted + this(ref return scope inout(selfType) rhs) inout { + if (rhs.memory) { + auto self = (cast(selfType)this); + auto other = (cast(selfType)rhs); - self.resize_(rhs.size_); - other._memcpy(self.memory, other.memory, other.size_); - } + self.resize_(rhs.size_); + other._memcpy(self.memory, other.memory, other.size_); } }