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-2020 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 <type_traits>
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward declaration of friend functions and operators
55 
56 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
57 class DynamicList;
58 
59 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
60 Ostream& operator<<
61 (
62  Ostream&,
64 );
65 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
66 Istream& operator>>
67 (
68  Istream&,
70 );
71 
72 
73 /*---------------------------------------------------------------------------*\
74  Class DynamicList Declaration
75 \*---------------------------------------------------------------------------*/
76 
77 template<class T, unsigned SizeInc=0, unsigned SizeMult=2, unsigned SizeDiv=1>
78 class DynamicList
79 :
80  public List<T>
81 {
82  static_assert
83  (
84  (SizeInc || SizeMult) && SizeDiv,
85  "Invalid sizing parameters"
86  );
87 
88  // Private Data
89 
90  //- The capacity (allocated size) of the underlying list.
91  label capacity_;
92 
93 
94 public:
95 
96  // Related types
97 
98  //- Declare friendship with the List class
99  friend class List<T>;
100 
101 
102  // Constructors
103 
104  //- Construct null
105  inline DynamicList();
106 
107  //- Construct given size
108  explicit inline DynamicList(const label);
109 
110  //- Construct with given size and value for all elements.
111  inline DynamicList(const label, const T&);
112 
113  //- Copy constructor
115 
116  //- Move constructor
118 
119  //- Construct from UList. Size set to UList size.
120  // Also constructs from DynamicList with different sizing parameters.
121  explicit inline DynamicList(const UList<T>&);
122 
123  //- Construct from UIndirectList. Size set to UIndirectList size.
124  explicit inline DynamicList(const UIndirectList<T>&);
125 
126  //- Move constructor
127  explicit inline DynamicList(List<T>&&);
128 
129  //- Construct from Istream. Size set to size of list read.
130  explicit DynamicList(Istream&);
131 
132 
133  // Member Functions
134 
135  // Access
136 
137  //- Size of the underlying storage.
138  inline label capacity() const;
139 
140 
141  // Edit
142 
143  //- Alter the size of the underlying storage.
144  // The addressed size will be truncated if needed to fit, but will
145  // remain otherwise untouched.
146  // Use this or reserve() in combination with append().
147  inline void setCapacity(const label);
148 
149  //- Alter the addressed list size.
150  // New space will be allocated if required.
151  // Use this to resize the list prior to using the operator[] for
152  // setting values (as per List usage).
153  inline void setSize(const label);
154 
155  //- Alter the addressed list size and fill new space with a
156  // constant.
157  inline void setSize(const label, const T&);
158 
159  //- Alter the addressed list size.
160  // New space will be allocated if required.
161  // Use this to resize the list prior to using the operator[] for
162  // setting values (as per List usage).
163  inline void resize(const label);
164 
165  //- Alter the addressed list size and fill new space with a
166  // constant.
167  inline void resize(const label, const T&);
168 
169  //- Reserve allocation space for at least this size.
170  // Never shrinks the allocated size, use setCapacity() for that.
171  inline void reserve(const label);
172 
173  //- Clear the addressed list, i.e. set the size to zero.
174  // Allocated size does not change
175  inline void clear();
176 
177  //- Clear the list and delete storage.
178  inline void clearStorage();
179 
180  //- Shrink the allocated space to the number of elements used.
181  // Returns a reference to the DynamicList.
183 
184  //- Transfer contents of the argument List into this.
185  inline void transfer(List<T>&);
186 
187  //- Transfer contents of the argument DynamicList into this.
189 
190 
191  // Member Operators
192 
193  //- Append an element at the end of the list
195  (
196  const T&
197  );
198 
199  //- Append a List at the end of this list
201  (
202  const UList<T>&
203  );
204 
205  //- Append a UIndirectList at the end of this list
207  (
208  const UIndirectList<T>&
209  );
210 
211  //- Remove and return the top element
212  inline T remove();
213 
214  //- Return non-const access to an element, resizing list if
215  // necessary
216  inline T& operator()(const label);
217 
218  //- Assignment of all addressed entries to the given value
219  inline void operator=(const T&);
220 
221  //- Assignment operator
222  inline void operator=
223  (
225  );
226 
227  //- Move assignment operator
228  inline void operator=
229  (
231  );
232 
233  //- Assignment to UList
234  inline void operator=(const UList<T>&);
235 
236  //- Move assignment to List
237  inline void operator=(List<T>&&);
238 
239  //- Assignment to UIndirectList
240  inline void operator=(const UIndirectList<T>&);
241 
242 
243  // STL member functions
244 
245  //- Erase an element, move the remaining elements to fill the gap
246  // and resize the List
247  typename UList<T>::iterator erase(typename UList<T>::iterator);
248 
249 
250  // IOstream Operators
251 
252  // Write DynamicList to Ostream.
253  friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv>
254  (
255  Ostream&,
257  );
258 
259  //- Read from Istream, discarding contents of existing DynamicList.
260  friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv>
261  (
262  Istream&,
264  );
265 };
266 
267 
268 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
269 
270 } // End namespace Foam
271 
272 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
273 
274 #include "DynamicListI.H"
275 
276 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
277 
278 #ifdef NoRepository
279  #include "DynamicList.C"
280 #endif
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 #endif
285 
286 // ************************************************************************* //
T & operator()(const label)
Return non-const access to an element, resizing list if.
Definition: DynamicListI.H:373
void resize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:216
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
UList< T >::iterator erase(typename UList< T >::iterator)
Erase an element, move the remaining elements to fill the gap.
Definition: DynamicListI.H:511
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:269
void reserve(const label)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:152
void setSize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:175
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
DynamicList()
Construct null.
Definition: DynamicListI.H:29
void setCapacity(const label)
Alter the size of the underlying storage.
Definition: DynamicListI.H:130
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
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:60
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:252
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
A List with indirect addressing.
Definition: fvMatrix.H:106
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:243
label capacity() const
Size of the underlying storage.
Definition: DynamicListI.H:121
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:236
void transfer(List< T > &)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:285
Namespace for OpenFOAM.
void operator=(const T &)
Assignment of all addressed entries to the given value.
Definition: DynamicListI.H:388