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 -------------------------------------------------------------------------------
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>& list
36 )
37 {
38  labelList order;
39  Foam::sortedOrder(list, order, typename UPtrList<T>::less(list));
40  return order;
41 }
42 
43 
44 template<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 
55 template<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 
72 template<class T>
73 void 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
85 template<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 
113 template<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 
143 template<class T>
145 (
146  const UPtrList<T>& list
147 )
148 {
149  return PtrListOps::names(list, predicates::always());
150 }
151 
152 
153 template<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 
176 template<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 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
labelList findMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Extract list indices for all items with &#39;name()&#39; that matches.
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.
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:175
Functions to operate on Pointer Lists.
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
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...
Definition: labelLists.C:44
const volScalarField & T
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
List< label > labelList
A List of labels.
Definition: List.H:62
void shuffle(UList< T > &list)
Randomise the list order.
Definition: UList.C:328
label firstMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Find first list item with &#39;name()&#39; that matches, -1 on failure.
A UPtrList compare binary predicate for normal sort order. Null entries (if any) sort to the end...
Definition: UPtrList.H:195