Commit 0c53a815 authored by Mark Olesen's avatar Mark Olesen
Browse files

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
parent 83669e28
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -59,14 +59,14 @@ class DynamicList;
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
Ostream& operator<<
(
Ostream&,
const DynamicList<T, SizeInc, SizeMult, SizeDiv>&
Ostream& os,
const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
);
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
Istream& operator>>
(
Istream&,
DynamicList<T, SizeInc, SizeMult, SizeDiv>&
Istream& is,
DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
);
......@@ -105,29 +105,37 @@ public:
inline DynamicList();
//- Construct given size.
explicit inline DynamicList(const label);
explicit inline DynamicList(const label nElem);
//- 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.
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.
// 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.
explicit inline DynamicList(std::initializer_list<T>);
explicit inline DynamicList(std::initializer_list<T> lst);
//- 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
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.
explicit DynamicList(Istream&);
explicit DynamicList(Istream& is);
// Member Functions
......@@ -143,31 +151,31 @@ public:
// The addressed size will be truncated if needed to fit, but will
// remain otherwise untouched.
// Use this or reserve() in combination with append().
inline void setCapacity(const label);
inline void setCapacity(const label nElem);
//- Alter the addressed list size.
// New space will be allocated if required.
// Use this to resize the list prior to using the operator[] for
// 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
// constant.
inline void setSize(const label, const T&);
inline void setSize(const label nElem, const T& t);
//- Alter the addressed list size.
// New space will be allocated if required.
// Use this to resize the list prior to using the operator[] for
// 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
// 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.
// 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.
// Allocated size does not change
......@@ -181,10 +189,13 @@ public:
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& shrink();
//- 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.
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
inline Xfer<List<T>> xfer();
......@@ -195,25 +206,25 @@ public:
//- Append an element at the end of the list
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append
(
const T&
const T& t
);
//- Append a List at the end of this list
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append
(
const UList<T>&
const UList<T>& lst
);
//- Append an initializer list at the end of this list.
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
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append
(
const UIndirectList<T>&
const UIndirectList<T>& lst
);
//- Remove and return the top element
......@@ -221,32 +232,35 @@ public:
//- Return non-const access to an element, resizing list if
// necessary
inline T& operator()(const label);
inline T& operator()(const label elemI);
//- Assignment of all addressed entries to the given value
inline void operator=(const T&);
inline void operator=(const T& t);
//- Assignment to DynamicList
inline void operator=
(
const DynamicList<T, SizeInc, SizeMult, SizeDiv>&
const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
);
//- Assignment to UList
inline void operator=(const UList<T>&);
inline void operator=(const UList<T>& lst);
//- Assignment from initializer list
inline void operator=(std::initializer_list<T>);
inline void operator=(std::initializer_list<T> lst);
//- Assignment to UIndirectList
inline void operator=(const UIndirectList<T>&);
inline void operator=(const UIndirectList<T>& lst);
// STL member functions
//- Erase an element, move the remaining elements to fill the gap
// 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
......@@ -254,15 +268,15 @@ public:
// Write DynamicList to Ostream.
friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv>
(
Ostream&,
const DynamicList<T, SizeInc, SizeMult, SizeDiv>&
Ostream& os,
const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
);
//- Read from Istream, discarding contents of existing DynamicList.
friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv>
(
Istream&,
DynamicList<T, SizeInc, SizeMult, SizeDiv>&
Istream& is,
DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
);
};
......
......@@ -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>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
(
......
......@@ -128,11 +128,12 @@ public:
explicit inline FixedList(const T& t);
//- 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>
inline FixedList(InputIterator first, InputIterator last);
inline FixedList(InputIterator begIter, InputIterator endIter);
//- Construct from an initializer list
inline FixedList(std::initializer_list<T> lst);
......
......@@ -37,7 +37,7 @@ inline Foam::FixedList<T, Size>::FixedList()
template<class T, unsigned Size>
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;
}
......@@ -45,11 +45,11 @@ inline Foam::FixedList<T, Size>::FixedList(const T& t)
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>
Foam::FixedList<T, Size>::FixedList
(
InputIterator first,
InputIterator last
InputIterator begIter,
InputIterator endIter
)
{
checkSize(std::distance(first, last));
checkSize(std::distance(begIter, endIter));
InputIterator iter = first;
for (unsigned i=0; i<Size; i++)
InputIterator iter = begIter;
for (unsigned i=0; i<Size; ++i)
{
v_[i] = *iter++;
v_[i] = *iter;
++iter;
}
}
template<class T, unsigned Size>
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>
......@@ -84,7 +92,7 @@ inline Foam::FixedList<T, Size>::FixedList(const UList<T>& lst)
{
checkSize(lst.size());
for (unsigned i=0; i<Size; i++)
for (unsigned i=0; i<Size; ++i)
{
v_[i] = lst[i];
}
......@@ -97,9 +105,10 @@ inline Foam::FixedList<T, Size>::FixedList(const SLList<T>& lst)
checkSize(lst.size());
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>
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];
}
......@@ -200,7 +209,7 @@ inline void Foam::FixedList<T, Size>::setSize(const label s)
template<class T, unsigned Size>
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];
}
......@@ -276,7 +285,7 @@ inline const T& Foam::FixedList<T, Size>::operator[](const label i) const
template<class T, unsigned 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];
}
......@@ -287,7 +296,7 @@ inline void Foam::FixedList<T, Size>::operator=(const UList<T>& lst)
{
checkSize(lst.size());
for (unsigned i=0; i<Size; i++)
for (unsigned i=0; i<Size; ++i)
{
v_[i] = lst[i];
}
......@@ -299,9 +308,10 @@ inline void Foam::FixedList<T, Size>::operator=(const SLList<T>& lst)
checkSize(lst.size());
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());
typename std::initializer_list<T>::iterator iter = lst.begin();
for (unsigned i=0; i<Size; i++)
auto iter = lst.begin();
for (unsigned i=0; i<Size; ++i)
{
v_[i] = *iter++;
v_[i] = *iter;
++iter;
}
}
template<class T, unsigned Size>
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;
}
......@@ -464,7 +475,7 @@ inline unsigned Foam::FixedList<T, Size>::Hash<HashT>::operator()
// Hash incrementally
unsigned val = seed;
for (unsigned i=0; i<Size; i++)
for (unsigned i=0; i<Size; ++i)
{
val = HashT()(lst[i], val);
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -160,7 +160,7 @@ Foam::List<T>::List(List<T>& a, bool reuse)
if (reuse)
{
this->v_ = a.v_;
a.v_ = 0;
a.v_ = nullptr;
a.size_ = 0;
}
else if (this->size_)
......@@ -186,19 +186,19 @@ Foam::List<T>::List(List<T>& a, bool reuse)
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_)
{
// 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();
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 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>
Foam::List<T>::List(const SLList<T>& lst)
:
......@@ -259,7 +261,7 @@ Foam::List<T>::List(const BiIndirectList<T>& lst)
template<class T>
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>
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);
if (newSize > oldSize)
......@@ -346,7 +348,7 @@ void Foam::List<T>::transfer(List<T>& a)
this->v_ = a.v_;
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_)
{
label i = 0;
for
(
typename SLList<T>::const_iterator iter = lst.begin();
iter != lst.end();
++iter
)
for (auto iter = lst.cbegin(); iter != lst.cend(); ++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());
typename std::initializer_list<T>::iterator iter = lst.begin();
auto iter = lst.begin();
forAll(*this, i)
{
this->operator[](i) = *iter++;
this->operator[](i) = *iter;
++iter;
}
}
......
......@@ -58,7 +58,7 @@ class Ostream;
// Forward declaration of friend functions and operators
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> class PtrList;
......@@ -95,22 +95,28 @@ class List
//- Copy list of given type
template<class List2>
inline void copyList(const List2&);
inline void copyList(const List2& lst);
//- Allocate storage and copy list of given type
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>
inline List(InputIterator first, InputIterator last, const label s);
inline List
(
InputIterator begIter,
InputIterator endIter,
const label s
);
protected:
//- Override size to be inconsistent with allocated storage.
// Use with care
inline void size(const label);
inline void size(const label n);
public:
......@@ -127,55 +133,56 @@ public:
inline List();
//- Construct with given size
explicit List(const label);
explicit List(const label s);
//- 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
List(const label, const zero);
List(const label s, const zero);
//- Copy constructor
List(const List<T>&);
List(const List<T>& a);
//- Copy constructor from list containing another type
template<class T2>
explicit List(const List<T2>&);