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-2019 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  forAll(*this, i)
177  {
178  if (this->ptrs_[i])
179  {
180  delete this->ptrs_[i];
181  }
182  }
183 
184  this->ptrs_.clear();
185 }
186 
187 
188 template<class T>
190 {
191  clear();
192  this->ptrs_.transfer(a.ptrs_);
193 }
194 
195 
196 template<class T>
198 {
199  if (oldToNew.size() != this->size())
200  {
202  << "Size of map (" << oldToNew.size()
203  << ") not equal to list size (" << this->size()
204  << ") for type " << typeid(T).name()
205  << abort(FatalError);
206  }
207 
208  List<T*> newPtrs_(this->ptrs_.size(), reinterpret_cast<T*>(0));
209 
210  forAll(*this, i)
211  {
212  label newI = oldToNew[i];
213 
214  if (newI < 0 || newI >= this->size())
215  {
217  << "Illegal index " << newI << nl
218  << "Valid indices are 0.." << this->size()-1
219  << " for type " << typeid(T).name()
220  << abort(FatalError);
221  }
222 
223  if (newPtrs_[newI])
224  {
226  << "reorder map is not unique; element " << newI
227  << " already set for type " << typeid(T).name()
228  << abort(FatalError);
229  }
230  newPtrs_[newI] = this->ptrs_[i];
231  }
232 
233  forAll(newPtrs_, i)
234  {
235  if (!newPtrs_[i])
236  {
238  << "Element " << i << " not set after reordering with type "
239  << typeid(T).name() << nl << abort(FatalError);
240  }
241  }
242 
243  this->ptrs_.transfer(newPtrs_);
244 }
245 
246 
247 template<class T>
249 {
250  List<T*> newPtrs_(newToOld.size(), reinterpret_cast<T*>(0));
251 
252  forAll(newToOld, newI)
253  {
254  label oldI = newToOld[newI];
255 
256  if (oldI >= 0 && oldI < this->size())
257  {
258  newPtrs_[newI] = this->ptrs_[oldI];
259  this->ptrs_[oldI] = nullptr;
260  }
261  }
262 
263  // Delete all remaining pointers
264  clear();
265 
266  // Take over new pointers
267  this->ptrs_.transfer(newPtrs_);
268 }
269 
270 
271 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
272 
273 template<class T>
275 {
276  if (this == &a)
277  {
279  << "attempted assignment to self for type " << typeid(T).name()
280  << abort(FatalError);
281  }
282 
283  if (this->size() == 0)
284  {
285  setSize(a.size());
286 
287  forAll(*this, i)
288  {
289  this->ptrs_[i] = (a[i]).clone().ptr();
290  }
291  }
292  else if (a.size() == this->size())
293  {
294  forAll(*this, i)
295  {
296  (*this)[i] = a[i];
297  }
298  }
299  else
300  {
302  << "bad size: " << a.size()
303  << " for type " << typeid(T).name()
304  << abort(FatalError);
305  }
306 }
307 
308 
309 template<class T>
311 {
312  if (this == &a)
313  {
315  << "attempted assignment to self for type " << typeid(T).name()
316  << abort(FatalError);
317  }
318 
319  transfer(a);
320 }
321 
322 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
323 
324 #include "PtrListIO.C"
325 
326 // ************************************************************************* //
void reorder(const labelUList &oldToNew)
Reorders elements. Ordering does not have to be done in.
Definition: PtrList.C:197
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
const iterator & end()
Definition: LList.H:286
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
An STL-conforming const_iterator.
Definition: LPtrList.H:221
points setSize(newPointi)
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:47
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.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
T clone(const T &t)
Definition: List.H:54
~PtrList()
Destructor.
Definition: PtrList.C:116
void operator=(const PtrList< T > &)
Assignment operator.
Definition: PtrList.C:274
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:54
errorManip< error > abort(error &err)
Definition: errorManip.H:131
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
static const char nl
Definition: Ostream.H:260
const volScalarField & T
Non-intrusive singly-linked pointer list.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:189
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:174
void shuffle(const labelUList &newToOld)
Reorders elements. Ordering does not have to be done in.
Definition: PtrList.C:248
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
PtrList()
Null Constructor.
Definition: PtrList.C:32