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/>.
24 \*---------------------------------------------------------------------------*/
28 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
30 template<class ListType>
31 ListType Foam::renumber
33 const labelUList& oldToNew,
38 ListType newLst(lst.size());
40 // ensure consistent addressable size (eg, DynamicList)
41 newLst.setSize(lst.size());
47 newLst[elemI] = oldToNew[lst[elemI]];
55 template<class ListType>
56 void Foam::inplaceRenumber
58 const labelUList& oldToNew,
66 lst[elemI] = oldToNew[lst[elemI]];
72 template<class ListType>
73 ListType Foam::reorder
75 const labelUList& oldToNew,
80 ListType newLst(lst.size());
82 // ensure consistent addressable size (eg, DynamicList)
83 newLst.setSize(lst.size());
87 if (oldToNew[elemI] >= 0)
89 newLst[oldToNew[elemI]] = lst[elemI];
93 newLst[elemI] = lst[elemI];
100 template<class ListType>
101 void Foam::inplaceReorder
103 const labelUList& oldToNew,
108 ListType newLst(lst.size());
110 // ensure consistent addressable size (eg, DynamicList)
111 newLst.setSize(lst.size());
115 if (oldToNew[elemI] >= 0)
117 newLst[oldToNew[elemI]] = lst[elemI];
121 newLst[elemI] = lst[elemI];
125 lst.transfer(newLst);
129 template<class Container>
130 void Foam::inplaceMapValue
132 const labelUList& oldToNew,
138 typename Container::iterator iter = lst.begin();
145 iter() = oldToNew[iter()];
151 template<class Container>
152 void Foam::inplaceMapKey
154 const labelUList& oldToNew,
158 Container newLst(lst.size());
162 typename Container::iterator iter = lst.begin();
169 newLst.insert(oldToNew[iter.key()], iter());
173 lst.transfer(newLst);
178 void Foam::sortedOrder
184 // list lengths must be identical
185 if (order.size() != lst.size())
187 // avoid copying any elements, they are overwritten anyhow
189 order.setSize(lst.size());
194 order[elemI] = elemI;
196 Foam::stableSort(order, typename UList<T>::less(lst));
201 void Foam::duplicateOrder
213 sortedOrder(lst, order);
216 for (label i = 0; i < order.size() - 1; ++i)
218 if (lst[order[i]] == lst[order[i+1]])
220 order[n++] = order[i];
228 void Foam::uniqueOrder
234 sortedOrder(lst, order);
236 if (order.size() > 1)
239 for (label i = 0; i < order.size() - 1; ++i)
241 if (lst[order[i]] != lst[order[i+1]])
243 order[n++] = order[i];
246 order[n++] = order[order.size()-1];
252 template<class T, class ListType>
253 ListType Foam::subset
255 const UList<T>& select,
260 // select must at least cover the list range
261 if (select.size() < lst.size())
263 FatalErrorIn("subset(const UList<T>&, const T&, const ListType&)")
264 << "select is of size " << select.size()
265 << "; but it must index a list of size " << lst.size()
266 << abort(FatalError);
269 ListType newLst(lst.size());
271 // ensure consistent addressable size (eg, DynamicList)
272 newLst.setSize(lst.size());
277 if (select[elemI] == value)
279 newLst[nElem++] = lst[elemI];
282 newLst.setSize(nElem);
288 template<class T, class ListType>
289 void Foam::inplaceSubset
291 const UList<T>& select,
296 // select must at least cover the list range
297 if (select.size() < lst.size())
299 FatalErrorIn("inplaceSubset(const UList<T>&, const T&, ListType&)")
300 << "select is of size " << select.size()
301 << "; but it must index a list of size " << lst.size()
302 << abort(FatalError);
308 if (select[elemI] == value)
312 lst[nElem] = lst[elemI];
322 template<class BoolListType, class ListType>
323 ListType Foam::subset
325 const BoolListType& select,
329 // select can have a different size
330 // eg, when it is a PackedBoolList or a labelHashSet
332 ListType newLst(lst.size());
334 // ensure consistent addressable size (eg, DynamicList)
335 newLst.setSize(lst.size());
342 newLst[nElem++] = lst[elemI];
345 newLst.setSize(nElem);
351 template<class BoolListType, class ListType>
352 void Foam::inplaceSubset
354 const BoolListType& select,
358 // select can have a different size
359 // eg, when it is a PackedBoolList or a labelHashSet
368 lst[nElem] = lst[elemI];
379 // coded as inversion from pointEdges to edges but completely general.
380 template<class InList, class OutList>
381 void Foam::invertManyToMany
384 const UList<InList>& pointEdges,
388 // Number of points per edge
389 labelList nPointsPerEdge(nEdges, 0);
391 forAll(pointEdges, pointI)
393 const InList& pEdges = pointEdges[pointI];
397 nPointsPerEdge[pEdges[j]]++;
402 edges.setSize(nEdges);
404 forAll(nPointsPerEdge, edgeI)
406 edges[edgeI].setSize(nPointsPerEdge[edgeI]);
411 forAll(pointEdges, pointI)
413 const InList& pEdges = pointEdges[pointI];
417 label edgeI = pEdges[j];
419 edges[edgeI][nPointsPerEdge[edgeI]++] = pointI;
425 template<class ListType>
426 Foam::label Foam::findIndex
429 typename ListType::const_reference t,
435 for (label i = start; i < l.size(); i++)
448 template<class ListType>
449 Foam::labelList Foam::findIndices
452 typename ListType::const_reference t,
459 for (label i = start; i < l.size(); i++)
468 labelList indices(n);
471 for (label i = start; i < l.size(); i++)
483 template<class ListType>
487 const labelUList& indices,
488 typename ListType::const_reference t
498 template<class ListType>
499 ListType Foam::createWithValues
502 const typename ListType::const_reference initValue,
503 const labelUList& indices,
504 typename ListType::const_reference setValue
507 ListType l(sz, initValue);
508 setValues(l, indices, setValue);
513 template<class ListType>
514 Foam::label Foam::findMax(const ListType& l, const label start)
516 if (start >= l.size())
523 for (label i = start+1; i < l.size(); i++)
535 template<class ListType>
536 Foam::label Foam::findMin(const ListType& l, const label start)
538 if (start >= l.size())
545 for (label i = start+1; i < l.size(); i++)
557 template<class ListType>
558 Foam::label Foam::findSortedIndex
561 typename ListType::const_reference t,
565 if (start >= l.size())
571 label high = l.size() - 1;
575 label mid = (low + high)/2;
595 template<class ListType>
596 Foam::label Foam::findLower
599 typename ListType::const_reference t,
603 if (start >= l.size())
609 label high = l.size() - 1;
611 while ((high - low) > 1)
613 label mid = (low + high)/2;
643 template<class Container, class T, int nRows>
644 Foam::List<Container> Foam::initList(const T elems[nRows])
646 List<Container> lst(nRows);
650 lst[rowI] = Container(elems[rowI]);
656 template<class Container, class T, int nRows, int nColumns>
657 Foam::List<Container> Foam::initListList(const T elems[nRows][nColumns])
659 List<Container> lst(nRows);
661 Container cols(nColumns);
666 cols[colI] = elems[rowI][colI];
674 // ************************************************************************* //