Implement Vector.remove

This commit is contained in:
Eugen Wissner 2017-01-03 13:21:19 +01:00
parent b8d5d4c2bd
commit 67952dabdb
1 changed files with 45 additions and 0 deletions

View File

@ -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.
*