reworked stringListOps to improve the flexibility

- added subsetStrings and inplaceSubsetString

- added class wordReListMatcher to wrap a match() for a UList<wordRe>
This commit is contained in:
Mark Olesen 2009-10-08 11:32:26 +02:00
parent 1194af6a8a
commit c45ea2c4f1
12 changed files with 511 additions and 135 deletions

View File

@ -27,6 +27,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "stringListOps.H"
#include "IStringStream.H"
#include "IOstreams.H"
using namespace Foam;
@ -36,21 +37,59 @@ using namespace Foam;
int main(int argc, char *argv[])
{
stringList sl(3);
sl[0] = "hello";
sl[1] = "heello";
sl[2] = "heeello";
stringList strLst
(
IStringStream
(
"("
"\"hello\""
"\"heello\""
"\"heeello\""
"\"bye\""
"\"bbye\""
"\"bbbye\""
"\"okey\""
"\"okkey\""
"\"okkkey\""
")"
)()
);
labelList matches = findStrings(".*ee.*", sl);
wordReList reLst(IStringStream("( okey \"[hy]e+.*\" )")());
Info<< "matches found for regexp .*ee.* : ";
Info<< "stringList " << strLst << nl;
labelList matches = findStrings(".*ee.*", strLst);
Info<< "matches found for regexp .*ee.* :" << nl << matches << nl;
forAll(matches, i)
{
Info<< " " << sl[matches[i]];
Info<< " -> " << strLst[matches[i]] << nl;
}
Info<< endl;
Info << "End\n" << endl;
matches = findStrings(reLst, strLst);
Info<< "matches found for " << reLst << nl << matches << nl;
forAll(matches, i)
{
Info<< " -> " << strLst[matches[i]] << nl;
}
Info<< endl;
stringList subLst = subsetStrings(".*ee.*", strLst);
Info<< "subset stringList: " << subLst << nl;
subLst = subsetStrings(reLst, strLst);
Info<< "subset stringList: " << subLst << nl;
inplaceSubsetStrings(reLst, strLst);
Info<< "subsetted stringList: " << strLst << nl;
inplaceSubsetStrings(".*l.*", strLst);
Info<< "subsetted stringList: " << strLst << nl;
Info<< "\nEnd\n" << endl;
return 0;
}

View File

