Loading...
Searching...
No Matches
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-2025 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>
37 PtrList<T>(),
38 capacity_(0)
39{}
40
41
42template<class T, int SizeMin>
43inline Foam::PtrDynList<T, SizeMin>::PtrDynList(const label len)
44:
45 PtrList<T>(),
46 capacity_(0)
47{
48 reserve(len);
49}
50
51
52template<class T, int SizeMin>
54(
55 std::pair<label,label> sizing
56)
57:
58 PtrList<T>(std::max(sizing.first, sizing.second)),
59 capacity_(PtrList<T>::size())
60{
62}
63
64
65template<class T, int SizeMin>
67(
68 const PtrDynList<T, SizeMin>& list
69)
71 PtrList<T>(list),
72 capacity_(PtrList<T>::size())
73{}
74
75
76template<class T, int SizeMin>
78(
80)
81:
82 PtrList<T>(std::move(list)),
83 capacity_(list.capacity())
85 list.setCapacity_unsafe(0); // All contents moved
86}
87
88
89template<class T, int SizeMin>
90template<int AnySizeMin>
92(
94)
95:
96 PtrList<T>(std::move(list)),
97 capacity_(list.capacity())
98{
99 list.setCapacity_unsafe(0); // All contents moved
100}
101
102
103template<class T, int SizeMin>
105(
106 PtrList<T>&& list
107) noexcept
109 PtrList<T>(std::move(list)),
110 capacity_(PtrList<T>::size())
111{}
112
113
114template<class T, int SizeMin>
116:
117 PtrList<T>(list),
118 capacity_(PtrList<T>::size())
119{}
120
121
122// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
123
124template<class T, int SizeMin>
125inline void Foam::PtrDynList<T, SizeMin>::reserve(const label len)
126{
127 if (capacity_ < len)
128 {
129 // Preserve addressed size
130 const label currLen = PtrList<T>::size();
131
132 // Consistent allocated sizing
134
135 // Increase capacity (eg, doubling)
136 capacity_ =
138
139 // No PtrList<T>::resize_copy(...) -> copying nullptr is cheap
140 PtrList<T>::resize(capacity_);
142 capacity_ = PtrList<T>::size();
144 }
145}
146
147
148template<class T, int SizeMin>
149inline void Foam::PtrDynList<T, SizeMin>::reserve_exact(const label len)
151 if (capacity_ < len)
152 {
153 // Preserve addressed size
154 const label currLen = PtrList<T>::size();
155
156 // Consistent allocated sizing
158
159 // No PtrList<T>::resize_copy(...) -> copying nullptr is cheap
161
162 capacity_ = PtrList<T>::size();
165}
166
167
168template<class T, int SizeMin>
169inline void Foam::PtrDynList<T, SizeMin>::resize(const label newLen)
170{
171 auto& ptrs = this->ptrs_;
172 const label oldLen = ptrs.size();
173
174 if (capacity_ < newLen)
175 {
176 // Extend list
177 this->reserve(newLen);
178 }
179 else if (newLen != oldLen)
180 {
181 // Truncation frees old pointers
182 for (label i = newLen; i < oldLen; ++i)
183 {
184 delete ptrs[i];
185 ptrs[i] = nullptr;
186 }
187 }
189 // Adjust addressed size
191}
192
193
194template<class T, int SizeMin>
195inline void Foam::PtrDynList<T, SizeMin>::resize_null(const label len)
196{
197 if (capacity_ < len)
198 {
199 // Consistent allocated sizing
201
202 // Increase capacity (eg, doubling)
203 capacity_ =
205
206 PtrList<T>::resize_null(capacity_);
207 }
208 else
209 {
210 PtrList<T>::free(); // Free (and nullify) old pointers
211 }
213 // Adjust addressed size
215}
216
217
218template<class T, int SizeMin>
221 PtrList<T>::free(); // Free (and nullify) old pointers
223}
224
225
226template<class T, int SizeMin>
228{
229 // Consistent allocated sizing
232 capacity_ = 0;
233}
234
235
236template<class T, int SizeMin>
238{
239 const label currLen = PtrList<T>::size();
240 if (currLen < capacity_)
241 {
244 capacity_ = PtrList<T>::size();
245 }
246}
247
248
249template<class T, int SizeMin>
251{
252 const label newLen = UPtrList<T>::squeezeNull();
254 return newLen;
255}
256
257
258template<class T, int SizeMin>
260{
261 if
262 (
264 (
265 static_cast<const PtrList<T>*>(this)
266 == static_cast<const PtrList<T>*>(&list)
268 )
269 {
270 return; // Self-swap is a no-op
271 }
272
273 // Remove unused storage
274 this->shrink_to_fit();
275
276 // Swap storage and addressable size
277 UPtrList<T>::swap(list);
278
279 // Update capacity
280 capacity_ = PtrList<T>::size();
281}
282
283
284template<class T, int SizeMin>
285template<int AnySizeMin>
287(
289) noexcept
290{
291 if
292 (
294 (
295 static_cast<const PtrList<T>*>(this)
296 == static_cast<const PtrList<T>*>(&other)
297 )
298 )
299 {
300 return; // Self-swap is a no-op
302
303 // Swap storage and addressable size
304 UPtrList<T>::swap(other);
306 // Swap capacity
307 std::swap(this->capacity_, other.capacity_);
308}
309
310
311template<class T, int SizeMin>
313{
314 // No check for self-assignment (different types)
315
316 // Consistent allocated sizing
319 capacity_ = PtrList<T>::size();
320}
321
322
323template<class T, int SizeMin>
324template<int AnySizeMin>
326(
328)
329{
330 if
331 (
333 (
334 static_cast<const PtrList<T>*>(this)
335 == static_cast<const PtrList<T>*>(&list)
336 )
337 )
338 {
339 return; // Self assignment is a no-op
340 }
341
342 // Consistent allocated sizing
344 PtrList<T>::transfer(static_cast<PtrList<T>&>(list));
345
346 capacity_ = list.capacity();
347 list.setCapacity_unsafe(0); // All contents moved
348}
349
350
351template<class T, int SizeMin>
352template<class... Args>
354{
355 T* ptr = new T(std::forward<Args>(args)...);
356 this->push_back(ptr);
357 return *ptr;
358}
359
360
361template<class T, int SizeMin>
363{
364 const label idx = this->size();
365 resize(idx + 1);
366 this->ptrs_[idx] = ptr;
367}
368
369
370template<class T, int SizeMin>
371inline void Foam::PtrDynList<T, SizeMin>::push_back(std::unique_ptr<T>&& ptr)
372{
373 this->push_back(ptr.release());
374}
375
376
377template<class T, int SizeMin>
379{
380 this->push_back(ptr.release());
381}
382
383
384template<class T, int SizeMin>
386{
387 this->push_back(ptr.ptr()); // release or clone
388}
389
390
391template<class T, int SizeMin>
393{
394 this->push_back(ptr.ptr()); // release or clone
395}
396
397
398template<class T, int SizeMin>
400{
401 const label idx = this->size();
402 const label len = other.size();
403
404 resize(idx + len);
405
406 for (label i = 0; i < len; ++i)
407 {
408 set(idx + i, other.release(i)); // Take pointer ownership
409 }
411 other.clear();
412}
413
414
415template<class T, int SizeMin>
416template<int AnySizeMin>
418(
420)
421{
422 if
423 (
425 (
426 static_cast<const PtrList<T>*>(this)
427 == static_cast<const PtrList<T>*>(&other)
428 )
429 )
430 {
432 << "Attempted push_back to self"
433 << abort(FatalError);
434 }
435
436 const label idx = this->size();
437 const label len = other.size();
438
439 resize(idx + len);
440
441 for (label i = 0; i < len; ++i)
442 {
443 set(idx + i, other.release(i)); // Take pointer ownership
445
446 other.clearStorage(); // Ensure capacity=0
447}
448
449
450template<class T, int SizeMin>
452{
453 if (n >= this->size())
454 {
455 this->clear();
456 }
457 else if (n > 0)
458 {
459 this->resize(this->size() - n);
460 }
461}
462
463
464template<class T, int SizeMin>
465template<class... Args>
467(
468 const label i,
469 Args&&... args
470)
471{
472 if (i >= this->size())
473 {
474 resize(i+1);
476 return PtrList<T>::emplace_set(i, std::forward<Args>(args)...);
477}
478
479
480template<class T, int SizeMin>
481template<class... Args>
483(
484 const label i,
485 Args&&... args
486)
488 return this->emplace_set(i, std::forward<Args>(args)...);
489}
490
491
492template<class T, int SizeMin>
493template<class... Args>
495(
496 const label i,
497 Args&&... args
498)
499{
500 if (i >= this->size())
501 {
502 resize(i+1);
503 }
504 return PtrList<T>::try_emplace(i, std::forward<Args>(args)...);
505}
506
507
508template<class T, int SizeMin>
510(
511 const label i,
512 T* ptr
513)
514{
515 if (i >= this->size())
516 {
517 resize(i+1);
519
520 return autoPtr<T>(UPtrList<T>::set(i, ptr));
521}
522
523
524template<class T, int SizeMin>
526(
527 const label i,
528 std::unique_ptr<T>&& ptr
530{
531 return this->set(i, ptr.release());
532}
533
534
535template<class T, int SizeMin>
537(
538 const label i,
539 autoPtr<T>&& ptr
541{
542 return this->set(i, ptr.release());
543}
544
545
546template<class T, int SizeMin>
548(
549 const label i,
550 const refPtr<T>& ptr
551)
553 return this->set(i, ptr.ptr()); // release or clone
554
555}
556
557
558template<class T, int SizeMin>
560(
561 const label i,
562 const tmp<T>& ptr
564{
565 return this->set(i, ptr.ptr()); // release or clone
566}
567
568
569template<class T, int SizeMin>
571(
572 Ostream& os,
573 const bool full
574) const
575{
576 if (full)
577 {
578 return this->ptrs_.printAddresses(os, capacity_);
579 }
580 else
581 {
582 return UPtrList<T>::printAddresses(os);
584}
585
586
587// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
588
589template<class T, int SizeMin>
591(
592 const UPtrList<T>& list
593)
594{
595 if
596 (
598 (
599 static_cast<const UPtrList<T>*>(this)
600 == static_cast<const UPtrList<T>*>(&list)
601 )
602 )
603 {
604 return; // Self-assignment is a no-op
605 }
607 this->resize(list.size());
609}
610
611
612template<class T, int SizeMin>
614(
615 const PtrDynList<T, SizeMin>& list
616)
617{
618 if
619 (
621 (
622 static_cast<const UPtrList<T>*>(this)
623 == static_cast<const UPtrList<T>*>(&list)
624 )
625 )
626 {
627 return; // Self-assignment is a no-op
628 }
629
630 this->resize(list.size());
631 PtrList<T>::operator=(static_cast<UPtrList<T>&>(list));
632}
633
634
635template<class T, int SizeMin>
636template<int AnySizeMin>
638(
639 const PtrDynList<T, AnySizeMin>& list
640)
641{
642 if
643 (
645 (
646 static_cast<const PtrList<T>*>(this)
647 == static_cast<const PtrList<T>*>(&list)
648 )
649 )
650 {
651 return; // Self-assignment is a no-op
652 }
653
654 this->resize(list.size());
655 PtrList<T>::operator=(static_cast<UPtrList<T>&>(list));
656 capacity_ = PtrList<T>::size();
657}
658
659
660template<class T, int SizeMin>
662(
663 PtrList<T>&& list
665{
666 this->transfer(list);
667}
668
669
670template<class T, int SizeMin>
672(
674)
676 this->transfer(list);
677}
678
679
680template<class T, int SizeMin>
681template<int AnySizeMin>
683(
685)
686{
687 this->transfer(list);
688}
689
690
691// ************************************************************************* //
label n
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A dynamically resizable PtrList with allocation management.
Definition PtrDynList.H:58
T & emplace(const label i, Args &&... args)
Same as emplace_set().
void transfer(PtrList< T > &list)
Transfer contents of the argument PtrList into this.
constexpr PtrDynList() noexcept
Default construct.
Definition PtrDynListI.H:28
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Ostream & printAddresses(Ostream &os, const bool full=false) const
Print pointer addresses to Ostream (debugging only).
void push_back(T *ptr)
Append an element to the end of the list.
T & emplace_back(Args &&... args)
Construct an element at the end of the list, return reference to the new list element.
void setCapacity_unsafe(label len) noexcept
Change the value for the list capacity directly (ADVANCED, UNSAFE) Does not perform any memory manage...
Definition PtrDynList.H:134
void swap(PtrList< T > &list)
Swap with plain PtrList content. Implies shrink_to_fit().
void reserve_exact(const label len)
Reserve allocation space for at least this size. If allocation is required, uses the specified size w...
void shrink_to_fit()
Shrink the allocated space to the number of elements used.
T & emplace_set(const label i, Args &&... args)
Construct and set a new element at given position, (discard old element at that location)....
void clearStorage()
Clear the list and delete storage.
void resize_null(const label len)
Set the addressed list to the given size, deleting all existing entries. Afterwards the list contains...
autoPtr< T > set(const label i, T *ptr)
Set element to given pointer and return old element (can be null). Auto-sizes list as required.
T & try_emplace(const label i, Args &&... args)
Like emplace_set() but will not overwrite an occupied location.
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers and adjust the addressable size acco...
void resize(const label len)
Alter the addressed list size.
void reserve(const label len)
Reserve allocation space for at least this size.
void clear()
Clear the addressed list, i.e. set the size to zero.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition PtrListI.H:289
void operator=(const UPtrList< T > &list)
Copy assignment.
Definition PtrListI.H:304
T & emplace_set(const label i, Args &&... args)
Construct and set a new element at given position, (discard old element at that location).
Definition PtrListI.H:191
T & try_emplace(const label i, Args &&... args)
Like emplace_set() but will not overwrite an occupied (non-null) location.
Definition PtrListI.H:210
void free()
Free memory and nullify all entries. Does not change the list size.
Definition PtrListI.H:106
void resize_null(const label newLen)
Set the addressed list to the given size, deleting all existing entries. Afterwards the list contains...
Definition PtrListI.H:113
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition PtrListI.H:98
constexpr PtrList() noexcept
Default construct.
Definition PtrListI.H:29
void resize(const label newLen)
Adjust size of PtrList.
Definition PtrList.C:124
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:89
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie,...
Definition UPtrList.H:366
T & first()
Reference to the first element of the list.
Definition UPtrList.H:849
Ostream & printAddresses(Ostream &os) const
Print pointer addresses to Ostream (debugging only).
Definition UPtrList.C:167
label size() const noexcept
The number of entries in the list.
Definition UPtrListI.H:106
label squeezeNull()
Squeeze out nullptr entries in the list of pointers after which any null pointers will be at the end ...
Definition UPtrList.C:38
Detail::PtrListDetail< Foam::Field< label > > ptrs_
Definition UPtrList.H:109
void setAddressableSize(const label n) noexcept
Adjust addressable size.
Definition UPtrListI.H:25
void swap(UPtrList< T > &list) noexcept
Swap content.
Definition UPtrListI.H:209
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition refPtrI.H:280
A class for managing temporary objects.
Definition tmp.H:75
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition tmpI.H:256
const volScalarField & T
patchWriters resize(patchIds.size())
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
surface1 clear()
IntType reserve_size(IntType requested, IntType capacity) noexcept
Calculate a reserve size (eg, doubling) based on the requested length and the current capacity.
Definition ListPolicy.H:293
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
triangles reserve(surf.size())
Foam::argList args(argc, argv)
#define FOAM_UNLIKELY(cond)
Definition stdFoam.H:64