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
25 \*---------------------------------------------------------------------------*/
29 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
31 template<class ListType>
32 ListType Foam::renumber
34 const UList<label>& oldToNew,
39 ListType newLst(lst.size());
45 newLst[elemI] = oldToNew[lst[elemI]];
53 template<class ListType>
54 void Foam::inplaceRenumber
56 const UList<label>& oldToNew,
64 lst[elemI] = oldToNew[lst[elemI]];
70 template<class ListType>
71 ListType Foam::reorder
73 const UList<label>& oldToNew,
78 ListType newLst(lst.size());
82 if (oldToNew[elemI] >= 0)
84 newLst[oldToNew[elemI]] = lst[elemI];
88 newLst[elemI] = lst[elemI];
95 template<class ListType>
96 void Foam::inplaceReorder
98 const UList<label>& oldToNew,
103 ListType newLst(lst.size());
107 if (oldToNew[elemI] >= 0)
109 newLst[oldToNew[elemI]] = lst[elemI];
113 newLst[elemI] = lst[elemI];
117 lst.transfer(newLst);
121 template<class Container>
122 void Foam::inplaceMapValue
124 const UList<label>& oldToNew,
130 typename Container::iterator iter = lst.begin();
137 iter() = oldToNew[iter()];
143 template<class Container>
144 void Foam::inplaceMapKey
146 const UList<label>& oldToNew,
150 Container newLst(lst.size());
154 typename Container::iterator iter = lst.begin();
161 newLst.insert(oldToNew[iter.key()], iter());
165 lst.transfer(newLst);
170 void Foam::sortedOrder
176 // list lengths must be identical
177 if (order.size() != lst.size())
179 // avoid copying any elements, they are overwritten anyhow
181 order.setSize(lst.size());
186 order[elemI] = elemI;
188 Foam::stableSort(order, typename UList<T>::less(lst));
193 void Foam::duplicateOrder
205 sortedOrder(lst, order);
208 for (label i = 0; i < order.size() - 1; ++i)
210 if (lst[order[i]] == lst[order[i+1]])
212 order[n++] = order[i];
220 void Foam::uniqueOrder
226 sortedOrder(lst, order);
228 if (order.size() > 1)
231 for (label i = 0; i < order.size() - 1; ++i)
233 if (lst[order[i]] != lst[order[i+1]])
235 order[n++] = order[i];
243 template<class T, class ListType>
244 ListType Foam::subset
246 const UList<T>& select,
251 // select must at least cover the list range
252 if (select.size() < lst.size())
254 FatalErrorIn("subset(const UList<T>&, const T&, const ListType&)")
255 << "select is of size " << select.size()
256 << "; but it must index a list of size " << lst.size()
257 << abort(FatalError);
260 ListType newLst(lst.size());
265 if (select[elemI] == value)
267 newLst[nElem++] = lst[elemI];
270 newLst.setSize(nElem);
276 template<class T, class ListType>
277 void Foam::inplaceSubset
279 const UList<T>& select,
284 // select must at least cover the list range
285 if (select.size() < lst.size())
287 FatalErrorIn("inplaceSubset(const UList<T>&, const T&, ListType&)")
288 << "select is of size " << select.size()
289 << "; but it must index a list of size " << lst.size()
290 << abort(FatalError);
296 if (select[elemI] == value)
300 lst[nElem] = lst[elemI];
310 template<class BoolListType, class ListType>
311 ListType Foam::subset
313 const BoolListType& select,
317 // select can have a different size
318 // eg, when it is a PackedBoolList or a labelHashSet
320 ListType newLst(lst.size());
327 newLst[nElem++] = lst[elemI];
330 newLst.setSize(nElem);
336 template<class BoolListType, class ListType>
337 void Foam::inplaceSubset
339 const BoolListType& select,
343 // select can have a different size
344 // eg, when it is a PackedBoolList or a labelHashSet
353 lst[nElem] = lst[elemI];
364 // coded as inversion from pointEdges to edges but completely general.
365 template<class InList, class OutList>
366 void Foam::invertManyToMany
369 const UList<InList>& pointEdges,
373 // Number of points per edge
374 labelList nPointsPerEdge(nEdges, 0);
376 forAll(pointEdges, pointI)
378 const InList& pEdges = pointEdges[pointI];
382 nPointsPerEdge[pEdges[j]]++;
387 edges.setSize(nEdges);
389 forAll(nPointsPerEdge, edgeI)
391 edges[edgeI].setSize(nPointsPerEdge[edgeI]);
396 forAll(pointEdges, pointI)
398 const InList& pEdges = pointEdges[pointI];
402 label edgeI = pEdges[j];
404 edges[edgeI][nPointsPerEdge[edgeI]++] = pointI;
410 template<class ListType>
411 Foam::label Foam::findIndex
414 typename ListType::const_reference t,
420 for (label i = start; i < l.size(); i++)
433 template<class ListType>
434 Foam::labelList Foam::findIndices
437 typename ListType::const_reference t,
444 for (label i = start; i < l.size(); i++)
453 labelList indices(n);
456 for (label i = start; i < l.size(); i++)
468 template<class ListType>
472 const UList<label>& indices,
473 typename ListType::const_reference t
483 template<class ListType>
484 ListType Foam::createWithValues
487 const typename ListType::const_reference initValue,
488 const UList<label>& indices,
489 typename ListType::const_reference setValue
492 ListType l(sz, initValue);
493 setValues(l, indices, setValue);
498 template<class ListType>
499 Foam::label Foam::findMax(const ListType& l, const label start)
501 if (start >= l.size())
508 for (label i = start+1; i < l.size(); i++)
520 template<class ListType>
521 Foam::label Foam::findMin(const ListType& l, const label start)
523 if (start >= l.size())
530 for (label i = start+1; i < l.size(); i++)
542 template<class ListType>
543 Foam::label Foam::findSortedIndex
546 typename ListType::const_reference t,
550 if (start >= l.size())
556 label high = l.size() - 1;
560 label mid = (low + high)/2;
580 template<class ListType>
581 Foam::label Foam::findLower
584 typename ListType::const_reference t,
588 if (start >= l.size())
594 label high = l.size() - 1;
596 while ((high - low) > 1)
598 label mid = (low + high)/2;
628 template<class Container, class T, int nRows>
629 Foam::List<Container> Foam::initList(const T elems[nRows])
631 List<Container> lst(nRows);
635 lst[rowI] = Container(elems[rowI]);
641 template<class Container, class T, int nRows, int nColumns>
642 Foam::List<Container> Foam::initListList(const T elems[nRows][nColumns])
644 List<Container> lst(nRows);
646 Container cols(nColumns);
651 cols[colI] = elems[rowI][colI];
659 // ************************************************************************* //