1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
28 Various functions to operate on Lists.
34 \*---------------------------------------------------------------------------*/
39 #include "labelList.H"
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 extern const labelList emptyLabelList;
48 //- Return reference to zero-sized list. Compare to List::null() which returns
49 // null pointer cast as list reference.
51 static const List<Type>& emptyList()
53 return *reinterpret_cast<const List<Type>* >(&emptyLabelList);
56 //- Renumber the values (not the indices) of a list.
57 // Negative ListType elements are left as is.
58 template<class ListType>
59 ListType renumber(const labelUList& oldToNew, const ListType&);
61 //- Inplace renumber the values of a list.
62 // Negative ListType elements are left as is.
63 template<class ListType>
64 void inplaceRenumber(const labelUList& oldToNew, ListType&);
67 //- Reorder the elements (indices, not values) of a list.
68 // Negative ListType elements are left as is.
69 template<class ListType>
70 ListType reorder(const labelUList& oldToNew, const ListType&);
72 //- Inplace reorder the elements of a list.
73 // Negative ListType elements are left as is.
74 template<class ListType>
75 void inplaceReorder(const labelUList& oldToNew, ListType&);
78 // Variants to work with iterators and sparse tables.
79 // Need to have iterators and insert()
81 //- Map values. Do not map negative values.
82 template<class Container>
83 void inplaceMapValue(const labelUList& oldToNew, Container&);
85 //- Recreate with mapped keys. Do not map elements with negative key.
86 template<class Container>
87 void inplaceMapKey(const labelUList& oldToNew, Container&);
90 //- Generate the (stable) sort order for the list
92 void sortedOrder(const UList<T>&, labelList& order);
94 //- Generate (sorted) indices corresponding to duplicate list values
96 void duplicateOrder(const UList<T>&, labelList& order);
98 //- Generate (sorted) indices corresponding to unique list values
100 void uniqueOrder(const UList<T>&, labelList& order);
102 //- Extract elements of List when select is a certain value.
103 // eg, to extract all selected elements:
104 // subset<bool, labelList>(selectedElems, true, lst);
105 template<class T, class ListType>
106 ListType subset(const UList<T>& select, const T& value, const ListType&);
108 //- Inplace extract elements of List when select is a certain value.
109 // eg, to extract all selected elements:
110 // inplaceSubset<bool, labelList>(selectedElems, true, lst);
111 template<class T, class ListType>
112 void inplaceSubset(const UList<T>& select, const T& value, ListType&);
114 //- Extract elements of List when select is true
115 // eg, to extract all selected elements:
116 // subset<boolList, labelList>(selectedElems, lst);
117 // Note a labelHashSet could also be used for the bool-list
118 template<class BoolListType, class ListType>
119 ListType subset(const BoolListType& select, const ListType&);
121 //- Inplace extract elements of List when select is true
122 // eg, to extract all selected elements:
123 // inplaceSubset<boolList, labelList>(selectedElems, lst);
124 // Note a labelHashSet could also be used for the bool-list
125 template<class BoolListType, class ListType>
126 void inplaceSubset(const BoolListType& select, ListType&);
128 //- Invert one-to-one map. Unmapped elements will be -1.
129 labelList invert(const label len, const labelUList&);
131 //- Invert one-to-many map. Unmapped elements will be size 0.
132 labelListList invertOneToMany(const label len, const labelUList&);
134 //- Invert many-to-many.
135 // Input and output types need to be inherited from List.
136 // eg, faces to pointFaces.
137 template<class InList, class OutList>
138 void invertManyToMany(const label len, const UList<InList>&, List<OutList>&);
140 template<class InList, class OutList>
141 List<OutList> invertManyToMany(const label len, const UList<InList>& in)
144 invertManyToMany<InList,OutList>(len, in, out);
148 //- Create identity map (map[i] == i) of given length
149 labelList identity(const label len);
151 //- Find first occurence of given element and return index,
152 // return -1 if not found. Linear search.
153 template<class ListType>
157 typename ListType::const_reference,
161 //- Find all occurences of given element. Linear search.
162 template<class ListType>
163 labelList findIndices
166 typename ListType::const_reference,
170 //- Opposite of findIndices: set values at indices to given value
171 template<class ListType>
175 const labelUList& indices,
176 typename ListType::const_reference
179 //- Opposite of findIndices: set values at indices to given value
180 template<class ListType>
181 ListType createWithValues
184 typename ListType::const_reference initValue,
185 const labelUList& indices,
186 typename ListType::const_reference setValue
189 //- Find index of max element (and larger than given element).
190 // return -1 if not found. Linear search.
191 template<class ListType>
192 label findMax(const ListType&, const label start=0);
195 //- Find index of min element (and less than given element).
196 // return -1 if not found. Linear search.
197 template<class ListType>
198 label findMin(const ListType&, const label start=0);
201 //- Find first occurence of given element in sorted list and return index,
202 // return -1 if not found. Binary search.
203 template<class ListType>
204 label findSortedIndex
207 typename ListType::const_reference,
212 //- Find last element < given value in sorted list and return index,
213 // return -1 if not found. Binary search.
214 template<class ListType>
218 typename ListType::const_reference,
223 //- To construct a List from a C array. Has extra Container type
224 // to initialise e.g. wordList from arrays of char*.
225 template<class Container, class T, int nRows>
226 List<Container> initList(const T[nRows]);
229 //- To construct a (square) ListList from a C array. Has extra Container type
230 // to initialise e.g. faceList from arrays of labels.
231 template<class Container, class T, int nRows, int nColumns>
232 List<Container> initListList(const T[nRows][nColumns]);
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 } // End namespace Foam
239 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 # include "ListOpsTemplates.C"
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 // ************************************************************************* //