ENH: avoid std::distance for std::initializer_list

- std::initializer_list has its own size() method, so no need to use
  std::distance.

STYLE/BUG: use separate iterator de-reference and increment in List

- avoids unnecessary copying of iterators, and avoids any potentially
  odd behaviour with the combination with incrementing.

ENH: support construct from iterator pair for DynamicList, SortableList
This commit is contained in:
Mark Olesen
2017-05-14 16:28:21 +02:00
parent 83669e284f
commit 0c53a815ed
9 changed files with 217 additions and 152 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -59,14 +59,14 @@ class DynamicList;
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
Ostream& operator<< Ostream& operator<<
( (
Ostream&, Ostream& os,
const DynamicList<T, SizeInc, SizeMult, SizeDiv>& const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
); );
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
Istream& operator>> Istream& operator>>
( (
Istream&, Istream& is,
DynamicList<T, SizeInc, SizeMult, SizeDiv>& DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
); );
@ -105,29 +105,37 @@ public:
inline DynamicList(); inline DynamicList();
//- Construct given size. //- Construct given size.
explicit inline DynamicList(const label); explicit inline DynamicList(const label nElem);
//- Construct with given size and value for all elements. //- Construct with given size and value for all elements.
inline DynamicList(const label, const T&); inline DynamicList(const label nElem, const T& a);
//- Construct copy. //- Construct copy.
inline DynamicList(const DynamicList<T, SizeInc, SizeMult, SizeDiv>&); inline DynamicList
(
const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
);
//- Construct from UList. Size set to UList size. //- Construct from UList. Size set to UList size.
// Also constructs from DynamicList with different sizing parameters. // Also constructs from DynamicList with different sizing parameters.
explicit inline DynamicList(const UList<T>&); explicit inline DynamicList(const UList<T>& lst);
//- Construct given begin/end iterators.
// Uses std::distance to determine the size.
template<class InputIterator>
inline DynamicList(InputIterator begIter, InputIterator endIter);
//- Construct from an initializer list. Size set to list size. //- Construct from an initializer list. Size set to list size.
explicit inline DynamicList(std::initializer_list<T>); explicit inline DynamicList(std::initializer_list<T> lst);
//- Construct from UIndirectList. Size set to UIndirectList size. //- Construct from UIndirectList. Size set to UIndirectList size.
explicit inline DynamicList(const UIndirectList<T>&); explicit inline DynamicList(const UIndirectList<T>& lst);
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
explicit inline DynamicList(const Xfer<List<T>>&); explicit inline DynamicList(const Xfer<List<T>>& lst);
//- Construct from Istream. Size set to size of list read. //- Construct from Istream. Size set to size of list read.
explicit DynamicList(Istream&); explicit DynamicList(Istream& is);
// Member Functions // Member Functions
@ -143,31 +151,31 @@ public:
// The addressed size will be truncated if needed to fit, but will // The addressed size will be truncated if needed to fit, but will
// remain otherwise untouched. // remain otherwise untouched.
// Use this or reserve() in combination with append(). // Use this or reserve() in combination with append().
inline void setCapacity(const label); inline void setCapacity(const label nElem);
//- Alter the addressed list size. //- Alter the addressed list size.
// New space will be allocated if required. // New space will be allocated if required.
// Use this to resize the list prior to using the operator[] for // Use this to resize the list prior to using the operator[] for
// setting values (as per List usage). // setting values (as per List usage).
inline void setSize(const label); inline void setSize(const label nElem);
//- Alter the addressed list size and fill new space with a //- Alter the addressed list size and fill new space with a
// constant. // constant.
inline void setSize(const label, const T&); inline void setSize(const label nElem, const T& t);
//- Alter the addressed list size. //- Alter the addressed list size.
// New space will be allocated if required. // New space will be allocated if required.
// Use this to resize the list prior to using the operator[] for // Use this to resize the list prior to using the operator[] for
// setting values (as per List usage). // setting values (as per List usage).
inline void resize(const label); inline void resize(const label nElem);
//- Alter the addressed list size and fill new space with a //- Alter the addressed list size and fill new space with a
// constant. // constant.
inline void resize(const label, const T&); inline void resize(const label nElem, const T& t);
//- Reserve allocation space for at least this size. //- Reserve allocation space for at least this size.
// Never shrinks the allocated size, use setCapacity() for that. // Never shrinks the allocated size, use setCapacity() for that.
inline void reserve(const label); inline void reserve(const label nElem);
//- Clear the addressed list, i.e. set the size to zero. //- Clear the addressed list, i.e. set the size to zero.
// Allocated size does not change // Allocated size does not change
@ -181,10 +189,13 @@ public:
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& shrink(); inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& shrink();
//- Transfer contents of the argument List into this. //- Transfer contents of the argument List into this.
inline void transfer(List<T>&); inline void transfer(List<T>& lst);
//- Transfer contents of the argument DynamicList into this. //- Transfer contents of the argument DynamicList into this.
inline void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&); inline void transfer
(
DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
);
//- Transfer contents to the Xfer container as a plain List //- Transfer contents to the Xfer container as a plain List
inline Xfer<List<T>> xfer(); inline Xfer<List<T>> xfer();
@ -195,25 +206,25 @@ public:
//- Append an element at the end of the list //- Append an element at the end of the list
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append
( (
const T& const T& t
); );
//- Append a List at the end of this list //- Append a List at the end of this list
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append
( (
const UList<T>& const UList<T>& lst
); );
//- Append an initializer list at the end of this list. //- Append an initializer list at the end of this list.
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append
( (
std::initializer_list<T> std::initializer_list<T> lst
); );
//- Append a UIndirectList at the end of this list //- Append a UIndirectList at the end of this list
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append
( (
const UIndirectList<T>& const UIndirectList<T>& lst
); );
//- Remove and return the top element //- Remove and return the top element
@ -221,32 +232,35 @@ public:
//- Return non-const access to an element, resizing list if //- Return non-const access to an element, resizing list if
// necessary // necessary
inline T& operator()(const label); inline T& operator()(const label elemI);
//- Assignment of all addressed entries to the given value //- Assignment of all addressed entries to the given value
inline void operator=(const T&); inline void operator=(const T& t);
//- Assignment to DynamicList //- Assignment to DynamicList
inline void operator= inline void operator=
( (
const DynamicList<T, SizeInc, SizeMult, SizeDiv>& const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
); );
//- Assignment to UList //- Assignment to UList
inline void operator=(const UList<T>&); inline void operator=(const UList<T>& lst);
//- Assignment from initializer list //- Assignment from initializer list
inline void operator=(std::initializer_list<T>); inline void operator=(std::initializer_list<T> lst);
//- Assignment to UIndirectList //- Assignment to UIndirectList
inline void operator=(const UIndirectList<T>&); inline void operator=(const UIndirectList<T>& lst);
// STL member functions // STL member functions
//- Erase an element, move the remaining elements to fill the gap //- Erase an element, move the remaining elements to fill the gap
// and resize the List // and resize the List
typename UList<T>::iterator erase(typename UList<T>::iterator); typename UList<T>::iterator erase
(
typename UList<T>::iterator curIter
);
// IOstream operators // IOstream operators
@ -254,15 +268,15 @@ public:
// Write DynamicList to Ostream. // Write DynamicList to Ostream.
friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv> friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv>
( (
Ostream&, Ostream& os,
const DynamicList<T, SizeInc, SizeMult, SizeDiv>& const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
); );
//- Read from Istream, discarding contents of existing DynamicList. //- Read from Istream, discarding contents of existing DynamicList.
friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv> friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv>
( (
Istream&, Istream& is,
DynamicList<T, SizeInc, SizeMult, SizeDiv>& DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
); );
}; };

