Commit 91cfe4c2 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: add wordRes::matching() method

- returns indices of matching entries.
parent dff2569c
......@@ -26,6 +26,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "stringListOps.H"
#include "FlatOutput.H"
#include "IOstreams.H"
#include "StringStream.H"
......@@ -64,7 +65,10 @@ int main(int argc, char *argv[])
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)
{
Info<< " -> " << strLst[matches[i]] << nl;
......
......@@ -518,7 +518,8 @@ int main(int argc, char *argv[])
patchNames[patchi] = surf.patches()[patchi].name();
}
labelList indices = findStrings(baffleSelect, patchNames);
labelList indices(baffleSelect.matching(patchNames));
for (const label patchId : indices)
{
surfBaffleRegions[patchId] = true;
......
......@@ -54,9 +54,9 @@ bool Foam::functionEntries::removeEntry::execute
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)
{
......@@ -72,7 +72,7 @@ bool Foam::functionEntries::removeEntry::execute
{
// Remove by pattern
const wordList dictKeys = parentDict.toc();
const labelList indices = findStrings(regExp(key), dictKeys);
const labelList indices = findStrings(key, dictKeys);
for (const auto idx : indices)
{
......
......@@ -1160,14 +1160,17 @@ void Foam::argList::parse
roots.resize(Pstream::nProcs()-1, fileName::null);
source = "-hostRoots";
ITstream is = this->lookup("hostRoots");
ITstream is(source, options_["hostRoots"]);
List<Tuple2<wordRe, fileName>> hostRoots(is);
checkITstream(is, "hostRoots");
for (const auto& hostRoot : hostRoots)
{
const wordRe& re = hostRoot.first();
labelList matched(findStrings(re, slaveMachine));
labelList matched
(
findStrings(hostRoot.first(), slaveMachine)
);
for (const label slavei : matched)
{
if (!roots[slavei].empty())
......
......@@ -26,7 +26,6 @@ License
#include "ZoneMesh.H"
#include "entry.H"
#include "demandDrivenData.H"
#include "stringListOps.H"
#include "Pstream.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......
......@@ -27,6 +27,12 @@ InNamspace
Description
Operations on lists of strings.
Namespace
Foam::stringListOps
Description
Various utility functions to work on lists of strings.
SourceFiles
stringListOpsTemplates.C
......@@ -133,7 +139,7 @@ namespace Foam
}
// Subsetting multi-string matches (similar to ListOp):
// Subsetting multi-string matches (similar to ListOp):
//- Extract elements of StringList when regular expression matches
// optionally invert the match
......@@ -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
// \deprecated (FEB-2018) Treating string as regex may be inefficient
// and lead to unintended results.
......@@ -378,8 +428,7 @@ namespace Foam
const bool invert=false
) = delete;
}
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -126,7 +126,7 @@ public:
//
// \param literal Force literal match only.
// \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.
// It stops if there is a literal match, or continues to examine
......@@ -135,6 +135,19 @@ public:
// match was found and UNKNOWN otherwise.
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
......
......@@ -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 * * * * * * * * * * * * * //
inline bool Foam::wordRes::operator()(const std::string& text) const
......
......@@ -146,7 +146,7 @@ void Foam::ensightMesh::correct()
if (!matcher.empty())
{
useAll = false;
matched = findStrings(matcher, patchNames);
matched = matcher.matching(patchNames);
}
}
......
......@@ -24,7 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "ddt2.H"
#include "stringListOps.H"
#include "volFields.H"
#include "dictionary.H"
#include "wordRes.H"
......
......@@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "externalCoupled.H"
#include "stringListOps.H"
#include "addToRunTimeSelectionTable.H"
#include "OSspecific.H"
#include "Fstream.H"
......
......@@ -24,7 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "zeroGradient.H"
#include "stringListOps.H"
#include "volFields.H"
#include "dictionary.H"
#include "wordRes.H"
......
......@@ -96,7 +96,7 @@ void Foam::decompositionConstraints::preserveFaceZones::add
const faceZoneMesh& fZones = mesh.faceZones();
const labelList zoneIDs(findStrings(zones_, fZones.names()));
const labelList zoneIDs(zones_.matching(fZones.names()));
label nUnblocked = 0;
......@@ -167,7 +167,7 @@ void Foam::decompositionConstraints::preserveFaceZones::apply
const faceZoneMesh& fZones = mesh.faceZones();
const labelList zoneIDs(findStrings(zones_, fZones.names()));
const labelList zoneIDs(zones_.matching(fZones.names()));
label nChanged = 0;
......
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