diff --git a/applications/test/List/Test-List.C b/applications/test/List/Test-List.C index dcf168fd920e65b5d62621c1eea01af166396cf6..65073700ab173bc4caee99792548a0e43f2afd0c 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 c86017245b31dfe889fb484e93d4932bfdfaf75d..0e4dee96ac722d7c05d9f0127181dcd3dd8abfb4 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<class T> -template<class List2> -void Foam::List<T>::CopyList(const List2& lst) -{ - if (this->size_) - { - this->v_ = new T[this->size_]; - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } - } -} - - -template<class T> -template<class InputIterator> -Foam::List<T>::List(InputIterator first, InputIterator last, const label s) -: - UList<T>(nullptr, s) -{ - if (this->size_) - { - this->v_ = new T[this->size_]; - - InputIterator iter = first; - forAll(*this, i) - { - this->operator[](i) = *iter++; - } - } -} - - // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // template<class T> @@ -84,10 +47,7 @@ Foam::List<T>::List(const label s) << abort(FatalError); } - if (this->size_) - { - this->v_ = new T[this->size_]; - } + alloc(); } @@ -103,10 +63,10 @@ Foam::List<T>::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<T>::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<T>::List(const List<T>& a) { if (this->size_) { - this->v_ = new T[this->size_]; + alloc(); #ifdef USEMEMCPY if (contiguous<T>()) @@ -174,7 +134,7 @@ Foam::List<T>::List(const List<T2>& 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<T>::List(List<T>& a, bool reuse) } else if (this->size_) { - this->v_ = new T[this->size_]; + alloc(); #ifdef USEMEMCPY if (contiguous<T>()) @@ -234,7 +194,7 @@ Foam::List<T>::List(const UList<T>& 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<T>::List(const FixedList<T, Size>& lst) : UList<T>(nullptr, Size) { - CopyList(lst); + allocCopyList(lst); } @@ -267,7 +227,7 @@ Foam::List<T>::List(const PtrList<T>& lst) : UList<T>(nullptr, lst.size()) { - CopyList(lst); + allocCopyList(lst); } @@ -283,7 +243,7 @@ Foam::List<T>::List(const UIndirectList<T>& lst) : UList<T>(nullptr, lst.size()) { - CopyList(lst); + allocCopyList(lst); } @@ -292,7 +252,7 @@ Foam::List<T>::List(const BiIndirectList<T>& lst) : UList<T>(nullptr, lst.size()) { - CopyList(lst); + allocCopyList(lst); } @@ -308,7 +268,10 @@ Foam::List<T>::List(std::initializer_list<T> lst) template<class T> Foam::List<T>::~List() { - if (this->v_) delete[] this->v_; + if (this->v_) + { + delete[] this->v_; + } } @@ -320,7 +283,7 @@ void Foam::List<T>::setSize(const label newSize) if (newSize < 0) { FatalErrorInFunction - << "bad set size " << newSize + << "bad size " << newSize << abort(FatalError); } @@ -347,8 +310,8 @@ void Foam::List<T>::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<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; -} - - template<class T> void Foam::List<T>::transfer(List<T>& a) { - if (this->v_) delete[] this->v_; + clear(); this->size_ = a.size_; this->v_ = a.v_; @@ -421,13 +375,7 @@ void Foam::List<T>::transfer(SortableList<T>& a) template<class T> void Foam::List<T>::operator=(const UList<T>& 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<T>::operator=(const List<T>& a) template<class T> void Foam::List<T>::operator=(const SLList<T>& 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<T>::operator=(const SLList<T>& lst) template<class T> void Foam::List<T>::operator=(const UIndirectList<T>& 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<class T> void Foam::List<T>::operator=(const BiIndirectList<T>& 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<class T> +void Foam::List<T>::operator=(std::initializer_list<T> lst) +{ + reAlloc(lst.size()); + typename std::initializer_list<T>::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 6996c8b2c323583ddec224c0e022e8c877bdc735..d93e5fe86d72ca2f71a0a26fc2e16770c7ff48d1 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<class List2> - void CopyList(const List2&); + inline void copyList(const List2&); + + //- Allocate storage and copy list of given type + template<class List2> + inline void allocCopyList(const List2&); //- Construct given start and end iterators and number of elements template<class InputIterator> - 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<T>&); //- Construct from an initializer list - List(std::initializer_list<T> lst); + List(std::initializer_list<T>); //- 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<T>&); + //- Construct from an initializer list + void operator=(std::initializer_list<T>); + //- 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 7d4b325140fb689ba0fb73612c98de231d85c571..5709c258ec7e2642aa1a478d36ab2f17fe8b5627 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<class T> +inline void Foam::List<T>::alloc() +{ + if (this->size_) + { + this->v_ = new T[this->size_]; + } +} + + +template<class T> +inline void Foam::List<T>::reAlloc(const label s) +{ + if (this->size_ != s) + { + clear(); + this->size_ = s; + alloc(); + } +} + + +template<class T> +template<class List2> +inline void Foam::List<T>::copyList(const List2& lst) +{ + if (this->size_) + { + forAll(*this, i) + { + this->operator[](i) = lst[i]; + } + } +} + + +template<class T> +template<class List2> +inline void Foam::List<T>::allocCopyList(const List2& lst) +{ + if (this->size_) + { + alloc(); + copyList(lst); + } +} + + +template<class T> +template<class InputIterator> +inline Foam::List<T>::List +( + InputIterator first, + InputIterator last, + const label s +) +: + UList<T>(nullptr, s) +{ + if (this->size_) + { + alloc(); + + InputIterator iter = first; + forAll(*this, i) + { + this->operator[](i) = *iter++; + } + } +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class T> @@ -46,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> inline void Foam::List<T>::resize(const label newSize) {