1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 Various functions to operate on Lists.
35 \*---------------------------------------------------------------------------*/
40 #include "labelList.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 //- Renumber the values (not the indices) of a list.
48 // Negative ListType elements are left as is.
49 template<class ListType>
50 ListType renumber(const UList<label>& oldToNew, const ListType&);
52 //- Inplace renumber the values of a list.
53 // Negative ListType elements are left as is.
54 template<class ListType>
55 void inplaceRenumber(const UList<label>& oldToNew, ListType&);
58 //- Reorder the elements (indices, not values) of a list.
59 // Negative ListType elements are left as is.
60 template<class ListType>
61 ListType reorder(const UList<label>& oldToNew, const ListType&);
63 //- Inplace reorder the elements of a list.
64 // Negative ListType elements are left as is.
65 template<class ListType>
66 void inplaceReorder(const UList<label>& oldToNew, ListType&);
69 // Variants to work with iterators and sparse tables.
70 // Need to have iterators and insert()
72 //- Map values. Do not map negative values.
73 template<class Container>
74 void inplaceMapValue(const UList<label>& oldToNew, Container&);
76 //- Recreate with mapped keys. Do not map elements with negative key.
77 template<class Container>
78 void inplaceMapKey(const UList<label>& oldToNew, Container&);
81 //- Generate the (stable) sort order for the list
83 void sortedOrder(const UList<T>&, labelList& order);
85 //- Generate (sorted) indices corresponding to duplicate list values
87 void duplicateOrder(const UList<T>&, labelList& order);
89 //- Generate (sorted) indices corresponding to unique list values
91 void uniqueOrder(const UList<T>&, labelList& order);
93 //- Extract elements of List when select is a certain value.
94 // eg, to extract all selected elements:
95 // subset<bool, labelList>(selectedElems, true, lst);
96 template<class T, class ListType>
97 ListType subset(const UList<T>& select, const T& value, const ListType&);
99 //- Inplace extract elements of List when select is a certain value.
100 // eg, to extract all selected elements:
101 // inplaceSubset<bool, labelList>(selectedElems, true, lst);
102 template<class T, class ListType>
103 void inplaceSubset(const UList<T>& select, const T& value, ListType&);
105 //- Extract elements of List when select is true
106 // eg, to extract all selected elements:
107 // subset<boolList, labelList>(selectedElems, lst);
108 // Note a labelHashSet could also be used for the bool-list
109 template<class BoolListType, class ListType>
110 ListType subset(const BoolListType& select, const ListType&);
112 //- Inplace extract elements of List when select is true
113 // eg, to extract all selected elements:
114 // inplaceSubset<boolList, labelList>(selectedElems, lst);
115 // Note a labelHashSet could also be used for the bool-list
116 template<class BoolListType, class ListType>
117 void inplaceSubset(const BoolListType& select, ListType&);
119 //- Invert one-to-one map. Unmapped elements will be -1.
120 labelList invert(const label len, const UList<label>&);
122 //- Invert one-to-many map. Unmapped elements will be size 0.
123 labelListList invertOneToMany(const label len, const UList<label>&);
125 //- Invert many-to-many.
126 // Input and output types need to be inherited from List.
127 // eg, faces to pointFaces.
128 template<class InList, class OutList>
129 void invertManyToMany(const label len, const UList<InList>&, List<OutList>&);
131 template<class InList, class OutList>
132 List<OutList> invertManyToMany(const label len, const UList<InList>& in)
135 invertManyToMany<InList,OutList>(len, in, out);
139 //- Create identity map (map[i] == i) of given length
140 labelList identity(const label len);
142 //- Find first occurence of given element and return index,
143 // return -1 if not found. Linear search.
144 template<class ListType>
148 typename ListType::const_reference,
152 //- Find all occurences of given element. Linear search.
153 template<class ListType>
154 labelList findIndices
157 typename ListType::const_reference,
161 //- Opposite of findIndices: set values at indices to given value
162 template<class ListType>
166 const UList<label>& indices,
167 typename ListType::const_reference
170 //- Opposite of findIndices: set values at indices to given value
171 template<class ListType>
172 ListType createWithValues
175 const typename ListType::const_reference initValue,
176 const UList<label>& indices,
177 typename ListType::const_reference setValue
180 //- Find index of max element (and larger than given element).
181 // return -1 if not found. Linear search.
182 template<class ListType>
183 label findMax(const ListType&, const label start=0);
186 //- Find index of min element (and less than given element).
187 // return -1 if not found. Linear search.
188 template<class ListType>
189 label findMin(const ListType&, const label start=0);
192 //- Find first occurence of given element in sorted list and return index,
193 // return -1 if not found. Binary search.
194 template<class ListType>
195 label findSortedIndex
198 typename ListType::const_reference,
203 //- Find last element < given value in sorted list and return index,
204 // return -1 if not found. Binary search.
205 template<class ListType>
209 typename ListType::const_reference,
214 //- To construct a List from a C array. Has extra Container type
215 // to initialise e.g. wordList from arrays of char*.
216 template<class Container, class T, int nRows>
217 List<Container> initList(const T[nRows]);
220 //- To construct a (square) ListList from a C array. Has extra Container type
221 // to initialise e.g. faceList from arrays of labels.
222 template<class Container, class T, int nRows, int nColumns>
223 List<Container> initListList(const T[nRows][nColumns]);
226 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 } // End namespace Foam
230 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 # include "ListOpsTemplates.C"
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
240 // ************************************************************************* //