initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / ListOps / ListOps.H
blob1504fb4d78363167fd2f4e934270702728afc71d
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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
25 InNamspace
26     Foam
28 Description
29     Various functions to operate on Lists.
31 SourceFiles
32     ListOps.C
33     ListOpsTemplates.C
35 \*---------------------------------------------------------------------------*/
37 #ifndef ListOps_H
38 #define ListOps_H
40 #include "labelList.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 namespace Foam
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
82 template<class T>
83 void sortedOrder(const UList<T>&, labelList& order);
85 //- Generate (sorted) indices corresponding to duplicate list values
86 template<class T>
87 void duplicateOrder(const UList<T>&, labelList& order);
89 //- Generate (sorted) indices corresponding to unique list values
90 template<class T>
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)
134     List<OutList> out;
135     invertManyToMany<InList,OutList>(len, in, out);
136     return 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>
145 label findIndex
147     const ListType&,
148     typename ListType::const_reference,
149     const label start=0
152 //- Find all occurences of given element. Linear search.
153 template<class ListType>
154 labelList findIndices
156     const ListType&,
157     typename ListType::const_reference,
158     const label start=0
161 //- Opposite of findIndices: set values at indices to given value
162 template<class ListType>
163 void setValues
165     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
174     const label sz,
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
197     const ListType&,
198     typename ListType::const_reference,
199     const label start=0
203 //- Find last element < given value in sorted list and return index,
204 //  return -1 if not found. Binary search.
205 template<class ListType>
206 label findLower
208     const ListType&,
209     typename ListType::const_reference,
210     const label start=0
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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
232 #ifdef NoRepository
233 #   include "ListOpsTemplates.C"
234 #endif
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
238 #endif
240 // ************************************************************************* //