Skip to content

Commit

Permalink
Multiple vec fixes that are useful to implement canvas_ity. I found q…
Browse files Browse the repository at this point in the history
…uite a bit of oddity in vector still.

- Remove bizzarre idata() method, normally inout should not contaminate identifiers. Fixed that in strings, I supposed the goal was to not return mutable C string from numem string
- vec.ptr() as alias to vec.data(), which is not inout. It fixed ref auto opOpAssign(string op = "~")(vector!T other) that uses other.ptr
- vec.length as alias of vec.size
- clear() and remove() will clean-up items in reverse manner just like the destructor, and only if not a basic type.
- ref inout(T) opIndex. else you can't index vector fields in const methods

- BREAKING remove is fixed (it was not possible to remove zero items with its API without crashing) and is now "delete [start, end)" instead of "delete [start, end]"
  Changed error recovery that swaps start and end to assertion.
  That makes the functions more similar in contract to c++ std::vector::erase and a bit more expected to me.
  IF the contract doesn't change, at least it should say plainly that this will crash if no items are removed.
  • Loading branch information
Guillaume Piolat committed Oct 4, 2024
1 parent 0e03ca8 commit 6d97870
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
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

0 comments on commit 6d97870

Please sign in to comment.