PtrDynListI.H
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) 2018-2019 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 "autoPtr.H"
29 #include "tmp.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class T, int SizeMin>
35 {
36  (this->ptrs_).setAddressableSize(len);
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
42 template<class T, int SizeMin>
43 inline constexpr Foam::PtrDynList<T, SizeMin>::PtrDynList() noexcept
44 :
45  PtrList<T>(),
46  capacity_(0)
47 {}
48 
49 
50 template<class T, int SizeMin>
52 :
53  PtrList<T>(len),
54  capacity_(len)
55 {
56  setAddressableSize(0);
57 }
58 
59 
60 template<class T, int SizeMin>
62 (
63  const PtrDynList<T, SizeMin>& list
64 )
65 :
66  PtrList<T>(list),
67  capacity_(PtrList<T>::size())
68 {}
69 
70 
71 template<class T, int SizeMin>
73 (
75 )
76 :
77  PtrList<T>(std::move(list)),
78  capacity_(list.capacity_)
79 {
80  list.clearStorage();
81 }
82 
83 
84 template<class T, int SizeMin>
86 :
87  PtrList<T>(list),
88  capacity_(PtrList<T>::size())
89 {}
90 
91 
92 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 
94 template<class T, int SizeMin>
96 {
97  return capacity_;
98 }
99 
100 
101 template<class T, int SizeMin>
103 {
104  label nextFree = PtrList<T>::size();
105  capacity_ = nElem;
106 
107  if (nextFree > capacity_)
108  {
109  // Truncate addressed sizes too
110  nextFree = capacity_;
111  }
112 
113  PtrList<T>::resize(capacity_);
114  setAddressableSize(nextFree);
115 }
116 
117 
118 template<class T, int SizeMin>
120 {
121  if (nElem > capacity_)
122  {
123  // Allocate more capacity if necessary
124 
125  capacity_ = max(SizeMin, max(nElem, label(2*capacity_)));
126 
127  // Adjust allocated size, leave addressed size untouched
128  const label nextFree = PtrList<T>::size();
129  PtrList<T>::resize(capacity_);
130  setAddressableSize(nextFree);
131  }
132 }
133 
134 
135 template<class T, int SizeMin>
137 {
138  auto& ptrs = this->ptrs_;
139 
140  const label oldLen = ptrs.size();
141 
142  if (newLen > capacity_)
143  {
144  // Allocate more capacity if necessary
145  capacity_ = max(SizeMin, max(newLen, label(2*capacity_)));
146 
147  PtrList<T>::resize(capacity_);
148  }
149  else if (newLen != oldLen)
150  {
151  // Truncation frees old pointers
152  for (label i=newLen; i<oldLen; ++i)
153  {
154  T* ptr = ptrs[i];
155 
156  if (ptr)
157  {
158  delete ptr;
159  }
160 
161  ptrs[i] = nullptr;
162  }
163  }
164 
165  setAddressableSize(newLen);
166 }
167 
168 
169 template<class T, int SizeMin>
171 {
172  this->resize(newLen);
173 }
174 
175 
176 template<class T, int SizeMin>
178 {
179  (this->ptrs_).free(); // free old pointers
180  setAddressableSize(0);
181 }
182 
183 
184 template<class T, int SizeMin>
186 {
188  capacity_ = 0;
189 }
190 
191 
192 template<class T, int SizeMin>
194 {
195  const label nextFree = PtrList<T>::size();
196 
197  // Allow addressing into the entire list
198  setAddressableSize(capacity_);
199 
200  return nextFree;
201 }
202 
203 
204 template<class T, int SizeMin>
206 {
207  label nextFree = PtrList<T>::size();
208  if (capacity_ > nextFree)
209  {
210  // Use the full list when resizing
211  setAddressableSize(capacity_);
212 
213  // The new size
214  capacity_ = nextFree;
215  PtrList<T>::resize(capacity_);
216  setAddressableSize(nextFree);
217  }
218 }
219 
220 
221 template<class T, int SizeMin>
223 {
224  const label newLen = UPtrList<T>::squeezeNull();
225  resize(newLen);
226  return newLen;
227 }
228 
229 
230 template<class T, int SizeMin>
232 {
233  const label idx = this->size();
234  resize(idx + 1);
235  this->ptrs_[idx] = ptr;
236 }
237 
238 
239 template<class T, int SizeMin>
241 {
242  this->append(const_cast<autoPtr<T>&>(aptr).release());
243 }
244 
245 
246 template<class T, int SizeMin>
248 {
249  this->append(tptr.ptr());
250 }
251 
252 
253 template<class T, int SizeMin>
255 {
256  const label idx = this->size();
257  const label len = other.size();
258 
259  resize(idx + len);
260 
261  for (label i=0; i < len; ++i)
262  {
263  set(idx + i, other.release(i)); // moves pointer
264  }
265 
266  other.clear();
267 }
268 
269 
270 template<class T, int SizeMin>
271 template<int AnySizeMin>
273 (
275 )
276 {
277  const label idx = this->size();
278  const label len = other.size();
279 
280  resize(idx + len);
281 
282  for (label i=0; i < len; ++i)
283  {
284  set(idx + i, other.release(i)); // moves pointer
285  }
286 
287  other.clearStorage(); // Ensure capacity=0
288 }
289 
290 
291 template<class T, int SizeMin>
293 {
294  // Location of last element and simultaneously the new size
295  const label idx = (this->size() - 1);
296 
297  if (idx < 0)
298  {
299  return nullptr; // List is empty
300  }
301 
302  autoPtr<T> old(this->ptrs_[idx]);
303  this->ptrs_[idx] = nullptr;
304  setAddressableSize(idx);
305 
306  return old;
307 }
308 
309 
310 template<class T, int SizeMin>
311 inline const T* Foam::PtrDynList<T, SizeMin>::set(const label i) const
312 {
313  return (i >= 0 && i < PtrList<T>::size()) ? PtrList<T>::set(i) : nullptr;
314 }
315 
316 
317 template<class T, int SizeMin>
319 (
320  const label i,
321  T* ptr
322 )
323 {
324  if (i >= this->size())
325  {
326  resize(i+1);
327  }
328 
329  return autoPtr<T>(UPtrList<T>::set(i, ptr));
330 }
331 
332 
333 template<class T, int SizeMin>
335 (
336  const label i,
337  const autoPtr<T>& aptr
338 )
339 {
340  return this->set(i, const_cast<autoPtr<T>&>(aptr).release());
341 }
342 
343 
344 template<class T, int SizeMin>
346 (
347  const label i,
348  const tmp<T>& tptr
349 )
350 {
351  return this->set(i, tptr.ptr());
352 }
353 
354 
355 template<class T, int SizeMin>
357 {
358  // Shrinking first is a bit annoying, but saves needing a special version.
359  shrink();
360  PtrList<T>::reorder(oldToNew);
361 }
362 
363 
364 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
365 
366 template<class T, int SizeMin>
368 (
369  const PtrList<T>& list
370 )
371 {
372  if (this == &list)
373  {
374  return; // Self-assignment is a no-op
375  }
376 
377  PtrList<T>::operator=(list);
378  capacity_ = PtrList<T>::size();
379 }
380 
381 
382 template<class T, int SizeMin>
384 (
385  const PtrDynList<T, SizeMin>& list
386 )
387 {
388  if (this == &list)
389  {
390  return; // Self-assignment is a no-op
391  }
392 
393  PtrList<T>::operator=(list);
394  capacity_ = PtrList<T>::size();
395 }
396 
397 
398 template<class T, int SizeMin>
399 template<int AnySizeMin>
401 (
402  const PtrDynList<T, AnySizeMin>& list
403 )
404 {
405  if (this == &list)
406  {
407  return; // Self-assignment is a no-op
408  }
409 
410  PtrList<T>::operator=(list);
411  capacity_ = PtrList<T>::size();
412 }
413 
414 
415 template<class T, int SizeMin>
417 (
418  PtrList<T>&& list
419 )
420 {
421  if (this == &list)
422  {
423  return; // Self-assignment is a no-op
424  }
425 
426  PtrList<T>::transfer(list);
427  capacity_ = PtrList<T>::size();
428  list.clearStorage();
429 }
430 
431 
432 template<class T, int SizeMin>
434 (
436 )
437 {
438  if (this == &list)
439  {
440  return; // Self-assignment is a no-op
441  }
442 
443  PtrList<T>::transfer(list);
444  capacity_ = list.capacity();
445  list.clearStorage();
446 }
447 
448 
449 template<class T, int SizeMin>
450 template<int AnySizeMin>
452 (
454 )
455 {
456  if (this == &list)
457  {
458  return; // Self-assignment is a no-op
459  }
460 
461  PtrList<T>::transfer(list);
462  capacity_ = list.capacity();
463  list.clearStorage();
464 }
465 
466 
467 // ************************************************************************* //
Foam::PtrDynList::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:119
Foam::PtrDynList::setSize
void setSize(const label newLen)
Same as resize()
Definition: PtrDynListI.H:170
Foam::PtrDynList::expandStorage
label expandStorage()
Expand the addressable size to fit the allocated capacity.
Definition: PtrDynListI.H:193
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::PtrDynList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynListI.H:95
Foam::PtrDynList::reorder
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:356
Foam::PtrDynList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:231
Foam::PtrDynList
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:53
Foam::PtrDynList::resize
void resize(const label newLen)
Alter the addressed list size.
Definition: PtrDynListI.H:136
Foam::PtrDynList::shrink
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:205
Foam::PtrDynList::remove
autoPtr< T > remove()
Remove and return the top element.
Definition: PtrDynListI.H:292
Foam::PtrDynList::setCapacity
void setCapacity(const label nElem)
Alter the size of the underlying storage.
Definition: PtrDynListI.H:102
Foam::label
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:62
resize
patchWriters resize(patchIds.size())
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:63
Foam::reorder
ListType reorder(const labelUList &oldToNew, const ListType &input, const bool prune=false)
Reorder the elements of a list.
Definition: ListOpsTemplates.C:80
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:65
Foam::PtrDynList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr.
Definition: PtrDynListI.H:311
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::PtrDynList::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: PtrDynListI.H:185
Foam::tmp::ptr
T * ptr() const
Definition: tmpI.H:296
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::PtrDynList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: PtrDynListI.H:177
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
tmp.H
clear
patchWriters clear()
Foam::UList
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:103
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Foam::PtrDynList::PtrDynList
constexpr PtrDynList() noexcept
Construct null.
Definition: PtrDynListI.H:43
Foam::PtrDynList::squeezeNull
label squeezeNull()
Definition: PtrDynListI.H:222
autoPtr.H