View File

@ -80,6 +80,19 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
{} {}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
template<class InputIterator>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
(
InputIterator begIter,
InputIterator endIter
)
:
List<T>(begIter, endIter),
capacity_(this->size())
{}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
( (

View File

@ -128,11 +128,12 @@ public:
explicit inline FixedList(const T& t); explicit inline FixedList(const T& t);
//- Construct from C-array //- Construct from C-array
explicit inline FixedList(const T v[Size]); explicit inline FixedList(const T lst[Size]);
//- Construct given start and end iterators //- Construct given begin/end iterators
// Uses std::distance when verifying the size.
template<class InputIterator> template<class InputIterator>
inline FixedList(InputIterator first, InputIterator last); inline FixedList(InputIterator begIter, InputIterator endIter);
//- Construct from an initializer list //- Construct from an initializer list
inline FixedList(std::initializer_list<T> lst); inline FixedList(std::initializer_list<T> lst);

View File

@ -37,7 +37,7 @@ inline Foam::FixedList<T, Size>::FixedList()
template<class T, unsigned Size> template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(const T& t) inline Foam::FixedList<T, Size>::FixedList(const T& t)
{ {
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = t; v_[i] = t;
} }
@ -45,11 +45,11 @@ inline Foam::FixedList<T, Size>::FixedList(const T& t)
template<class T, unsigned Size> template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(const T v[Size]) inline Foam::FixedList<T, Size>::FixedList(const T lst[Size])
{ {
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = v[i]; v_[i] = lst[i];
} }
} }
@ -58,25 +58,33 @@ template<class T, unsigned Size>
template<class InputIterator> template<class InputIterator>
Foam::FixedList<T, Size>::FixedList Foam::FixedList<T, Size>::FixedList
( (
InputIterator first, InputIterator begIter,
InputIterator last InputIterator endIter
) )
{ {
checkSize(std::distance(first, last)); checkSize(std::distance(begIter, endIter));
InputIterator iter = first; InputIterator iter = begIter;
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = *iter++; v_[i] = *iter;
++iter;
} }
} }
template<class T, unsigned Size> template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(std::initializer_list<T> lst) inline Foam::FixedList<T, Size>::FixedList(std::initializer_list<T> lst)
: {
FixedList<T, Size>(lst.begin(), lst.end()) checkSize(lst.size());
{}
auto iter = lst.begin();
for (unsigned i=0; i<Size; ++i)
{
v_[i] = *iter;
++iter;
}
}
template<class T, unsigned Size> template<class T, unsigned Size>
@ -84,7 +92,7 @@ inline Foam::FixedList<T, Size>::FixedList(const UList<T>& lst)
{ {
checkSize(lst.size()); checkSize(lst.size());
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = lst[i]; v_[i] = lst[i];
} }
@ -97,9 +105,10 @@ inline Foam::FixedList<T, Size>::FixedList(const SLList<T>& lst)
checkSize(lst.size()); checkSize(lst.size());
typename SLList<T>::const_iterator iter = lst.begin(); typename SLList<T>::const_iterator iter = lst.begin();
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = *iter++; v_[i] = *iter;
++iter;
} }
} }
@ -107,7 +116,7 @@ inline Foam::FixedList<T, Size>::FixedList(const SLList<T>& lst)
template<class T, unsigned Size> template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(const FixedList<T, Size>& lst) inline Foam::FixedList<T, Size>::FixedList(const FixedList<T, Size>& lst)
{ {
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = lst[i]; v_[i] = lst[i];
} }
@ -200,7 +209,7 @@ inline void Foam::FixedList<T, Size>::setSize(const label s)
template<class T, unsigned Size> template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::transfer(const FixedList<T, Size>& lst) inline void Foam::FixedList<T, Size>::transfer(const FixedList<T, Size>& lst)
{ {
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = lst[i]; v_[i] = lst[i];
} }
@ -276,7 +285,7 @@ inline const T& Foam::FixedList<T, Size>::operator[](const label i) const
template<class T, unsigned Size> template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::operator=(const T lst[Size]) inline void Foam::FixedList<T, Size>::operator=(const T lst[Size])
{ {
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = lst[i]; v_[i] = lst[i];
} }
@ -287,7 +296,7 @@ inline void Foam::FixedList<T, Size>::operator=(const UList<T>& lst)
{ {
checkSize(lst.size()); checkSize(lst.size());
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = lst[i]; v_[i] = lst[i];
} }
@ -299,9 +308,10 @@ inline void Foam::FixedList<T, Size>::operator=(const SLList<T>& lst)
checkSize(lst.size()); checkSize(lst.size());
typename SLList<T>::const_iterator iter = lst.begin(); typename SLList<T>::const_iterator iter = lst.begin();
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = *iter++; v_[i] = *iter;
++iter;
} }
} }
@ -310,17 +320,18 @@ inline void Foam::FixedList<T, Size>::operator=(std::initializer_list<T> lst)
{ {
checkSize(lst.size()); checkSize(lst.size());
typename std::initializer_list<T>::iterator iter = lst.begin(); auto iter = lst.begin();
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = *iter++; v_[i] = *iter;
++iter;
} }
} }
template<class T, unsigned Size> template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::operator=(const T& t) inline void Foam::FixedList<T, Size>::operator=(const T& t)
{ {
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
v_[i] = t; v_[i] = t;
} }
@ -464,7 +475,7 @@ inline unsigned Foam::FixedList<T, Size>::Hash<HashT>::operator()
// Hash incrementally // Hash incrementally
unsigned val = seed; unsigned val = seed;
for (unsigned i=0; i<Size; i++) for (unsigned i=0; i<Size; ++i)
{ {
val = HashT()(lst[i], val); val = HashT()(lst[i], val);
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -160,7 +160,7 @@ Foam::List<T>::List(List<T>& a, bool reuse)
if (reuse) if (reuse)
{ {
this->v_ = a.v_; this->v_ = a.v_;
a.v_ = 0; a.v_ = nullptr;
a.size_ = 0; a.size_ = 0;
} }
else if (this->size_) else if (this->size_)
@ -186,19 +186,19 @@ Foam::List<T>::List(List<T>& a, bool reuse)
template<class T> template<class T>
Foam::List<T>::List(const UList<T>& a, const labelUList& map) Foam::List<T>::List(const UList<T>& a, const labelUList& mapAddressing)
: :
UList<T>(nullptr, map.size()) UList<T>(nullptr, mapAddressing.size())
{ {
if (this->size_) if (this->size_)
{ {
// Note:cannot use List_ELEM since third argument has to be index. // Note: cannot use List_ELEM since third argument has to be index.
alloc(); alloc();
forAll(*this, i) forAll(*this, i)
{ {
this->operator[](i) = a[map[i]]; this->operator[](i) = a[mapAddressing[i]];
} }
} }
} }
@ -206,9 +206,9 @@ Foam::List<T>::List(const UList<T>& a, const labelUList& map)
template<class T> template<class T>
template<class InputIterator> template<class InputIterator>
Foam::List<T>::List(InputIterator first, InputIterator last) Foam::List<T>::List(InputIterator begIter, InputIterator endIter)
: :
List<T>(first, last, std::distance(first, last)) List<T>(begIter, endIter, std::distance(begIter, endIter))
{} {}
@ -231,6 +231,8 @@ Foam::List<T>::List(const PtrList<T>& lst)
} }
// Note: using first/last is not entirely accurate.
// But since the list size is correct, last() is actually ignored.
template<class T> template<class T>
Foam::List<T>::List(const SLList<T>& lst) Foam::List<T>::List(const SLList<T>& lst)
: :
@ -259,7 +261,7 @@ Foam::List<T>::List(const BiIndirectList<T>& lst)
template<class T> template<class T>
Foam::List<T>::List(std::initializer_list<T> lst) Foam::List<T>::List(std::initializer_list<T> lst)
: :
List<T>(lst.begin(), lst.end()) List<T>(lst.begin(), lst.end(), lst.size())
{} {}
@ -326,7 +328,7 @@ void Foam::List<T>::setSize(const label newSize)
template<class T> template<class T>
void Foam::List<T>::setSize(const label newSize, const T& a) void Foam::List<T>::setSize(const label newSize, const T& a)
{ {
label oldSize = label(this->size_); const label oldSize = label(this->size_);
this->setSize(newSize); this->setSize(newSize);
if (newSize > oldSize) if (newSize > oldSize)
@ -346,7 +348,7 @@ void Foam::List<T>::transfer(List<T>& a)
this->v_ = a.v_; this->v_ = a.v_;
a.size_ = 0; a.size_ = 0;
a.v_ = 0; a.v_ = nullptr;
} }
@ -419,14 +421,9 @@ void Foam::List<T>::operator=(const SLList<T>& lst)
if (this->size_) if (this->size_)
{ {
label i = 0; label i = 0;
for for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter)
(
typename SLList<T>::const_iterator iter = lst.begin();
iter != lst.end();
++iter
)
{ {
this->operator[](i++) = iter(); this->operator[](i++) = *iter;
} }
} }
} }
@ -453,10 +450,11 @@ void Foam::List<T>::operator=(std::initializer_list<T> lst)
{ {
reAlloc(lst.size()); reAlloc(lst.size());
typename std::initializer_list<T>::iterator iter = lst.begin(); auto iter = lst.begin();
forAll(*this, i) forAll(*this, i)
{ {
this->operator[](i) = *iter++; this->operator[](i) = *iter;
++iter;
} }
} }

