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-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
32template<class T>
34(
35 const UPtrList<T>& list
36)
37{
38 labelList order;
39 Foam::sortedOrder(list, order, typename UPtrList<T>::less(list));
40 return order;
41}
42
43
44template<class T>
46(
47 const UPtrList<T>& list,
48 labelList& order
49)
50{
51 Foam::sortedOrder(list, order, typename UPtrList<T>::less(list));
52}
53
54
55template<class T, class ListComparePredicate>
57(
58 const UPtrList<T>& list,
59 labelList& order,
60 const ListComparePredicate& comp
61)
62{
63 // List lengths must be identical. Old content is overwritten
64 order.resize_nocopy(list.size());
65
66 // Same as std::iota and ListOps::identity
67 label value = 0;
68 for (label& item : order)
69 {
70 item = value;
71 ++value;
72 }
73
74 std::stable_sort(order.begin(), order.end(), comp);
75}
76
77
78template<class T>
80{
81 // Cannot use std::shuffle directly since that would dereference
82 // the list entries, which may contain null pointers.
83 // The alternative would be to expose the pointer details (a bit ugly).
84 labelList order(identity(list.size()));
85 Foam::shuffle(order);
86 list.sortOrder(order, false); // false = no nullptr check
87}
88
89
90// Templated implementation for types(), names(), etc - file-scope
91template<class ReturnType, class T, class AccessOp>
93(
94 const UPtrList<T>& list,
95 const AccessOp& aop
96)
97{
98 const label len = list.size();
99
100 List<ReturnType> output(len);
101
102 label count = 0;
103 for (label i = 0; i < len; ++i)
104 {
105 const T* ptr = list.get(i);
106
107 if (bool(ptr))
108 {
109 output[count++] = aop(*ptr);
110 }
111 }
112
113 output.resize(count);
114
115 return output;
116}
117
118
119template<class T, class UnaryMatchPredicate>
121(
122 const UPtrList<T>& list,
123 const UnaryMatchPredicate& matcher
124)
125{
126 // Possible: const auto aop = nameOp<T>();
127
128 const label len = list.size();
129
130 List<word> output(len);
131
132 label count = 0;
133 for (label i = 0; i < len; ++i)
134 {
135 const T* ptr = list.get(i);
136
137 if (bool(ptr))
138 {
139 if (matcher(ptr->name()))
140 {
141 output[count++] = (ptr->name());
142 }
143 }
144 }
145
146 output.resize(count);
147
148 return output;
149}
150
151
152template<class T>
154(
155 const UPtrList<T>& list
156)
157{
158 return PtrListOps::names(list, predicates::always());
159}
160
161
162template<class T, class UnaryMatchPredicate>
164(
165 const UPtrList<T>& list,
166 const UnaryMatchPredicate& matcher
167)
168{
169 const label len = list.size();
170
171 for (label i = 0; i < len; ++i)
172 {
173 const T* ptr = list.get(i);
174
175 if (bool(ptr) && matcher(ptr->name()))
176 {
177 return i;
178 }
179 }
180
181 return -1;
182}
183
184
185template<class T, class UnaryMatchPredicate>
187(
188 const UPtrList<T>& list,
189 const UnaryMatchPredicate& matcher
190)
191{
192 const label len = list.size();
193
194 labelList output(len);
195
196 label count = 0;
197 for (label i = 0; i < len; ++i)
198 {
199 const T* ptr = list.get(i);
200
201 if (bool(ptr) && matcher(ptr->name()))
202 {
203 output[count++] = i;
204 }
205 }
206
207 output.resize(count);
208
209 return output;
210}
211
212
213// ************************************************************************* //
Functions to operate on Pointer Lists.
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:146
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
void sortOrder(const labelUList &order, const bool check=false)
Definition: UPtrList.C:118
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:106
const volScalarField & T
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:78
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
labelList findMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Extract list indices for all items with 'name()' that matches.
label firstMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Find first list item with 'name()' that matches, -1 on failure.
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
void shuffle(UList< T > &list)
Randomise the list order.
Definition: UList.C:370
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
Definition: labelList.C:38
List< label > labelList
A List of labels.
Definition: List.H:66
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.