All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DynamicList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::DynamicList
26 
27 Description
28  A 1D vector of objects of type <T> that resizes itself as necessary to
29  accept the new objects.
30 
31  Internal storage is a compact array and the list can be shrunk to compact
32  storage. The increase of list size is controlled by three template
33  parameters, which allows the list storage to either increase by the given
34  increment or by the given multiplier and divider (allowing non-integer
35  multiples).
36 
37 SourceFiles
38  DynamicListI.H
39  DynamicList.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef DynamicList_H
44 #define DynamicList_H
45 
46 #include "List.H"
47 #include "DynamicListFwd.H"
48 #include <type_traits>
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward declaration of friend functions and operators
56 
57 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
58 Ostream& operator<<
59 (
60  Ostream&,
62 );
63 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
64 Istream& operator>>
65 (
66  Istream&,
68 );
69 
70 
71 /*---------------------------------------------------------------------------*\
72  Class DynamicList Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
76 class DynamicList
77 :
78  public List<T>
79 {
80  static_assert
81  (
82  (SizeInc || SizeMult) && SizeDiv,
83  "Invalid sizing parameters"
84  );
85 
86  // Private Data
87 
88  //- The capacity (allocated size) of the underlying list.
89  label capacity_;
90 
91 
92 public:
93 
94  // Related types
95 
96  //- Declare friendship with the List class
97  friend class List<T>;
98 
99 
100  // Constructors
101 
102  //- Construct null
103  inline DynamicList();
104 
105  //- Construct given size
106  explicit inline DynamicList(const label);
107 
108  //- Construct with given size and value for all elements.
109  inline DynamicList(const label, const T&);
110 
111  //- Copy constructor
113 
114  //- Move constructor
116 
117  //- Construct from UList. Size set to UList size.
118  // Also constructs from DynamicList with different sizing parameters.
119  explicit inline DynamicList(const UList<T>&);
120 
121  //- Construct from UIndirectList. Size set to UIndirectList size.
122  explicit inline DynamicList(const UIndirectList<T>&);
123 
124  //- Move constructor
125  explicit inline DynamicList(List<T>&&);
126 
127  //- Construct from Istream. Size set to size of list read.
128  explicit DynamicList(Istream&);
129 
130 
131  // Member Functions
132 
133  // Access
134 
135  //- Size of the underlying storage.
136  inline label capacity() const;
137 
138 
139  // Edit
140 
141  //- Alter the size of the underlying storage.
142  // The addressed size will be truncated if needed to fit, but will
143  // remain otherwise untouched.
144  // Use this or reserve() in combination with append().
145  inline void setCapacity(const label);
146 
147  //- Alter the addressed list size.
148  // New space will be allocated if required.
149  // Use this to resize the list prior to using the operator[] for
150  // setting values (as per List usage).
151  inline void setSize(const label);
152 
153  //- Alter the addressed list size and fill new space with a
154  // constant.
155  inline void setSize(const label, const T&);
156 
157  //- Alter the addressed list size.
158  // New space will be allocated if required.
159  // Use this to resize the list prior to using the operator[] for
160  // setting values (as per List usage).
161  inline void resize(const label);
162 
163  //- Alter the addressed list size and fill new space with a
164  // constant.
165  inline void resize(const label, const T&);
166 
167  //- Reserve allocation space for at least this size.
168  // Never shrinks the allocated size, use setCapacity() for that.
169  inline void reserve(const label);
170 
171  //- Clear the addressed list, i.e. set the size to zero.
172  // Allocated size does not change
173  inline void clear();
174 
175  //- Clear the list and delete storage.
176  inline void clearStorage();
177 
178  //- Shrink the allocated space to the number of elements used.
179  // Returns a reference to the DynamicList.
181 
182  //- Transfer contents of the argument List into this.
183  inline void transfer(List<T>&);
184 
185  //- Transfer contents of the argument DynamicList into this.
187 
188 
189  // Member Operators
190 
191  //- Append an element at the end of the list
193  (
194  const T&
195  );
196 
197  //- Append a List at the end of this list
199  (
200  const UList<T>&
201  );
202 
203  //- Append a UIndirectList at the end of this list
205  (
206  const UIndirectList<T>&
207  );
208 
209  //- Remove and return the top element
210  inline T remove();
211 
212  //- Return non-const access to an element, resizing list if
213  // necessary
214  inline T& operator()(const label);
215 
216  //- Assignment of all addressed entries to the given value
217  inline void operator=(const T&);
218 
219  //- Assignment operator
220  inline void operator=
221  (
223  );
224 
225  //- Move assignment operator
226  inline void operator=
227  (
229  );
230 
231  //- Assignment to UList
232  inline void operator=(const UList<T>&);
233 
234  //- Move assignment to List
235  inline void operator=(List<T>&&);
236 
237  //- Assignment to UIndirectList
238  inline void operator=(const UIndirectList<T>&);
239 
240 
241  // STL member functions
242 
243  //- Erase an element, move the remaining elements to fill the gap
244  // and resize the List
245  typename UList<T>::iterator erase(typename UList<T>::iterator);
246 
247 
248  // IOstream Operators
249 
250  // Write DynamicList to Ostream.
251  friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv>
252  (
253  Ostream&,
255  );
256 
257  //- Read from Istream, discarding contents of existing DynamicList.
258  friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv>
259  (
260  Istream&,
262  );
263 };
264 
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 } // End namespace Foam
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 #include "DynamicListI.H"
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 #ifdef NoRepository
277  #include "DynamicList.C"
278 #endif
279 
280 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281 
282 #endif
283 
284 // ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:78
T remove()
Remove and return the top element.
Definition: DynamicListI.H:351
void transfer(List< T > &)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:285
void setCapacity(const label)
Alter the size of the underlying storage.
Definition: DynamicListI.H:130
void resize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:216
label capacity() const
Size of the underlying storage.
Definition: DynamicListI.H:121
void operator=(const T &)
Assignment of all addressed entries to the given value.
Definition: DynamicListI.H:388
void reserve(const label)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:152
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:243
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:252
DynamicList()
Construct null.
Definition: DynamicListI.H:29
T & operator()(const label)
Return non-const access to an element, resizing list if.
Definition: DynamicListI.H:373
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:236
UList< T >::iterator erase(typename UList< T >::iterator)
Erase an element, move the remaining elements to fill the gap.
Definition: DynamicListI.H:511
void setSize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:175
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A List with indirect addressing.
Definition: UIndirectList.H:60
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:74
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:272
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)