Bladeren bron

Fixed a bad vector handling

channel
Ludovic 'Archivist' Lagouardette 3 jaren geleden
bovenliggende
commit
3453681c48
1 gewijzigde bestanden met toevoegingen van 50 en 4 verwijderingen
  1. +50
    -4
      include/gp/containers/vector.hpp

+ 50
- 4
include/gp/containers/vector.hpp Bestand weergeven

@ -24,11 +24,21 @@ namespace gp{
using associated_riterator = 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)
: ary()
, alloc(v)
{}
/**
* @brief Construct a new vector object from another vector, copying it
*
* @param oth the other vector
*/
vector(vector& oth)
: 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)
: ary(oth.ary)
, sz(oth.sz)
@ -55,6 +70,12 @@ namespace gp{
oth.ary = nullptr;
}
/**
* @brief Copy assignment
*
* @param oth
* @return vector& the reference to this changed vector
*/
vector& operator=(vector& oth)
{
gp_config::assertion(reserve(oth.size()), "could not reserve space on assign");
@ -75,12 +96,37 @@ namespace gp{
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)
{
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;
}

Laden…
Annuleren
Opslaan