[port]: completed List container implementation with new functions.

This commit is contained in:
Danial Khazaei 2019-07-08 21:10:15 +04:30
parent 4e8d74617b
commit 9e401eedca
No known key found for this signature in database
GPG key ID: 8389A6DEEC8D5025
3 changed files with 32 additions and 68 deletions

View file

@ -264,7 +264,10 @@ Foam::List<T>::List(const BiIndirectList<T>& lst)
template<class T> template<class T>
Foam::List<T>::~List() Foam::List<T>::~List()
{ {
if (this->v_) delete[] this->v_; if (this->v_)
{
delete[] this->v_;
}
} }
@ -275,7 +278,7 @@ void Foam::List<T>::setSize(const label newSize)
{ {
if (newSize < 0) if (newSize < 0)
{ {
FatalErrorIn("List<T>::setSize(const label)") FatalErrorInFunction
<< "bad set size " << newSize << "bad set size " << newSize
<< abort(FatalError); << abort(FatalError);
} }
@ -303,8 +306,8 @@ void Foam::List<T>::setSize(const label newSize)
while (i--) *--av = *--vv; while (i--) *--av = *--vv;
} }
} }
if (this->v_) delete[] this->v_;
clear();
this->size_ = newSize; this->size_ = newSize;
this->v_ = nv; this->v_ = nv;
} }
@ -331,21 +334,12 @@ void Foam::List<T>::setSize(const label newSize, const T& a)
} }
template<class T>
void Foam::List<T>::clear()
{
if (this->v_) delete[] this->v_;
this->size_ = 0;
this->v_ = 0;
}
// Transfer the contents of the argument List into this List // Transfer the contents of the argument List into this List
// and anull the argument list // and anull the argument list
template<class T> template<class T>
void Foam::List<T>::transfer(List<T>& a) void Foam::List<T>::transfer(List<T>& a)
{ {
if (this->v_) delete[] this->v_; clear();
this->size_ = a.size_; this->size_ = a.size_;
this->v_ = a.v_; this->v_ = a.v_;
@ -384,13 +378,7 @@ void Foam::List<T>::transfer(SortableList<T>& a)
template<class T> template<class T>
void Foam::List<T>::operator=(const UList<T>& a) void Foam::List<T>::operator=(const UList<T>& a)
{ {
if (a.size_ != this->size_) reAlloc(a.size_);
{
if (this->v_) delete[] this->v_;
this->v_ = 0;
this->size_ = a.size_;
if (this->size_) this->v_ = new T[this->size_];
}
if (this->size_) if (this->size_)
{ {
@ -418,7 +406,7 @@ void Foam::List<T>::operator=(const List<T>& a)
{ {
if (this == &a) if (this == &a)
{ {
FatalErrorIn("List<T>::operator=(const List<T>&)") FatalErrorInFunction
<< "attempted assignment to self" << "attempted assignment to self"
<< abort(FatalError); << abort(FatalError);
} }
@ -431,13 +419,7 @@ void Foam::List<T>::operator=(const List<T>& a)
template<class T> template<class T>
void Foam::List<T>::operator=(const SLList<T>& lst) void Foam::List<T>::operator=(const SLList<T>& lst)
{ {
if (lst.size() != this->size_) reAlloc(lst.size());
{
if (this->v_) delete[] this->v_;
this->v_ = 0;
this->size_ = lst.size();
if (this->size_) this->v_ = new T[this->size_];
}
if (this->size_) if (this->size_)
{ {
@ -459,18 +441,8 @@ void Foam::List<T>::operator=(const SLList<T>& lst)
template<class T> template<class T>
void Foam::List<T>::operator=(const IndirectList<T>& lst) void Foam::List<T>::operator=(const IndirectList<T>& lst)
{ {
if (lst.size() != this->size_) reAlloc(lst.size());
{ copyList(lst);
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];
}
} }
@ -478,18 +450,8 @@ void Foam::List<T>::operator=(const IndirectList<T>& lst)
template<class T> template<class T>
void Foam::List<T>::operator=(const UIndirectList<T>& lst) void Foam::List<T>::operator=(const UIndirectList<T>& lst)
{ {
if (lst.size() != this->size_) reAlloc(lst.size());
{ copyList(lst);
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];
}
} }
@ -497,18 +459,8 @@ void Foam::List<T>::operator=(const UIndirectList<T>& lst)
template<class T> template<class T>
void Foam::List<T>::operator=(const BiIndirectList<T>& lst) void Foam::List<T>::operator=(const BiIndirectList<T>& lst)
{ {
if (lst.size() != this->size_) reAlloc(lst.size());
{ copyList(lst);
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];
}
} }
// * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * // // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //

View file

@ -205,8 +205,8 @@ public:
//- Reset size of List and value for new elements. //- Reset size of List and value for new elements.
void setSize(const label, const T&); void setSize(const label, const T&);
//- Clear the list, i.e. set size to zero. //- Clear the list, i.e. set size to zero
void clear(); inline void clear();
//- Append an element at the end of the list //- Append an element at the end of the list
inline void append(const T&); inline void append(const T&);

View file

@ -91,8 +91,7 @@ inline Foam::List<T>::List
InputIterator iter = first; InputIterator iter = first;
forAll(*this, i) forAll(*this, i)
{ {
this->operator[](i) = *iter; this->operator[](i) = *iter++;
++iter;
} }
} }
} }
@ -121,6 +120,19 @@ inline const Foam::List<T>& Foam::List<T>::null()
} }
template<class T>
inline void Foam::List<T>::clear()
{
if (this->v_)
{
delete[] this->v_;
this->v_ = 0;
}
this->size_ = 0;
}
template<class T> template<class T>
inline void Foam::List<T>::resize(const label newSize) inline void Foam::List<T>::resize(const label newSize)
{ {