openfoam/src/OpenFOAM/primitives/strings/lists/stringListOpsTemplates.C
Mark Olesen a8d2ebf298 ENH: cleanup wordRe interfaces etc.
- ensure that the string-related classes have consistently similar
  matching methods. Use operator()(const std::string) as an entry
  point for the match() method, which makes it easier to use for
  filters and predicates. In some cases this will also permit using
  a HashSet as a match predicate.

regExp
====
- the set method now returns a bool to signal that the requested
  pattern was compiled.

wordRe
====
- have separate constructors with the compilation option (was previously
  a default parameter). This leaves the single parameter constructor
  explicit, but the two parameter version is now non-explicit, which
  makes it easier to use when building lists.

- renamed compile-option from REGEX (to REGEXP) for consistency with
  with the <regex.h>, <regex> header names etc.

wordRes
====
- renamed from wordReListMatcher -> wordRes. For reduced typing and
  since it behaves as an entity only slightly related to its underlying
  list nature.

- Provide old name as typedef and include for code transition.

- pass through some list methods into wordRes

hashedWordList
====
- hashedWordList[const word& name] now returns a -1 if the name is is
  not found in the list of indices. That has been a pending change
  ever since hashedWordList was generalized out of speciesTable
  (Oct-2010).

- add operator()(const word& name) for easy use as a predicate

STYLE: adjust parameter names in stringListOps

- reflect if the parameter is being used as a primary matcher, or the
  matcher will be derived from the parameter.
  For example,
      (const char* re), which first creates a regExp
      versus (const regExp& matcher) which is used directly.
2017-05-16 23:54:43 +02:00

105 lines
2.8 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
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 3 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, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class UnaryMatchPredicate, class StringType>
Foam::labelList Foam::findMatchingStrings
(
const UnaryMatchPredicate& matcher,
const UList<StringType>& lst,
const bool invert
)
{
labelList indices(lst.size());
label count = 0;
forAll(lst, elemi)
{
if (matcher(lst[elemi]) ? !invert : invert)
{
indices[count++] = elemi;
}
}
indices.setSize(count);
return indices;
}
template<class UnaryMatchPredicate, class StringListType>
StringListType Foam::subsetMatchingStrings
(
const UnaryMatchPredicate& matcher,
const StringListType& lst,
const bool invert
)
{
// Create as a copy
StringListType newLst(lst.size());
// Ensure consistent addressable size (eg, DynamicList)
newLst.setSize(lst.size());
label count = 0;
forAll(lst, elemi)
{
if (matcher(lst[elemi]) ? !invert : invert)
{
newLst[count++] = lst[elemi];
}
}
newLst.setSize(count);
return newLst;
}
template<class UnaryMatchPredicate, class StringListType>
void Foam::inplaceSubsetMatchingStrings
(
const UnaryMatchPredicate& matcher,
StringListType& lst,
const bool invert
)
{
label count = 0;
forAll(lst, elemi)
{
if (matcher(lst[elemi]) ? !invert : invert)
{
if (count != elemi)
{
lst[count] = lst[elemi];
}
++count;
}
}
lst.setSize(count);
}
// ************************************************************************* //