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-2021 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;
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  // List lengths must be identical. Old content is overwritten
64  order.resize_nocopy(input.size());
65 
66  ListOps::identity(order);
67 
68  Foam::stableSort(order, comp);
69 }
70 
71 
72 template<class T>
74 {
75  labelList order;
76  sortedOrder(list, order);
77  list.sortOrder(order, false); // false = allow nullptr
78 }
79 
80 
81 template<class T, class Compare>
82 void Foam::sort(UPtrList<T>& list, const Compare& comp)
83 {
84  labelList order;
85  sortedOrder(list, order, comp);
86  list.sortOrder(order, false); // false = allow nullptr
87 }
88 
89 
90 template<class T>
92 {
93  labelList order(identity(list.size()));
94  Foam::shuffle(order);
95  list.sortOrder(order, false); // false = allow nullptr
96 }
97 
98 
99 
100 // Templated implementation for types(), names(), etc - file-scope
101 template<class ReturnType, class T, class AccessOp>
103 (
104  const UPtrList<T>& list,
105  const AccessOp& aop
106 )
107 {
108  const label len = list.size();
109 
110  List<ReturnType> output(len);
111 
112  label count = 0;
113  for (label i = 0; i < len; ++i)
114  {
115  const T* ptr = list.get(i);
116 
117  if (bool(ptr))
118  {
119  output[count++] = aop(*ptr);
120  }
121  }
122 
123  output.resize(count);
124 
125  return output;
126 }
127 
128 
129 template<class T, class UnaryMatchPredicate>
131 (
132  const UPtrList<T>& list,
133  const UnaryMatchPredicate& matcher
134 )
135 {
136  // Possible: const auto aop = nameOp<T>();
137 
138  const label len = list.size();
139 
140  List<word> output(len);
141 
142  label count = 0;
143  for (label i = 0; i < len; ++i)
144  {
145  const T* ptr = list.get(i);
146 
147  if (bool(ptr))
148  {
149  if (matcher(ptr->name()))
150  {
151  output[count++] = (ptr->name());
152  }
153  }
154  }
155 
156  output.resize(count);
157 
158  return output;
159 }
160 
161 
162 template<class T>
164 (
165  const UPtrList<T>& list
166 )
167 {
168  return PtrListOps::names(list, predicates::always());
169 }
170 
171 
172 template<class T, class UnaryMatchPredicate>
174 (
175  const UPtrList<T>& list,
176  const UnaryMatchPredicate& matcher
177 )
178 {
179  const label len = list.size();
180 
181  for (label i = 0; i < len; ++i)
182  {
183  const T* ptr = list.get(i);
184 
185  if (bool(ptr) && matcher(ptr->name()))
186  {
187  return i;
188  }
189  }
190 
191  return -1;
192 }
193 
194 
195 template<class T, class UnaryMatchPredicate>
197 (
198  const UPtrList<T>& list,
199  const UnaryMatchPredicate& matcher
200 )
201 {
202  const label len = list.size();
203 
204  labelList output(len);
205 
206  label count = 0;
207  for (label i = 0; i < len; ++i)
208  {
209  const T* ptr = list.get(i);
210 
211  if (bool(ptr) && matcher(ptr->name()))
212  {
213  output[count++] = i;
214  }
215  }
216 
217  output.resize(count);
218 
219  return output;
220 }
221 
222 
223 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::UPtrList::size
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:106
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Foam::PtrListOps::findMatching
labelList findMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Extract list indices for all items with 'name()' that matches.
PtrListOps.H
Functions to operate on Pointer Lists.
Foam::List::resize_nocopy
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:146
Foam::PtrListOps::less
A UPtrList compare binary predicate for normal sort.
Definition: PtrListOps.H:99
Foam::stableSort
void stableSort(UList< T > &a)
Definition: UList.C:275
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:62
Foam::UPtrList::sortOrder
void sortOrder(const labelUList &order, const bool testNull=true)
Definition: UPtrList.C:129
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::shuffle
void shuffle(UList< T > &a)
Definition: UList.C:289
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::PtrListOps::firstMatching
label firstMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Find first list item with 'name()' that matches, -1 on failure.
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::sortedOrder
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)