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-------------------------------------------------------------------------------
10License
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
34template<class T, int SizeMin>
36:
37 PtrList<T>(),
38 capacity_(0)
39{}
40
41
42template<class T, int SizeMin>
44:
45 PtrList<T>(len),
46 capacity_(len)
47{
49}
50
51
52template<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
63template<class T, int SizeMin>
65(
67)
68:
69 PtrList<T>(std::move(list)),
70 capacity_(list.capacity_)
71{
72 list.clearStorage();
73}
74
75
76template<class T, int SizeMin>
79 PtrList<T>&& list
80)
82 PtrList<T>(std::move(list)),
83 capacity_(PtrList<T>::size())
84{}
85
86
87template<class T, int SizeMin>
89:
90 PtrList<T>(list),
91 capacity_(PtrList<T>::size())
92{}
94
95// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96
97template<class T, int SizeMin>
99{
100 return capacity_;
101}
102
103
104template<class T, int SizeMin>
105inline void Foam::PtrDynList<T, SizeMin>::reserve(const label len)
106{
107 if (capacity_ < len)
108 {
109 // Preserve addressed size
110 const label currLen = PtrList<T>::size();
111
112 // Increase capacity (doubling)
113 capacity_ = max(SizeMin, max(len, label(2*capacity_)));
114
115 PtrList<T>::resize(capacity_);
117 }
118}
119
120
121template<class T, int SizeMin>
122inline void Foam::PtrDynList<T, SizeMin>::resize(const label newLen)
123{
124 auto& ptrs = this->ptrs_;
125
126 const label oldLen = ptrs.size();
127
128 if (capacity_ < newLen)
129 {
130 // Increase capacity (doubling)
131 capacity_ = max(SizeMin, max(newLen, label(2*capacity_)));
132
133 PtrList<T>::resize(capacity_);
135 else if (newLen != oldLen)
136 {
137 // Truncation frees old pointers
138 for (label i = newLen; i < oldLen; ++i)
139 {
140 delete ptrs[i];
141 ptrs[i] = nullptr;
142 }
143 }
144
145 // Adjust addressed size
147}
148
149
150template<class T, int SizeMin>
153 (this->ptrs_).free(); // free old pointers
155}
157
158template<class T, int SizeMin>
162 capacity_ = 0;
164
165
166template<class T, int SizeMin>
168{
169 const label currLen = PtrList<T>::size();
170
171 // Allow addressing into the entire list
173
174 return currLen;
176
177
178template<class T, int SizeMin>
180{
181 const label currLen = PtrList<T>::size();
182 if (currLen < capacity_)
183 {
184 // Adjust addressable size to trigger proper resizing
186
187 PtrList<T>::resize(currLen);
188 capacity_ = PtrList<T>::size();
189 }
190}
191
192
193template<class T, int SizeMin>
196 const label newLen = UPtrList<T>::squeezeNull();
197 resize(newLen);
198 return newLen;
199}
200
202template<class T, int SizeMin>
203template<int AnySizeMin>
205(
208{
209 if
211 static_cast<const PtrList<T>*>(this)
212 == static_cast<const PtrList<T>*>(&other)
214 {
215 return; // Self-swap is a no-op
216 }
217
218 // Swap storage and addressable size
220
221 // Swap capacity
222 std::swap(this->capacity_, other.capacity_);
223}
224
225
226template<class T, int SizeMin>
227template<class... Args>
230 this->append(new T(std::forward<Args>(args)...));
231}
233
234template<class T, int SizeMin>
237 const label idx = this->size();
238 resize(idx + 1);
239 this->ptrs_[idx] = ptr;
240}
241
242
243template<class T, int SizeMin>
245{
246 this->append(ptr.release());
247}
248
249
250template<class T, int SizeMin>
252{
253 this->append(ptr.release());
254}
255
256
257template<class T, int SizeMin>
258inline void Foam::PtrDynList<T, SizeMin>::append(std::unique_ptr<T>&& ptr)
259{
260 this->append(ptr.release());
261}
262
263
264template<class T, int SizeMin>
266{
267 this->append(ptr.ptr());
268}
269
270
271template<class T, int SizeMin>
273{
274 this->append(ptr.ptr());
275}
276
277
278template<class T, int SizeMin>
280{
281 const label idx = this->size();
282 const label len = other.size();
283
284 resize(idx + len);
285
286 for (label i = 0; i < len; ++i)
287 {
288 set(idx + i, other.release(i)); // Take pointer ownership
289 }
290
291 other.clear();
292}
293
294
295template<class T, int SizeMin>
296template<int AnySizeMin>
298(
300)
301{
302 if
303 (
304 static_cast<const PtrList<T>*>(this)
305 == static_cast<const PtrList<T>*>(&other)
306 )
307 {
309 << "Attempted append to self"
310 << abort(FatalError);
311 }
312
313 const label idx = this->size();
314 const label len = other.size();
315
316 resize(idx + len);
317
318 for (label i = 0; i < len; ++i)
319 {
320 set(idx + i, other.release(i)); // Take pointer ownership
321 }
322
323 other.clearStorage(); // Ensure capacity=0
324}
325
326
327template<class T, int SizeMin>
329{
330 // Location of last element and simultaneously the new size
331 const label idx = (this->size() - 1);
332
333 if (idx < 0)
334 {
335 return nullptr; // List is empty
336 }
337
338 autoPtr<T> old(this->ptrs_[idx]);
339 this->ptrs_[idx] = nullptr;
341
342 return old;
343}
344
345
346template<class T, int SizeMin>
347template<class... Args>
349(
350 const label i,
351 Args&&... args
352)
353{
354 return this->set(i, new T(std::forward<Args>(args)...));
355}
356
357
358template<class T, int SizeMin>
360(
361 const label i,
362 T* ptr
363)
364{
365 if (i >= this->size())
366 {
367 resize(i+1);
368 }
369
370 return autoPtr<T>(UPtrList<T>::set(i, ptr));
371}
372
373
374template<class T, int SizeMin>
376(
377 const label i,
378 autoPtr<T>& ptr
379)
380{
381 return this->set(i, ptr.release());
382}
383
384
385template<class T, int SizeMin>
387(
388 const label i,
389 autoPtr<T>&& ptr
390)
391{
392 return this->set(i, ptr.release());
393}
394
395
396template<class T, int SizeMin>
398(
399 const label i,
400 std::unique_ptr<T>&& ptr
401)
402{
403 return this->set(i, ptr.release());
404}
405
406
407template<class T, int SizeMin>
409(
410 const label i,
411 const refPtr<T>& ptr
412)
413{
414 return this->set(i, ptr.ptr());
415}
416
417
418template<class T, int SizeMin>
420(
421 const label i,
422 const tmp<T>& ptr
423)
424{
425 return this->set(i, ptr.ptr());
426}
427
428
429template<class T, int SizeMin>
431{
432 // Shrinking first is a bit annoying, but saves needing a special version.
433 shrink();
434 PtrList<T>::reorder(oldToNew);
435}
436
437
438// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
439
440template<class T, int SizeMin>
442(
443 const PtrList<T>& list
444)
445{
446 if (this == &list)
447 {
448 return; // Self-assignment is a no-op
449 }
450
452 capacity_ = PtrList<T>::size();
453}
454
455
456template<class T, int SizeMin>
458(
459 const PtrDynList<T, SizeMin>& list
460)
461{
462 if (this == &list)
463 {
464 return; // Self-assignment is a no-op
465 }
466
468 capacity_ = PtrList<T>::size();
469}
470
471
472template<class T, int SizeMin>
473template<int AnySizeMin>
475(
476 const PtrDynList<T, AnySizeMin>& list
477)
478{
479 if
480 (
481 static_cast<const PtrList<T>*>(this)
482 == static_cast<const PtrList<T>*>(&list)
483 )
484 {
485 return; // Self-assignment is a no-op
486 }
487
489 capacity_ = PtrList<T>::size();
490}
491
492
493template<class T, int SizeMin>
495(
496 PtrList<T>&& list
497)
498{
499 if (this == &list)
500 {
501 return; // Self-assignment is a no-op
502 }
503
505 capacity_ = PtrList<T>::size();
506 list.clearStorage();
507}
508
509
510template<class T, int SizeMin>
512(
514)
515{
516 if (this == &list)
517 {
518 return; // Self-assignment is a no-op
519 }
520
522 capacity_ = list.capacity();
523 list.clearStorage();
524}
525
526
527template<class T, int SizeMin>
528template<int AnySizeMin>
530(
532)
533{
534 if
535 (
536 static_cast<const PtrList<T>*>(this)
537 == static_cast<const PtrList<T>*>(&list)
538 )
539 {
540 return; // Self-assignment is a no-op
541 }
542
544 capacity_ = list.capacity();
545 list.clearStorage();
546}
547
548
549// ************************************************************************* //
virtual bool resize()
Resize the ODE solver.
Definition: Euler.C:53
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:64
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:179
constexpr PtrDynList() noexcept
Default construct.
Definition: PtrDynListI.H:35
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrDynListI.H:228
autoPtr< T > remove()
Remove and return the top element.
Definition: PtrDynListI.H:328
void clearStorage()
Clear the list and delete storage.
Definition: PtrDynListI.H:159
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynListI.H:98
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
Definition: PtrDynListI.H:167
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:430
label squeezeNull()
Definition: PtrDynListI.H:194
void reserve(const label len)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:105
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: PtrDynListI.H:151
void swap(PtrDynList< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
Definition: PtrDynListI.H:205
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:97
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:133
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
void setAddressableSize(const label n) noexcept
Set addressed size to be inconsistent with allocated storage.
Definition: UListI.H:413
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
void swap(UPtrList< T > &list)
Swap content.
Definition: UPtrListI.H:148
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers.
Definition: UPtrList.C:45
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:100
A class for managing references or pointers (no reference counting)
Definition: refPtr.H:58
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: refPtrI.H:260
A class for managing temporary objects.
Definition: tmp.H:65
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:255
bool set() const
Are all the vector set.
Definition: triadI.H:76
bool append() const noexcept
True if output format uses an append mode.
const volScalarField & T
patchWriters resize(patchIds.size())
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
errorManip< error > abort(error &err)
Definition: errorManip.H:144
const direction noexcept
Definition: Scalar.H:223
error FatalError
Foam::argList args(argc, argv)