View File

@ -58,7 +58,7 @@ class Ostream;
// Forward declaration of friend functions and operators // Forward declaration of friend functions and operators
template<class T> class List; template<class T> class List;
template<class T> Istream& operator>>(Istream&, List<T>&); template<class T> Istream& operator>>(Istream& is, List<T>& L);
template<class T, unsigned Size> class FixedList; template<class T, unsigned Size> class FixedList;
template<class T> class PtrList; template<class T> class PtrList;
@ -95,22 +95,28 @@ class List
//- Copy list of given type //- Copy list of given type
template<class List2> template<class List2>
inline void copyList(const List2&); inline void copyList(const List2& lst);
//- Allocate storage and copy list of given type //- Allocate storage and copy list of given type
template<class List2> template<class List2>
inline void allocCopyList(const List2&); inline void allocCopyList(const List2& lst);
//- Construct given start and end iterators and number of elements //- Construct given begin/end iterators and number of elements
// Since the size is provided, the end iterator is actually ignored.
template<class InputIterator> template<class InputIterator>
inline List(InputIterator first, InputIterator last, const label s); inline List
(
InputIterator begIter,
InputIterator endIter,
const label s
);
protected: protected:
//- Override size to be inconsistent with allocated storage. //- Override size to be inconsistent with allocated storage.
// Use with care // Use with care
inline void size(const label); inline void size(const label n);
public: public:
@ -127,55 +133,56 @@ public:
inline List(); inline List();
//- Construct with given size //- Construct with given size
explicit List(const label); explicit List(const label s);
//- Construct with given size and value for all elements //- Construct with given size and value for all elements
List(const label, const T&); List(const label s, const T& a);
//- Construct with given size initializing all elements to zero //- Construct with given size initializing all elements to zero
List(const label, const zero); List(const label s, const zero);
//- Copy constructor //- Copy constructor
List(const List<T>&); List(const List<T>& a);
//- Copy constructor from list containing another type //- Copy constructor from list containing another type
template<class T2> template<class T2>
explicit List(const List<T2>&); explicit List(const List<T2>& a);
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
List(const Xfer<List<T>>&); List(const Xfer<List<T>>& lst);
//- Construct as copy or re-use as specified //- Construct as copy or re-use as specified
List(List<T>&, bool reuse); List(List<T>& a, bool reuse);
//- Construct as subset //- Construct as subset
List(const UList<T>&, const labelUList& mapAddressing); List(const UList<T>& a, const labelUList& mapAddressing);
//- Construct given start and end iterators //- Construct given begin/end iterators.
// Uses std::distance to determine the size.
template<class InputIterator> template<class InputIterator>
List(InputIterator first, InputIterator last); List(InputIterator begIter, InputIterator endIter);
//- Construct as copy of FixedList<T, Size> //- Construct as copy of FixedList<T, Size>
template<unsigned Size> template<unsigned Size>
explicit List(const FixedList<T, Size>&); explicit List(const FixedList<T, Size>& lst);
//- Construct as copy of PtrList<T> //- Construct as copy of PtrList<T>
explicit List(const PtrList<T>&); explicit List(const PtrList<T>& lst);
//- Construct as copy of SLList<T> //- Construct as copy of SLList<T>
explicit List(const SLList<T>&); explicit List(const SLList<T>& lst);
//- Construct as copy of UIndirectList<T> //- Construct as copy of UIndirectList<T>
explicit List(const UIndirectList<T>&); explicit List(const UIndirectList<T>& lst);
//- Construct as copy of BiIndirectList<T> //- Construct as copy of BiIndirectList<T>
explicit List(const BiIndirectList<T>&); explicit List(const BiIndirectList<T>& lst);
//- Construct from an initializer list //- Construct from an initializer list
List(std::initializer_list<T>); List(std::initializer_list<T> lst);
//- Construct from Istream //- Construct from Istream
List(Istream&); List(Istream& is);
//- Clone //- Clone
inline autoPtr<List<T>> clone() const; inline autoPtr<List<T>> clone() const;
@ -200,47 +207,48 @@ public:
// Edit // Edit
//- Alias for setSize(const label) //- Alias for setSize(const label)
inline void resize(const label); inline void resize(const label newSize);
//- Alias for setSize(const label, const T&) //- Alias for setSize(const label, const T&)
inline void resize(const label, const T&); inline void resize(const label newSize, const T& a);
//- Reset size of List //- Reset size of List
void setSize(const label); void setSize(const label newSize);
//- 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 newSize, const T& a);
//- Clear the list, i.e. set size to zero //- Clear the list, i.e. set size to zero
inline 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& t);
//- Append a List at the end of this list //- Append a List at the end of this list
inline void append(const UList<T>&); inline void append(const UList<T>& lst);
//- Append a UIndirectList at the end of this list //- Append a UIndirectList at the end of this list
inline void append(const UIndirectList<T>&); inline void append(const UIndirectList<T>& lst);
//- Transfer the contents of the argument List into this list //- Transfer the contents of the argument List into this list
// and annul the argument list // and annul the argument list
void transfer(List<T>&); void transfer(List<T>& a);
//- Transfer the contents of the argument List into this list //- Transfer the contents of the argument List into this list
// and annul the argument list // and annul the argument list
template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&); void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>& a);
//- Transfer the contents of the argument List into this list //- Transfer the contents of the argument List into this list
// and annul the argument list // and annul the argument list
void transfer(SortableList<T>&); void transfer(SortableList<T>& a);
//- Transfer contents to the Xfer container //- Transfer contents to the Xfer container
inline Xfer<List<T>> xfer(); inline Xfer<List<T>> xfer();
//- Return subscript-checked element of UList //- Return subscript-checked element of UList.
inline T& newElmt(const label); // Resize list if required.
inline T& newElmt(const label i);
//- Disallow implicit shallowCopy //- Disallow implicit shallowCopy
@ -250,25 +258,25 @@ public:
// Member operators // Member operators
//- Assignment to UList operator. Takes linear time //- Assignment to UList operator. Takes linear time
void operator=(const UList<T>&); void operator=(const UList<T>& a);
//- Assignment operator. Takes linear time //- Assignment operator. Takes linear time
void operator=(const List<T>&); void operator=(const List<T>& a);
//- Assignment to SLList operator. Takes linear time //- Assignment to SLList operator. Takes linear time
void operator=(const SLList<T>&); void operator=(const SLList<T>& lst);
//- Assignment to UIndirectList operator. Takes linear time //- Assignment to UIndirectList operator. Takes linear time
void operator=(const UIndirectList<T>&); void operator=(const UIndirectList<T>& lst);
//- Assignment to BiIndirectList operator. Takes linear time //- Assignment to BiIndirectList operator. Takes linear time
void operator=(const BiIndirectList<T>&); void operator=(const BiIndirectList<T>& lst);
//- Assignment to an initializer list //- Assignment to an initializer list
void operator=(std::initializer_list<T>); void operator=(std::initializer_list<T> lst);
//- Assignment of all entries to the given value //- Assignment of all entries to the given value
inline void operator=(const T&); inline void operator=(const T& t);
//- Assignment of all entries to zero //- Assignment of all entries to zero
inline void operator=(const zero); inline void operator=(const zero);
@ -278,7 +286,7 @@ public:
//- Read List from Istream, discarding contents of existing List //- Read List from Istream, discarding contents of existing List
friend Istream& operator>> <T> friend Istream& operator>> <T>
(Istream&, List<T>&); (Istream& is, List<T>& L);
}; };
@ -290,7 +298,7 @@ public:
// \endcode // \endcode
// Mostly useful for handling command-line arguments // Mostly useful for handling command-line arguments
template<class T> template<class T>
List<T> readList(Istream&); List<T> readList(Istream& is);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -77,8 +77,8 @@ template<class T>
template<class InputIterator> template<class InputIterator>
inline Foam::List<T>::List inline Foam::List<T>::List
( (
InputIterator first, InputIterator begIter,
InputIterator last, InputIterator endIter,
const label s const label s
) )
: :
@ -88,10 +88,11 @@ inline Foam::List<T>::List
{ {
alloc(); alloc();
InputIterator iter = first; InputIterator iter = begIter;
forAll(*this, i) forAll(*this, i)
{ {
this->operator[](i) = *iter++; this->operator[](i) = *iter;
++iter;
} }
} }
} }
@ -126,7 +127,7 @@ inline void Foam::List<T>::clear()
if (this->v_) if (this->v_)
{ {
delete[] this->v_; delete[] this->v_;
this->v_ = 0; this->v_ = nullptr;
} }
this->size_ = 0; this->size_ = 0;

