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

ENH: add stringListOps with whitelist/blacklist matching

- refactored from ensightMesh, foamToVTK, surfaceMeshExtract

STYLE: use wordRes matching() method instead of findString() function
parent a456e9ae
......@@ -172,10 +172,26 @@ labelList getSelectedPatches
const wordRes& blacklist
)
{
DynamicList<label> patchIDs(patches.size());
// Name-based selection
labelList indices
(
stringListOps::findMatching
(
patches,
whitelist,
blacklist,
nameOp<polyPatch>()
)
);
// Remove undesirable patches
for (const polyPatch& pp : patches)
label count = 0;
for (const label patchi : indices)
{
const polyPatch& pp = patches[patchi];
if (isType<emptyPolyPatch>(pp))
{
continue;
......@@ -185,33 +201,13 @@ labelList getSelectedPatches
break; // No processor patches for parallel output
}
const word& patchName = pp.name();
bool accept = false;
if (whitelist.size())
{
const auto matched = whitelist.matched(patchName);
accept =
(
matched == wordRe::LITERAL
? true
: (matched == wordRe::REGEX && !blacklist.match(patchName))
);
}
else
{
accept = !blacklist.match(patchName);
}
if (accept)
{
patchIDs.append(pp.index());
}
indices[count] = patchi;
++count;
}
return patchIDs.shrink();
indices.resize(count);
return indices;
}
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -67,10 +67,26 @@ labelList getSelectedPatches
const wordRes& blacklist
)
{
DynamicList<label> patchIDs(patches.size());
// Name-based selection
labelList indices
(
stringListOps::findMatching
(
patches,
whitelist,
blacklist,
nameOp<polyPatch>()
)
);
// Remove undesirable patches
for (const polyPatch& pp : patches)
label count = 0;
for (const label patchi : indices)
{
const polyPatch& pp = patches[patchi];
if (isType<emptyPolyPatch>(pp))
{
continue;
......@@ -80,33 +96,13 @@ labelList getSelectedPatches
break; // No processor patches for parallel output
}
const word& patchName = pp.name();
bool accept = false;
if (whitelist.size())
{
const auto matched = whitelist.matched(patchName);
accept =
(
matched == wordRe::LITERAL
? true
: (matched == wordRe::REGEX && !blacklist.match(patchName))
);
}
else
{
accept = !blacklist.match(patchName);
}
if (accept)
{
patchIDs.append(pp.index());
}
indices[count] = patchi;
++count;
}
return patchIDs.shrink();
indices.resize(count);
return indices;
}
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2018 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -47,6 +47,7 @@ SourceFiles
#include "labelList.H"
#include "stringList.H"
#include "wordRes.H"
#include "flipOp.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -346,6 +347,32 @@ struct foundOp
}
};
//- Return ids for items with matching names.
// Uses a combination of whitelist and blacklist.
//
// An empty whitelist accepts everything that is not blacklisted.
// A regex match is trumped by a literal match.
//
// Eg,
// \verbatim
// input: ( abc apple wall wall1 wall2 )
// whitelist: ( abc def "wall.*" )
// blacklist: ( "[ab].*" wall )
//
// result: (abc wall1 wall2)
// \endverbatim
//
// \return List indices for matches
template<class StringListType, class AccessOp = noOp>
labelList findMatching
(
const StringListType& input,
const wordRes& whitelist,
const wordRes& blacklist = wordRes(),
AccessOp aop = noOp()
);
} // End namespace stringListOps
......
......@@ -109,4 +109,57 @@ void Foam::inplaceSubsetMatchingStrings
}
template<class StringListType, class AccessOp>
Foam::labelList Foam::stringListOps::findMatching
(
const StringListType& input,
const wordRes& whitelist,
const wordRes& blacklist,
AccessOp aop
)
{
const label len = input.size();
if (whitelist.empty() && blacklist.empty())
{
return identity(len);
}
labelList indices(len);
label count = 0;
for (label i=0; i < len; ++i)
{
const std::string& text = aop(input[i]);
bool accept = false;
if (whitelist.size())
{
const auto result = whitelist.matched(text);
accept =
(
result == wordRe::LITERAL
? true
: (result == wordRe::REGEX && !blacklist.match(text))
);
}
else
{
accept = !blacklist.match(text);
}
if (accept)
{
indices[count] = i;
++count;
}
}
indices.resize(count);
return indices;
}
// ************************************************************************* //
......@@ -42,69 +42,6 @@ const Foam::label Foam::ensightMesh::internalZone = -1;
namespace Foam
{
// Find matching ids based on whitelist, blacklist
//
// An empty whitelist accepts everything that is not blacklisted.
// A regex match is trumped by a literal match.
//
// Eg,
// input: ( abc apple wall wall1 wall2 )
// whitelist: ( abc def "wall.*" )
// blacklist: ( "[ab].*" wall )
//
// result: (abc wall1 wall2)
//
static labelList getSelected
(
const UList<word>& input,
const wordRes& whitelist,
const wordRes& blacklist
)
{
const label len = input.size();
if (whitelist.empty() && blacklist.empty())
{
return identity(len);
}
labelList indices(len);
label count = 0;
for (label i=0; i < len; ++i)
{
const auto& text = input[i];
bool accept = false;
if (whitelist.size())
{
const auto result = whitelist.matched(text);
accept =
(
result == wordRe::LITERAL
? true
: (result == wordRe::REGEX && !blacklist.match(text))
);
}
else
{
accept = !blacklist.match(text);
}
if (accept)
{
indices[count] = i;
++count;
}
}
indices.resize(count);
return indices;
}
// Patch names without processor patches
static wordList nonProcessorPatchNames(const polyBoundaryMesh& bmesh)
{
......@@ -237,7 +174,7 @@ void Foam::ensightMesh::correct()
const labelList patchIds =
(
option().useBoundaryMesh()
? getSelected
? stringListOps::findMatching
(
patchNames,
option().patchSelection(),
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -124,10 +124,13 @@ bool Foam::functionObjects::mapFields::mapFieldType() const
const fvMesh& mapRegion = mapRegionPtr_();
wordList fieldNames(this->mesh_.names(VolFieldType::typeName));
const labelList selected = findStrings(fieldNames_, fieldNames);
const labelList selected(fieldNames_.matching(fieldNames));
for (const label fieldi : selected)
{
const word& fieldName = fieldNames[fieldi];
const VolFieldType& field = lookupObject<VolFieldType>(fieldName);
if (!mapRegion.foundObject<VolFieldType>(fieldName))
......@@ -160,7 +163,7 @@ bool Foam::functionObjects::mapFields::mapFieldType() const
evaluateConstraintTypes(mappedField);
}
return selected.size() > 0;
return !selected.empty();
}
......@@ -172,7 +175,9 @@ bool Foam::functionObjects::mapFields::writeFieldType() const
const fvMesh& mapRegion = mapRegionPtr_();
wordList fieldNames(this->mesh_.names(VolFieldType::typeName));
const labelList selected = findStrings(fieldNames_, fieldNames);
const labelList selected(fieldNames_.matching(fieldNames));
for (const label fieldi : selected)
{
const word& fieldName = fieldNames[fieldi];
......@@ -185,7 +190,7 @@ bool Foam::functionObjects::mapFields::writeFieldType() const
Log << " " << fieldName << ": written";
}
return selected.size() > 0;
return !selected.empty();
}
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2013-2016 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -865,11 +865,13 @@ bool Foam::functionObjects::regionSizeDistribution::write()
// Collect some more field
{
wordList scalarNames(obr_.names(volScalarField::typeName));
labelList selected = findStrings(fields_, scalarNames);
forAll(selected, i)
const labelList selected(fields_.matching(scalarNames));
for (const label fieldi : selected)
{
const word& fldName = scalarNames[selected[i]];
const word& fldName = scalarNames[fieldi];
Log << " Scalar field " << fldName << endl;
const scalarField& fld = obr_.lookupObject
......@@ -894,11 +896,13 @@ bool Foam::functionObjects::regionSizeDistribution::write()
}
{
wordList vectorNames(obr_.names(volVectorField::typeName));
labelList selected = findStrings(fields_, vectorNames);
forAll(selected, i)
const labelList selected(fields_.matching(vectorNames));
for (const label fieldi : selected)
{
const word& fldName = vectorNames[selected[i]];
const word& fldName = vectorNames[fieldi];
Log << " Vector field " << fldName << endl;
vectorField fld = obr_.lookupObject
......
Markdown is supported
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