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-2019 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 PtrDynList_H
42 #define PtrDynList_H
43 
44 #include "PtrList.H"
45 #include <type_traits>
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 
54 template<class T, int SizeMin> class PtrDynList;
55 
56 /*---------------------------------------------------------------------------*\
57  Class PtrDynList Declaration
58 \*---------------------------------------------------------------------------*/
59 
60 template<class T, int SizeMin=64>
61 class PtrDynList
62 :
63  public PtrList<T>
64 {
65  static_assert(SizeMin > 0, "Invalid min size parameter");
66 
67  // Private Data
68 
69  //- The capacity (allocated size) of the list.
70  label capacity_;
71 
72 
73  // Private Member Functions
74 
75  //- Adjust addressable size
76  void setAddressableSize(const label len);
77 
78 
79 public:
80 
81  // Constructors
82 
83  //- Construct null
84  inline constexpr PtrDynList() noexcept;
85 
86  //- Construct with given capacity.
87  inline explicit PtrDynList(const label len);
88 
89  //- Copy construct using 'clone()' method on each element
90  inline PtrDynList(const PtrDynList<T, SizeMin>& list);
91 
92  //- Move construct
93  inline PtrDynList(PtrDynList<T, SizeMin>&& list);
94 
95  //- Take ownerskip of pointers in the list, set old pointers to null.
96  inline explicit PtrDynList(UList<T*>& list);
97 
98 
99  //- Destructor
100  ~PtrDynList() = default;
101 
102 
103  // Member Functions
104 
105  // Access
106 
107  //- Size of the underlying storage.
108  inline label capacity() const noexcept;
109 
110 
111  // Edit
112 
113  //- Delete the allocated entries, but retain the list size.
114  using PtrList<T>::free;
115 
116  //- Alter the size of the underlying storage.
117  inline void setCapacity(const label nElem);
118 
119  //- Alter the addressed list size.
120  inline void resize(const label newLen);
121 
122  //- Same as resize()
123  inline void setSize(const label newLen);
124 
125  //- Reserve allocation space for at least this size.
126  // Never shrinks the allocated size, use setCapacity() for that.
127  inline void reserve(const label nElem);
128 
129  //- Clear the addressed list, i.e. set the size to zero.
130  // Allocated size does not change
131  inline void clear();
132 
133  //- Clear the list and delete storage.
134  inline void clearStorage();
135 
136  //- Expand the addressable size to fit the allocated capacity.
137  // Returns the previous addressable size.
138  inline label expandStorage();
139 
140  //- Shrink the allocated space to the number of elements used.
141  inline void shrink();
142 
143  //- Squeeze out intermediate nullptr entries in the list of pointers
144  //- and adjust the addressable size accordingly.
145  // \return the number of non-null entries
146  inline label squeezeNull();
147 
148  //- Append an element to the end of the list
149  inline void append(T* ptr);
150 
151  //- Move append an element to the end of the list
152  inline void append(const autoPtr<T>& aptr);
153 
154  //- Move or clone append a tmp to the end of the list
155  inline void append(const tmp<T>& tptr);
156 
157  //- Move append another list to the end of this list.
158  inline void append(PtrList<T>&& other);
159 
160  //- Move append another list to the end of this list.
161  template<int AnySizeMin>
162  inline void append(PtrDynList<T, AnySizeMin>&& other);
163 
164  //- Remove and return the top element
165  inline autoPtr<T> remove();
166 
167  //- Return const pointer to element (if set) or nullptr.
168  // The return value can be tested as a bool.
169  inline const T* set(const label i) const;
170 
171  //- Set element to given pointer and return old element (can be null)
172  inline autoPtr<T> set(const label i, T* ptr);
173 
174  //- Set element to given autoPtr and return old element
175  inline autoPtr<T> set(const label i, const autoPtr<T>& aptr);
176 
177  //- Set element to given tmp and return old element
178  inline autoPtr<T> set(const label i, const tmp<T>& tptr);
179 
180  //- Reorder elements. Reordering must be unique (ie, shuffle).
181  inline void reorder(const labelUList& oldToNew);
182 
183 
184  // Member Operators
185 
186  //- Copy (clone) assignment
187  inline void operator=(const PtrList<T>& list);
188 
189  //- Copy (clone) assignment
190  inline void operator=(const PtrDynList<T, SizeMin>& list);
191 
192  //- Copy (clone) assignment with different sizing parameters
193  template<int AnySizeMin>
194  inline void operator=(const PtrDynList<T, AnySizeMin>& list);
195 
196  //- Move assignment
197  inline void operator=(PtrList<T>&& list);
198 
199  //- Move assignment
200  inline void operator=(PtrDynList<T, SizeMin>&& list);
201 
202  //- Move assignment with different sizing parameters
203  template<int AnySizeMin>
204  inline void operator=(PtrDynList<T, AnySizeMin>&& list);
205 
206 };
207 
208 
209 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210 
211 } // End namespace Foam
212 
213 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214 
215 #include "PtrDynListI.H"
216 
217 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
218 
219 #endif
220 
221 // ************************************************************************* //
Foam::PtrDynList::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:119
Foam::PtrDynList::setSize
void setSize(const label newLen)
Same as resize()
Definition: PtrDynListI.H:170
Foam::PtrDynList::expandStorage
label expandStorage()
Expand the addressable size to fit the allocated capacity.
Definition: PtrDynListI.H:193
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::PtrDynList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynListI.H:95
Foam::PtrDynList::reorder
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:356
Foam::PtrDynList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:231
Foam::PtrDynList
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:53
Foam::PtrDynList::resize
void resize(const label newLen)
Alter the addressed list size.
Definition: PtrDynListI.H:136
Foam::PtrDynList::shrink
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:205
Foam::PtrDynList::remove
autoPtr< T > remove()
Remove and return the top element.
Definition: PtrDynListI.H:292
Foam::PtrDynList::setCapacity
void setCapacity(const label nElem)
Alter the size of the underlying storage.
Definition: PtrDynListI.H:102
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
Foam::PtrDynList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr.
Definition: PtrDynListI.H:311
Foam::PtrDynList::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: PtrDynListI.H:185
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::PtrDynList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: PtrDynListI.H:177
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
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
PtrList.H
Foam::PtrDynList::PtrDynList
constexpr PtrDynList() noexcept
Construct null.
Definition: PtrDynListI.H:43
Foam::PtrDynList::squeezeNull
label squeezeNull()
Definition: PtrDynListI.H:222