diff --git a/src/foam/containers/Lists/List/List.C b/src/foam/containers/Lists/List/List.C index fd5efb256..47f3aa4de 100644 --- a/src/foam/containers/Lists/List/List.C +++ b/src/foam/containers/Lists/List/List.C @@ -264,7 +264,10 @@ Foam::List::List(const BiIndirectList& lst) template Foam::List::~List() { - if (this->v_) delete[] this->v_; + if (this->v_) + { + delete[] this->v_; + } } @@ -275,7 +278,7 @@ void Foam::List::setSize(const label newSize) { if (newSize < 0) { - FatalErrorIn("List::setSize(const label)") + FatalErrorInFunction << "bad set size " << newSize << abort(FatalError); } @@ -303,8 +306,8 @@ void Foam::List::setSize(const label newSize) while (i--) *--av = *--vv; } } - if (this->v_) delete[] this->v_; + clear(); this->size_ = newSize; this->v_ = nv; } @@ -331,21 +334,12 @@ void Foam::List::setSize(const label newSize, const T& a) } -template -void Foam::List::clear() -{ - if (this->v_) delete[] this->v_; - this->size_ = 0; - this->v_ = 0; -} - - // Transfer the contents of the argument List into this List // and anull the argument list template void Foam::List::transfer(List& a) { - if (this->v_) delete[] this->v_; + clear(); this->size_ = a.size_; this->v_ = a.v_; @@ -384,13 +378,7 @@ void Foam::List::transfer(SortableList& a) template void Foam::List::operator=(const UList& a) { - if (a.size_ != this->size_) - { - if (this->v_) delete[] this->v_; - this->v_ = 0; - this->size_ = a.size_; - if (this->size_) this->v_ = new T[this->size_]; - } + reAlloc(a.size_); if (this->size_) { @@ -418,7 +406,7 @@ void Foam::List::operator=(const List& a) { if (this == &a) { - FatalErrorIn("List::operator=(const List&)") + FatalErrorInFunction << "attempted assignment to self" << abort(FatalError); } @@ -431,13 +419,7 @@ void Foam::List::operator=(const List& a) template void Foam::List::operator=(const SLList& lst) { - if (lst.size() != this->size_) - { - if (this->v_) delete[] this->v_; - this->v_ = 0; - this->size_ = lst.size(); - if (this->size_) this->v_ = new T[this->size_]; - } + reAlloc(lst.size()); if (this->size_) { @@ -459,18 +441,8 @@ void Foam::List::operator=(const SLList& lst) template void Foam::List::operator=(const IndirectList& lst) { - if (lst.size() != this->size_) - { - if (this->v_) delete[] this->v_; - this->v_ = 0; - this->size_ = lst.size(); - if (this->size_) this->v_ = new T[this->size_]; - } - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } + reAlloc(lst.size()); + copyList(lst); } @@ -478,18 +450,8 @@ void Foam::List::operator=(const IndirectList& lst) template void Foam::List::operator=(const UIndirectList& lst) { - if (lst.size() != this->size_) - { - if (this->v_) delete[] this->v_; - this->v_ = 0; - this->size_ = lst.size(); - if (this->size_) this->v_ = new T[this->size_]; - } - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } + reAlloc(lst.size()); + copyList(lst); } @@ -497,18 +459,8 @@ void Foam::List::operator=(const UIndirectList& lst) template void Foam::List::operator=(const BiIndirectList& lst) { - if (lst.size() != this->size_) - { - if (this->v_) delete[] this->v_; - this->v_ = 0; - this->size_ = lst.size(); - if (this->size_) this->v_ = new T[this->size_]; - } - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } + reAlloc(lst.size()); + copyList(lst); } // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * // diff --git a/src/foam/containers/Lists/List/List.H b/src/foam/containers/Lists/List/List.H index 2a37e5c35..67ba73ff7 100644 --- a/src/foam/containers/Lists/List/List.H +++ b/src/foam/containers/Lists/List/List.H @@ -205,8 +205,8 @@ public: //- Reset size of List and value for new elements. void setSize(const label, const T&); - //- Clear the list, i.e. set size to zero. - void clear(); + //- Clear the list, i.e. set size to zero + inline void clear(); //- Append an element at the end of the list inline void append(const T&); diff --git a/src/foam/containers/Lists/List/ListI.H b/src/foam/containers/Lists/List/ListI.H index 05bacc9d0..c4d827bcf 100644 --- a/src/foam/containers/Lists/List/ListI.H +++ b/src/foam/containers/Lists/List/ListI.H @@ -91,8 +91,7 @@ inline Foam::List::List InputIterator iter = first; forAll(*this, i) { - this->operator[](i) = *iter; - ++iter; + this->operator[](i) = *iter++; } } } @@ -121,6 +120,19 @@ inline const Foam::List& Foam::List::null() } +template +inline void Foam::List::clear() +{ + if (this->v_) + { + delete[] this->v_; + this->v_ = 0; + } + + this->size_ = 0; +} + + template inline void Foam::List::resize(const label newSize) {