FreeFOAM The Cross-Platform CFD Toolkit
ListOps.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
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 InNamspace
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 <OpenFOAM/labelList.H>
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 //- Renumber the values (not the indices) of a list.
47 // Negative ListType elements are left as is.
48 template<class ListType>
49 ListType renumber(const UList<label>& oldToNew, const ListType&);
50 
51 //- Inplace renumber the values of a list.
52 // Negative ListType elements are left as is.
53 template<class ListType>
54 void inplaceRenumber(const UList<label>& oldToNew, ListType&);
55 
56 
57 //- Reorder the elements (indices, not values) of a list.
58 // Negative ListType elements are left as is.
59 template<class ListType>
60 ListType reorder(const UList<label>& oldToNew, const ListType&);
61 
62 //- Inplace reorder the elements of a list.
63 // Negative ListType elements are left as is.
64 template<class ListType>
65 void inplaceReorder(const UList<label>& oldToNew, ListType&);
66 
67 
68 // Variants to work with iterators and sparse tables.
69 // Need to have iterators and insert()
70 
71 //- Map values. Do not map negative values.
72 template<class Container>
73 void inplaceMapValue(const UList<label>& oldToNew, Container&);
74 
75 //- Recreate with mapped keys. Do not map elements with negative key.
76 template<class Container>
77 void inplaceMapKey(const UList<label>& oldToNew, Container&);
78 
79 
80 //- Generate the (stable) sort order for the list
81 template<class T>
82 void sortedOrder(const UList<T>&, labelList& order);
83 
84 //- Generate (sorted) indices corresponding to duplicate list values
85 template<class T>
86 void duplicateOrder(const UList<T>&, labelList& order);
87 
88 //- Generate (sorted) indices corresponding to unique list values
89 template<class T>
90 void uniqueOrder(const UList<T>&, labelList& order);
91 
92 //- Extract elements of List when select is a certain value.
93 // eg, to extract all selected elements:
94 // subset<bool, labelList>(selectedElems, true, lst);
95 template<class T, class ListType>
96 ListType subset(const UList<T>& select, const T& value, const ListType&);
97 
98 //- Inplace extract elements of List when select is a certain value.
99 // eg, to extract all selected elements:
100 // inplaceSubset<bool, labelList>(selectedElems, true, lst);
101 template<class T, class ListType>
102 void inplaceSubset(const UList<T>& select, const T& value, ListType&);
103 
104 //- Extract elements of List when select is true
105 // eg, to extract all selected elements:
106 // subset<boolList, labelList>(selectedElems, lst);
107 // Note a labelHashSet could also be used for the bool-list
108 template<class BoolListType, class ListType>
109 ListType subset(const BoolListType& select, const ListType&);
110 
111 //- Inplace extract elements of List when select is true
112 // eg, to extract all selected elements:
113 // inplaceSubset<boolList, labelList>(selectedElems, lst);
114 // Note a labelHashSet could also be used for the bool-list
115 template<class BoolListType, class ListType>
116 void inplaceSubset(const BoolListType& select, ListType&);
117 
118 //- Invert one-to-one map. Unmapped elements will be -1.
119 labelList invert(const label len, const UList<label>&);
120 
121 //- Invert one-to-many map. Unmapped elements will be size 0.
122 labelListList invertOneToMany(const label len, const UList<label>&);
123 
124 //- Invert many-to-many.
125 // Input and output types need to be inherited from List.
126 // eg, faces to pointFaces.
127 template<class InList, class OutList>
128 void invertManyToMany(const label len, const UList<InList>&, List<OutList>&);
129 
130 template<class InList, class OutList>
131 List<OutList> invertManyToMany(const label len, const UList<InList>& in)
132 {
133  List<OutList> out;
134  invertManyToMany<InList,OutList>(len, in, out);
135  return out;
136 }
137 
138 //- Create identity map (map[i] == i) of given length
139 labelList identity(const label len);
140 
141 //- Find first occurence of given element and return index,
142 // return -1 if not found. Linear search.
143 template<class ListType>
144 label findIndex
145 (
146  const ListType&,
147  typename ListType::const_reference,
148  const label start=0
149 );
150 
151 //- Find all occurences of given element. Linear search.
152 template<class ListType>
154 (
155  const ListType&,
156  typename ListType::const_reference,
157  const label start=0
158 );
159 
160 //- Opposite of findIndices: set values at indices to given value
161 template<class ListType>
162 void setValues
163 (
164  ListType&,
165  const UList<label>& indices,
166  typename ListType::const_reference
167 );
168 
169 //- Opposite of findIndices: set values at indices to given value
170 template<class ListType>
171 ListType createWithValues
172 (
173  const label sz,
174  typename ListType::const_reference initValue,
175  const UList<label>& indices,
176  typename ListType::const_reference setValue
177 );
178 
179 //- Find index of max element (and larger than given element).
180 // return -1 if not found. Linear search.
181 template<class ListType>
182 label findMax(const ListType&, const label start=0);
183 
184 
185 //- Find index of min element (and less than given element).
186 // return -1 if not found. Linear search.
187 template<class ListType>
188 label findMin(const ListType&, const label start=0);
189 
190 
191 //- Find first occurence of given element in sorted list and return index,
192 // return -1 if not found. Binary search.
193 template<class ListType>
194 label findSortedIndex
195 (
196  const ListType&,
197  typename ListType::const_reference,
198  const label start=0
199 );
200 
201 
202 //- Find last element < given value in sorted list and return index,
203 // return -1 if not found. Binary search.
204 template<class ListType>
205 label findLower
206 (
207  const ListType&,
208  typename ListType::const_reference,
209  const label start=0
210 );
211 
212 
213 //- To construct a List from a C array. Has extra Container type
214 // to initialise e.g. wordList from arrays of char*.
215 template<class Container, class T, int nRows>
216 List<Container> initList(const T[nRows]);
217 
218 
219 //- To construct a (square) ListList from a C array. Has extra Container type
220 // to initialise e.g. faceList from arrays of labels.
221 template<class Container, class T, int nRows, int nColumns>
222 List<Container> initListList(const T[nRows][nColumns]);
223 
224 
225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226 
227 } // End namespace Foam
228 
229 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
230 
231 #ifdef NoRepository
232 # include "ListOpsTemplates.C"
233 #endif
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 #endif
238 
239 // ************************ vim: set sw=4 sts=4 et: ************************ //
240