View File

@ -28,7 +28,7 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T> template<class T>
Foam::SortableList<T>::SortableList() inline Foam::SortableList<T>::SortableList()
{} {}
@ -51,14 +51,14 @@ Foam::SortableList<T>::SortableList(const Xfer<List<T>>& values)
template<class T> template<class T>
Foam::SortableList<T>::SortableList(const label size) inline Foam::SortableList<T>::SortableList(const label size)
: :
List<T>(size) List<T>(size)
{} {}
template<class T> template<class T>
Foam::SortableList<T>::SortableList(const label size, const T& val) inline Foam::SortableList<T>::SortableList(const label size, const T& val)
: :
List<T>(size, val) List<T>(size, val)
{} {}
@ -72,6 +72,20 @@ Foam::SortableList<T>::SortableList(const SortableList<T>& lst)
{} {}
template<class T>
template<class InputIterator>
inline Foam::SortableList<T>::SortableList
(
InputIterator begIter,
InputIterator endIter
)
:
List<T>(begIter, endIter)
{
sort();
}
template<class T> template<class T>
Foam::SortableList<T>::SortableList(std::initializer_list<T> values) Foam::SortableList<T>::SortableList(std::initializer_list<T> values)
: :
@ -140,9 +154,9 @@ Foam::Xfer<Foam::List<T>> Foam::SortableList<T>::xfer()
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T> template<class T>
inline void Foam::SortableList<T>::operator=(const T& t) inline void Foam::SortableList<T>::operator=(const T& val)
{ {
UList<T>::operator=(t); UList<T>::operator=(val);
} }

