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-2021 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 
78 //- Reorder the elements (indices, not values) of a list.
79 // Negative ListType elements are left as is.
80 template<class ListType>
81 ListType reorder(const labelUList& oldToNew, const ListType&);
82 
83 //- Inplace reorder the elements of a list.
84 // Negative ListType elements are left as is.
85 template<class ListType>
86 void inplaceReorder(const labelUList& oldToNew, ListType&);
87 
88 
89 // Variants to work with iterators and sparse tables.
90 // Need to have iterators and insert()
91 
92 //- Map values. Do not map negative values.
93 template<class Container>
94 void inplaceMapValue(const labelUList& oldToNew, Container&);
95 
96 //- Recreate with mapped keys. Do not map elements with negative key.
97 template<class Container>
98 void inplaceMapKey(const labelUList& oldToNew, Container&);
99 
100 
101 //- Generate the (stable) sort order for the list
102 template<class T>
103 void sortedOrder(const UList<T>&, labelList& order);
104 
105 template<class T, class Cmp>
106 void sortedOrder(const UList<T>&, labelList& order, const Cmp& cmp);
107 
108 //- Generate (sorted) indices corresponding to duplicate list values
109 template<class T>
110 void duplicateOrder(const UList<T>&, labelList& order);
111 
112 template<class T, class Cmp>
113 void duplicateOrder(const UList<T>&, labelList& order, const Cmp& cmp);
114 
115 //- Generate (sorted) indices corresponding to unique list values
116 template<class T>
117 void uniqueOrder(const UList<T>&, labelList& order);
118 
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 template<class InList, class OutList>
162 {
163  List<OutList> out;
164  invertManyToMany<InList,OutList>(len, in, out);
165  return out;
166 }
167 
168 //- Create identity map (map[i] == i) of given length
169 labelList identity(const label len);
170 
171 //- Count the number of occurrences of a value in a list
172 template<class ListType>
173 label count(const ListType& l, typename ListType::const_reference x);
174 
175 //- Find first occurrence of given element and return index,
176 // return -1 if not found. Linear search.
177 template<class ListType>
179 (
180  const ListType&,
181  typename ListType::const_reference,
182  const label start=0
183 );
184 
185 //- Find all occurrences of given element. Linear search.
186 template<class ListType>
188 (
189  const ListType&,
190  typename ListType::const_reference,
191  const label start=0
192 );
193 
194 //- Opposite of findIndices: set values at indices to given value
195 template<class ListType>
196 void setValues
197 (
198  ListType&,
199  const labelUList& indices,
200  typename ListType::const_reference
201 );
202 
203 //- Opposite of findIndices: set values at indices to given value
204 template<class ListType>
205 ListType createWithValues
206 (
207  const label sz,
208  typename ListType::const_reference initValue,
209  const labelUList& indices,
210  typename ListType::const_reference setValue
211 );
212 
213 //- Find index of max element (and larger than given element).
214 // return -1 if not found. Linear search.
215 template<class ListType>
216 label findMax(const ListType&, const label start=0);
217 
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 
225 //- Find first occurrence of given element in sorted list and return index,
226 // return -1 if not found. Binary search.
227 template<class ListType>
229 (
230  const ListType&,
231  typename ListType::const_reference,
232  const label start=0
233 );
234 
235 
236 //- Find last element < given value in sorted list and return index,
237 // return -1 if not found. Binary search.
238 template<class ListType, class BinaryOp>
240 (
241  const ListType&,
242  typename ListType::const_reference,
243  const label stary,
244  const BinaryOp& bop
245 );
246 
247 
248 //- Find last element < given value in sorted list and return index,
249 // return -1 if not found. Binary search.
250 template<class ListType>
252 (
253  const ListType&,
254  typename ListType::const_reference,
255  const label start=0
256 );
257 
258 
259 //- To construct a List from a C array. Has extra Container type
260 // to initialise e.g. wordList from arrays of char*.
261 template<class Container, class T, int mRows>
262 List<Container> initList(const T[mRows]);
263 
264 
265 //- To construct a (square) ListList from a C array. Has extra Container type
266 // to initialise e.g. faceList from arrays of labels.
267 template<class Container, class T, int mRows, int nColumns>
268 List<Container> initListList(const T[mRows][nColumns]);
269 
270 
271 //- Helper class for list to append y onto the end of x
272 template<class T>
274 {
275 public:
276  void operator()(List<T>& x, const List<T>& y) const;
277 };
278 
279 
280 //- Reverse a list. First element becomes last element etc.
281 template<class ListType>
282 ListType reverseList(const ListType& list);
283 
284 
285 //- Inplace reversal of a list using Swap.
286 template<class ListType>
287 void inplaceReverseList(ListType& list);
288 
289 
290 //- Rotate a list by n places. If n is positive rotate clockwise/right/down.
291 // If n is negative rotate anti-clockwise/left/up.
292 template<class ListType>
293 ListType rotateList(const ListType& list, const label n);
294 
295 
296 //- Inplace reversal of a list using the Reversal Block Swapping algorithm.
297 template<template<typename> class ListType, class DataType>
298 void inplaceRotateList(ListType<DataType>& list, label n);
299 
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 } // End namespace Foam
304 
305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306 
307 #ifdef NoRepository
308  #include "ListOpsTemplates.C"
309 #endif
310 
311 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
312 
313 #endif
314 
315 // ************************************************************************* //
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,.
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
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
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.
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
void operator()(List< T > &x, const List< T > &y) const
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.
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
Helper class for list to append y onto the end of x.
Definition: ListOps.H:273
Namespace for OpenFOAM.