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>
33 (
34  const UnaryMatchPredicate& matcher,
35  const UList<StringType>& input,
36  const bool invert
37 )
38 {
39  const label len = input.size();
40 
41  labelList indices(len);
42 
43  label count = 0;
44  for (label i=0; i < len; ++i)
45  {
46  if (matcher(input[i]) ? !invert : invert)
47  {
48  indices[count] = i;
49  ++count;
50  }
51  }
52  indices.resize(count);
53 
54  return indices;
55 }
56 
57 
58 template<class UnaryMatchPredicate, class StringListType>
59 StringListType Foam::subsetMatchingStrings
60 (
61  const UnaryMatchPredicate& matcher,
62  const StringListType& input,
63  const bool invert
64 )
65 {
66  const label len = input.size();
67 
68  StringListType output(len);
69  output.resize(len); // Consistent sizing (eg, DynamicList)
70 
71  label count = 0;
72  for (label i=0; i < len; ++i)
73  {
74  if (matcher(input[i]) ? !invert : invert)
75  {
76  output[count] = input[i];
77  ++count;
78  }
79  }
80  output.resize(count);
81 
82  return output;
83 }
84 
85 
86 template<class UnaryMatchPredicate, class StringListType>
88 (
89  const UnaryMatchPredicate& matcher,
90  StringListType& input,
91  const bool invert
92 )
93 {
94  const label len = input.size();
95 
96  label count = 0;
97  for (label i=0; i < len; ++i)
98  {
99  if (matcher(input[i]) ? !invert : invert)
100  {
101  if (count != i)
102  {
103  input[count] = std::move(input[i]);
104  }
105  ++count;
106  }
107  }
108  input.resize(count);
109 }
110 
111 
112 template<class StringListType, class AccessOp>
114 (
115  const StringListType& input,
116  const wordRes& allow,
117  const wordRes& deny,
118  AccessOp aop
119 )
120 {
121  const label len = input.size();
122 
123  if (allow.empty() && deny.empty())
124  {
125  return identity(len);
126  }
127 
128  labelList indices(len);
129 
130  label count = 0;
131  for (label i=0; i < len; ++i)
132  {
133  const std::string& text = aop(input[i]);
134 
135  bool accept = false;
136 
137  if (allow.size())
138  {
139  const auto result = allow.matched(text);
140 
141  accept =
142  (
143  result == wordRe::LITERAL
144  ? true
145  : (result == wordRe::REGEX && !deny.match(text))
146  );
147  }
148  else
149  {
150  accept = !deny.match(text);
151  }
152 
153  if (accept)
154  {
155  indices[count] = i;
156  ++count;
157  }
158  }
159  indices.resize(count);
160 
161  return indices;
162 }
163 
164 
165 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
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:60
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:88
Foam::List< label >
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::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.