From 5080d3cd1fd2d82a028c648721f55fa1c756d404 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 15 Jan 2021 14:59:19 +0100 Subject: [PATCH 1/3] ENH: additional PtrListOps to simplify gathering information - get: uses access operation to get values for each list item Example, PtrListOps::get(mesh.boundaryMesh(), nameOp()); - names: the name() of each list item filtered for matches - firstMatching: index of first item with a matching name() - findMatching: indices of items with a matching match name() Example, PtrListOps::findMatching(mesh.boundaryMesh(), wordRes( ... )); STYLE: deprecate transitional getNameOp, getTypeOp - use nameOp, typeOp (word.H) instead --- .../PtrLists/PtrListOps/PtrListOps.H | 53 +++++++- .../PtrLists/PtrListOps/PtrListOpsTemplates.C | 116 +++++++++++++++++- src/OpenFOAM/primitives/ops/ops.H | 31 ++--- 3 files changed, 174 insertions(+), 26 deletions(-) diff --git a/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOps.H b/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOps.H index 08c1d1683b..421c4fd590 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOps.H +++ b/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOps.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -136,8 +136,59 @@ struct greater }; +//- List of values generated by applying the access operation +//- to each list item. +// +// For example, +// \code +// PtrListOps::get(mesh.boundaryMesh(), nameOp()); +// \endcode +template +List get +( + const UPtrList& list, + const AccessOp& aop +); + + +//- List of names generated by calling \c name() for each list item +//- and filtered for matches +// +// For example, +// \code +// wordRes matches(...); +// PtrListOps::names(mesh.boundaryMesh(), matches); +// +// PtrListOps::names(mesh.boundaryMesh(), predicates::always()); +// \endcode +template +List names +( + const UPtrList& list, + const UnaryMatchPredicate& matcher +); + + +//- Find first list item with 'name()' that matches, -1 on failure +template +label firstMatching +( + const UPtrList& list, + const UnaryMatchPredicate& matcher +); + + +//- Extract list indices for all items with 'name()' that matches +template +labelList findMatching +( + const UPtrList& list, + const UnaryMatchPredicate& matcher +); + } // End namespace ListOps + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOpsTemplates.C b/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOpsTemplates.C index 0feeb4bba8..a4182de016 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOpsTemplates.C +++ b/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOpsTemplates.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019-2020 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -103,4 +103,118 @@ void Foam::shuffle(UPtrList& list) } + +// Templated implementation for types(), names(), etc - file-scope +template +Foam::List Foam::PtrListOps::get +( + const UPtrList& list, + const AccessOp& aop +) +{ + const label len = list.size(); + + List output(len); + + label count = 0; + for (label i = 0; i < len; ++i) + { + const T* ptr = list.get(i); + + if (bool(ptr)) + { + output[count++] = aop(*ptr); + } + } + + output.resize(count); + + return output; +} + + +template +Foam::List Foam::PtrListOps::names +( + const UPtrList& list, + const UnaryMatchPredicate& matcher +) +{ + // Possible: const auto aop = nameOp(); + + const label len = list.size(); + + List output(len); + + label count = 0; + for (label i = 0; i < len; ++i) + { + const T* ptr = list.get(i); + + if (bool(ptr)) + { + if (matcher(ptr->name())) + { + output[count++] = (ptr->name()); + } + } + } + + output.resize(count); + + return output; +} + + +template +Foam::label Foam::PtrListOps::firstMatching +( + const UPtrList& list, + const UnaryMatchPredicate& matcher +) +{ + const label len = list.size(); + + for (label i = 0; i < len; ++i) + { + const T* ptr = list.get(i); + + if (bool(ptr) && matcher(ptr->name())) + { + return i; + } + } + + return -1; +} + + +template +Foam::labelList Foam::PtrListOps::findMatching +( + const UPtrList& list, + const UnaryMatchPredicate& matcher +) +{ + const label len = list.size(); + + labelList output(len); + + label count = 0; + for (label i = 0; i < len; ++i) + { + const T* ptr = list.get(i); + + if (bool(ptr) && matcher(ptr->name())) + { + output[count++] = i; + } + } + + output.resize(count); + + return output; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/ops/ops.H b/src/OpenFOAM/primitives/ops/ops.H index 4411f85460..347209d4ce 100644 --- a/src/OpenFOAM/primitives/ops/ops.H +++ b/src/OpenFOAM/primitives/ops/ops.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -271,30 +271,13 @@ struct compareOp }; -//- General get operation to extract the 'name' from an object as a word. -// The default implementation uses the 'name()' method commonly used within -// OpenFOAM. -template -struct getNameOp -{ - word operator()(const T& x) const WARNRETURN - { - return x.name(); - } -}; +//- Deprecated(2020-11) use nameOp (word.H) +// \deprecated(2020-11) use nameOp +template struct getNameOp : nameOp {}; - -//- General get operation to extract the 'type' from an object as a word. -// The default implementation uses the 'type()' method commonly used within -// OpenFOAM. -template -struct getTypeOp -{ - word operator()(const T& x) const WARNRETURN - { - return x.type(); - } -}; +//- Deprecated(2020-11) use typeOp (word.H) +// \deprecated(2020-11) use typeOp +template struct getTypeOp : typeOp {}; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // From f24e3f113f08fd109e4d8151e51074f17dc95345 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 15 Jan 2021 14:59:33 +0100 Subject: [PATCH 2/3] ENH: use PtrListOps to reduce code --- .../polyBoundaryMesh/polyBoundaryMesh.C | 121 +++------------ .../meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C | 143 +++--------------- .../meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H | 28 +--- .../polyTopoChanger/polyTopoChanger.C | 25 +-- .../faMesh/faBoundaryMesh/faBoundaryMesh.C | 97 ++---------- .../coordinate/systems/coordinateSystems.C | 136 +++-------------- 6 files changed, 80 insertions(+), 470 deletions(-) diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C index 67c4cb10ad..a413e8037e 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C @@ -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 - 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 - 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 - 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(*this, getNameOp()); + return PtrListOps::get(*this, nameOp()); } Foam::wordList Foam::polyBoundaryMesh::types() const { - return getMethodImpl(*this, getTypeOp()); + return PtrListOps::get(*this, typeOp()); } Foam::wordList Foam::polyBoundaryMesh::physicalTypes() const { return - getMethodImpl + PtrListOps::get ( *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 + PtrListOps::get