Browse Source

Fixed a bad vector handling

channel
Ludovic 'Archivist' Lagouardette 3 years ago
parent
commit
3453681c48
1 changed files with 50 additions and 4 deletions
  1. +50
    -4
      include/gp/containers/vector.hpp

+ 50
- 4
include/gp/containers/vector.hpp View File

@ -24,11 +24,21 @@ namespace gp{
using associated_riterator = pointer_iterator<T, -1>; using associated_riterator = pointer_iterator<T, -1>;
using associated_const_riterator = const_pointer_iterator<T, -1>; using associated_const_riterator = const_pointer_iterator<T, -1>;
/**
* @brief Construct a new vector object from an allocator
*
* @param v the allocator
*/
vector(allocator& v) vector(allocator& v)
: ary() : ary()
, alloc(v) , alloc(v)
{} {}
/**
* @brief Construct a new vector object from another vector, copying it
*
* @param oth the other vector
*/
vector(vector& oth) vector(vector& oth)
: alloc(oth.alloc) : alloc(oth.alloc)
{ {
@ -46,6 +56,11 @@ namespace gp{
} }
} }
/**
* @brief Construct a new vector object by moving objects out of another vector
*
* @param oth
*/
vector(vector&& oth) vector(vector&& oth)
: ary(oth.ary) : ary(oth.ary)
, sz(oth.sz) , sz(oth.sz)
@ -55,6 +70,12 @@ namespace gp{
oth.ary = nullptr; oth.ary = nullptr;
} }
/**
* @brief Copy assignment
*
* @param oth
* @return vector& the reference to this changed vector
*/
vector& operator=(vector& oth) vector& operator=(vector& oth)
{ {
gp_config::assertion(reserve(oth.size()), "could not reserve space on assign"); gp_config::assertion(reserve(oth.size()), "could not reserve space on assign");
@ -75,12 +96,37 @@ namespace gp{
return *this; return *this;
} }
/**
* @brief Move assignment
*
* Will not change the allocator of the local vector, is EXPENSIVE if the both vectors have different allocators
*
* @param oth
* @return vector&
*/
vector& operator=(vector&& oth) vector& operator=(vector&& oth)
{ {
gp::swap(ary, oth.ary);
gp::swap(alloc, oth.alloc);
gp::swap(sz, oth.sz);
gp::swap(cap, oth.cap);
if(&alloc.get() == &oth.alloc.get())
{
gp::swap(ary, oth.ary);
gp::swap(sz, oth.sz);
gp::swap(cap, oth.cap);
} else {
for(auto& elem : *this) {
elem->~T();
}
sz = 0;
if(capacity()<oth.size()) {
reserve(oth.size());
}
size_t idx = 0;
for(auto& elem : oth) {
new(ary+idx) T(gp::move(elem));
elem.~T();
}
sz = idx;
oth.sz = 0;
}
return *this; return *this;
} }

Loading…
Cancel
Save