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-2020 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 "refPtr.H"
30 #include "tmp.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class T, int SizeMin>
35 inline void Foam::PtrDynList<T, SizeMin>::setAddressableSize(const label len)
36 {
37  (this->ptrs_).setAddressableSize(len);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 
43 template<class T, int SizeMin>
44 inline constexpr Foam::PtrDynList<T, SizeMin>::PtrDynList() noexcept
45 :
46  PtrList<T>(),
47  capacity_(0)
48 {}
49 
50 
51 template<class T, int SizeMin>
53 :
54  PtrList<T>(len),
55  capacity_(len)
56 {
57  setAddressableSize(0);
58 }
59 
60 
61 template<class T, int SizeMin>
63 (
64  const PtrDynList<T, SizeMin>& list
65 )
66 :
67  PtrList<T>(list),
68  capacity_(PtrList<T>::size())
69 {}
70 
71 
72 template<class T, int SizeMin>
74 (
76 )
77 :
78  PtrList<T>(std::move(list)),
79  capacity_(list.capacity_)
80 {
81  list.clearStorage();
82 }
83 
84 
85 template<class T, int SizeMin>
87 :
88  PtrList<T>(list),
89  capacity_(PtrList<T>::size())
90 {}
91 
92 
93 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
94 
95 template<class T, int SizeMin>
96 inline Foam::label Foam::PtrDynList<T, SizeMin>::capacity() const noexcept
97 {
98  return capacity_;
99 }
100 
101 
102 template<class T, int SizeMin>
103 inline const T* Foam::PtrDynList<T, SizeMin>::get(const label i) const
104 {
105  return (i >= 0 && i < PtrList<T>::size()) ? PtrList<T>::get(i) : nullptr;
106 }
107 
108 
109 template<class T, int SizeMin>
110 inline void Foam::PtrDynList<T, SizeMin>::setCapacity(const label nElem)
111 {
112  label nextFree = PtrList<T>::size();
113  capacity_ = nElem;
114 
115  if (nextFree > capacity_)
116  {
117  // Truncate addressed sizes too
118  nextFree = capacity_;
119  }
120 
121  PtrList<T>::resize(capacity_);
122  setAddressableSize(nextFree);
123 }
124 
125 
126 template<class T, int SizeMin>
127 inline void Foam::PtrDynList<T, SizeMin>::reserve(const label nElem)
128 {
129  if (nElem > capacity_)
130  {
131  // Allocate more capacity if necessary
132 
133  capacity_ = max(SizeMin, max(nElem, label(2*capacity_)));
134 
135  // Adjust allocated size, leave addressed size untouched
136  const label nextFree = PtrList<T>::size();
137  PtrList<T>::resize(capacity_);
138  setAddressableSize(nextFree);
139  }
140 }
141 
142 
143 template<class T, int SizeMin>
144 inline void Foam::PtrDynList<T, SizeMin>::resize(const label newLen)
145 {
146  auto& ptrs = this->ptrs_;
147 
148  const label oldLen = ptrs.size();
149 
150  if (newLen > capacity_)
151  {
152  // Allocate more capacity if necessary
153  capacity_ = max(SizeMin, max(newLen, label(2*capacity_)));
154 
155  PtrList<T>::resize(capacity_);
156  }
157  else if (newLen != oldLen)
158  {
159  // Truncation frees old pointers
160  for (label i=newLen; i<oldLen; ++i)
161  {
162  delete ptrs[i];
163  ptrs[i] = nullptr;
164  }
165  }
166 
167  setAddressableSize(newLen);
168 }
169 
170 
171 template<class T, int SizeMin>
172 inline void Foam::PtrDynList<T, SizeMin>::setSize(const label newLen)
173 {
174  this->resize(newLen);
175 }
176 
177 
178 template<class T, int SizeMin>
180 {
181  (this->ptrs_).free(); // free old pointers
182  setAddressableSize(0);
183 }
184 
185 
186 template<class T, int SizeMin>
188 {
190  capacity_ = 0;
191 }
192 
193 
194 template<class T, int SizeMin>
196 {
197  const label nextFree = PtrList<T>::size();
198 
199  // Allow addressing into the entire list
200  setAddressableSize(capacity_);
201 
202  return nextFree;
203 }
204 
205 
206 template<class T, int SizeMin>
208 {
209  label nextFree = PtrList<T>::size();
210  if (capacity_ > nextFree)
211  {
212  // Use the full list when resizing
213  setAddressableSize(capacity_);
214 
215  // The new size
216  capacity_ = nextFree;
217  PtrList<T>::resize(capacity_);
218  setAddressableSize(nextFree);
219  }
220 }
221 
222 
223 template<class T, int SizeMin>
225 {
226  const label newLen = UPtrList<T>::squeezeNull();
227  resize(newLen);
228  return newLen;
229 }
230 
231 
232 template<class T, int SizeMin>
233 template<class... Args>
235 {
236  this->append(new T(std::forward<Args>(args)...));
237 }
238 
239 
240 template<class T, int SizeMin>
242 {
243  const label idx = this->size();
244  resize(idx + 1);
245  this->ptrs_[idx] = ptr;
246 }
247 
248 
249 template<class T, int SizeMin>
251 {
252  this->append(ptr.release());
253 }
254 
255 
256 template<class T, int SizeMin>
258 {
259  this->append(ptr.release());
260 }
261 
262 
263 template<class T, int SizeMin>
264 inline void Foam::PtrDynList<T, SizeMin>::append(std::unique_ptr<T>&& ptr)
265 {
266  this->append(ptr.release());
267 }
268 
269 
270 template<class T, int SizeMin>
272 {
273  this->append(ptr.ptr());
274 }
275 
276 
277 template<class T, int SizeMin>
279 {
280  this->append(ptr.ptr());
281 }
282 
283 
284 template<class T, int SizeMin>
286 {
287  const label idx = this->size();
288  const label len = other.size();
289 
290  resize(idx + len);
291 
292  for (label i=0; i < len; ++i)
293  {
294  set(idx + i, other.release(i)); // moves pointer
295  }
296 
297  other.clear();
298 }
299 
300 
301 template<class T, int SizeMin>
302 template<int AnySizeMin>
304 (
306 )
307 {
308  const label idx = this->size();
309  const label len = other.size();
310 
311  resize(idx + len);
312 
313  for (label i=0; i < len; ++i)
314  {
315  set(idx + i, other.release(i)); // moves pointer
316  }
317 
318  other.clearStorage(); // Ensure capacity=0
319 }
320 
321 
322 template<class T, int SizeMin>
324 {
325  // Location of last element and simultaneously the new size
326  const label idx = (this->size() - 1);
327 
328  if (idx < 0)
329  {
330  return nullptr; // List is empty
331  }
332 
333  autoPtr<T> old(this->ptrs_[idx]);
334  this->ptrs_[idx] = nullptr;
335  setAddressableSize(idx);
336 
337  return old;
338 }
339 
340 
341 template<class T, int SizeMin>
342 template<class... Args>
344 (
345  const label i,
346  Args&&... args
347 )
348 {
349  return this->set(i, new T(std::forward<Args>(args)...));
350 }
351 
352 
353 template<class T, int SizeMin>
355 (
356  const label i,
357  T* ptr
358 )
359 {
360  if (i >= this->size())
361  {
362  resize(i+1);
363  }
364 
365  return autoPtr<T>(UPtrList<T>::set(i, ptr));
366 }
367 
368 
369 template<class T, int SizeMin>
371 (
372  const label i,
373  autoPtr<T>& ptr
374 )
375 {
376  return this->set(i, ptr.release());
377 }
378 
379 
380 template<class T, int SizeMin>
382 (
383  const label i,
384  autoPtr<T>&& ptr
385 )
386 {
387  return this->set(i, ptr.release());
388 }
389 
390 
391 template<class T, int SizeMin>
393 (
394  const label i,
395  std::unique_ptr<T>&& ptr
396 )
397 {
398  return this->set(i, ptr.release());
399 }
400 
401 
402 template<class T, int SizeMin>
404 (
405  const label i,
406  const refPtr<T>& ptr
407 )
408 {
409  return this->set(i, ptr.ptr());
410 }
411 
412 
413 template<class T, int SizeMin>
415 (
416  const label i,
417  const tmp<T>& ptr
418 )
419 {
420  return this->set(i, ptr.ptr());
421 }
422 
423 
424 template<class T, int SizeMin>
426 {
427  // Shrinking first is a bit annoying, but saves needing a special version.
428  shrink();
429  PtrList<T>::reorder(oldToNew);
430 }
431 
432 
433 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
434 
435 template<class T, int SizeMin>
437 (
438  const PtrList<T>& list
439 )
440 {
441  if (this == &list)
442  {
443  return; // Self-assignment is a no-op
444  }
445 
446  PtrList<T>::operator=(list);
447  capacity_ = PtrList<T>::size();
448 }
449 
450 
451 template<class T, int SizeMin>
453 (
454  const PtrDynList<T, SizeMin>& list
455 )
456 {
457  if (this == &list)
458  {
459  return; // Self-assignment is a no-op
460  }
461 
462  PtrList<T>::operator=(list);
463  capacity_ = PtrList<T>::size();
464 }
465 
466 
467 template<class T, int SizeMin>
468 template<int AnySizeMin>
470 (
471  const PtrDynList<T, AnySizeMin>& list
472 )
473 {
474  // Cannot compare 'this' for different types, so use cdata()
475  if (this->cdata() == list.cdata())
476  {
477  return; // Self-assignment is a no-op
478  }
479 
480  PtrList<T>::operator=(list);
481  capacity_ = PtrList<T>::size();
482 }
483 
484 
485 template<class T, int SizeMin>
487 (
488  PtrList<T>&& list
489 )
490 {
491  if (this == &list)
492  {
493  return; // Self-assignment is a no-op
494  }
495 
496  PtrList<T>::transfer(list);
497  capacity_ = PtrList<T>::size();
498  list.clearStorage();
499 }
500 
501 
502 template<class T, int SizeMin>
504 (
506 )
507 {
508  if (this == &list)
509  {
510  return; // Self-assignment is a no-op
511  }
512 
513  PtrList<T>::transfer(list);
514  capacity_ = list.capacity();
515  list.clearStorage();
516 }
517 
518 
519 template<class T, int SizeMin>
520 template<int AnySizeMin>
522 (
524 )
525 {
526  // Cannot compare 'this' for different types, so use cdata()
527  if (this->cdata() == list.cdata())
528  {
529  return; // Self-assignment is a no-op
530  }
531 
532  PtrList<T>::transfer(list);
533  capacity_ = list.capacity();
534  list.clearStorage();
535 }
536 
537 
538 // ************************************************************************* //
Foam::PtrDynList::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:127
Foam::PtrDynList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr,.
Definition: PtrDynList.H:117
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::PtrDynList::setSize
void setSize(const label newLen)
Same as resize()
Definition: PtrDynListI.H:172
Foam::PtrDynList::expandStorage
label expandStorage()
Expand the addressable size to fit the allocated capacity.
Definition: PtrDynListI.H:195
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
refPtr.H
Foam::PtrDynList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynListI.H:96
Foam::PtrDynList::reorder
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:425
Foam::PtrDynList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:241
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Foam::PtrDynList
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:54
Foam::PtrDynList::resize
void resize(const label newLen)
Alter the addressed list size.
Definition: PtrDynListI.H:144
Foam::PtrDynList::shrink
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:207
Foam::PtrDynList::remove
autoPtr< T > remove()
Remove and return the top element.
Definition: PtrDynListI.H:323
Foam::PtrDynList::setCapacity
void setCapacity(const label nElem)
Alter the size of the underlying storage.
Definition: PtrDynListI.H:110
resize
patchWriters resize(patchIds.size())
Foam::autoPtr::release
T * release() noexcept
Return pointer to the managed object and release ownership.
Definition: autoPtrI.H:100
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:62
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:62
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:187
Foam::tmp::ptr
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:259
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:179
Foam::refPtr::ptr
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: refPtrI.H:235
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
Foam::PtrDynList::emplace
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
Foam::PtrDynList::PtrDynList
constexpr PtrDynList() noexcept
Construct null.
Definition: PtrDynListI.H:44
Foam::PtrDynList::emplace_append
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrDynListI.H:234
args
Foam::argList args(argc, argv)
Foam::PtrDynList::squeezeNull
label squeezeNull()
Definition: PtrDynListI.H:224
Foam::PtrDynList::get
const T * get(const label i) const
Return const pointer to element (can be nullptr),.
Definition: PtrDynListI.H:103
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60
autoPtr.H