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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
31template<class UnaryMatchPredicate, class StringType>
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
53template<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
80template<class UnaryMatchPredicate, class StringListType>
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
108template<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
134template<class StringListType, class AccessOp>
136(
137 const StringListType& input,
138 const wordRes::filter& pred,
139 AccessOp aop
140)
141{
142 const label len = input.size();
143
144 if (pred.empty())
145 {
146 // Accept all
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 if (pred(text))
158 {
159 indices[count] = i;
160 ++count;
161 }
162 }
163 indices.resize(count);
164
165 return indices;
166}
167
168
169template<class StringListType, class AccessOp>
171(
172 const StringListType& input,
173 const wordRes& allow,
174 const wordRes& deny,
175 AccessOp aop
176)
177{
178 if (allow.empty() && deny.empty())
179 {
180 // Accept all
181 return identity(input.size());
182 }
183
184 // Use combined accept/reject filter
185 const wordRes::filter pred(allow, deny);
186
187 return stringListOps::findMatching(input, pred, aop);
188}
189
190
191// ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:78
labelList findMatching(const StringListType &input, const wordRes::filter &pred, AccessOp aop=identityOp())
Return ids for items with matching names.
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
Definition: labelList.C:38
List< label > labelList
A List of labels.
Definition: List.H:66
label firstMatchingString(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Find first list item that matches, -1 on failure.
labelList findMatchingStrings(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Extract list indices for all matches.
void inplaceSubsetMatchingStrings(const UnaryMatchPredicate &matcher, StringListType &input, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
StringListType subsetMatchingStrings(const UnaryMatchPredicate &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36