Implement SharedVector::clear() for C++

This commit is contained in:
Simon Hausmann 2021-08-11 19:07:13 +02:00 committed by Simon Hausmann
parent 856a049627
commit 511027dad0
2 changed files with 44 additions and 0 deletions

View file

@ -127,6 +127,21 @@ struct SharedVector
inner->size++;
}
/// Clears the vector and removes all elements. The capacity remains unaffected.
void clear()
{
// Detach first to retain capacity, so that the begin() call doesn't detach
// (which it would to inner->size instead of capacity)
detach(inner->capacity);
{
auto b = begin(), e = end();
inner->size = 0;
for (auto it = b; it < e; ++it) {
it->~T();
}
}
}
/// Returns true if the vector \a a has the same number of elements as \a b
/// and all the elements also compare equal; false otherwise.
friend bool operator==(const SharedVector &a, const SharedVector &b)
@ -139,6 +154,9 @@ struct SharedVector
/// and all the elements also compare equal; true otherwise.
friend bool operator!=(const SharedVector &a, const SharedVector &b) { return !(a == b); }
/// \private
std::size_t capacity() const { return inner->capacity; }
private:
void detach(std::size_t expected_capacity)
{