@ -66,7 +66,9 @@ bool Foam::functionEntries::removeEntry::execute
)
{
wordList dictKeys = parentDict.toc();
labelList indices = findStrings<word>(readList<wordRe>(is), dictKeys);
wordReList patterns(is);
labelList indices = findStrings(patterns, dictKeys);
forAll(indices, indexI)
{

View File

@ -36,54 +36,262 @@ SourceFiles
#ifndef stringListOps_H
#define stringListOps_H
#include "regExp.H"
#include "labelList.H"
#include "stringList.H"
#include "wordReList.H"
#include "wordReListMatcher.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
//- Return list indices for strings matching the regular expression
template<class StringType>
labelList findStrings
(
const char* regexpPattern,
const UList<StringType>&
);
//- Return list indices for strings matching the regular expression
template<class StringType>
labelList findStrings
(
const std::string& regexpPattern,
const UList<StringType>&
);
//- Return list indices for strings matching the regular expression
template<class StringType>
labelList findStrings
(
const wordRe&,
const UList<StringType>&
);
//- Return list indices for strings matching one of the regular expression
template<class StringType>
labelList findStrings
(
const UList<wordRe>&,
const UList<StringType>&
);
// single-string matches:
//- Return true if string matches one of the regular expressions
template<class StringType>
bool findStrings
inline bool findStrings
(
const UList<wordRe>&,
const StringType& str
const wordReListMatcher& matcher,
const std::string& str
)
{
return matcher.match(str);
}
// multi-string matches:
//- Return list indices for matching strings
template<class Matcher, class StringType>
labelList findMatchingStrings
(
const Matcher&,
const UList<StringType>&,
const bool invert=false
);
//- Return list indices for strings matching the regular expression
// Template partial specialization of findMatchingStrings
template<class StringType>
labelList findStrings
(
const regExp& re,
const UList<StringType>& lst,
const bool invert=false
)
{
return findMatchingStrings(re, lst, invert);
}
//- Return list indices for strings matching the regular expression
// Template partial specialization of findMatchingStrings
template<class StringType>
labelList findStrings
(
const char* rePattern,
const UList<StringType>& lst,
const bool invert=false
)
{
return findStrings(regExp(rePattern), lst, invert);
}
//- Return list indices for strings matching the regular expression
// Template partial specialization of findMatchingStrings
template<class StringType>
labelList findStrings
(
const std::string& rePattern,
const UList<StringType>& lst,
const bool invert=false
)
{
return findMatchingStrings(regExp(rePattern), lst, invert);
}
//- Return list indices for strings matching the regular expression
// Template partial specialization of findMatchingStrings
template<class StringType>
labelList findStrings
(
const wordRe& wre,
const UList<StringType>& lst,
const bool invert=false
)
{
return findMatchingStrings(wre, lst, invert);
}
//- Return list indices for strings matching one of the regular expression
// Template partial specialization of findMatchingStrings
template<class StringType>
labelList findStrings
(
const wordReListMatcher& matcher,
const UList<StringType>& lst,
const bool invert=false
)
{
return findMatchingStrings(matcher, lst, invert);
}
// subsetting multi-string matches (similar to ListOp):
//- Extract elements of StringList when regular expression matches
// optionally invert the match
// eg, to extract all selected elements:
// subsetMatchingStrings<regExp, stringList>(myRegExp, lst);
template<class Matcher, class StringListType>
StringListType subsetMatchingStrings
(
const Matcher&,
const StringListType&,
const bool invert=false
);
//- Extract elements of StringList when regular expression matches
// Template partial specialization of subsetMatchingStrings
template<class StringListType>
StringListType subsetStrings
(
const regExp& re,
const StringListType& lst,
const bool invert=false
)
{
return subsetMatchingStrings(re, lst, invert);
}
//- Extract elements of StringList when regular expression matches
// Template partial specialization of subsetMatchingStrings
template<class StringListType>
StringListType subsetStrings
(
const char* rePattern,
const StringListType& lst,
const bool invert=false
)
{
return subsetMatchingStrings(regExp(rePattern), lst, invert);
}
//- Extract elements of StringList when regular expression matches
// Template partial specialization of subsetMatchingStrings
template<class StringListType>
StringListType subsetStrings
(
const std::string& rePattern,
const StringListType& lst,
const bool invert=false
)
{
return subsetMatchingStrings(regExp(rePattern), lst, invert);
}
//- Extract elements of StringList when regular expression matches
// Template partial specialization of subsetMatchingStrings
template<class StringListType>
StringListType subsetStrings
(
const wordRe& wre,
const StringListType& lst,
const bool invert=false
)
{
return subsetMatchingStrings(wre, lst, invert);
}
//- Extract elements of StringList when regular expression matches
// Template partial specialization of subsetMatchingStrings
template<class StringListType>
StringListType subsetStrings
(
const wordReListMatcher& matcher,
const StringListType& lst,
const bool invert=false
)
{
return subsetMatchingStrings(matcher, lst, invert);
}
//- Inplace extract elements of StringList when regular expression matches
// optionally invert the match
// eg, to extract all selected elements:
// inplaceSubsetMatchingStrings<regExp, stringList>(myRegExp, lst);
template<class Matcher, class StringListType>
void inplaceSubsetMatchingStrings
(
const Matcher&,
StringListType&,
const bool invert=false
);
//- Inplace extract elements of StringList when regular expression matches
// Template partial specialization of inplaceSubsetMatchingStrings
template<class StringListType>
void inplaceSubsetStrings
(
const regExp& re,
StringListType& lst,
const bool invert=false
)
{
inplaceSubsetMatchingStrings(re, lst, invert);
}
//- Inplace extract elements of StringList when regular expression matches
// Template partial specialization of inplaceSubsetMatchingStrings
template<class StringListType>
void inplaceSubsetStrings
(
const char* rePattern,
StringListType& lst,
const bool invert=false
)
{
inplaceSubsetMatchingStrings(regExp(rePattern), lst, invert);
}
//- Inplace extract elements of StringList when regular expression matches
// Template partial specialization of inplaceSubsetMatchingStrings
template<class StringListType>
void inplaceSubsetStrings
(
const std::string& rePattern,
StringListType& lst,
const bool invert=false
)
{
inplaceSubsetMatchingStrings(regExp(rePattern), lst, invert);
}
//- Inplace extract elements of StringList when regular expression matches
// Template partial specialization of inplaceSubsetMatchingStrings
template<class StringListType>
void inplaceSubsetStrings
(
const wordRe& wre,
StringListType& lst,
const bool invert=false
)
{
inplaceSubsetMatchingStrings(wre, lst, invert);
}
//- Inplace extract elements of StringList when regular expression matches
// Template partial specialization of inplaceSubsetMatchingStrings
template<class StringListType>
void inplaceSubsetStrings
(
const wordReListMatcher& matcher,
StringListType& lst,
const bool invert=false
)
{
inplaceSubsetMatchingStrings(matcher, lst, invert);
}
}

View File

@ -24,126 +24,73 @@ License
\*---------------------------------------------------------------------------*/
#include "labelList.H"
#include "regExp.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class StringType>
Foam::labelList Foam::findStrings
template<class Matcher, class StringType>
Foam::labelList Foam::findMatchingStrings
(
const char* pattern,
const UList<StringType>& lst
const Matcher& matcher,
const UList<StringType>& lst,
const bool invert
)
{
regExp re(pattern);
labelList matched(lst.size());
labelList indices(lst.size());
label matchI = 0;
label nElem = 0;
forAll(lst, elemI)
{
if (re.match(lst[elemI]))
if (matcher.match(lst[elemI]) ? !invert : invert)
{
matched[matchI++] = elemI;
indices[nElem++] = elemI;
}
}
matched.setSize(matchI);
indices.setSize(nElem);
return matched;
return indices;
}
template<class StringType>
Foam::labelList Foam::findStrings
template<class Matcher, class StringListType>
StringListType Foam::subsetMatchingStrings
(
const std::string& pattern,
const UList<StringType>& lst
const Matcher& matcher,
const StringListType& lst,
const bool invert
)
{
regExp re(pattern);
labelList matched(lst.size());
StringListType newLst(lst.size());
label matchI = 0;
label nElem = 0;
forAll(lst, elemI)
{
if (re.match(lst[elemI]))
if (matcher.match(lst[elemI]) ? !invert : invert)
{
matched[matchI++] = elemI;
newLst[nElem++] = lst[elemI];
}
}
matched.setSize(matchI);
newLst.setSize(nElem);
return matched;
return newLst;
}
template<class StringType>
Foam::labelList Foam::findStrings
template<class Matcher, class StringListType>
void Foam::inplaceSubsetMatchingStrings
(
const wordRe& wre,
const UList<StringType>& lst
const Matcher& matcher,
StringListType& lst,
const bool invert
)
{
labelList matched(lst.size());
label matchI = 0;
label nElem = 0;
forAll(lst, elemI)
{
if (wre.match(lst[elemI]))
if (matcher.match(lst[elemI]) ? !invert : invert)
{
matched[matchI++] = elemI;
lst[nElem++] = lst[elemI];
}
}
matched.setSize(matchI);
return matched;
}
template<class StringType>
Foam::labelList Foam::findStrings
(
const UList<wordRe>& wreLst,
const UList<StringType>& lst
)
{
labelList matched(lst.size());
label matchI = 0;
forAll(lst, elemI)
{
forAll(wreLst, reI)
{
if (wreLst[reI].match(lst[elemI]))
{
matched[matchI++] = elemI;
break;
}
}
}
matched.setSize(matchI);
return matched;
}
template<class StringType>
bool Foam::findStrings
(
const UList<wordRe>& wreLst,
const StringType& str
)
{
forAll(wreLst, reI)
{
if (wreLst[reI].match(str))
{
return true;
}
}
return false;
lst.setSize(nElem);
}

View File

@ -0,0 +1,101 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::wordReListMatcher
Description
A wrapper for matching a List of wordRe.
Note
The constructor should remain non-explicit. This allows automatic
conversion from UList\<wordRe\> to wordReListMatcher in search
functions.
SourceFiles
wordReListMatcherI.H
\*---------------------------------------------------------------------------*/
#ifndef wordReListMatcher_H
#define wordReListMatcher_H
#include "wordReList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class wordReListMatcher Declaration
\*---------------------------------------------------------------------------*/
class wordReListMatcher
{
// Private data
//- Reference to underlying list
const UList<wordRe>& reList_;
public:
// Constructors
//- Construct from a List of wordRe
inline wordReListMatcher(const UList<wordRe>&);
// Member Functions
// Access
inline label size() const;
inline bool empty() const;
//- Return underlying list of wordRe
inline const UList<wordRe>& operator()() const;
// Searching
//- Return true if string matches any of the regular expressions
// Smart match as regular expression or as a string.
// Optionally specify a literal match only.
inline bool match(const string&, bool literalMatch=false) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "wordReListMatcherI.H"
#endif
// ************************************************************************* //

View File

@ -0,0 +1,79 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::wordReListMatcher::wordReListMatcher
(
const UList<wordRe>& lst
)
:
reList_(lst)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline Foam::label Foam::wordReListMatcher::size() const
{
return reList_.size();
}
inline bool Foam::wordReListMatcher::empty() const
{
return reList_.empty();
}
inline const Foam::UList<Foam::wordRe>&
Foam::wordReListMatcher::operator()() const
{
return reList_;
}
inline bool Foam::wordReListMatcher::match
(
const string& str,
bool literalMatch
) const
{
const label nElem = reList_.size();
for (label elemI = 0; elemI < nElem; ++elemI)
{
if (reList_[elemI].match(str, literalMatch))
{
return true;
}
}
return false;
}
// ************************************************************************* //

View File

@ -170,7 +170,7 @@ public:
//- Smart match as regular expression or as a string
// Optionally specify a literal match only
inline bool match(const string&, bool literalMatch=false) const;
inline bool match(const std::string&, bool literalMatch=false) const;
//- Miscellaneous

View File

@ -176,12 +176,12 @@ inline void Foam::wordRe::clear()
}
inline bool Foam::wordRe::match(const string& str, bool literalMatch) const
inline bool Foam::wordRe::match(const std::string& str, bool literalMatch) const
{
if (literalMatch || !re_.exists())
{
// check as string
return (*this == str);
return (str == *this);
}
else
{

View File

@ -97,7 +97,7 @@ Foam::Map<Foam::word> Foam::boundaryRegion::names() const
Foam::Map<Foam::word> Foam::boundaryRegion::names
(
const List<wordRe>& patterns
const UList<wordRe>& patterns
) const
{
Map<word> lookup;

View File

@ -112,7 +112,7 @@ public:
Map<word> names() const;
//- Return a Map of (id => names) selected by patterns
Map<word> names(const List<wordRe>& patterns) const;
Map<word> names(const UList<wordRe>& patterns) const;
//- Return a Map of (id => type)
Map<word> boundaryTypes() const;

View File

@ -169,7 +169,7 @@ Foam::Map<Foam::word> Foam::cellTable::names() const
Foam::Map<Foam::word> Foam::cellTable::names
(
const List<wordRe>& patterns
const UList<wordRe>& patterns
) const
{
Map<word> lookup;

View File

@ -139,7 +139,7 @@ public:
Map<word> names() const;
//- Return a Map of (id => names) selected by patterns
Map<word> names(const List<wordRe>& patterns) const;
Map<word> names(const UList<wordRe>& patterns) const;
//- Return a Map of (id => name) for materialType (fluid | solid | shell)
Map<word> selectType(const word& materialType) const;