Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple vec fixes that are useful to implement canvas_ity. #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions source/numem/collections/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,10 @@ public:
Gets the C data pointer
*/
@trusted
T* data() {
inout(T)* data() inout {
return memory;
}

/**
Gets the C data pointer as an inout pointer
*/
@trusted
inout(T)* idata() inout {
return cast(inout(T)*)memory;
}
alias ptr = data; ///ditto

/**
Gets the C data pointer atomically
Expand Down Expand Up @@ -278,6 +271,7 @@ public:
size_t size() inout {
return size_;
}
alias length = size; ///ditto

/**
Gets the capacity of the vector
Expand All @@ -302,8 +296,10 @@ public:
void clear() {

// Delete elements in the array.
foreach(item; 0..size_) {
nogc_delete(memory[item]);
static if (!isBasicType!T) {
foreach_reverse(item; 0..size_) {
nogc_delete(this.memory[item]);
}
}

this.size_ = 0;
Expand All @@ -315,7 +311,9 @@ public:
@trusted
void remove(size_t position) {
if (position < size_) {
nogc_delete(memory[position]);
static if (!isBasicType!T) {
nogc_delete(memory[position]);
}

// Move memory region around so that the deleted element is overwritten.
memmove(memory+position, memory+position+1, size_*(T*).sizeof);
Expand All @@ -325,31 +323,26 @@ public:
}

/**
Erases element at position
Erases element at position [start, end)
End is NOT included in that range.
*/
@trusted
void remove(size_t start, size_t end) {

// Flip inputs if they are reversed, just in case.
if (end > start) {
size_t tmp = start;
start = end;
end = tmp;
}
assert(start <= end && end <= size_);

if (start < size_ && end < size_) {

// NOTE: the ".." operator is start inclusive, end exclusive.
foreach(i; start..end+1)
// NOTE: the ".." operator is start inclusive, end exclusive.
static if (!isBasicType!T) {
foreach_reverse(i; start..end)
nogc_delete(memory[i]);
}

// Copy over old elements
size_t span = (end+1)-start;
// memory[start..start+span] = memory[end..end+span];
memmove(memory+start, memory+end, span*(T*).sizeof);
// Copy over old elements
size_t span = end-start;
// memory[start..start+span] = memory[end..end+span];
memmove(memory+start, memory+end, span*(T*).sizeof);

size_ -= span;
}
size_ -= span;
}

/**
Expand Down Expand Up @@ -485,7 +478,7 @@ public:
Override for $ operator
*/
@trusted
size_t opDollar() {
size_t opDollar() const {
return size_;
}

Expand Down Expand Up @@ -519,7 +512,7 @@ public:
Allows getting an item from the vector.
*/
@trusted
ref T opIndex(size_t index) {
ref inout(T) opIndex(size_t index) inout {
return memory[index];
}

Expand Down Expand Up @@ -572,3 +565,12 @@ unittest {
vector!(shared_ptr!A) v;
v ~= a; // Used to crash, see Issue #2
}

@("vector: delete")
unittest {
vector!int v;
v ~= [1, 2, 3];
v.remove(0, 0);
v.remove(1, 1);
v.remove(2, 2);
}
10 changes: 5 additions & 5 deletions source/numem/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,16 @@ public:
/**
Returns C string
*/
inout(T)* toCStringi() inout {
return cast(inout(T)*)this.vec_.idata();
immutable(T)* toCString() immutable {
return this.vec_.data();
}

/**
Returns C string
*/
@trusted
const(T)* toCString() {
return cast(const(T)*)this.vec_.data();
const(T)* toCString() const {
return this.vec_.data();
}

/**
Expand Down Expand Up @@ -359,7 +359,7 @@ public:
import core.stdc.string : strncmp;
if (this.size() < s.size()) return -1;
if (this.size() > s.size()) return 1;
return strncmp(this.toCStringi(), s.toCStringi(), this.size());
return strncmp(this.toCString(), s.toCString(), this.size());
}

/**
Expand Down
Loading