Loading...
Searching...
No Matches
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-2023 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
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 Foam::identity(order, 0);
67
68 std::stable_sort(order.begin(), order.end(), comp);
69}
70
71
72template<class T>
73void Foam::shuffle(UPtrList<T>& list)
74{
75 // Cannot use std::shuffle directly since that would dereference
76 // the list entries, which may contain null pointers.
77 // The alternative would be to expose the pointer details (a bit ugly).
78 labelList order(identity(list.size()));
79 Foam::shuffle(order);
80 list.sortOrder(order, false); // false = no nullptr check
81}
82
83
84// Templated implementation for types(), names(), etc - file-scope
85template<class ReturnType, class T, class AccessOp>
87(
88 const UPtrList<T>& list,
89 const AccessOp& aop
90)
91{
92 const label len = list.size();
93
94 List<ReturnType> output(len);
95
96 label count = 0;
97 for (label i = 0; i < len; ++i)
98 {
99 const T* ptr = list.get(i);
100
101 if (bool(ptr))
102 {
103 output[count++] = aop(*ptr);
104 }
105 }
106
107 output.resize(count);
108
109 return output;
110}
111
112
113template<class T, class UnaryMatchPredicate>
115(
116 const UPtrList<T>& list,
117 const UnaryMatchPredicate& matcher
118)
119{
120 // Possible: const auto aop = nameOp<T>();
121
122 const label len = list.size();
123
124 List<word> output(len);
125
126 label count = 0;
127 for (label i = 0; i < len; ++i)
128 {
129 const T* ptr = list.get(i);
130
131 if (bool(ptr) && matcher(ptr->name()))
132 {
133 output[count++] = (ptr->name());
134 }
135 }
136
137 output.resize(count);
138
139 return output;
140}
141
142
143template<class T>
145(
146 const UPtrList<T>& list
147)
148{
149 return PtrListOps::names(list, predicates::always());
150}
151
152
153template<class T, class UnaryMatchPredicate>
155(
156 const UPtrList<T>& list,
157 const UnaryMatchPredicate& matcher
158)
159{
160 const label len = list.size();
161
162 for (label i = 0; i < len; ++i)
163 {
164 const T* ptr = list.get(i);
165
166 if (bool(ptr) && matcher(ptr->name()))
167 {
168 return i;
169 }
170 }
171
172 return -1;
173}
174
175
176template<class T, class UnaryMatchPredicate>
178(
179 const UPtrList<T>& list,
180 const UnaryMatchPredicate& matcher
181)
182{
183 const label len = list.size();
184
185 labelList output(len);
186
187 label count = 0;
188 for (label i = 0; i < len; ++i)
189 {
190 const T* ptr = list.get(i);
191
192 if (bool(ptr) && matcher(ptr->name()))
193 {
194 output[count++] = i;
195 }
196 }
197
198 output.resize(count);
199
200 return output;
201}
202
203
204// ************************************************************************* //
Functions to operate on Pointer Lists.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition ListI.H:171
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition UListI.H:454
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
void sortOrder(const labelUList &order, const bool check=false)
Reorder elements according to new order mapping (newToOld). Reordering must be unique (ie,...
Definition UPtrList.C:112
label size() const noexcept
The number of entries 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:73
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
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)
List of names generated by calling name() for each list item and filtered for matches.
void shuffle(UList< T > &list)
Randomise the list order.
Definition UList.C:315
List< label > labelList
A List of labels.
Definition List.H:62
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
A UPtrList compare binary predicate for normal sort order. Null entries (if any) sort to the end.
Definition UPtrList.H:196