util: implement prevector's move ctor & move assignment

Using swap() was rather wasteful because it had to copy the whole direct
memory data twice. Also, due to the swap() in move assignment the moved-from
object might hold on to unused memory for longer than necessary.
This commit is contained in:
Martin Leitner-Ankerl 2023-03-26 14:40:32 +02:00
parent fffc86f49f
commit bfb9291a86

View file

@ -264,8 +264,10 @@ public:
fill(item_ptr(0), other.begin(), other.end());
}
prevector(prevector<N, T, Size, Diff>&& other) noexcept {
swap(other);
prevector(prevector<N, T, Size, Diff>&& other) noexcept
: _union(std::move(other._union)), _size(other._size)
{
other._size = 0;
}
prevector& operator=(const prevector<N, T, Size, Diff>& other) {
@ -277,7 +279,12 @@ public:
}
prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
swap(other);
if (!is_direct()) {
free(_union.indirect_contents.indirect);
}
_union = std::move(other._union);
_size = other._size;
other._size = 0;
return *this;
}