PtrDynList.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 -------------------------------------------------------------------------------
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 Class
27  Foam::PtrDynList
28 
29 Description
30  A dynamically resizable PtrList with allocation management.
31 
32 See Also
33  Foam::UPtrList
34  Foam::PtrList
35 
36 SourceFiles
37  PtrDynListI.H
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef Foam_PtrDynList_H
42 #define Foam_PtrDynList_H
43 
44 #include "PtrList.H"
45 #include <type_traits>
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 template<class T, int SizeMin> class PtrDynList;
54 
55 /*---------------------------------------------------------------------------*\
56  Class PtrDynList Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 template<class T, int SizeMin=64>
60 class PtrDynList
61 :
62  public PtrList<T>
63 {
64  static_assert(SizeMin > 0, "Invalid min size parameter");
65 
66  // Private Data
67 
68  //- The capacity (allocated size) of the list.
69  label capacity_;
70 
71 
72 public:
73 
74  // Constructors
75 
76  //- Default construct
77  inline constexpr PtrDynList() noexcept;
78 
79  //- Construct with given initial capacity
80  inline explicit PtrDynList(const label len);
81 
82  //- Construct with given size and capacity
83  inline explicit PtrDynList(const std::pair<label,label>& sizing);
84 
85  //- Copy construct using 'clone()' method on each element
86  inline PtrDynList(const PtrDynList<T, SizeMin>& list);
87 
88  //- Move construct
89  inline PtrDynList(PtrDynList<T, SizeMin>&& list);
90 
91  //- Move construct with different sizing parameters
92  template<int AnySizeMin>
94 
95  //- Move construct from PtrList
96  inline PtrDynList(PtrList<T>&& list) noexcept;
97 
98  //- Take ownership of pointers in the list, set old pointers to null.
99  inline explicit PtrDynList(UList<T*>& list);
100 
101 
102  //- Destructor, sync allocated size before list destruction
104 
105 
106  // Member Functions
107 
108  // Sizing
109 
110  //- Size of the underlying storage.
111  label capacity() const noexcept { return capacity_; }
112 
113  //- Change the value for the list capacity directly (ADVANCED, UNSAFE)
114  //- Does not perform any memory management or resizing.
115  void setCapacity_unsafe(const label len) noexcept { capacity_ = len; }
116 
117  //- Reserve allocation space for at least this size.
118  inline void reserve(const label len);
119 
120  //- Reserve allocation space for at least this size.
121  //- If allocation is required, uses the specified size
122  //- without any other resizing logic.
123  inline void reserve_exact(const label len);
124 
125  //- Alter the addressed list size.
126  inline void resize(const label newLen);
127 
128  //- Set the addressed list to the given size,
129  //- deleting all existing entries.
130  //- Afterwards the list contains all \c nullptr entries.
131  inline void resize_null(const label newLen);
132 
133  //- Clear the addressed list, i.e. set the size to zero.
134  // Allocated size does not change
135  inline void clear();
136 
137  //- Clear the list and delete storage.
138  inline void clearStorage();
139 
140  //- Shrink the allocated space to the number of elements used.
141  inline void shrink_to_fit();
142 
143  //- Shrink the internal bookkeeping of the allocated space to the
144  //- number of addressed elements without affecting allocation.
145  // \note when empty() it will delete any allocated memory.
146  inline void shrink_unsafe();
147 
148  //- Alias for shrink_to_fit()
149  void shrink() { this->shrink_to_fit(); }
150 
151 
152  // Edit
153 
154  //- Squeeze out intermediate nullptr entries in the list of pointers
155  //- and adjust the addressable size accordingly.
156  // \return the number of non-null entries
157  inline label squeezeNull();
158 
159  //- Swap with plain PtrList content. Implies shrink_to_fit().
160  inline void swap(PtrList<T>& list);
161 
162  //- Swap content, independent of sizing parameter
163  template<int AnySizeMin>
164  inline void swap(PtrDynList<T, AnySizeMin>& other) noexcept;
165 
166  //- Transfer contents of the argument PtrList into this.
167  inline void transfer(PtrList<T>& list);
168 
169  //- Transfer contents of any sized PtrDynList into this.
170  template<int AnySizeMin>
171  inline void transfer(PtrDynList<T, AnySizeMin>& list);
172 
173  //- Construct an element at the end of the list,
174  //- return reference to the new list element
175  template<class... Args>
176  inline T& emplace_back(Args&&... args);
177 
178  //- Append an element to the end of the list
179  inline void push_back(T* ptr);
180 
181  //- Move append an element to the end of the list
182  inline void push_back(std::unique_ptr<T>&& ptr);
183 
184  //- Move append an element to the end of the list
185  inline void push_back(autoPtr<T>&& ptr);
186 
187  //- Move or clone append a tmp to the end of the list
188  inline void push_back(const refPtr<T>& ptr);
189 
190  //- Move or clone append a tmp to the end of the list
191  inline void push_back(const tmp<T>& ptr);
192 
193  //- Move append another list to the end of this list.
194  inline void push_back(PtrList<T>&& other);
195 
196  //- Move append another list to the end of this list.
197  template<int AnySizeMin>
198  inline void push_back(PtrDynList<T, AnySizeMin>&& other);
199 
200  //- Reduce size by 1 or more elements. Can be called on an empty list.
201  inline void pop_back(label n = 1);
202 
203  //- Construct and set a new element at given position,
204  //- (discard old element at that location).
205  //- Auto-sizes list as required.
206  // \param i - the location to set
207  // \param args arguments to forward to the constructor of the element
208  // \return reference to the new list element.
209  template<class... Args>
210  inline T& emplace_set(const label i, Args&&... args);
211 
212  //- Same as emplace_set()
213  template<class... Args>
214  inline T& emplace(const label i, Args&&... args);
215 
216  //- Like emplace_set() but will not overwrite an occupied location.
217  // \param i - the location to set (unless already defined)
218  // \param args arguments to forward to the constructor of the element
219  // \return reference to the existing or the new list element.
220  template<class... Args>
221  inline T& try_emplace(const label i, Args&&... args);
222 
223  //- Set element to given pointer and return old element (can be null).
224  //- Auto-sizes list as required.
225  inline autoPtr<T> set(const label i, T* ptr);
226 
227  //- Set element to given pointer and return old element
228  //- Auto-sizes list as required.
229  inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
230 
231  //- Set element to given autoPtr and return old element
232  //- Auto-sizes list as required.
233  inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
234 
235  //- Set element to given refPtr and return old element
236  //- Auto-sizes list as required.
237  inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
238 
239  //- Set element to given tmp and return old element
240  //- Auto-sizes list as required.
241  inline autoPtr<T> set(const label i, const tmp<T>& ptr);
242 
243  //- Reorder elements. Reordering must be unique (ie, shuffle).
244  inline void reorder(const labelUList& oldToNew);
245 
246 
247  // Member Operators
248 
249  //- Copy (clone) assignment
250  inline void operator=(const PtrList<T>& list);
251 
252  //- Copy (clone) assignment
253  inline void operator=(const PtrDynList<T, SizeMin>& list);
254 
255  //- Copy (clone) assignment with different sizing parameters
256  template<int AnySizeMin>
257  inline void operator=(const PtrDynList<T, AnySizeMin>& list);
258 
259  //- Move assignment
260  inline void operator=(PtrList<T>&& list);
261 
262  //- Move assignment
263  inline void operator=(PtrDynList<T, SizeMin>&& list);
264 
265  //- Move assignment with different sizing parameters
266  template<int AnySizeMin>
267  inline void operator=(PtrDynList<T, AnySizeMin>&& list);
268 
269 
270  // Housekeeping
271 
272  //- Disallow push_back with autoPtr without std::move
273  void push_back(autoPtr<T>& ptr) = delete;
274 
275  //- Set element to given autoPtr and return old element
276  //FOAM_DEPRECATED_FOR(2022-10, "set(autoPtr&&))")
277  autoPtr<T> set(const label i, autoPtr<T>& ptr)
278  {
279  return this->set(i, ptr.release());
280  }
281 
282  //- Same as resize()
283  void setSize(const label n) { this->resize(n); }
284 
285  //- Move append an element to the end of the list
286  void append(autoPtr<T>& ptr) { this->push_back(std::move(ptr)); }
287 
288  //- Append an element to the end of the list
289  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
290  void append(T* ptr) { this->push_back(ptr); }
291 
292  //- Move append an element to the end of the list
293  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
294  void append(std::unique_ptr<T>&& ptr)
295  {
296  this->push_back(std::move(ptr));
297  }
298 
299  //- Move append an element to the end of the list
300  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
301  void append(autoPtr<T>&& ptr) { this->push_back(std::move(ptr)); }
302 
303  //- Move or clone append a tmp to the end of the list
304  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
305  void append(const refPtr<T>& ptr) { this->push_back(ptr); }
306 
307  //- Move or clone append a tmp to the end of the list
308  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
309  void append(const tmp<T>& ptr) { this->push_back(ptr); }
310 
311  //- Move append another list to the end of this list.
312  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
313  void append(PtrList<T>&& other) { this->push_back(std::move(other)); }
314 
315  //- Move append another list to the end of this list.
316  template<int AnySizeMin>
317  void append(PtrDynList<T, AnySizeMin>&& other)
318  {
319  this->push_back(std::move(other));
320  }
321 };
322 
323 
324 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
325 
326 } // End namespace Foam
327 
328 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
329 
330 #include "PtrDynListI.H"
331 
332 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
333 
334 #endif
335 
336 // ************************************************************************* //
constexpr PtrDynList() noexcept
Default construct.
Definition: PtrDynListI.H:28
void shrink_to_fit()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:224
void setCapacity_unsafe(const label len) noexcept
Change the value for the list capacity directly (ADVANCED, UNSAFE) Does not perform any memory manage...
Definition: PtrDynList.H:134
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
void shrink()
Alias for shrink_to_fit()
Definition: PtrDynList.H:188
void operator=(const PtrList< T > &list)
Copy (clone) assignment.
Definition: PtrDynListI.H:573
~PtrDynList()
Destructor, sync allocated size before list destruction.
Definition: PtrDynList.H:118
void reserve_exact(const label len)
Reserve allocation space for at least this size. If allocation is required, uses the specified size w...
Definition: PtrDynListI.H:142
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers and adjust the addressable size acco...
Definition: PtrDynListI.H:249
void transfer(PtrList< T > &list)
Transfer contents of the argument PtrList into this.
Definition: PtrDynListI.H:305
T & emplace_set(const label i, Args &&... args)
Construct and set a new element at given position, (discard old element at that location). Auto-sizes list as required.
Definition: PtrDynListI.H:458
T & emplace_back(Args &&... args)
Construct an element at the end of the list, return reference to the new list element.
Definition: PtrDynListI.H:347
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynList.H:128
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:561
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:105
void resize_null(const label newLen)
Set the addressed list to the given size, deleting all existing entries. Afterwards the list contains...
Definition: PtrDynListI.H:187
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:28
void setSize(const label n)
Same as resize()
Definition: PtrDynList.H:390
const direction noexcept
Definition: scalarImpl.H:255
void clearStorage()
Clear the list and delete storage.
Definition: PtrDynListI.H:216
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: PtrDynListI.H:442
T & try_emplace(const label i, Args &&... args)
Like emplace_set() but will not overwrite an occupied location.
Definition: PtrDynListI.H:486
void push_back(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:356
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:48
void swap(PtrList< T > &list)
Swap with plain PtrList content. Implies shrink_to_fit().
Definition: PtrDynListI.H:258
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:54
void shrink_unsafe()
Shrink the internal bookkeeping of the allocated space to the number of addressed elements without af...
Definition: PtrDynListI.H:237
void resize(const label newLen)
Alter the addressed list size.
Definition: PtrDynListI.H:157
label n
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
void reserve(const label len)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:124
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)
T & emplace(const label i, Args &&... args)
Same as emplace_set()
Definition: PtrDynListI.H:474
void setAddressableSize(const label n) noexcept
Adjust addressable size.
Definition: UPtrListI.H:25
Namespace for OpenFOAM.
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: PtrDynListI.H:208
void append(autoPtr< T > &ptr)
Move append an element to the end of the list.
Definition: PtrDynList.H:395