ENH: add wordRes::matching() method

- returns indices of matching entries.
This commit is contained in:
Mark Olesen
2018-11-09 21:21:15 +01:00
parent dff2569c7f
commit 91cfe4c271
13 changed files with 114 additions and 18 deletions

View File

@ -26,6 +26,7 @@ Description
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "stringListOps.H" #include "stringListOps.H"
#include "FlatOutput.H"
#include "IOstreams.H" #include "IOstreams.H"
#include "StringStream.H" #include "StringStream.H"
@ -64,7 +65,10 @@ int main(int argc, char *argv[])
matches = findStrings(reLst, strLst); matches = findStrings(reLst, strLst);
Info<< "matches found for " << reLst << nl << matches << nl; Info<< "matching " << flatOutput(reLst) << " => "
<< reLst.matching(strLst) << nl;
Info<< "matches found for " << flatOutput(reLst) << " => "
<< matches << nl;
forAll(matches, i) forAll(matches, i)
{ {
Info<< " -> " << strLst[matches[i]] << nl; Info<< " -> " << strLst[matches[i]] << nl;

View File

@ -518,7 +518,8 @@ int main(int argc, char *argv[])
patchNames[patchi] = surf.patches()[patchi].name(); patchNames[patchi] = surf.patches()[patchi].name();
} }
labelList indices = findStrings(baffleSelect, patchNames); labelList indices(baffleSelect.matching(patchNames));
for (const label patchId : indices) for (const label patchId : indices)
{ {
surfBaffleRegions[patchId] = true; surfBaffleRegions[patchId] = true;

View File

@ -54,9 +54,9 @@ bool Foam::functionEntries::removeEntry::execute
Istream& is Istream& is
) )
{ {
const List<keyType> patterns = functionEntry::readStringList<keyType>(is); const wordRes patterns(functionEntry::readStringList<wordRe>(is));
for (const keyType& key : patterns) for (const wordRe& key : patterns)
{ {
if (key.isLiteral() && key.find('/') != string::npos) if (key.isLiteral() && key.find('/') != string::npos)
{ {
@ -72,7 +72,7 @@ bool Foam::functionEntries::removeEntry::execute
{ {
// Remove by pattern // Remove by pattern
const wordList dictKeys = parentDict.toc(); const wordList dictKeys = parentDict.toc();
const labelList indices = findStrings(regExp(key), dictKeys); const labelList indices = findStrings(key, dictKeys);
for (const auto idx : indices) for (const auto idx : indices)
{ {

View File

@ -1160,14 +1160,17 @@ void Foam::argList::parse
roots.resize(Pstream::nProcs()-1, fileName::null); roots.resize(Pstream::nProcs()-1, fileName::null);
source = "-hostRoots"; source = "-hostRoots";
ITstream is = this->lookup("hostRoots"); ITstream is(source, options_["hostRoots"]);
List<Tuple2<wordRe, fileName>> hostRoots(is); List<Tuple2<wordRe, fileName>> hostRoots(is);
checkITstream(is, "hostRoots"); checkITstream(is, "hostRoots");
for (const auto& hostRoot : hostRoots) for (const auto& hostRoot : hostRoots)
{ {
const wordRe& re = hostRoot.first(); labelList matched
labelList matched(findStrings(re, slaveMachine)); (
findStrings(hostRoot.first(), slaveMachine)
);
for (const label slavei : matched) for (const label slavei : matched)
{ {
if (!roots[slavei].empty()) if (!roots[slavei].empty())

View File

@ -26,7 +26,6 @@ License
#include "ZoneMesh.H" #include "ZoneMesh.H"
#include "entry.H" #include "entry.H"
#include "demandDrivenData.H" #include "demandDrivenData.H"
#include "stringListOps.H"
#include "Pstream.H" #include "Pstream.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

View File

@ -27,6 +27,12 @@ InNamspace
Description Description
Operations on lists of strings. Operations on lists of strings.
Namespace
Foam::stringListOps
Description
Various utility functions to work on lists of strings.
SourceFiles SourceFiles
stringListOpsTemplates.C stringListOpsTemplates.C
@ -306,6 +312,50 @@ namespace Foam
} }
/*---------------------------------------------------------------------------*\
Namespace stringListOps Declaration
\*---------------------------------------------------------------------------*/
namespace stringListOps
{
//- Functor to determine if a string is exists in a list of strings.
// For example,
//
// \code
// reduce(text, stringListOps::foundOp<word>(myNames));
// \endcode
template<class StringType>
struct foundOp
{
const UList<StringType>& values;
foundOp(const UList<StringType>& list)
:
values(list)
{}
bool operator()(const std::string& text) const
{
return values.found(text);
}
};
} // End namespace stringListOps
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Housekeeping
namespace Foam
{
//- Find using C-string as a regex //- Find using C-string as a regex
// \deprecated (FEB-2018) Treating string as regex may be inefficient // \deprecated (FEB-2018) Treating string as regex may be inefficient
// and lead to unintended results. // and lead to unintended results.
@ -378,8 +428,7 @@ namespace Foam
const bool invert=false const bool invert=false
) = delete; ) = delete;
} } // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -126,7 +126,7 @@ public:
// //
// \param literal Force literal match only. // \param literal Force literal match only.
// \return True if text matches ANY of the entries. // \return True if text matches ANY of the entries.
inline bool match(const std::string& text, bool literal = false) const; inline bool match(const std::string& text, bool literal=false) const;
//- Smart match in the list of matchers, returning the match type. //- Smart match in the list of matchers, returning the match type.
// It stops if there is a literal match, or continues to examine // It stops if there is a literal match, or continues to examine
@ -135,6 +135,19 @@ public:
// match was found and UNKNOWN otherwise. // match was found and UNKNOWN otherwise.
inline wordRe::compOption matched(const std::string& text) const; inline wordRe::compOption matched(const std::string& text) const;
//- Extract list indices for all matches.
//
// \param input A list of string inputs to match against
// \param invert invert the matching logic
// \return The locations (indices) in the input list where match()
// is true
template<class StringType>
inline labelList matching
(
const UList<StringType>& input,
const bool invert=false
) const;
// Member Operators // Member Operators

View File

@ -95,6 +95,32 @@ Foam::wordRes::matched(const std::string& text) const
} }
template<class StringType>
inline Foam::labelList Foam::wordRes::matching
(
const UList<StringType>& input,
const bool invert
) const
{
const label len = input.size();
labelList indices(len);
label count = 0;
for (label i=0; i < len; ++i)
{
if (match(input[i]) ? !invert : invert)
{
indices[count] = i;
++count;
}
}
indices.resize(count);
return indices;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline bool Foam::wordRes::operator()(const std::string& text) const inline bool Foam::wordRes::operator()(const std::string& text) const

View File

@ -146,7 +146,7 @@ void Foam::ensightMesh::correct()
if (!matcher.empty()) if (!matcher.empty())
{ {
useAll = false; useAll = false;
matched = findStrings(matcher, patchNames); matched = matcher.matching(patchNames);
} }
} }

View File

@ -24,7 +24,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "ddt2.H" #include "ddt2.H"
#include "stringListOps.H"
#include "volFields.H" #include "volFields.H"
#include "dictionary.H" #include "dictionary.H"
#include "wordRes.H" #include "wordRes.H"

View File

@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "externalCoupled.H" #include "externalCoupled.H"
#include "stringListOps.H"
#include "addToRunTimeSelectionTable.H" #include "addToRunTimeSelectionTable.H"
#include "OSspecific.H" #include "OSspecific.H"
#include "Fstream.H" #include "Fstream.H"

View File

@ -24,7 +24,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "zeroGradient.H" #include "zeroGradient.H"
#include "stringListOps.H"
#include "volFields.H" #include "volFields.H"
#include "dictionary.H" #include "dictionary.H"
#include "wordRes.H" #include "wordRes.H"

View File

@ -96,7 +96,7 @@ void Foam::decompositionConstraints::preserveFaceZones::add
const faceZoneMesh& fZones = mesh.faceZones(); const faceZoneMesh& fZones = mesh.faceZones();
const labelList zoneIDs(findStrings(zones_, fZones.names())); const labelList zoneIDs(zones_.matching(fZones.names()));
label nUnblocked = 0; label nUnblocked = 0;
@ -167,7 +167,7 @@ void Foam::decompositionConstraints::preserveFaceZones::apply
const faceZoneMesh& fZones = mesh.faceZones(); const faceZoneMesh& fZones = mesh.faceZones();
const labelList zoneIDs(findStrings(zones_, fZones.names())); const labelList zoneIDs(zones_.matching(fZones.names()));
label nChanged = 0; label nChanged = 0;