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 -------------------------------------------------------------------------------
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  // 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 
78 template<class T>
79 void Foam::shuffle(UPtrList<T>& list)
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
91 template<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 
119 template<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) && matcher(ptr->name()))
138  {
139  output[count++] = (ptr->name());
140  }
141  }
142 
143  output.resize(count);
144 
145  return output;
146 }
147 
148 
149 template<class T>
151 (
152  const UPtrList<T>& list
153 )
154 {
155  return PtrListOps::names(list, predicates::always());
156 }
157 
158 
159 template<class T, class UnaryMatchPredicate>
161 (
162  const UPtrList<T>& list,
163  const UnaryMatchPredicate& matcher
164 )
165 {
166  const label len = list.size();
167 
168  for (label i = 0; i < len; ++i)
169  {
170  const T* ptr = list.get(i);
171 
172  if (bool(ptr) && matcher(ptr->name()))
173  {
174  return i;
175  }
176  }
177 
178  return -1;
179 }
180 
181 
182 template<class T, class UnaryMatchPredicate>
184 (
185  const UPtrList<T>& list,
186  const UnaryMatchPredicate& matcher
187 )
188 {
189  const label len = list.size();
190 
191  labelList output(len);
192 
193  label count = 0;
194  for (label i = 0; i < len; ++i)
195  {
196  const T* ptr = list.get(i);
197 
198  if (bool(ptr) && matcher(ptr->name()))
199  {
200  output[count++] = i;
201  }
202  }
203 
204  output.resize(count);
205 
206  return output;
207 }
208 
209 
210 // ************************************************************************* //
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:139
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)
Definition: labelList.C:31
const volScalarField & T
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:58
List< label > labelList
A List of labels.
Definition: List.H:62
void shuffle(UList< T > &list)
Randomise the list order.
Definition: UList.C:376
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