diff --git a/applications/test/List/Test-List.C b/applications/test/List/Test-List.C index dcf168fd92..65073700ab 100644 --- a/applications/test/List/Test-List.C +++ b/applications/test/List/Test-List.C @@ -100,6 +100,14 @@ int main(int argc, char *argv[]) {8, 1, 0} }; Info<< "list5: " << list5 << endl; + list5 = + { + {8, 1, 0}, + {5, 3, 1}, + {10, 2, 2} + + }; + Info<< "list5: " << list5 << endl; list4.swap(list5); Info<< "Swapped via the swap() method" << endl; diff --git a/src/OpenFOAM/containers/Lists/List/List.C b/src/OpenFOAM/containers/Lists/List/List.C index c86017245b..0e4dee96ac 100644 --- a/src/OpenFOAM/containers/Lists/List/List.C +++ b/src/OpenFOAM/containers/Lists/List/List.C @@ -33,43 +33,6 @@ License #include "BiIndirectList.H" #include "contiguous.H" -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // - -template -template -void Foam::List::CopyList(const List2& lst) -{ - if (this->size_) - { - this->v_ = new T[this->size_]; - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } - } -} - - -template -template -Foam::List::List(InputIterator first, InputIterator last, const label s) -: - UList(nullptr, s) -{ - if (this->size_) - { - this->v_ = new T[this->size_]; - - InputIterator iter = first; - forAll(*this, i) - { - this->operator[](i) = *iter++; - } - } -} - - // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // template @@ -84,10 +47,7 @@ Foam::List::List(const label s) << abort(FatalError); } - if (this->size_) - { - this->v_ = new T[this->size_]; - } + alloc(); } @@ -103,10 +63,10 @@ Foam::List::List(const label s, const T& a) << abort(FatalError); } + alloc(); + if (this->size_) { - this->v_ = new T[this->size_]; - List_ACCESS(T, (*this), vp); List_FOR_ALL((*this), i) List_ELEM((*this), vp, i) = a; @@ -127,10 +87,10 @@ Foam::List::List(const label s, const zero) << abort(FatalError); } + alloc(); + if (this->size_) { - this->v_ = new T[this->size_]; - List_ACCESS(T, (*this), vp); List_FOR_ALL((*this), i) List_ELEM((*this), vp, i) = Zero; @@ -146,7 +106,7 @@ Foam::List::List(const List& a) { if (this->size_) { - this->v_ = new T[this->size_]; + alloc(); #ifdef USEMEMCPY if (contiguous()) @@ -174,7 +134,7 @@ Foam::List::List(const List& a) { if (this->size_) { - this->v_ = new T[this->size_]; + alloc(); List_ACCESS(T, (*this), vp); List_CONST_ACCESS(T2, a, ap); @@ -205,7 +165,7 @@ Foam::List::List(List& a, bool reuse) } else if (this->size_) { - this->v_ = new T[this->size_]; + alloc(); #ifdef USEMEMCPY if (contiguous()) @@ -234,7 +194,7 @@ Foam::List::List(const UList& a, const labelUList& map) { // Note:cannot use List_ELEM since third argument has to be index. - this->v_ = new T[this->size_]; + alloc(); forAll(*this, i) { @@ -258,7 +218,7 @@ Foam::List::List(const FixedList& lst) : UList(nullptr, Size) { - CopyList(lst); + allocCopyList(lst); } @@ -267,7 +227,7 @@ Foam::List::List(const PtrList& lst) : UList(nullptr, lst.size()) { - CopyList(lst); + allocCopyList(lst); } @@ -283,7 +243,7 @@ Foam::List::List(const UIndirectList& lst) : UList(nullptr, lst.size()) { - CopyList(lst); + allocCopyList(lst); } @@ -292,7 +252,7 @@ Foam::List::List(const BiIndirectList& lst) : UList(nullptr, lst.size()) { - CopyList(lst); + allocCopyList(lst); } @@ -308,7 +268,10 @@ Foam::List::List(std::initializer_list lst) template Foam::List::~List() { - if (this->v_) delete[] this->v_; + if (this->v_) + { + delete[] this->v_; + } } @@ -320,7 +283,7 @@ void Foam::List::setSize(const label newSize) if (newSize < 0) { FatalErrorInFunction - << "bad set size " << newSize + << "bad size " << newSize << abort(FatalError); } @@ -347,8 +310,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; } @@ -375,19 +338,10 @@ 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; -} - - template void Foam::List::transfer(List& a) { - if (this->v_) delete[] this->v_; + clear(); this->size_ = a.size_; this->v_ = a.v_; @@ -421,13 +375,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_) { @@ -466,13 +414,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_) { @@ -493,35 +435,28 @@ void Foam::List::operator=(const SLList& 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); } 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_]; - } + reAlloc(lst.size()); + copyList(lst); +} + +template +void Foam::List::operator=(std::initializer_list lst) +{ + reAlloc(lst.size()); + + typename std::initializer_list::iterator iter = lst.begin(); forAll(*this, i) { - this->operator[](i) = lst[i]; + this->operator[](i) = *iter++; } } diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index 6996c8b2c3..d93e5fe86d 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -85,13 +85,23 @@ class List { // Private member functions + //- Allocate list storage + inline void alloc(); + + //- Reallocate list storage to the given size + inline void reAlloc(const label s); + //- Copy list of given type template - void CopyList(const List2&); + inline void copyList(const List2&); + + //- Allocate storage and copy list of given type + template + inline void allocCopyList(const List2&); //- Construct given start and end iterators and number of elements template - List(InputIterator first, InputIterator last, const label s); + inline List(InputIterator first, InputIterator last, const label s); protected: @@ -160,7 +170,7 @@ public: explicit List(const BiIndirectList&); //- Construct from an initializer list - List(std::initializer_list lst); + List(std::initializer_list); //- Construct from Istream List(Istream&); @@ -200,7 +210,7 @@ public: void setSize(const label, const T&); //- Clear the list, i.e. set size to zero - void clear(); + inline void clear(); //- Append an element at the end of the list inline void append(const T&); @@ -252,6 +262,9 @@ public: //- Assignment from BiIndirectList operator. Takes linear time void operator=(const BiIndirectList&); + //- Construct from an initializer list + void operator=(std::initializer_list); + //- Assignment of all entries to the given value inline void operator=(const T&); diff --git a/src/OpenFOAM/containers/Lists/List/ListI.H b/src/OpenFOAM/containers/Lists/List/ListI.H index 7d4b325140..5709c258ec 100644 --- a/src/OpenFOAM/containers/Lists/List/ListI.H +++ b/src/OpenFOAM/containers/Lists/List/ListI.H @@ -23,6 +23,80 @@ License \*---------------------------------------------------------------------------*/ +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +template +inline void Foam::List::alloc() +{ + if (this->size_) + { + this->v_ = new T[this->size_]; + } +} + + +template +inline void Foam::List::reAlloc(const label s) +{ + if (this->size_ != s) + { + clear(); + this->size_ = s; + alloc(); + } +} + + +template +template +inline void Foam::List::copyList(const List2& lst) +{ + if (this->size_) + { + forAll(*this, i) + { + this->operator[](i) = lst[i]; + } + } +} + + +template +template +inline void Foam::List::allocCopyList(const List2& lst) +{ + if (this->size_) + { + alloc(); + copyList(lst); + } +} + + +template +template +inline Foam::List::List +( + InputIterator first, + InputIterator last, + const label s +) +: + UList(nullptr, s) +{ + if (this->size_) + { + alloc(); + + InputIterator iter = first; + forAll(*this, i) + { + this->operator[](i) = *iter++; + } + } +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template @@ -46,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) {