View File

@ -65,27 +65,32 @@ public:
// Constructors // Constructors
//- Null constructor, sort later (eg, after assignment or transfer) //- Null constructor, sort later (eg, after assignment or transfer)
SortableList(); inline SortableList();
//- Construct from UList, sorting immediately //- Construct from UList, sorting immediately
explicit SortableList(const UList<T>&); explicit SortableList(const UList<T>& values);
//- Construct from transferred List, sorting immediately //- Construct from transferred List, sorting immediately
explicit SortableList(const Xfer<List<T>>&); explicit SortableList(const Xfer<List<T>>& values);
//- Construct given size. Sort later on //- Construct given size. Sort later on
// The indices remain empty until the list is sorted // The indices remain empty until the list is sorted
explicit SortableList(const label size); explicit inline SortableList(const label size);
//- Construct given size and initial value. Sort later on //- Construct given size and initial value. Sort later on
// The indices remain empty until the list is sorted // The indices remain empty until the list is sorted
SortableList(const label size, const T&); inline SortableList(const label size, const T& val);
//- Construct given begin/end iterators.
// Uses std::distance to determine the size.
template<class InputIterator>
inline SortableList(InputIterator begIter, InputIterator endIter);
//- Construct as copy //- Construct as copy
SortableList(const SortableList<T>&); inline SortableList(const SortableList<T>& lst);
//- Construct from an initializer list, sorting immediately //- Construct from an initializer list, sorting immediately
SortableList(std::initializer_list<T>); SortableList(std::initializer_list<T> values);
// Member Functions // Member Functions
@ -122,16 +127,16 @@ public:
// Member Operators // Member Operators
//- Assignment of all entries to the given value //- Assignment of all entries to the given value
inline void operator=(const T&); inline void operator=(const T& val);
//- Assignment to UList operator. Takes linear time //- Assignment to UList operator. Takes linear time
inline void operator=(const UList<T>&); inline void operator=(const UList<T>& lst);
//- Assignment operator. Takes linear time //- Assignment operator. Takes linear time
inline void operator=(const SortableList<T>&); inline void operator=(const SortableList<T>& lst);
//- Assignment to an initializer list //- Assignment to an initializer list
void operator=(std::initializer_list<T>); void operator=(std::initializer_list<T> lst);
}; };