initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / Lists / ListOps / ListOpsTemplates.C
blob2a06082b59248f0f1912edbf76aa2f9aaebaef93
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 \*---------------------------------------------------------------------------*/
27 #include "ListOps.H"
29 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
31 template<class List>
32 List Foam::renumber
34     const labelList& oldToNew,
35     const List& lst
38     // Create copy
39     List newLst(lst.size());
41     forAll(lst, elemI)
42     {
43         if (lst[elemI] >= 0)
44         {
45             newLst[elemI] = oldToNew[lst[elemI]];
46         }
47     }
49     return newLst;
53 template<class List>
54 void Foam::inplaceRenumber
56     const labelList& oldToNew,
57     List& lst
60     forAll(lst, elemI)
61     {
62         if (lst[elemI] >= 0)
63         {
64             lst[elemI] = oldToNew[lst[elemI]];
65         }
66     }
70 template<class List>
71 List Foam::reorder
73     const labelList& oldToNew,
74     const List& lst
77     // Create copy
78     List newLst(lst.size());
80     forAll(lst, elemI)
81     {
82         if (oldToNew[elemI] >= 0)
83         {
84             newLst[oldToNew[elemI]] = lst[elemI];
85         }
86         else
87         {
88             newLst[elemI] = lst[elemI];
89         }
90     }
91     return newLst;
95 template<class List>
96 void Foam::inplaceReorder
98     const labelList& oldToNew,
99     List& lst
102     // Create copy
103     List newLst(lst.size());
105     forAll(lst, elemI)
106     {
107         if (oldToNew[elemI] >= 0)
108         {
109             newLst[oldToNew[elemI]] = lst[elemI];
110         }
111         else
112         {
113             newLst[elemI] = lst[elemI];
114         }
115     }
117     lst.transfer(newLst);
121 template<class Container>
122 void Foam::inplaceMapValue
124     const labelList& oldToNew,
125     Container& lst
128     for
129     (
130         typename Container::iterator iter = lst.begin();
131         iter != lst.end();
132         ++iter
133     )
134     {
135         if (iter() >= 0)
136         {
137             iter() = oldToNew[iter()];
138         }
139     }
143 template<class Container>
144 void Foam::inplaceMapKey
146     const labelList& oldToNew,
147     Container& lst
150     Container newLst(lst);
152     for
153     (
154         typename Container::iterator iter = lst.begin();
155         iter != lst.end();
156         ++iter
157     )
158     {
159         if (iter.key() >= 0)
160         {
161             newLst.insert(oldToNew[iter.key()], iter());
162         }
163     }
164     
165     lst.transfer(newLst);
169 template<class T, class List>
170 List Foam::subset(const UList<T>& regions, const T& region, const List& lst)
172     if (regions.size() < lst.size())
173     {
174         FatalErrorIn("subset(const UList<T>&, const T&, const List&)")
175             << "Regions is of size " << regions.size()
176             << "; list it is supposed to index is of size " << lst.size()
177             << abort(FatalError);
178     }
180     List newLst(lst.size());
182     label nElem = 0;
183     forAll(lst, elemI)
184     {
185         if (regions[elemI] == region)
186         {
187             newLst[nElem++] = lst[elemI];
188         }
189     }
190     newLst.setSize(nElem);
192     return newLst;
196 template<class T, class List>
197 void Foam::inplaceSubset(const UList<T>& regions, const T& region, List& lst)
199     if (regions.size() < lst.size())
200     {
201         FatalErrorIn("inplaceSubset(const UList<T>&, const T&, List&)")
202             << "Regions is of size " << regions.size()
203             << "; list it is supposed to index is of size " << lst.size()
204             << abort(FatalError);
205     }
207     label nElem = 0;
208     forAll(lst, elemI)
209     {
210         if (regions[elemI] == region)
211         {
212             if (nElem != elemI)
213             {
214                 lst[nElem] = lst[elemI];
215             }
216             ++nElem;
217         }
218     }
220     lst.setSize(nElem);
224 // As clarification coded as inversion from pointEdges to edges but completely
225 // general.
226 template<class InList, class OutList>
227 void Foam::invertManyToMany
229     const label nEdges,
230     const UList<InList>& pointEdges,
231     List<OutList>& edges
234     // Number of points per edge
235     labelList nPointsPerEdge(nEdges, 0);
237     forAll(pointEdges, pointI)
238     {
239         const InList& pEdges = pointEdges[pointI];
241         forAll(pEdges, j)
242         {
243             nPointsPerEdge[pEdges[j]]++;
244         }
245     }
247     // Size edges
248     edges.setSize(nEdges);
250     forAll(nPointsPerEdge, edgeI)
251     {
252         edges[edgeI].setSize(nPointsPerEdge[edgeI]);
253     }
254     nPointsPerEdge = 0;
256     // Fill edges
257     forAll(pointEdges, pointI)
258     {
259         const InList& pEdges = pointEdges[pointI];
261         forAll(pEdges, j)
262         {
263             label edgeI = pEdges[j];
265             edges[edgeI][nPointsPerEdge[edgeI]++] = pointI;
266         }
267     }
271 template<class List>
272 Foam::label Foam::findIndex
274     const List& l,
275     typename List::const_reference t,
276     const label start
279     label index = -1;
281     for (label i = start; i < l.size(); i++)
282     {
283         if (l[i] == t)
284         {
285             index = i;
286             break;
287         }
288     }
290     return index;
294 template<class List>
295 Foam::labelList Foam::findIndices
297     const List& l,
298     typename List::const_reference t,
299     const label start
302     // Count occurrences
303     label n = 0;
305     for (label i = start; i < l.size(); i++)
306     {
307         if (l[i] == t)
308         {
309             n++;
310         }
311     }
313     // Create and fill
314     labelList indices(n);
315     n = 0;
317     for (label i = start; i < l.size(); i++)
318     {
319         if (l[i] == t)
320         {
321             indices[n++] = i;
322         }
323     }
325     return indices;
329 template<class List>
330 void Foam::setValues
332     List& l,
333     const labelList& indices,
334     typename List::const_reference t
337     forAll(indices, i)
338     {
339         l[indices[i]] = t;
340     }
344 template<class List>
345 List Foam::createWithValues
347     const label sz,
348     const typename List::const_reference initValue,
349     const labelList& indices,
350     typename List::const_reference setValue
353     List l(sz, initValue);
354     setValues(l, indices, setValue);
355     return l;
359 template<class List>
360 Foam::label Foam::findMax(const List& l, const label start)
362     if (start >= l.size())
363     {
364         return -1;
365     }
367     label index = start;
369     for (label i = start+1; i < l.size(); i++)
370     {
371         if (l[i] > l[index])
372         {
373             index = i;
374         }
375     }
377     return index;
381 template<class List>
382 Foam::label Foam::findMin(const List& l, const label start)
384     if (start >= l.size())
385     {
386         return -1;
387     }
389     label index = start;
391     for (label i = start+1; i < l.size(); i++)
392     {
393         if (l[i] < l[index])
394         {
395             index = i;
396         }
397     }
399     return index;
403 template<class List>
404 Foam::label Foam::findSortedIndex
406     const List& l,
407     typename List::const_reference t,
408     const label start
411     if (start >= l.size())
412     {
413         return -1;
414     }
416     label low = start;
417     label high = l.size() - 1;
419     while (low <= high)
420     {
421         label mid = (low + high)/2;
423         if (t < l[mid])
424         {
425             high = mid - 1;
426         }
427         else if (t > l[mid])
428         {
429             low = mid + 1;
430         }
431         else
432         {
433             return mid;
434         }
435     }
437     return -1;
441 template<class List>
442 Foam::label Foam::findLower
444     const List& l,
445     typename List::const_reference t,
446     const label start
449     if (start >= l.size())
450     {
451         return -1;
452     }
454     label low = start;
455     label high = l.size() - 1;
457     while ((high - low) > 1)
458     {
459         label mid = (low + high)/2;
461         if (l[mid] < t)
462         {
463             low = mid;
464         }
465         else
466         {
467             high = mid;
468         }
469     }
471     if (l[high] < t)
472     {
473         return high;
474     }
475     else
476     {
477         if (l[low] < t)
478         {
479             return low;
480         }
481         else
482         {
483             return -1;
484         }
485     }
489 template<class Container, class T, int nRows>
490 Foam::List<Container> Foam::initList(const T elems[nRows])
492     List<Container> faces(nRows);
494     forAll(faces, faceI)
495     {
496         faces[faceI] = Container(elems[faceI]);
497     }
498     return faces;
502 template<class Container, class T, int nRows, int nColumns>
503 Foam::List<Container> Foam::initListList(const T elems[nRows][nColumns])
505     List<Container> faces(nRows);
507     Container f(nColumns);
508     forAll(faces, faceI)
509     {
510         forAll(f, i)
511         {
512             f[i] = elems[faceI][i];
513         }
514         faces[faceI] = f;
515     }
516     return faces;
520 // ************************************************************************* //