PtrListOpsTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2019-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "PtrListOps.H"
29 
30 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
31 
32 template<class T>
34 (
35  const UPtrList<T>& input
36 )
37 {
38  labelList order(input.size());
39  sortedOrder(input, order, typename PtrListOps::less<T>(input));
40  return order;
41 }
42 
43 
44 template<class T>
46 (
47  const UPtrList<T>& input,
48  labelList& order
49 )
50 {
51  sortedOrder(input, order, typename PtrListOps::less<T>(input));
52 }
53 
54 
55 template<class T, class ListComparePredicate>
57 (
58  const UPtrList<T>& input,
59  labelList& order,
60  const ListComparePredicate& comp
61 )
62 {
63  const label len = input.size();
64 
65  // List lengths must be identical
66  if (order.size() != len)
67  {
68  // Avoid copying elements, they are overwritten anyhow
69  order.clear();
70  order.resize(len);
71  }
72 
73  ListOps::identity(order);
74 
75  Foam::stableSort(order, comp);
76 }
77 
78 
79 template<class T>
81 {
82  labelList order(list.size());
83  sortedOrder(list, order);
84  list.sortOrder(order, false); // false = allow nullptr
85 }
86 
87 
88 template<class T, class Compare>
89 void Foam::sort(UPtrList<T>& list, const Compare& comp)
90 {
91  labelList order(list.size());
92  sortedOrder(list, order, comp);
93  list.sortOrder(order, false); // false = allow nullptr
94 }
95 
96 
97 template<class T>
99 {
100  labelList order(identity(list.size()));
101  Foam::shuffle(order);
102  list.sortOrder(order, false); // false = allow nullptr
103 }
104 
105 
106 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
Foam::UPtrList::size
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:97
PtrListOps.H
Functions to operate on Pointer Lists.
Foam::PtrListOps::less
A UPtrList compare binary predicate for normal sort.
Definition: PtrListOps.H:98
Foam::stableSort
void stableSort(UList< T > &a)
Definition: UList.C:268
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:254
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:62
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::UPtrList::sortOrder
void sortOrder(const labelUList &order, const bool testNull=true)
Definition: UPtrList.C:129
Foam::shuffle
void shuffle(UList< T > &a)
Definition: UList.C:282
Foam::List< label >
Foam::identity
labelList identity(const label len, label start=0)
Create identity map of the given length with (map[i] == i)
Definition: labelList.C:38
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:115
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::sortedOrder
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.