diff --git a/source/numem/collections/vector.d b/source/numem/collections/vector.d index 36f8fe2..02f8a10 100644 --- a/source/numem/collections/vector.d +++ b/source/numem/collections/vector.d @@ -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 @@ -278,6 +271,7 @@ public: size_t size() inout { return size_; } + alias length = size; ///ditto /** Gets the capacity of the vector @@ -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; @@ -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); @@ -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; } /** @@ -485,7 +478,7 @@ public: Override for $ operator */ @trusted - size_t opDollar() { + size_t opDollar() const { return size_; } @@ -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]; } @@ -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); +} diff --git a/source/numem/string.d b/source/numem/string.d index ccea065..d5c884e 100644 --- a/source/numem/string.d +++ b/source/numem/string.d @@ -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(); } /** @@ -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()); } /**