Skip to content

Commit

Permalink
Fix vector copy constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Sep 27, 2024
1 parent 924f8c8 commit eec2a93
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pushes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
dub test --config=unittest --build=release
68 changes: 31 additions & 37 deletions source/numem/collections/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_);
}
}

Expand Down

0 comments on commit eec2a93

Please sign in to comment.