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-2021 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 template<class T, int SizeMin>
35 inline constexpr Foam::PtrDynList<T, SizeMin>::PtrDynList() noexcept
36 :
37  PtrList<T>(),
38  capacity_(0)
39 {}
40 
41 
42 template<class T, int SizeMin>
44 :
45  PtrList<T>(len),
46  capacity_(len)
47 {
49 }
50 
51 
52 template<class T, int SizeMin>
54 (
55  const PtrDynList<T, SizeMin>& list
56 )
57 :
58  PtrList<T>(list),
59  capacity_(PtrList<T>::size())
60 {}
61 
62 
63 template<class T, int SizeMin>
65 (
67 )
68 :
69  PtrList<T>(std::move(list)),
70  capacity_(list.capacity_)
71 {
72  list.clearStorage();
73 }
74 
75 
76 template<class T, int SizeMin>
78 :
79  PtrList<T>(list),
80  capacity_(PtrList<T>::size())
81 {}
82 
83 
84 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
85 
86 template<class T, int SizeMin>
87 inline Foam::label Foam::PtrDynList<T, SizeMin>::capacity() const noexcept
88 {
89  return capacity_;
90 }
91 
92 
93 template<class T, int SizeMin>
94 inline const T* Foam::PtrDynList<T, SizeMin>::get(const label i) const
95 {
96  return (i >= 0 && i < PtrList<T>::size()) ? PtrList<T>::get(i) : nullptr;
97 }
98 
99 
100 template<class T, int SizeMin>
101 inline void Foam::PtrDynList<T, SizeMin>::reserve(const label len)
102 {
103  if (capacity_ < len)
104  {
105  // Preserve addressed size
106  const label currLen = PtrList<T>::size();
107 
108  // Increase capacity (doubling)
109  capacity_ = max(SizeMin, max(len, label(2*capacity_)));
110 
111  PtrList<T>::resize(capacity_);
113  }
114 }
115 
116 
117 template<class T, int SizeMin>
118 inline void Foam::PtrDynList<T, SizeMin>::resize(const label newLen)
119 {
120  auto& ptrs = this->ptrs_;
121 
122  const label oldLen = ptrs.size();
123 
124  if (capacity_ < newLen)
125  {
126  // Increase capacity (doubling)
127  capacity_ = max(SizeMin, max(newLen, label(2*capacity_)));
128 
129  PtrList<T>::resize(capacity_);
130  }
131  else if (newLen != oldLen)
132  {
133  // Truncation frees old pointers
134  for (label i = newLen; i < oldLen; ++i)
135  {
136  delete ptrs[i];
137  ptrs[i] = nullptr;
138  }
139  }
140 
141  // Adjust addressed size
143 }
144 
145 
146 template<class T, int SizeMin>
148 {
149  (this->ptrs_).free(); // free old pointers
151 }
152 
153 
154 template<class T, int SizeMin>
156 {
158  capacity_ = 0;
159 }
160 
161 
162 template<class T, int SizeMin>
163 inline Foam::label Foam::PtrDynList<T, SizeMin>::expandStorage() noexcept
164 {
165  const label currLen = PtrList<T>::size();
166 
167  // Allow addressing into the entire list
169 
170  return currLen;
171 }
172 
173 
174 template<class T, int SizeMin>
176 {
177  const label currLen = PtrList<T>::size();
178  if (currLen < capacity_)
179  {
180  // Adjust addressable size to trigger proper resizing
182 
183  PtrList<T>::resize(currLen);
184  capacity_ = PtrList<T>::size();
185  }
186 }
187 
188 
189 template<class T, int SizeMin>
191 {
192  const label newLen = UPtrList<T>::squeezeNull();
193  resize(newLen);
194  return newLen;
195 }
196 
197 
198 template<class T, int SizeMin>
199 template<int AnySizeMin>
201 (
203 )
204 {
205  // Cannot compare 'this' for different types, so use cdata()
206  if (this->cdata() == other.cdata())
207  {
208  return; // Self-swap is a no-op
209  }
210 
211  // Swap storage and addressable size
212  UPtrList<T>::swap(other);
213 
214  // Swap capacity
215  std::swap(this->capacity_, other.capacity_);
216 }
217 
218 
219 template<class T, int SizeMin>
220 template<class... Args>
222 {
223  this->append(new T(std::forward<Args>(args)...));
224 }
225 
226 
227 template<class T, int SizeMin>
229 {
230  const label idx = this->size();
231  resize(idx + 1);
232  this->ptrs_[idx] = ptr;
233 }
234 
235 
236 template<class T, int SizeMin>
238 {
239  this->append(ptr.release());
240 }
241 
242 
243 template<class T, int SizeMin>
245 {
246  this->append(ptr.release());
247 }
248 
249 
250 template<class T, int SizeMin>
251 inline void Foam::PtrDynList<T, SizeMin>::append(std::unique_ptr<T>&& ptr)
252 {
253  this->append(ptr.release());
254 }
255 
256 
257 template<class T, int SizeMin>
259 {
260  this->append(ptr.ptr());
261 }
262 
263 
264 template<class T, int SizeMin>
266 {
267  this->append(ptr.ptr());
268 }
269 
270 
271 template<class T, int SizeMin>
273 {
274  const label idx = this->size();
275  const label len = other.size();
276 
277  resize(idx + len);
278 
279  for (label i=0; i < len; ++i)
280  {
281  set(idx + i, other.release(i)); // moves pointer
282  }
283 
284  other.clear();
285 }
286 
287 
288 template<class T, int SizeMin>
289 template<int AnySizeMin>
291 (
293 )
294 {
295  const label idx = this->size();
296  const label len = other.size();
297 
298  resize(idx + len);
299 
300  for (label i=0; i < len; ++i)
301  {
302  set(idx + i, other.release(i)); // moves pointer
303  }
304 
305  other.clearStorage(); // Ensure capacity=0
306 }
307 
308 
309 template<class T, int SizeMin>
311 {
312  // Location of last element and simultaneously the new size
313  const label idx = (this->size() - 1);
314 
315  if (idx < 0)
316  {
317  return nullptr; // List is empty
318  }
319 
320  autoPtr<T> old(this->ptrs_[idx]);
321  this->ptrs_[idx] = nullptr;
323 
324  return old;
325 }
326 
327 
328 template<class T, int SizeMin>
329 template<class... Args>
331 (
332  const label i,
333  Args&&... args
334 )
335 {
336  return this->set(i, new T(std::forward<Args>(args)...));
337 }
338 
339 
340 template<class T, int SizeMin>
342 (
343  const label i,
344  T* ptr
345 )
346 {
347  if (i >= this->size())
348  {
349  resize(i+1);
350  }
351 
352  return autoPtr<T>(UPtrList<T>::set(i, ptr));
353 }
354 
355 
356 template<class T, int SizeMin>
358 (
359  const label i,
360  autoPtr<T>& ptr
361 )
362 {
363  return this->set(i, ptr.release());
364 }
365 
366 
367 template<class T, int SizeMin>
369 (
370  const label i,
371  autoPtr<T>&& ptr
372 )
373 {
374  return this->set(i, ptr.release());
375 }
376 
377 
378 template<class T, int SizeMin>
380 (
381  const label i,
382  std::unique_ptr<T>&& ptr
383 )
384 {
385  return this->set(i, ptr.release());
386 }
387 
388 
389 template<class T, int SizeMin>
391 (
392  const label i,
393  const refPtr<T>& ptr
394 )
395 {
396  return this->set(i, ptr.ptr());
397 }
398 
399 
400 template<class T, int SizeMin>
402 (
403  const label i,
404  const tmp<T>& ptr
405 )
406 {
407  return this->set(i, ptr.ptr());
408 }
409 
410 
411 template<class T, int SizeMin>
413 {
414  // Shrinking first is a bit annoying, but saves needing a special version.
415  shrink();
416  PtrList<T>::reorder(oldToNew);
417 }
418 
419 
420 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
421 
422 template<class T, int SizeMin>
424 (
425  const PtrList<T>& list
426 )
427 {
428  if (this == &list)
429  {
430  return; // Self-assignment is a no-op
431  }
432 
433  PtrList<T>::operator=(list);
434  capacity_ = PtrList<T>::size();
435 }
436 
437 
438 template<class T, int SizeMin>
440 (
441  const PtrDynList<T, SizeMin>& list
442 )
443 {
444  if (this == &list)
445  {
446  return; // Self-assignment is a no-op
447  }
448 
449  PtrList<T>::operator=(list);
450  capacity_ = PtrList<T>::size();
451 }
452 
453 
454 template<class T, int SizeMin>
455 template<int AnySizeMin>
457 (
458  const PtrDynList<T, AnySizeMin>& list
459 )
460 {
461  // Cannot compare 'this' for different types, so use cdata()
462  if (this->cdata() == list.cdata())
463  {
464  return; // Self-assignment is a no-op
465  }
466 
467  PtrList<T>::operator=(list);
468  capacity_ = PtrList<T>::size();
469 }
470 
471 
472 template<class T, int SizeMin>
474 (
475  PtrList<T>&& list
476 )
477 {
478  if (this == &list)
479  {
480  return; // Self-assignment is a no-op
481  }
482 
483  PtrList<T>::transfer(list);
484  capacity_ = PtrList<T>::size();
485  list.clearStorage();
486 }
487 
488 
489 template<class T, int SizeMin>
491 (
493 )
494 {
495  if (this == &list)
496  {
497  return; // Self-assignment is a no-op
498  }
499 
500  PtrList<T>::transfer(list);
501  capacity_ = list.capacity();
502  list.clearStorage();
503 }
504 
505 
506 template<class T, int SizeMin>
507 template<int AnySizeMin>
509 (
511 )
512 {
513  // Cannot compare 'this' for different types, so use cdata()
514  if (this->cdata() == list.cdata())
515  {
516  return; // Self-assignment is a no-op
517  }
518 
519  PtrList<T>::transfer(list);
520  capacity_ = list.capacity();
521  list.clearStorage();
522 }
523 
524 
525 // ************************************************************************* //
Foam::PtrDynList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr,.
Definition: PtrDynList.H:111
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::swap
void swap(PtrDynList< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
Definition: PtrDynListI.H:201
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
refPtr.H
Foam::PtrDynList::expandStorage
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
Definition: PtrDynListI.H:163
Foam::PtrDynList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynListI.H:87
Foam::PtrDynList::reorder
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:412
Foam::PtrDynList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:228
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:118
Foam::PtrDynList::shrink
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:175
Foam::PtrDynList::remove
autoPtr< T > remove()
Remove and return the top element.
Definition: PtrDynListI.H:310
resize
patchWriters resize(patchIds.size())
Foam::autoPtr::release
T * release() noexcept
Release ownership and return the pointer.
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:59
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:155
Foam::tmp::ptr
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:255
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:147
Foam::refPtr::ptr
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: refPtrI.H:260
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::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::PtrDynList::PtrDynList
constexpr PtrDynList() noexcept
Default construct.
Definition: PtrDynListI.H:35
Foam::PtrDynList::emplace_append
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrDynListI.H:221
Foam::PtrDynList::reserve
void reserve(const label len)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:101
args
Foam::argList args(argc, argv)
Foam::PtrDynList::squeezeNull
label squeezeNull()
Definition: PtrDynListI.H:190
Foam::PtrDynList::get
const T * get(const label i) const
Return const pointer to element (can be nullptr),.
Definition: PtrDynListI.H:94
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60
autoPtr.H