stringListOpsTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9  Copyright (C) 2017-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
31 template<class UnaryMatchPredicate, class StringType>
32 Foam::label Foam::firstMatchingString
33 (
34  const UnaryMatchPredicate& matcher,
35  const UList<StringType>& input,
36  const bool invert
37 )
38 {
39  const label len = input.size();
40 
41  for (label i=0; i < len; ++i)
42  {
43  if (matcher(input[i]) ? !invert : invert)
44  {
45  return i;
46  }
47  }
48 
49  return -1;
50 }
51 
52 
53 template<class UnaryMatchPredicate, class StringType>
55 (
56  const UnaryMatchPredicate& matcher,
57  const UList<StringType>& input,
58  const bool invert
59 )
60 {
61  const label len = input.size();
62 
63  labelList indices(len);
64 
65  label count = 0;
66  for (label i=0; i < len; ++i)
67  {
68  if (matcher(input[i]) ? !invert : invert)
69  {
70  indices[count] = i;
71  ++count;
72  }
73  }
74  indices.resize(count);
75 
76  return indices;
77 }
78 
79 
80 template<class UnaryMatchPredicate, class StringListType>
81 StringListType Foam::subsetMatchingStrings
82 (
83  const UnaryMatchPredicate& matcher,
84  const StringListType& input,
85  const bool invert
86 )
87 {
88  const label len = input.size();
89 
90  StringListType output(len);
91  output.resize(len); // Consistent sizing (eg, DynamicList)
92 
93  label count = 0;
94  for (label i=0; i < len; ++i)
95  {
96  if (matcher(input[i]) ? !invert : invert)
97  {
98  output[count] = input[i];
99  ++count;
100  }
101  }
102  output.resize(count);
103 
104  return output;
105 }
106 
107 
108 template<class UnaryMatchPredicate, class StringListType>
110 (
111  const UnaryMatchPredicate& matcher,
112  StringListType& input,
113  const bool invert
114 )
115 {
116  const label len = input.size();
117 
118  label count = 0;
119  for (label i=0; i < len; ++i)
120  {
121  if (matcher(input[i]) ? !invert : invert)
122  {
123  if (count != i)
124  {
125  input[count] = std::move(input[i]);
126  }
127  ++count;
128  }
129  }
130  input.resize(count);
131 }
132 
133 
134 template<class StringListType, class AccessOp>
136 (
137  const StringListType& input,
138  const wordRes& allow,
139  const wordRes& deny,
140  AccessOp aop
141 )
142 {
143  const label len = input.size();
144 
145  if (allow.empty() && deny.empty())
146  {
147  return identity(len);
148  }
149 
150  labelList indices(len);
151 
152  label count = 0;
153  for (label i=0; i < len; ++i)
154  {
155  const std::string& text = aop(input[i]);
156 
157  bool accept = false;
158 
159  if (allow.size())
160  {
161  const auto result = allow.matched(text);
162 
163  accept =
164  (
165  result == wordRe::LITERAL
166  ? true
167  : (result == wordRe::REGEX && !deny.match(text))
168  );
169  }
170  else
171  {
172  accept = !deny.match(text);
173  }
174 
175  if (accept)
176  {
177  indices[count] = i;
178  ++count;
179  }
180  }
181  indices.resize(count);
182 
183  return indices;
184 }
185 
186 
187 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Foam::subsetMatchingStrings
StringListType subsetMatchingStrings(const UnaryMatchPredicate &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
Definition: stringListOpsTemplates.C:82
Foam::invert
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36
Foam::stringListOps::findMatching
labelList findMatching(const StringListType &input, const wordRes &allow, const wordRes &deny=wordRes(), AccessOp aop=noOp())
Return ids for items with matching names.
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::inplaceSubsetMatchingStrings
void inplaceSubsetMatchingStrings(const UnaryMatchPredicate &matcher, StringListType &input, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
Definition: stringListOpsTemplates.C:110
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::identity
labelList identity(const label len, label start=0)
Create identity map of the given length with (map[i] == i)
Definition: labelList.C:38
Foam::firstMatchingString
label firstMatchingString(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Find first list item that matches, -1 on failure.
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::findMatchingStrings
labelList findMatchingStrings(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Extract list indices for all matches.