ENH: use PtrListOps to reduce code

This commit is contained in:
Mark Olesen
2021-01-15 14:59:33 +01:00
parent 5080d3cd1f
commit f24e3f113f
6 changed files with 80 additions and 470 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -34,6 +34,7 @@ License
#include "lduSchedule.H"
#include "globalMeshData.H"
#include "stringListOps.H"
#include "PtrListOps.H"
#include "edgeHashes.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -43,81 +44,6 @@ namespace Foam
defineTypeNameAndDebug(polyBoundaryMesh, 0);
}
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
// Templated implementation for types(), names(), etc - file-scope
template<class ListType, class GetOp>
static ListType getMethodImpl
(
const polyPatchList& list,
const GetOp& getop
)
{
const label len = list.size();
ListType output(len);
for (label i = 0; i < len; ++i)
{
output[i] = getop(list[i]);
}
return output;
}
// Templated implementation for indices() - file-scope
template<class UnaryMatchPredicate>
static labelList indicesImpl
(
const polyPatchList& list,
const UnaryMatchPredicate& matcher
)
{
const label len = list.size();
labelList output(len);
label count = 0;
for (label i = 0; i < len; ++i)
{
if (matcher(list[i].name()))
{
output[count++] = i;
}
}
output.resize(count);
return output;
}
// Templated implementation for findIndex() - file-scope
template<class UnaryMatchPredicate>
label findIndexImpl
(
const polyPatchList& list,
const UnaryMatchPredicate& matcher
)
{
const label len = list.size();
for (label i = 0; i < len; ++i)
{
if (matcher(list[i].name()))
{
return i;
}
}
return -1;
}
} // End namespace Foam
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -592,20 +518,20 @@ Foam::label Foam::polyBoundaryMesh::nNonProcessor() const
Foam::wordList Foam::polyBoundaryMesh::names() const
{
return getMethodImpl<wordList>(*this, getNameOp<polyPatch>());
return PtrListOps::get<word>(*this, nameOp<polyPatch>());
}
Foam::wordList Foam::polyBoundaryMesh::types() const
{
return getMethodImpl<wordList>(*this, getTypeOp<polyPatch>());
return PtrListOps::get<word>(*this, typeOp<polyPatch>());
}
Foam::wordList Foam::polyBoundaryMesh::physicalTypes() const
{
return
getMethodImpl<wordList>
PtrListOps::get<word>
(
*this,
[](const polyPatch& p) { return p.physicalType(); }
@ -616,7 +542,7 @@ Foam::wordList Foam::polyBoundaryMesh::physicalTypes() const
Foam::labelList Foam::polyBoundaryMesh::patchStarts() const
{
return
getMethodImpl<labelList>
PtrListOps::get<label>
(
*this,
[](const polyPatch& p) { return p.start(); }
@ -627,7 +553,7 @@ Foam::labelList Foam::polyBoundaryMesh::patchStarts() const
Foam::labelList Foam::polyBoundaryMesh::patchSizes() const
{
return
getMethodImpl<labelList>
PtrListOps::get<label>
(
*this,
[](const polyPatch& p) { return p.size(); }
@ -681,8 +607,7 @@ Foam::labelList Foam::polyBoundaryMesh::indices
if (key.isPattern())
{
const regExp matcher(key);
patchIndices = indicesImpl(*this, matcher);
patchIndices = PtrListOps::findMatching(*this, matcher);
// Only examine patch groups if requested and when they exist.
if (useGroups && !groupPatchIDs().empty())
@ -713,8 +638,7 @@ Foam::labelList Foam::polyBoundaryMesh::indices
// Special version of above for reduced memory footprint
const word& matcher = key;
const label patchId = findIndexImpl(*this, matcher);
const label patchId = PtrListOps::firstMatching(*this, matcher);
if (patchId >= 0)
{
@ -750,14 +674,14 @@ Foam::label Foam::polyBoundaryMesh::findIndex(const keyType& key) const
else if (key.isPattern())
{
// Find as regex
regExp matcher(key);
return findIndexImpl(*this, matcher);
const regExp matcher(key);
return PtrListOps::firstMatching(*this, matcher);
}
else
{
// Find as literal string
const word& matcher = key;
return findIndexImpl(*this, matcher);
return PtrListOps::firstMatching(*this, matcher);
}
}
@ -773,7 +697,7 @@ Foam::label Foam::polyBoundaryMesh::findPatchID
return -1;
}
const label patchId = findIndexImpl(*this, patchName);
const label patchId = PtrListOps::firstMatching(*this, patchName);
if (patchId >= 0)
{
@ -782,15 +706,18 @@ Foam::label Foam::polyBoundaryMesh::findPatchID
if (!allowNotFound)
{
string regionStr;
if (mesh_.name() != polyMesh::defaultRegion)
{
regionStr = "in region '" + mesh_.name() + "' ";
}
FatalErrorInFunction
<< "Patch '" << patchName << "' not found. "
<< "Available patch names " << regionStr << "include: " << names()
<< "Available patch names";
if (polyMesh::defaultRegion != mesh_.name())
{
FatalError
<< " in region '" << mesh_.name() << "'";
}
FatalError
<< " include: " << names() << endl
<< exit(FatalError);
}
@ -799,7 +726,7 @@ Foam::label Foam::polyBoundaryMesh::findPatchID
{
Pout<< "label polyBoundaryMesh::findPatchID(const word&) const"
<< "Patch named " << patchName << " not found. "
<< "List of available patch names: " << names() << endl;
<< "Available patch names: " << names() << endl;
}
// Not found, return -1

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd.
Copyright (C) 2016-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -30,6 +30,7 @@ License
#include "entry.H"
#include "demandDrivenData.H"
#include "Pstream.H"
#include "PtrListOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -138,91 +139,6 @@ bool Foam::ZoneMesh<ZoneType, MeshType>::read()
}
// Templated implementation for names()
template<class ZoneType, class MeshType>
template<class UnaryMatchPredicate>
Foam::wordList Foam::ZoneMesh<ZoneType, MeshType>::namesImpl
(
const PtrList<ZoneType>& list,
const UnaryMatchPredicate& matcher,
const bool doSort
)
{
const label len = list.size();
wordList output(len);
label count = 0;
for (label i = 0; i < len; ++i)
{
const word& itemName = list[i].name();
if (matcher(itemName))
{
output[count++] = itemName;
}
}
output.resize(count);
if (doSort)
{
Foam::sort(output);
}
return output;
}
template<class ZoneType, class MeshType>
template<class UnaryMatchPredicate>
Foam::labelList Foam::ZoneMesh<ZoneType, MeshType>::indicesImpl
(
const PtrList<ZoneType>& list,
const UnaryMatchPredicate& matcher
)
{
const label len = list.size();
labelList output(len);
label count = 0;
for (label i = 0; i < len; ++i)
{
if (matcher(list[i].name()))
{
output[count++] = i;
}
}
output.resize(count);
return output;
}
template<class ZoneType, class MeshType>
template<class UnaryMatchPredicate>
Foam::label Foam::ZoneMesh<ZoneType, MeshType>::findIndexImpl
(
const PtrList<ZoneType>& list,
const UnaryMatchPredicate& matcher
)
{
const label len = list.size();
for (label i = 0; i < len; ++i)
{
if (matcher(list[i].name()))
{
return i;
}
}
return -1;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class ZoneType, class MeshType>
@ -323,32 +239,14 @@ Foam::label Foam::ZoneMesh<ZoneType, MeshType>::whichZone
template<class ZoneType, class MeshType>
Foam::wordList Foam::ZoneMesh<ZoneType, MeshType>::types() const
{
const PtrList<ZoneType>& zones = *this;
wordList list(zones.size());
forAll(zones, zonei)
{
list[zonei] = zones[zonei].type();
}
return list;
return PtrListOps::get<word>(*this, typeOp<ZoneType>());
}
template<class ZoneType, class MeshType>
Foam::wordList Foam::ZoneMesh<ZoneType, MeshType>::names() const
{
const PtrList<ZoneType>& zones = *this;
wordList list(zones.size());
forAll(zones, zonei)
{
list[zonei] = zones[zonei].name();
}
return list;
return PtrListOps::get<word>(*this, nameOp<ZoneType>());
}
@ -358,7 +256,7 @@ Foam::wordList Foam::ZoneMesh<ZoneType, MeshType>::names
const wordRe& matcher
) const
{
return namesImpl(*this, matcher, false);
return PtrListOps::names(*this, matcher);
}
@ -369,7 +267,7 @@ Foam::wordList Foam::ZoneMesh<ZoneType, MeshType>::names
)
const
{
return namesImpl(*this, matcher, false);
return PtrListOps::names(*this, matcher);
}
@ -389,7 +287,10 @@ Foam::wordList Foam::ZoneMesh<ZoneType, MeshType>::sortedNames
const wordRe& matcher
) const
{
return namesImpl(*this, matcher, true);
wordList sorted(this->names(matcher));
Foam::sort(sorted);
return sorted;
}
@ -400,7 +301,10 @@ Foam::wordList Foam::ZoneMesh<ZoneType, MeshType>::sortedNames
)
const
{
return namesImpl(*this, matcher, true);
wordList sorted(this->names(matcher));
Foam::sort(sorted);
return sorted;
}
@ -417,14 +321,14 @@ Foam::labelList Foam::ZoneMesh<ZoneType, MeshType>::indices
else if (key.isPattern())
{
// Match as regex
regExp matcher(key);
return indicesImpl(*this, matcher);
const regExp matcher(key);
return PtrListOps::findMatching(*this, matcher);
}
else
{
// Compare as literal string
const word& matcher = key;
return indicesImpl(*this, matcher);
return PtrListOps::findMatching(*this, matcher);
}
}
@ -439,8 +343,7 @@ Foam::labelList Foam::ZoneMesh<ZoneType, MeshType>::indices
{
return labelList();
}
return indicesImpl(*this, matcher);
return PtrListOps::findMatching(*this, matcher);
}
@ -457,14 +360,14 @@ Foam::label Foam::ZoneMesh<ZoneType, MeshType>::findIndex
else if (key.isPattern())
{
// Find as regex
regExp matcher(key);
return findIndexImpl(*this, matcher);
const regExp matcher(key);
return PtrListOps::firstMatching(*this, matcher);
}
else
{
// Find as literal string
const word& matcher = key;
return findIndexImpl(*this, matcher);
return PtrListOps::firstMatching(*this, matcher);
}
}
@ -475,7 +378,7 @@ Foam::label Foam::ZoneMesh<ZoneType, MeshType>::findIndex
const wordRes& matcher
) const
{
return (matcher.empty() ? -1 : findIndexImpl(*this, matcher));
return (matcher.empty() ? -1 : PtrListOps::firstMatching(*this, matcher));
}
@ -490,7 +393,7 @@ Foam::label Foam::ZoneMesh<ZoneType, MeshType>::findZoneID
return -1;
}
label zoneId = findIndexImpl(*this, zoneName);
label zoneId = PtrListOps::firstMatching(*this, zoneName);
if (zoneId < 0)
{

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd.
Copyright (C) 2016-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -85,32 +85,6 @@ class ZoneMesh
//- Create zone map
void calcZoneMap() const;
//- Templated implementation for names()
template<class UnaryMatchPredicate>
static wordList namesImpl
(
const PtrList<ZoneType>& list,
const UnaryMatchPredicate& matcher,
const bool doSort
);
//- Templated implementation for indices()
template<class UnaryMatchPredicate>
static labelList indicesImpl
(
const PtrList<ZoneType>& list,
const UnaryMatchPredicate& matcher
);
//- Templated implementation for findIndex()
template<class UnaryMatchPredicate>
static label findIndexImpl
(
const PtrList<ZoneType>& list,
const UnaryMatchPredicate& matcher
);
//- No copy construct
ZoneMesh(const ZoneMesh&) = delete;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -30,6 +30,7 @@ License
#include "polyMesh.H"
#include "polyTopoChange.H"
#include "Time.H"
#include "PtrListOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -135,31 +136,13 @@ Foam::polyTopoChanger::polyTopoChanger(polyMesh& mesh)
Foam::wordList Foam::polyTopoChanger::types() const
{
const PtrList<polyMeshModifier>& modifiers = *this;
wordList lst(modifiers.size());
forAll(modifiers, i)
{
lst[i] = modifiers[i].type();
}
return lst;
return PtrListOps::get<word>(*this, typeOp<polyMeshModifier>());
}
Foam::wordList Foam::polyTopoChanger::names() const
{
const PtrList<polyMeshModifier>& modifiers = *this;
wordList lst(modifiers.size());
forAll(modifiers, i)
{
lst[i] = modifiers[i].name();
}
return lst;
return PtrListOps::get<word>(*this, typeOp<polyMeshModifier>());
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -29,6 +29,7 @@ License
#include "faBoundaryMesh.H"
#include "faMesh.H"
#include "primitiveMesh.H"
#include "PtrListOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -37,82 +38,6 @@ namespace Foam
defineTypeNameAndDebug(faBoundaryMesh, 0);
}
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
// Templated implementation for types(), names(), etc - file-scope
template<class ListType, class GetOp>
static ListType getMethodImpl
(
const faPatchList& list,
const GetOp& getop
)
{
const label len = list.size();
ListType output(len);
for (label i = 0; i < len; ++i)
{
output[i] = getop(list[i]);
}
return output;
}
// Templated implementation for indices() - file-scope
template<class UnaryMatchPredicate>
static labelList indicesImpl
(
const faPatchList& list,
const UnaryMatchPredicate& matcher
)
{
const label len = list.size();
labelList output(len);
label count = 0;
for (label i = 0; i < len; ++i)
{
if (matcher(list[i].name()))
{
output[count++] = i;
}
}
output.resize(count);
return output;
}
// Templated implementation for findIndex() - file-scope
template<class UnaryMatchPredicate>
label findIndexImpl
(
const faPatchList& list,
const UnaryMatchPredicate& matcher
)
{
const label len = list.size();
for (label i = 0; i < len; ++i)
{
if (matcher(list[i].name()))
{
return i;
}
}
return -1;
}
} // End namespace Foam
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::faBoundaryMesh::faBoundaryMesh
@ -220,13 +145,13 @@ Foam::lduInterfacePtrsList Foam::faBoundaryMesh::interfaces() const
Foam::wordList Foam::faBoundaryMesh::names() const
{
return getMethodImpl<wordList>(*this, getNameOp<faPatch>());
return PtrListOps::get<word>(*this, nameOp<faPatch>());
}
Foam::wordList Foam::faBoundaryMesh::types() const
{
return getMethodImpl<wordList>(*this, getTypeOp<faPatch>());
return PtrListOps::get<word>(*this, typeOp<faPatch>());
}
@ -244,8 +169,7 @@ Foam::labelList Foam::faBoundaryMesh::indices
if (key.isPattern())
{
const regExp matcher(key);
return indicesImpl(*this, matcher);
return PtrListOps::findMatching(*this, matcher);
}
else
{
@ -253,8 +177,7 @@ Foam::labelList Foam::faBoundaryMesh::indices
// Special version of above for reduced memory footprint
const word& matcher = key;
const label patchId = findIndexImpl(*this, matcher);
const label patchId = PtrListOps::firstMatching(*this, matcher);
if (patchId >= 0)
{
@ -275,14 +198,14 @@ Foam::label Foam::faBoundaryMesh::findIndex(const keyType& key) const
else if (key.isPattern())
{
// Find as regex
regExp matcher(key);
return findIndexImpl(*this, matcher);
const regExp matcher(key);
return PtrListOps::firstMatching(*this, matcher);
}
else
{
// Find as literal string
const word& matcher = key;
return findIndexImpl(*this, matcher);
return PtrListOps::firstMatching(*this, matcher);
}
}
@ -294,7 +217,7 @@ Foam::label Foam::faBoundaryMesh::findPatchID(const word& patchName) const
return -1;
}
return findIndexImpl(*this, patchName);
return PtrListOps::firstMatching(*this, patchName);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -27,6 +27,8 @@ License
\*---------------------------------------------------------------------------*/
#include "coordinateSystems.H"
#include "predicates.H"
#include "PtrListOps.H"
#include "Time.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -42,99 +44,8 @@ namespace Foam
static const char* headerTypeCompat = "IOPtrList<coordinateSystem>";
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
// Templated implementation for names() - file-scope
template<class UnaryMatchPredicate>
static wordList namesImpl
(
const PtrList<coordinateSystem>& list,
const UnaryMatchPredicate& matcher,
const bool doSort
)
{
const label len = list.size();
wordList output(len);
label count = 0;
for (label i = 0; i < len; ++i)
{
const word& itemName = list[i].name();
if (matcher(itemName))
{
output[count++] = itemName;
}
}
output.resize(count);
if (doSort)
{
Foam::sort(output);
}
return output;
}
// Templated implementation for indices() - file-scope
template<class UnaryMatchPredicate>
static labelList indicesImpl
(
const PtrList<coordinateSystem>& list,
const UnaryMatchPredicate& matcher
)
{
const label len = list.size();
labelList output(len);
label count = 0;
for (label i = 0; i < len; ++i)
{
if (matcher(list[i].name()))
{
output[count++] = i;
}
}
output.resize(count);
return output;
}
// Templated implementation for findIndex() - file-scope
template<class UnaryMatchPredicate>
label findIndexImpl
(
const PtrList<coordinateSystem>& list,
const UnaryMatchPredicate& matcher
)
{
const label len = list.size();
for (label i = 0; i < len; ++i)
{
if (matcher(list[i].name()))
{
return i;
}
}
return -1;
}
} // End namespace Foam
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::coordinateSystems::readFromStream(const bool valid)
{
Istream& is = readStream(word::null, valid);
@ -277,14 +188,14 @@ Foam::labelList Foam::coordinateSystems::indices(const keyType& key) const
else if (key.isPattern())
{
// Match as regex
regExp matcher(key);
return indicesImpl(*this, matcher);
const regExp matcher(key);
return PtrListOps::findMatching(*this, matcher);
}
else
{
// Compare as literal string
const word& matcher = key;
return indicesImpl(*this, matcher);
return PtrListOps::findMatching(*this, matcher);
}
}
@ -295,7 +206,7 @@ Foam::labelList Foam::coordinateSystems::indices(const wordRes& matcher) const
{
return labelList();
}
return indicesImpl(*this, matcher);
return PtrListOps::findMatching(*this, matcher);
}
@ -305,18 +216,17 @@ Foam::label Foam::coordinateSystems::findIndex(const keyType& key) const
{
return -1;
}
if (key.isPattern())
else if (key.isPattern())
{
// Find as regex
regExp matcher(key);
return findIndexImpl(*this, matcher);
const regExp matcher(key);
return PtrListOps::firstMatching(*this, matcher);
}
else
{
// Find as literal string
const word& matcher = key;
return findIndexImpl(*this, matcher);
return PtrListOps::firstMatching(*this, matcher);
}
}
@ -327,7 +237,7 @@ Foam::label Foam::coordinateSystems::findIndex(const wordRes& matcher) const
{
return -1;
}
return findIndexImpl(*this, matcher);
return PtrListOps::firstMatching(*this, matcher);
}
@ -384,17 +294,7 @@ Foam::coordinateSystems::lookup(const word& name) const
Foam::wordList Foam::coordinateSystems::names() const
{
const PtrList<coordinateSystem>& list = *this;
wordList result(list.size());
forAll(list, i)
{
result[i] = list[i].name();
}
return result;
// return ListOps::create<word>(list, nameOp<coordinateSystem>());
return PtrListOps::names(*this, predicates::always{});
}
@ -407,27 +307,27 @@ Foam::wordList Foam::coordinateSystems::names(const keyType& key) const
else if (key.isPattern())
{
// Find as regex
regExp matcher(key);
return namesImpl(*this, matcher, false);
const regExp matcher(key);
return PtrListOps::names(*this, matcher);
}
else
{
// Find as literal string
const word& matcher = key;
return namesImpl(*this, matcher, false);
return PtrListOps::names(*this, matcher);
}
}
Foam::wordList Foam::coordinateSystems::names(const wordRe& matcher) const
{
return namesImpl(*this, matcher, false);
return PtrListOps::names(*this, matcher);
}
Foam::wordList Foam::coordinateSystems::names(const wordRes& matcher) const
{
return namesImpl(*this, matcher, false);
return PtrListOps::names(*this, matcher);
}