Skip to content

Commit

Permalink
fix(pzvec): move assignment fix when this was manvector
Browse files Browse the repository at this point in the history
  • Loading branch information
orlandini committed Sep 5, 2023
1 parent 69d2783 commit b89d766
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
14 changes: 14 additions & 0 deletions UnitTest_PZ/TestVecTypes/VecTypesUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ TEST_CASE("mixedvecs_tests","[test_vectypes]") {
}
REQUIRE(true);


try{
TPZManVector<int,3> manvec(3,0);

auto func = [](TPZVec<int> &vec){
TPZVec<int> myothervec(2,0);
vec = std::move(myothervec);
};

func(manvec);
}catch(...){
REQUIRE(false);
}
REQUIRE(true);

//move assignment operator
try{
Expand Down
50 changes: 37 additions & 13 deletions Util/pzvec.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,19 +383,43 @@ TPZVec<T> &TPZVec<T>::operator=(const TPZVec<T> &copy){
template< class T >
TPZVec<T> &TPZVec<T>::operator=(TPZVec<T> &&rval){
if(this != &rval){
fNElements = rval.fNElements;
if(fStore) delete [] fStore;
if(rval.fNAlloc){
fStore = rval.fStore;
}else{
fStore = new T[fNElements];
for (int64_t i = 0; i < fNElements; i++)
fStore[i] = rval.fStore[i];
}
fNAlloc = fNElements;//perhaps rval.fNalloc was 0
rval.fStore = nullptr;
rval.fNElements = 0;
rval.fNAlloc = 0;

if(fNAlloc && rval.fNAlloc){
//scenario 1: both vectors have allocated dynamic memory
delete [] fStore;
fNAlloc = rval.fNAlloc;
fNElements = rval.fNElements;
fStore = rval.fStore;
rval.fStore = nullptr;
rval.fNAlloc = 0;
rval.fNElements = 0;
return *this;
}
if(!fNAlloc && fNElements >= rval.fNElements){
//scenario 2: our static memory can fit their elements
for (int64_t i = 0; i < fNElements; i++){
fStore[i] = rval.fStore[i];
}
fNElements = rval.fNElements;
//no need to modify rval, it was just a copy
return *this;
}
if(fNAlloc) delete [] fStore;


fNElements = rval.fNElements;
if(rval.fNAlloc){
fStore = rval.fStore;
rval.fStore = nullptr;
rval.fNElements = 0;
rval.fNAlloc = 0;
}else{
fStore = new T[fNElements];
for (int64_t i = 0; i < fNElements; i++){
fStore[i] = rval.fStore[i];
}
}
fNAlloc = fNElements;
}
return *this;
}
Expand Down

0 comments on commit b89d766

Please sign in to comment.