Skip to content
Snippets Groups Projects
Commit 22574e71 authored by Henry Weller's avatar Henry Weller
Browse files

List: Reinstated construction from two iterators and added construction from an initializer list

Until C++ supports 'concepts' the only way to support construction from
two iterators is to provide a constructor of the form:

        template<class InputIterator>
        List(InputIterator first, InputIterator last);

which for some types conflicts with

        //- Construct with given size and value for all elements
        List(const label, const T&);

e.g. to construct a list of 5 scalars initialized to 0:

    List<scalar> sl(5, 0);

causes a conflict because the initialization type is 'int' rather than
'scalar'.  This conflict may be resolved by specifying the type of the
initialization value:

    List<scalar> sl(5, scalar(0));

The new initializer list contructor provides a convenient and efficient alternative
to using 'IStringStream' to provide an initial list of values:

    List<vector> list4(IStringStream("((0 1 2) (3 4 5) (6 7 8))")());

or

    List<vector> list4
    {
        vector(0, 1, 2),
        vector(3, 4, 5),
        vector(6, 7, 8)
    };
parent 6f82d23d
Branches
Tags
1 merge request!60Merge foundation
......@@ -64,7 +64,12 @@ int main(int argc, char *argv[])
List<vector> list1(IStringStream("1 ((0 1 2))")());
Info<< "list1: " << list1 << endl;
List<vector> list2(IStringStream("((0 1 2) (3 4 5) (6 7 8))")());
List<vector> list2
{
vector(0, 1, 2),
vector(3, 4, 5),
vector(6, 7, 8)
};
Info<< "list2: " << list2 << endl;
list1.append(list2);
......@@ -80,17 +85,32 @@ int main(int argc, char *argv[])
Info<< "list2: " << list2 << nl
<< "list3: " << list3 << endl;
List<vector> list4(IStringStream("((0 1 2) (3 4 5) (6 7 8))")());
List<vector> list5(IStringStream("((5 3 1) (10 2 2) (8 1 0))")());
Info<< "list4: " << list4 << nl
<< "list5: " << list5 << endl;
List<vector> list4
{
vector(0, 1, 2),
vector(3, 4, 5),
vector(6, 7, 8)
};
Info<< "list4: " << list4 << endl;
List<vector> list5
{
vector(5, 3, 1),
vector(10, 2, 2),
vector(8, 1, 0)
};
Info<< "list5: " << list5 << endl;
list4.swap(list5);
Info<< "Swapped via the swap() method" << endl;
Info<< "list4: " << list4 << nl
<< "list5: " << list5 << endl;
List<vector> list6(list4.begin(), list4.end());
Info<< "list6: " << list6 << endl;
// Subset
const labelList map(IStringStream("2 (0 2)")());
const labelList map{0, 2};
List<vector> subList3(list3, map);
Info<< "Elements " << map << " out of " << list3
<< " => " << subList3 << endl;
......
......@@ -33,6 +33,43 @@ 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>(NULL, s)
{
if (this->size_)
{
this->v_ = new T[this->size_];
InputIterator iter = first;
forAll(*this, i)
{
this->operator[](i) = *iter++;
}
}
}
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template<class T>
......@@ -201,27 +238,27 @@ Foam::List<T>::List(const UList<T>& a, const labelUList& map)
forAll(*this, i)
{
this->v_[i] = a[map[i]];
this->operator[](i) = a[map[i]];
}
}
}
template<class T>
template<class InputIterator>
Foam::List<T>::List(InputIterator first, InputIterator last)
:
List<T>(first, last, std::distance(first, last))
{}
template<class T>
template<unsigned Size>
Foam::List<T>::List(const FixedList<T, Size>& lst)
:
UList<T>(NULL, Size)
{
if (this->size_)
{
this->v_ = new T[this->size_];
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
CopyList(lst);
}
......@@ -230,39 +267,15 @@ Foam::List<T>::List(const PtrList<T>& lst)
:
UList<T>(NULL, lst.size())
{
if (this->size_)
{
this->v_ = new T[this->size_];
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
CopyList(lst);
}
template<class T>
Foam::List<T>::List(const SLList<T>& lst)
:
UList<T>(NULL, lst.size())
{
if (this->size_)
{
this->v_ = new T[this->size_];
label i = 0;
for
(
typename SLList<T>::const_iterator iter = lst.begin();
iter != lst.end();
++iter
)
{
this->operator[](i++) = iter();
}
}
}
List<T>(lst.first(), lst.last(), lst.size())
{}
template<class T>
......@@ -270,15 +283,7 @@ Foam::List<T>::List(const UIndirectList<T>& lst)
:
UList<T>(NULL, lst.size())
{
if (this->size_)
{
this->v_ = new T[this->size_];
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
CopyList(lst);
}
......@@ -287,18 +292,17 @@ Foam::List<T>::List(const BiIndirectList<T>& lst)
:
UList<T>(NULL, lst.size())
{
if (this->size_)
{
this->v_ = new T[this->size_];
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
CopyList(lst);
}
template<class T>
Foam::List<T>::List(std::initializer_list<T> lst)
:
List<T>(lst.begin(), lst.end())
{}
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
template<class T>
......
......@@ -43,6 +43,7 @@ SourceFiles
#include "UList.H"
#include "autoPtr.H"
#include "Xfer.H"
#include <initializer_list>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -82,6 +83,16 @@ class List
:
public UList<T>
{
// Private member functions
//- Copy list of given type
template<class List2>
void CopyList(const List2&);
//- Construct given start and end iterators and number of elements
template<class InputIterator>
List(InputIterator first, InputIterator last, const label s);
protected:
......@@ -97,6 +108,7 @@ public:
//- Return a null List
inline static const List<T>& null();
// Constructors
//- Null constructor
......@@ -114,7 +126,7 @@ public:
//- Copy constructor
List(const List<T>&);
//- Copy constructor from list of another type
//- Copy constructor from list containing another type
template<class T2>
explicit List(const List<T2>&);
......@@ -127,6 +139,10 @@ public:
//- Construct as subset
List(const UList<T>&, const labelUList& mapAddressing);
//- Construct given start and end iterators
template<class InputIterator>
List(InputIterator first, InputIterator last);
//- Construct as copy of FixedList<T, Size>
template<unsigned Size>
explicit List(const FixedList<T, Size>&);
......@@ -143,6 +159,9 @@ public:
//- Construct as copy of BiIndirectList<T>
explicit List(const BiIndirectList<T>&);
//- Construct from an initializer list
List(std::initializer_list<T> lst);
//- Construct from Istream
List(Istream&);
......
......@@ -333,7 +333,7 @@ bool Foam::chemPointISAT<CompType, ThermoType>::inEOA(const scalarField& phiq)
bool isMechRedActive = chemistry_.mechRed()->active();
label dim = (isMechRedActive) ? nActiveSpecies_ : completeSpaceSize()-2;
scalar epsTemp=0;
List<scalar> propEps(completeSpaceSize(),0);
List<scalar> propEps(completeSpaceSize(), scalar(0));
for (label i=0; i<completeSpaceSize()-2; i++)
{
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment