PtrList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "PtrList.H"
27 #include "SLPtrList.H"
28 
29 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
30 
31 template<class T>
33 :
34  UPtrList<T>()
35 {}
36 
37 
38 template<class T>
40 :
41  UPtrList<T>(s)
42 {}
43 
44 
45 template<class T>
47 :
48  UPtrList<T>(a.size())
49 {
50  forAll(*this, i)
51  {
52  this->ptrs_[i] = (a[i]).clone().ptr();
53  }
54 }
55 
56 
57 template<class T>
58 template<class CloneArg>
59 Foam::PtrList<T>::PtrList(const PtrList<T>& a, const CloneArg& cloneArg)
60 :
61  UPtrList<T>(a.size())
62 {
63  forAll(*this, i)
64  {
65  this->ptrs_[i] = (a[i]).clone(cloneArg).ptr();
66  }
67 }
68 
69 
70 template<class T>
72 {
73  transfer(lst);
74 }
75 
76 
77 template<class T>
79 :
80  UPtrList<T>(a, reuse)
81 {
82  if (!reuse)
83  {
84  forAll(*this, i)
85  {
86  this->ptrs_[i] = (a[i]).clone().ptr();
87  }
88  }
89 }
90 
91 
92 template<class T>
94 :
95  UPtrList<T>(sll.size())
96 {
97  if (sll.size())
98  {
99  label i = 0;
100  for
101  (
102  typename SLPtrList<T>::const_iterator iter = sll.begin();
103  iter != sll.end();
104  ++iter
105  )
106  {
107  this->ptrs_[i++] = (iter()).clone().ptr();
108  }
109  }
110 }
111 
112 
113 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
114 
115 template<class T>
117 {
118  forAll(*this, i)
119  {
120  if (this->ptrs_[i])
121  {
122  delete this->ptrs_[i];
123  }
124  }
125 }
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 
130 template<class T>
131 void Foam::PtrList<T>::setSize(const label newSize)
132 {
133  if (newSize < 0)
134  {
136  << "bad set size " << newSize
137  << " for type " << typeid(T).name()
138  << abort(FatalError);
139  }
140 
141  label oldSize = this->size();
142 
143  if (newSize == 0)
144  {
145  clear();
146  }
147  else if (newSize < oldSize)
148  {
149  label i;
150  for (i=newSize; i<oldSize; i++)
151  {
152  if (this->ptrs_[i])
153  {
154  delete this->ptrs_[i];
155  }
156  }
157 
158  this->ptrs_.setSize(newSize);
159  }
160  else // newSize > oldSize
161  {
162  this->ptrs_.setSize(newSize);
163 
164  label i;
165  for (i=oldSize; i<newSize; i++)
166  {
167  this->ptrs_[i] = nullptr;
168  }
169  }
170 }
171 
172 
173 template<class T>
175 {
176  label j = 0;
177  forAll(*this, i)
178  {
179  if (this->ptrs_[i])
180  {
181  if (j != i)
182  {
183  this->ptrs_[j] = this->ptrs_[i];
184  }
185 
186  j++;
187  }
188  }
189 
190  if (j != this->size())
191  {
192  this->ptrs_.setSize(j);
193  }
194 }
195 
196 
197 template<class T>
199 {
200  forAll(*this, i)
201  {
202  if (this->ptrs_[i])
203  {
204  delete this->ptrs_[i];
205  }
206  }
207 
208  this->ptrs_.clear();
209 }
210 
211 
212 template<class T>
214 {
215  clear();
216  this->ptrs_.transfer(a.ptrs_);
217 }
218 
219 
220 template<class T>
222 {
223  if (oldToNew.size() != this->size())
224  {
226  << "Size of map (" << oldToNew.size()
227  << ") not equal to list size (" << this->size()
228  << ") for type " << typeid(T).name()
229  << abort(FatalError);
230  }
231 
232  List<T*> newPtrs_(this->ptrs_.size(), reinterpret_cast<T*>(0));
233 
234  forAll(*this, i)
235  {
236  label newI = oldToNew[i];
237 
238  if (newI < 0 || newI >= this->size())
239  {
241  << "Illegal index " << newI << nl
242  << "Valid indices are 0.." << this->size()-1
243  << " for type " << typeid(T).name()
244  << abort(FatalError);
245  }
246 
247  if (newPtrs_[newI])
248  {
250  << "reorder map is not unique; element " << newI
251  << " already set for type " << typeid(T).name()
252  << abort(FatalError);
253  }
254  newPtrs_[newI] = this->ptrs_[i];
255  }
256 
257  forAll(newPtrs_, i)
258  {
259  if (!newPtrs_[i])
260  {
262  << "Element " << i << " not set after reordering with type "
263  << typeid(T).name() << nl << abort(FatalError);
264  }
265  }
266 
267  this->ptrs_.transfer(newPtrs_);
268 }
269 
270 
271 template<class T>
273 {
274  List<T*> newPtrs_(newToOld.size(), reinterpret_cast<T*>(0));
275 
276  forAll(newToOld, newI)
277  {
278  label oldI = newToOld[newI];
279 
280  if (oldI >= 0 && oldI < this->size())
281  {
282  newPtrs_[newI] = this->ptrs_[oldI];
283  this->ptrs_[oldI] = nullptr;
284  }
285  }
286 
287  // Delete all remaining pointers
288  clear();
289 
290  // Take over new pointers
291  this->ptrs_.transfer(newPtrs_);
292 }
293 
294 
295 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
296 
297 template<class T>
299 {
300  if (this == &a)
301  {
303  << "attempted assignment to self for type " << typeid(T).name()
304  << abort(FatalError);
305  }
306 
307  if (this->size() == 0)
308  {
309  setSize(a.size());
310 
311  forAll(*this, i)
312  {
313  this->ptrs_[i] = (a[i]).clone().ptr();
314  }
315  }
316  else if (a.size() == this->size())
317  {
318  forAll(*this, i)
319  {
320  (*this)[i] = a[i];
321  }
322  }
323  else
324  {
326  << "bad size: " << a.size()
327  << " for type " << typeid(T).name()
328  << abort(FatalError);
329  }
330 }
331 
332 
333 template<class T>
335 {
336  if (this == &a)
337  {
339  << "attempted assignment to self for type " << typeid(T).name()
340  << abort(FatalError);
341  }
342 
343  transfer(a);
344 }
345 
346 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347 
348 #include "PtrListIO.C"
349 
350 // ************************************************************************* //
Non-intrusive singly-linked pointer list.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
const iterator & end()
Definition: LList.H:286
iterator begin()
Definition: LList.H:281
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:65
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrList.C:174
PtrList()
Null Constructor.
Definition: PtrList.C:32
void reorder(const labelUList &oldToNew)
Reorders elements. Ordering does not have to be done in.
Definition: PtrList.C:221
void shuffle(const labelUList &newToOld)
Reorders elements. Ordering does not have to be done in.
Definition: PtrList.C:272
~PtrList()
Destructor.
Definition: PtrList.C:116
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:198
void operator=(const PtrList< T > &)
Assignment operator.
Definition: PtrList.C:298
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:213
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
An STL-conforming const_iterator.
Definition: ULPtrList.H:187
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:66
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
tUEqn clear()
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
T clone(const T &t)
Definition: List.H:55
error FatalError
static const char nl
Definition: Ostream.H:266
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
points setSize(newPointi)