From 12837a67d50a091c24333d2cb32e9a70ae9d188b Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 26 Jul 2010 13:18:59 +0200 Subject: [PATCH] ENH: add ZoneMesh::findIndices() method STYLE: use findIndices instead of findAll for coordinateSystems --- .../surfaceMeshConvert/surfaceMeshConvert.C | 12 ++-- .../surfaceMeshExport/surfaceMeshExport.C | 12 ++-- .../surfaceMeshImport/surfaceMeshImport.C | 12 ++-- .../meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C | 63 +++++++++++++++++- .../meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H | 10 ++- .../cfdTools/general/porousMedia/porousZone.C | 20 +----- .../coordinateSystems/coordinateSystem.C | 8 +-- .../coordinateSystems/coordinateSystems.C | 66 ++++++++++--------- .../coordinateSystems/coordinateSystems.H | 12 ++-- 9 files changed, 137 insertions(+), 78 deletions(-) diff --git a/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C b/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C index 248ac3e3d0..3d14464e71 100644 --- a/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C +++ b/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C @@ -206,8 +206,8 @@ int main(int argc, char *argv[]) { const word csName = args["from"]; - label csId = csLst.find(csName); - if (csId < 0) + const label csIndex = csLst.findIndex(csName); + if (csIndex < 0) { FatalErrorIn(args.executable()) << "Cannot find -from " << csName << nl @@ -215,15 +215,15 @@ int main(int argc, char *argv[]) << exit(FatalError); } - fromCsys.reset(new coordinateSystem(csLst[csId])); + fromCsys.reset(new coordinateSystem(csLst[csIndex])); } if (args.optionFound("to")) { const word csName = args["to"]; - label csId = csLst.find(csName); - if (csId < 0) + const label csIndex = csLst.findIndex(csName); + if (csIndex < 0) { FatalErrorIn(args.executable()) << "Cannot find -to " << csName << nl @@ -231,7 +231,7 @@ int main(int argc, char *argv[]) << exit(FatalError); } - toCsys.reset(new coordinateSystem(csLst[csId])); + toCsys.reset(new coordinateSystem(csLst[csIndex])); } diff --git a/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C b/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C index 3995031b53..0631f195e2 100644 --- a/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C +++ b/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C @@ -195,8 +195,8 @@ int main(int argc, char *argv[]) { const word csName = args["from"]; - label csId = csLst.find(csName); - if (csId < 0) + const label csIndex = csLst.findIndex(csName); + if (csIndex < 0) { FatalErrorIn(args.executable()) << "Cannot find -from " << csName << nl @@ -204,15 +204,15 @@ int main(int argc, char *argv[]) << exit(FatalError); } - fromCsys.reset(new coordinateSystem(csLst[csId])); + fromCsys.reset(new coordinateSystem(csLst[csIndex])); } if (args.optionFound("to")) { const word csName = args["to"]; - label csId = csLst.find(csName); - if (csId < 0) + const label csIndex = csLst.findIndex(csName); + if (csIndex < 0) { FatalErrorIn(args.executable()) << "Cannot find -to " << csName << nl @@ -220,7 +220,7 @@ int main(int argc, char *argv[]) << exit(FatalError); } - toCsys.reset(new coordinateSystem(csLst[csId])); + toCsys.reset(new coordinateSystem(csLst[csIndex])); } diff --git a/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C b/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C index 853becfc89..3aee72a52d 100644 --- a/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C +++ b/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C @@ -208,8 +208,8 @@ int main(int argc, char *argv[]) { const word csName = args["from"]; - label csId = csLst.find(csName); - if (csId < 0) + const label csIndex = csLst.findIndex(csName); + if (csIndex < 0) { FatalErrorIn(args.executable()) << "Cannot find -from " << csName << nl @@ -217,15 +217,15 @@ int main(int argc, char *argv[]) << exit(FatalError); } - fromCsys.reset(new coordinateSystem(csLst[csId])); + fromCsys.reset(new coordinateSystem(csLst[csIndex])); } if (args.optionFound("to")) { const word csName = args["to"]; - label csId = csLst.find(csName); - if (csId < 0) + const label csIndex = csLst.findIndex(csName); + if (csIndex < 0) { FatalErrorIn(args.executable()) << "Cannot find -to " << csName << nl @@ -233,7 +233,7 @@ int main(int argc, char *argv[]) << exit(FatalError); } - toCsys.reset(new coordinateSystem(csLst[csId])); + toCsys.reset(new coordinateSystem(csLst[csIndex])); } diff --git a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C index babb31b6e8..7df85dc6d5 100644 --- a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C @@ -26,6 +26,7 @@ License #include "ZoneMesh.H" #include "entry.H" #include "demandDrivenData.H" +#include "stringListOps.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -241,6 +242,66 @@ Foam::wordList Foam::ZoneMesh::names() const } +template +Foam::labelList Foam::ZoneMesh::findIndices +( + const keyType& key +) const +{ + labelList indices; + if (key.isPattern()) + { + indices = findStrings(key, this->names()); + } + else + { + indices.setSize(this->size()); + label nFound = 0; + forAll(*this, i) + { + if (key == operator[](i).name()) + { + indices[nFound++] = i; + } + } + indices.setSize(nFound); + } + + return indices; +} + + +template +Foam::label Foam::ZoneMesh::findIndex +( + const keyType& key +) const +{ + if (key.isPattern()) + { + labelList indices = this->findIndices(key); + // return first element + if (!indices.empty()) + { + return indices[0]; + } + } + else + { + forAll(*this, i) + { + if (key == operator[](i).name()) + { + return i; + } + } + } + + // not found + return -1; +} + + template Foam::label Foam::ZoneMesh::findZoneID ( @@ -265,7 +326,7 @@ Foam::label Foam::ZoneMesh::findZoneID << "List of available zone names: " << names() << endl; } - // A dummy return to keep the compiler happy + // not found return -1; } diff --git a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H index 03b9c216be..8854d9533b 100644 --- a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H +++ b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H @@ -121,8 +121,8 @@ public: // Return -1 if the object is not in the zone const Map