Commit e42c2281 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: cleanup List constructors (issue #725)

- add copy construct from UList

- remove copy construct from dissimilar types.

  This templated constructor was too generous in what it accepted.
  For the special cases where a copy constructor is required with
  a change in the data type, now use the createList factory method,
  which accepts a unary operator. Eg,

      auto scalars = scalarList::createList
      (
          labels,
          [](const label& val){ return 1.5*val; }
      );
parent e2332d6b
......@@ -48,6 +48,23 @@ See also
#include <list>
#include <numeric>
#include <functional>
namespace Foam
{
// Verify inheritance
class MyStrings
:
public List<string>
{
public:
using List<string>::List;
};
} // end namespace Foam
using namespace Foam;
......@@ -66,6 +83,14 @@ void testFind(const T& val, const ListType& lst)
}
void printMyString(const UList<string>& lst)
{
MyStrings slist2(lst);
Info<<slist2 << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -76,6 +101,7 @@ int main(int argc, char *argv[])
argList::addOption("wordList", "wordList");
argList::addOption("stringList", "stringList");
argList::addOption("float", "xx");
argList::addBoolOption("transform", "Test List::createList functionality");
argList::addBoolOption("flag");
#include "setRootCase.H"
......@@ -336,6 +362,85 @@ int main(int argc, char *argv[])
Info<<"-flag:" << args["flag"] << endl;
}
if (args.found("transform"))
{
Info<< nl << "Test List::createList functionality" << nl;
const auto labels = identity(15);
Info<< "labels: " << flatOutput(labels) << endl;
{
auto scalars = List<scalar>::createList
(
labels,
[](const label& val){ return scalar(1.5*val); }
);
Info<< "scalars: " << flatOutput(scalars) << endl;
}
{
auto vectors = List<vector>::createList
(
labels,
[](const label& val){ return vector(1.2*val, -1.2*val, 0); }
);
Info<< "vectors: " << flatOutput(vectors) << endl;
}
{
auto longs = List<long>::createList
(
labels,
[](const label& val){ return val; }
);
Info<< "longs: " << flatOutput(longs) << endl;
}
{
auto negs = List<label>::createList
(
labels,
std::negate<label>()
);
Info<< "negs: " << flatOutput(negs) << endl;
}
{
auto scalars = List<scalar>::createList
(
labelRange::null.cbegin(),
labelRange::identity(15).cend(),
[](const label& val){ return scalar(-1.125*val); }
);
Info<< "scalars: " << flatOutput(scalars) << endl;
}
#if WM_LABEL_SIZE == 32
{
List<int64_t> input(10);
std::iota(input.begin(), input.end(), 0);
auto output = List<label>::createList
(
input,
toLabel<int64_t>()
);
Info<< "label (from int64): " << flatOutput(output) << endl;
}
#elif WM_LABEL_SIZE == 64
{
List<int32_t> input(10);
std::iota(input.begin(), input.end(), 0);
auto output = List<label>::createList
(
input,
toLabel<int32_t>()
);
Info<< "label (from int32): " << flatOutput(output) << endl;
}
#endif
}
if (args.readIfPresent<scalar>("float", xxx))
{
Info<<"read float " << xxx << endl;
......@@ -354,6 +459,8 @@ int main(int argc, char *argv[])
if (args.found("stringList"))
{
sLst = args.readList<string>("stringList");
printMyString(sLst);
}
Info<< nl
......
......@@ -1007,7 +1007,11 @@ Foam::label Foam::checkGeometry
globalFaces().gather
(
UPstream::worldComm,
labelList(UPstream::procID(UPstream::worldComm)),
labelList::createList
(
UPstream::procID(UPstream::worldComm),
toLabel<int>() // int -> label
),
ami.srcWeightsSum(),
mergedWeights
);
......@@ -1057,7 +1061,11 @@ Foam::label Foam::checkGeometry
globalFaces().gather
(
UPstream::worldComm,
labelList(UPstream::procID(UPstream::worldComm)),
labelList::createList
(
UPstream::procID(UPstream::worldComm),
toLabel<int>() // int -> label
),
ami.tgtWeightsSum(),
mergedWeights
);
......
......@@ -84,7 +84,11 @@ void writeWeights
globalFaces().gather
(
UPstream::worldComm,
labelList(UPstream::procID(UPstream::worldComm)),
labelList::createList
(
UPstream::procID(UPstream::worldComm),
toLabel<int>() // int -> label
),
wghtSum,
mergedWeights
);
......
......@@ -162,13 +162,13 @@ bool writeZones(const word& name, const fileName& meshDir, Time& runTime)
}
// Reduction for non-empty strings
class uniqueEqOp
// Reduction for non-empty strings.
template<class StringType>
struct uniqueEqOp
{
public:
void operator()(stringList& x, const stringList& y) const
void operator()(List<StringType>& x, const List<StringType>& y) const
{
stringList newX(x.size()+y.size());
List<StringType> newX(x.size()+y.size());
label n = 0;
forAll(x, i)
{
......@@ -215,8 +215,8 @@ bool writeOptionalMeshObject
bool haveFile = io.typeHeaderOk<IOField<label>>(false);
// Make sure all know if there is a valid class name
stringList classNames(1, io.headerClassName());
combineReduce(classNames, uniqueEqOp());
wordList classNames(1, io.headerClassName());
combineReduce(classNames, uniqueEqOp<word>());
// Check for correct type
if (classNames[0] == T::typeName)
......@@ -395,7 +395,7 @@ int main(int argc, char *argv[])
// Check for lagrangian
stringList lagrangianDirs
fileNameList lagrangianDirs
(
1,
fileHandler().filePath
......@@ -406,7 +406,7 @@ int main(int argc, char *argv[])
)
);
combineReduce(lagrangianDirs, uniqueEqOp());
combineReduce(lagrangianDirs, uniqueEqOp<fileName>());
if (!lagrangianDirs.empty())
{
......@@ -434,7 +434,7 @@ int main(int argc, char *argv[])
);
}
stringList cloudDirs
fileNameList cloudDirs
(
fileHandler().readDir
(
......@@ -443,7 +443,7 @@ int main(int argc, char *argv[])
)
);
combineReduce(cloudDirs, uniqueEqOp());
combineReduce(cloudDirs, uniqueEqOp<fileName>());
forAll(cloudDirs, i)
{
......@@ -464,13 +464,11 @@ int main(int argc, char *argv[])
IOobjectList sprayObjs(runTime, runTime.timeName(), dir);
// Combine with all other cloud objects
stringList sprayFields(sprayObjs.sortedToc());
combineReduce(sprayFields, uniqueEqOp());
wordList sprayFields(sprayObjs.sortedToc());
combineReduce(sprayFields, uniqueEqOp<word>());
forAll(sprayFields, fieldi)
for (const word& name : sprayFields)
{
const word& name = sprayFields[fieldi];
// Note: try the various field types. Make sure to
// exit once sucessful conversion to avoid re-read
// converted file.
......
......@@ -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) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -35,17 +35,76 @@ License
#include <utility>
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class T>
template<class T2, class UnaryOperation>
Foam::List<T> Foam::List<T>::createList
(
const UList<T2>& input,
const UnaryOperation& op
)
{
const label len = input.size();
List<T> output(len);
if (len)
{
List_ACCESS(T, output, out);
List_CONST_ACCESS(T2, input, in);
for (label i = 0; i < len; ++i)
{
out[i] = op(in[i]);
}
}
return output;
}
template<class T>
template<class InputIterator, class UnaryOperation>
Foam::List<T> Foam::List<T>::createList
(
InputIterator begIter,
InputIterator endIter,
const UnaryOperation& op
)
{
const label len = std::distance(begIter, endIter);
List<T> output(len);
if (len)
{
List_ACCESS(T, output, out);
InputIterator iter = begIter;
for (label i = 0; i < len; ++i)
{
out[i] = op(*iter);
++iter;
}
}
return output;
}
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template<class T>
Foam::List<T>::List(const label s)
Foam::List<T>::List(const label len)
:
UList<T>(nullptr, s)
UList<T>(nullptr, len)
{
if (this->size_ < 0)
if (len < 0)
{
FatalErrorInFunction
<< "bad size " << this->size_
<< "bad size " << len
<< abort(FatalError);
}
......@@ -54,23 +113,23 @@ Foam::List<T>::List(const label s)
template<class T>
Foam::List<T>::List(const label s, const T& val)
Foam::List<T>::List(const label len, const T& val)
:
UList<T>(nullptr, s)
UList<T>(nullptr, len)
{
if (this->size_ < 0)
if (len < 0)
{
FatalErrorInFunction
<< "bad size " << this->size_
<< "bad size " << len
<< abort(FatalError);
}
alloc();
if (this->size_)
if (len)
{
alloc();
List_ACCESS(T, (*this), vp);
List_FOR_ALL((*this), i)
for (label i=0; i < len; ++i)
{
vp[i] = val;
}
......@@ -79,23 +138,23 @@ Foam::List<T>::List(const label s, const T& val)
template<class T>
Foam::List<T>::List(const label s, const zero)
Foam::List<T>::List(const label len, const zero)
:
UList<T>(nullptr, s)
UList<T>(nullptr, len)
{
if (this->size_ < 0)
if (len < 0)
{
FatalErrorInFunction
<< "bad size " << this->size_
<< "bad size " << len
<< abort(FatalError);
}
alloc();
if (this->size_)
if (len)
{
alloc();
List_ACCESS(T, (*this), vp);
List_FOR_ALL((*this), i)
for (label i=0; i < len; ++i)
{
vp[i] = Zero;
}
......@@ -104,7 +163,7 @@ Foam::List<T>::List(const label s, const zero)
template<class T>
Foam::List<T>::List(const List<T>& a)
Foam::List<T>::List(const UList<T>& a)
:
UList<T>(nullptr, a.size_)
{
......@@ -132,20 +191,28 @@ Foam::List<T>::List(const List<T>& a)
template<class T>
template<class T2>
Foam::List<T>::List(const List<T2>& a)
Foam::List<T>::List(const List<T>& a)
:
UList<T>(nullptr, a.size())
UList<T>(nullptr, a.size_)
{
if (this->size_)
{
alloc();
List_ACCESS(T, (*this), vp);
List_CONST_ACCESS(T2, a, ap);
List_FOR_ALL((*this), i)
#ifdef USEMEMCPY
if (contiguous<T>())
{
vp[i] = T(ap[i]);
memcpy(this->v_, a.v_, this->byteSize());
}
else
#endif
{
List_ACCESS(T, (*this), vp);
List_CONST_ACCESS(T, a, ap);
List_FOR_ALL((*this), i)
{
vp[i] = ap[i];
}
}
}
}
......@@ -191,13 +258,14 @@ Foam::List<T>::List(const UList<T>& lst, const labelUList& mapAddressing)
:
UList<T>(nullptr, mapAddressing.size())
{
if (this->size_)
const label len = mapAddressing.size();
if (len)
{
alloc();
List_ACCESS(T, (*this), vp);
const label len = (*this).size();
for (label i=0; i < len; ++i)
{
vp[i] = lst[mapAddressing[i]];
......@@ -220,7 +288,8 @@ Foam::List<T>::List(const FixedList<T, Size>& lst)
:
UList<T>(nullptr, Size)
{
allocCopyList(lst);
alloc();
copyList(lst);
}
......@@ -229,7 +298,8 @@ Foam::List<T>::List(const PtrList<T>& lst)
:
UList<T>(nullptr, lst.size())
{
allocCopyList(lst);
alloc();
copyList(lst);
}
......@@ -245,7 +315,8 @@ Foam::List<T>::List(const UIndirectList<T>& lst)
:
UList<T>(nullptr, lst.size())
{
allocCopyList(lst);
alloc();
copyList(lst);
}
......@@ -254,7 +325,8 @@ Foam::List<T>::List(const BiIndirectList<T>& lst)
:
UList<T>(nullptr, lst.size())
{
allocCopyList(lst);
alloc();
copyList(lst);
}
......@@ -329,7 +401,7 @@ void Foam::List<T>::setSize(const label newSize)
{
if (newSize > 0)
{
T* nv = new T[label(newSize)];
T* nv = new T[newSize];
const label overlap = min(this->size_, newSize);
......@@ -367,7 +439,7 @@ void Foam::List<T>::setSize(const label newSize)
template<class T>
void Foam::List<T>::setSize(const label newSize, const T& val)
{
const label oldSize = label(this->size_);
const label oldSize = this->size_;
this->setSize(newSize);
List_ACCESS(T, *this, vp);
......@@ -474,28 +546,58 @@ void Foam::List<T>::operator=(const SLList<T>& lst)
template<class T>
void Foam::List<T>::operator=(const UIndirectList<T>& lst)
{
reAlloc(lst.size());
copyList(lst);
const label len = lst.size();
reAlloc(len);
if (len)
{
List_ACCESS(T, (*this), vp);
for (label i=0; i<len; ++i)
{
vp[i] = lst[i];
}
}
}
template<class T>
void Foam::List<T>::operator=(const BiIndirectList<T>& lst)
{
reAlloc(lst.size());
copyList(lst);
const label len = lst.size();
reAlloc(len);
if (len)
{
List_ACCESS(T, (*this), vp);
for (label i=0; i<len; ++i)
{
vp[i] = lst[i];
}
}
}
template<class T>
void Foam::List<T>::operator=(std::initializer_list<T> lst)
{
reAlloc(lst.size());
const label len = lst.size();
reAlloc(len);
label i = 0;
for (const auto& val : lst)
if (len)
{
this->operator[](i++) = val;
List_ACCESS(T, (*this), vp);
label i = 0;
for (const auto& val : lst)
{
vp[i] = val;
++i;
}
}
}
......
......@@ -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) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -50,16 +50,12 @@ SourceFiles
namespace Foam
{
// Forward declaration of classes
// Forward declarations
class Istream;
class Ostream;