From 67952dabdb28e504cdc0e34c9f7151e7ffb71ff2 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 3 Jan 2017 13:21:19 +0100 Subject: [PATCH] Implement Vector.remove --- source/tanya/container/vector.d | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/source/tanya/container/vector.d b/source/tanya/container/vector.d index 0e67b54..0992733 100644 --- a/source/tanya/container/vector.d +++ b/source/tanya/container/vector.d @@ -643,6 +643,51 @@ struct Vector(T) assert(v.removeBack(3) == 0); } + /** + * Remove all elements beloning to $(D_PARAM r). + * + * Params: + * r = Range originally obtained from this vector. + * + * Returns: Elements in $(D_PARAM r) after removing. + */ + Range!T remove(Range!T r) @trusted + in + { + assert(r.begin >= vector && r.end <= vector + length_); + } + body + { + const T* end = vector + length_; + for (T* a = r.begin, b = r.end; b != end; ++a, ++b) + { + *a = *b; + } + length_ -= r.length; + return r; + } + + /// + unittest + { + auto v = Vector!int(IL(5, 18, 17, 2, 4, 6, 1)); + + v.remove(v[1..3]); + assert(v == Vector!int(IL(5, 2, 4, 6, 1))); + + v.remove(v[4..4]); + assert(v == Vector!int(IL(5, 2, 4, 6, 1))); + + v.remove(v[4..5]); + assert(v == Vector!int(IL(5, 2, 4, 6))); + + v.remove(v[]); + assert(v.empty); + + v.remove(v[]); + assert(v.empty); + } + /** * Inserts the $(D_PARAM el) into the vector. *