1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
28 Various functions to operate on Lists.
34 \*---------------------------------------------------------------------------*/
39 #include "labelList.H"
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
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&);
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&);
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&);
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&);
68 // Variants to work with iterators and sparse tables.
69 // Need to have iterators and insert()
71 //- Map values. Do not map negative values.
72 template<class Container>
73 void inplaceMapValue(const UList<label>& oldToNew, Container&);
75 //- Recreate with mapped keys. Do not map elements with negative key.
76 template<class Container>
77 void inplaceMapKey(const UList<label>& oldToNew, Container&);
80 //- Generate the (stable) sort order for the list
82 void sortedOrder(const UList<T>&, labelList& order);
84 //- Generate (sorted) indices corresponding to duplicate list values
86 void duplicateOrder(const UList<T>&, labelList& order);
88 //- Generate (sorted) indices corresponding to unique list values
90 void uniqueOrder(const UList<T>&, labelList& order);
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&);
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&);
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&);
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&);
118 //- Invert one-to-one map. Unmapped elements will be -1.
119 labelList invert(const label len, const UList<label>&);
121 //- Invert one-to-many map. Unmapped elements will be size 0.
122 labelListList invertOneToMany(const label len, const UList<label>&);
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>&);
130 template<class InList, class OutList>
131 List<OutList> invertManyToMany(const label len, const UList<InList>& in)
134 invertManyToMany<InList,OutList>(len, in, out);
138 //- Create identity map (map[i] == i) of given length
139 labelList identity(const label len);
141 //- Find first occurence of given element and return index,
142 // return -1 if not found. Linear search.
143 template<class ListType>
147 typename ListType::const_reference,
151 //- Find all occurences of given element. Linear search.
152 template<class ListType>
153 labelList findIndices
156 typename ListType::const_reference,
160 //- Opposite of findIndices: set values at indices to given value
161 template<class ListType>
165 const UList<label>& indices,
166 typename ListType::const_reference
169 //- Opposite of findIndices: set values at indices to given value
170 template<class ListType>
171 ListType createWithValues
174 typename ListType::const_reference initValue,
175 const UList<label>& indices,
176 typename ListType::const_reference setValue
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);
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);
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
197 typename ListType::const_reference,
202 //- Find last element < given value in sorted list and return index,
203 // return -1 if not found. Binary search.
204 template<class ListType>
208 typename ListType::const_reference,
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]);
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]);
225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227 } // End namespace Foam
229 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
232 # include "ListOpsTemplates.C"
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 // ************************************************************************* //