stringListOpsTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
27 
28 template<class Matcher, class StringType>
30 (
31  const Matcher& matcher,
32  const UList<StringType>& lst,
33  const bool invert
34 )
35 {
36  labelList indices(lst.size());
37 
38  label nElem = 0;
39  forAll(lst, elemI)
40  {
41  if (matcher.match(lst[elemI]) ? !invert : invert)
42  {
43  indices[nElem++] = elemI;
44  }
45  }
46  indices.setSize(nElem);
47 
48  return indices;
49 }
50 
51 
52 template<class Matcher, class StringListType>
53 StringListType Foam::subsetMatchingStrings
54 (
55  const Matcher& matcher,
56  const StringListType& lst,
57  const bool invert
58 )
59 {
60  // Create copy
61  StringListType newLst(lst.size());
62 
63  // ensure consistent addressable size (eg, DynamicList)
64  newLst.setSize(lst.size());
65 
66  label nElem = 0;
67  forAll(lst, elemI)
68  {
69  if (matcher.match(lst[elemI]) ? !invert : invert)
70  {
71  newLst[nElem++] = lst[elemI];
72  }
73  }
74  newLst.setSize(nElem);
75 
76  return newLst;
77 }
78 
79 
80 template<class Matcher, class StringListType>
82 (
83  const Matcher& matcher,
84  StringListType& lst,
85  const bool invert
86 )
87 {
88  label nElem = 0;
89  forAll(lst, elemI)
90  {
91  if (matcher.match(lst[elemI]) ? !invert : invert)
92  {
93  lst[nElem++] = lst[elemI];
94  }
95  }
96  lst.setSize(nElem);
97 }
98 
99 
100 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
StringListType subsetMatchingStrings(const Matcher &, const StringListType &, const bool invert=false)
Extract elements of StringList when regular expression matches.
labelList findMatchingStrings(const Matcher &, const UList< StringType > &, const bool invert=false)
Return list indices for matching strings.
labelList invert(const label len, const labelUList &)
Invert one-to-one map. Unmapped elements will be -1.
Definition: ListOps.C:37
List< label > labelList
A List of labels.
Definition: labelList.H:56
void inplaceSubsetMatchingStrings(const Matcher &, StringListType &, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.