ListOps.H
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-2022 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 InNamespace
25  Foam
26 
27 Description
28  Various functions to operate on Lists.
29 
30 SourceFiles
31  ListOps.C
32  ListOpsTemplates.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef ListOps_H
37 #define ListOps_H
38 
39 #include "labelList.H"
40 #include "ops.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 extern const labelList emptyLabelList;
48 
49 //- Return reference to zero-sized list. Compare to List::null() which returns
50 // null pointer cast as list reference.
51 template<class Type>
52 static const List<Type>& emptyList()
53 {
54  return *reinterpret_cast<const List<Type>*>(&emptyLabelList);
55 }
56 
57 //- Renumber the values (not the indices) of a list.
58 // Negative ListType elements are left as is.
59 template<class ListType>
60 ListType renumber(const labelUList& oldToNew, const ListType&);
61 
62 //- As above, but given a pre-constructed result list
63 template<class ListType>
64 void renumber(const labelUList& oldToNew, const ListType&, ListType&);
65 
66 //- As above, but for a single label. Termination condition.
67 inline void renumber(const labelUList& oldToNew, const label, label&);
68 
69 //- Inplace renumber the values of a list.
70 // Negative ListType elements are left as is.
71 template<class ListType>
72 void inplaceRenumber(const labelUList& oldToNew, ListType&);
73 
74 //- As above, but for a single label. Termination condition.
75 inline void inplaceRenumber(const labelUList& oldToNew, label&);
76 
77 //- Reorder the elements (indices, not values) of a list.
78 // Negative ListType elements are left as is.
79 template<class ListType>
80 ListType reorder(const labelUList& oldToNew, const ListType&);
81 
82 //- Inplace reorder the elements of a list.
83 // Negative ListType elements are left as is.
84 template<class ListType>
85 void inplaceReorder(const labelUList& oldToNew, ListType&);
86 
87 //- Map values. Do not map negative values.
88 template<class Container>
89 void inplaceMapValue(const labelUList& oldToNew, Container&);
90 
91 //- Recreate with mapped keys. Do not map elements with negative key.
92 template<class Container>
93 void inplaceMapKey(const labelUList& oldToNew, Container&);
94 
95 //- Generate the (stable) sort order for the list
96 template<class T>
97 void sortedOrder(const UList<T>&, labelList& order);
98 
99 //- Generate the (stable) sort order for the list, for a given comparison
100 // operator
101 template<class T, class Cmp>
102 void sortedOrder(const UList<T>&, labelList& order, const Cmp& cmp);
103 
104 //- Generate (sorted) indices corresponding to duplicate list values
105 template<class T>
106 void duplicateOrder(const UList<T>&, labelList& order);
107 
108 //- Generate (sorted) indices corresponding to duplicate list values, for a
109 // given comparison operator
110 template<class T, class Cmp>
111 void duplicateOrder(const UList<T>&, labelList& order, const Cmp& cmp);
112 
113 //- Generate (sorted) indices corresponding to unique list values
114 template<class T>
115 void uniqueOrder(const UList<T>&, labelList& order);
116 
117 //- Generate (sorted) indices corresponding to unique list values, for a given
118 // comparison operator
119 template<class T, class Cmp>
120 void uniqueOrder(const UList<T>&, labelList& order, const Cmp& cmp);
121 
122 //- Extract elements of List when select is a certain value.
123 // eg, to extract all selected elements:
124 // subset<bool, labelList>(selectedElems, true, lst);
125 template<class T, class ListType>
126 ListType subset(const UList<T>& select, const T& value, const ListType&);
127 
128 //- Inplace extract elements of List when select is a certain value.
129 // eg, to extract all selected elements:
130 // inplaceSubset<bool, labelList>(selectedElems, true, lst);
131 template<class T, class ListType>
132 void inplaceSubset(const UList<T>& select, const T& value, ListType&);
133 
134 //- Extract elements of List when select is true
135 // eg, to extract all selected elements:
136 // subset<boolList, labelList>(selectedElems, lst);
137 // Note a labelHashSet could also be used for the bool-list
138 template<class BoolListType, class ListType>
139 ListType subset(const BoolListType& select, const ListType&);
140 
141 //- Inplace extract elements of List when select is true
142 // eg, to extract all selected elements:
143 // inplaceSubset<boolList, labelList>(selectedElems, lst);
144 // Note a labelHashSet could also be used for the bool-list
145 template<class BoolListType, class ListType>
146 void inplaceSubset(const BoolListType& select, ListType&);
147 
148 //- Invert one-to-one map. Unmapped elements will be -1.
149 labelList invert(const label len, const labelUList&);
150 
151 //- Invert one-to-many map. Unmapped elements will be size 0.
152 labelListList invertOneToMany(const label len, const labelUList&);
153 
154 //- Invert many-to-many.
155 // Input and output types need to be inherited from List.
156 // eg, faces to pointFaces.
157 template<class InList, class OutList>
158 void invertManyToMany(const label len, const UList<InList>&, List<OutList>&);
159 
160 //- Invert many-to-many.
161 template<class InList, class OutList>
163 {
164  List<OutList> out;
165  invertManyToMany<InList,OutList>(len, in, out);
166  return out;
167 }
168 
169 //- Create identity map (map[i] == i) of given length
170 labelList identity(const label len);
171 
172 //- Count the number of occurrences of a value in a list
173 template<class ListType>
174 label count(const ListType& l, typename ListType::const_reference x);
175 
176 //- Find first occurrence of given element and return index,
177 // return -1 if not found. Linear search.
178 template<class ListType>
180 (
181  const ListType&,
182  typename ListType::const_reference,
183  const label start=0
184 );
185 
186 //- Find all occurrences of given element. Linear search.
187 template<class ListType>
189 (
190  const ListType&,
191  typename ListType::const_reference,
192  const label start=0
193 );
194 
195 //- Opposite of findIndices: set values at indices to given value
196 template<class ListType>
197 void setValues
198 (
199  ListType&,
200  const labelUList& indices,
201  typename ListType::const_reference
202 );
203 
204 //- Opposite of findIndices: set values at indices to given value
205 template<class ListType>
206 ListType createWithValues
207 (
208  const label sz,
209  typename ListType::const_reference initValue,
210  const labelUList& indices,
211  typename ListType::const_reference setValue
212 );
213 
214 //- Find index of max element (and larger than given element).
215 // return -1 if not found. Linear search.
216 template<class ListType>
217 label findMax(const ListType&, const label start=0);
218 
219 //- Find index of min element (and less than given element).
220 // return -1 if not found. Linear search.
221 template<class ListType>
222 label findMin(const ListType&, const label start=0);
223 
224 //- Find first occurrence of given element in sorted list and return index,
225 // return -1 if not found. Binary search.
226 template<class ListType>
228 (
229  const ListType&,
230  typename ListType::const_reference,
231  const label start=0
232 );
233 
234 //- Find last element < given value in sorted list and return index,
235 // return -1 if not found. Binary search.
236 template<class ListType, class BinaryOp>
238 (
239  const ListType&,
240  typename ListType::const_reference,
241  const label stary,
242  const BinaryOp& bop
243 );
244 
245 //- Find last element < given value in sorted list and return index,
246 // return -1 if not found. Binary search.
247 template<class ListType>
249 (
250  const ListType&,
251  typename ListType::const_reference,
252  const label start=0
253 );
254 
255 //- To construct a List from a C array. Has extra Container type
256 // to initialise e.g. wordList from arrays of char*.
257 template<class Container, class T, int mRows>
258 List<Container> initList(const T[mRows]);
259 
260 //- To construct a (square) ListList from a C array. Has extra Container type
261 // to initialise e.g. faceList from arrays of labels.
262 template<class Container, class T, int mRows, int nColumns>
263 List<Container> initListList(const T[mRows][nColumns]);
264 
265 //- Reverse a list. First element becomes last element etc.
266 template<class ListType>
267 ListType reverseList(const ListType& list);
268 
269 //- Inplace reversal of a list using Swap.
270 template<class ListType>
271 void inplaceReverseList(ListType& list);
272 
273 //- Rotate a list by n places. If n is positive rotate clockwise/right/down.
274 // If n is negative rotate anti-clockwise/left/up.
275 template<class ListType>
276 ListType rotateList(const ListType& list, const label n);
277 
278 //- Inplace reversal of a list using the Reversal Block Swapping algorithm.
279 template<template<typename> class ListType, class DataType>
280 void inplaceRotateList(ListType<DataType>& list, label n);
281 
282 //- Operator to apply a binary operation to a pair of lists
283 template<class BinaryOp>
284 struct ListOp;
285 
286 //- Operator to apply a binary operation to a pair of lists
287 template<class Type, template<class> class BinaryOp>
288 struct ListOp<BinaryOp<Type>>
289 {
290  List<Type> operator()(const List<Type>& a, const List<Type>& b) const;
291 };
292 
293 //- Operator to apply a binary-equals operation to a pair of lists
294 template<class BinaryOp>
295 struct ListEqOp;
296 
297 //- Operator to apply a binary-equals operation to a pair of lists
298 template<class Type, template<class> class BinaryEqOp>
299 struct ListEqOp<BinaryEqOp<Type>>
300 {
301  void operator()(List<Type>& a, const List<Type>& b) const;
302 };
303 
304 //- List operator to append one list onto another
305 template<class T>
307 {
308  void operator()(List<T>& x, const List<T>& y) const;
309 };
310 
311 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
312 
313 } // End namespace Foam
314 
315 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
316 
317 #ifdef NoRepository
318  #include "ListOpsTemplates.C"
319 #endif
320 
321 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
322 
323 #endif
324 
325 // ************************************************************************* //
void inplaceSubset(const UList< T > &select, const T &value, ListType &)
Inplace extract elements of List when select is a certain value.
void inplaceMapKey(const labelUList &oldToNew, Container &)
Recreate with mapped keys. Do not map elements with negative key.
ListType renumber(const labelUList &oldToNew, const ListType &)
Renumber the values (not the indices) of a list.
void duplicateOrder(const UList< T > &, labelList &order)
Generate (sorted) indices corresponding to duplicate list values.
void inplaceReorder(const labelUList &oldToNew, ListType &)
Inplace reorder the elements of a list.
label findSortedIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element in sorted list and return index,.
void sortedOrder(const UList< T > &, labelList &order)
Generate the (stable) sort order for the list.
void inplaceRenumber(const labelUList &oldToNew, ListType &)
Inplace renumber the values of a list.
ListType rotateList(const ListType &list, const label n)
Rotate a list by n places. If n is positive rotate clockwise/right/down.
labelListList invertOneToMany(const label len, const labelUList &)
Invert one-to-many map. Unmapped elements will be size 0.
Definition: ListOps.C:67
Operator to apply a binary-equals operation to a pair of lists.
Definition: ListOps.H:295
const dimensionedScalar b
Wien displacement law constant: default SI units: [m K].
Definition: createFields.H:27
void uniqueOrder(const UList< T > &, labelList &order)
Generate (sorted) indices corresponding to unique list values.
Combination-Reduction operation for a parallel run.
List< Container > initList(const T[mRows])
To construct a List from a C array. Has extra Container type.
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
Holds information (coordinate and normal) regarding nearest wall point.
void setValues(ListType &, const labelUList &indices, typename ListType::const_reference)
Opposite of findIndices: set values at indices to given value.
labelList identity(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
label findMin(const ListType &, const label start=0)
Find index of min element (and less than given element).
labelList findIndices(const ListType &, typename ListType::const_reference, const label start=0)
Find all occurrences of given element. Linear search.
ListType createWithValues(const label sz, typename ListType::const_reference initValue, const labelUList &indices, typename ListType::const_reference setValue)
Opposite of findIndices: set values at indices to given value.
void inplaceReverseList(ListType &list)
Inplace reversal of a list using Swap.
scalar y
labelList invert(const label len, const labelUList &)
Invert one-to-one map. Unmapped elements will be -1.
Definition: ListOps.C:37
static const List< Type > & emptyList()
Return reference to zero-sized list. Compare to List::null() which returns.
Definition: ListOps.H:52
List< label > labelList
A List of labels.
Definition: labelList.H:56
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
ListType reverseList(const ListType &list)
Reverse a list. First element becomes last element etc.
label findMax(const ListType &, const label start=0)
Find index of max element (and larger than given element).
ListType reorder(const labelUList &oldToNew, const ListType &)
Reorder the elements (indices, not values) of a list.
List< Container > initListList(const T[mRows][nColumns])
To construct a (square) ListList from a C array. Has extra Container type.
void inplaceMapValue(const labelUList &oldToNew, Container &)
Map values. Do not map negative values.
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const labelList emptyLabelList
Definition: ListOps.C:31
void inplaceRotateList(ListType< DataType > &list, label n)
Inplace reversal of a list using the Reversal Block Swapping algorithm.
Operator to apply a binary operation to a pair of lists.
Definition: ListOps.H:284
void invertManyToMany(const label len, const UList< InList > &, List< OutList > &)
Invert many-to-many.
ListType subset(const UList< T > &select, const T &value, const ListType &)
Extract elements of List when select is a certain value.
label findLower(const ListType &, typename ListType::const_reference, const label stary, const BinaryOp &bop)
Find last element < given value in sorted list and return index,.
label n
List operator to append one list onto another.
Definition: ListOps.H:306
Namespace for OpenFOAM.