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-2018 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  //- Construct copy.
115 
116  //- Construct from UList. Size set to UList size.
117  // Also constructs from DynamicList with different sizing parameters.
118  explicit inline DynamicList(const UList<T>&);
119 
120  //- Construct from UIndirectList. Size set to UIndirectList size.
121  explicit inline DynamicList(const UIndirectList<T>&);
122 
123  //- Construct by transferring the parameter contents
124  explicit inline DynamicList(const Xfer<List<T>>&);
125 
126  //- Construct from Istream. Size set to size of list read.
127  explicit DynamicList(Istream&);
128 
129 
130  // Member Functions
131 
132  // Access
133 
134  //- Size of the underlying storage.
135  inline label capacity() const;
136 
137  // Edit
138 
139  //- Alter the size of the underlying storage.
140  // The addressed size will be truncated if needed to fit, but will
141  // remain otherwise untouched.
142  // Use this or reserve() in combination with append().
143  inline void setCapacity(const label);
144 
145  //- Alter the addressed list size.
146  // New space will be allocated if required.
147  // Use this to resize the list prior to using the operator[] for
148  // setting values (as per List usage).
149  inline void setSize(const label);
150 
151  //- Alter the addressed list size and fill new space with a
152  // constant.
153  inline void setSize(const label, const T&);
154 
155  //- Alter the addressed list size.
156  // New space will be allocated if required.
157  // Use this to resize the list prior to using the operator[] for
158  // setting values (as per List usage).
159  inline void resize(const label);
160 
161  //- Alter the addressed list size and fill new space with a
162  // constant.
163  inline void resize(const label, const T&);
164 
165  //- Reserve allocation space for at least this size.
166  // Never shrinks the allocated size, use setCapacity() for that.
167  inline void reserve(const label);
168 
169  //- Clear the addressed list, i.e. set the size to zero.
170  // Allocated size does not change
171  inline void clear();
172 
173  //- Clear the list and delete storage.
174  inline void clearStorage();
175 
176  //- Shrink the allocated space to the number of elements used.
177  // Returns a reference to the DynamicList.
179 
180  //- Transfer contents of the argument List into this.
181  inline void transfer(List<T>&);
182 
183  //- Transfer contents of the argument DynamicList into this.
185 
186  //- Transfer contents to the Xfer container as a plain List
187  inline Xfer<List<T>> xfer();
188 
189 
190  // Member Operators
191 
192  //- Append an element at the end of the list
194  (
195  const T&
196  );
197 
198  //- Append a List at the end of this list
200  (
201  const UList<T>&
202  );
203 
204  //- Append a UIndirectList at the end of this list
206  (
207  const UIndirectList<T>&
208  );
209 
210  //- Remove and return the top element
211  inline T remove();
212 
213  //- Return non-const access to an element, resizing list if
214  // necessary
215  inline T& operator()(const label);
216 
217  //- Assignment of all addressed entries to the given value
218  inline void operator=(const T&);
219 
220  //- Assignment to DynamicList
221  inline void operator=
222  (
224  );
225 
226  //- Assignment to UList
227  inline void operator=(const UList<T>&);
228 
229  //- Assignment to UIndirectList
230  inline void operator=(const UIndirectList<T>&);
231 
232 
233  // STL member functions
234 
235  //- Erase an element, move the remaining elements to fill the gap
236  // and resize the List
237  typename UList<T>::iterator erase(typename UList<T>::iterator);
238 
239 
240  // IOstream operators
241 
242  // Write DynamicList to Ostream.
243  friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv>
244  (
245  Ostream&,
247  );
248 
249  //- Read from Istream, discarding contents of existing DynamicList.
250  friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv>
251  (
252  Istream&,
254  );
255 };
256 
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 } // End namespace Foam
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 
264 #include "DynamicListI.H"
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 #ifdef NoRepository
269  #include "DynamicList.C"
270 #endif
271 
272 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
273 
274 #endif
275 
276 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
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
T & operator()(const label)
Return non-const access to an element, resizing list if.
Definition: DynamicListI.H:369
void resize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:204
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
UList< T >::iterator erase(typename UList< T >::iterator)
Erase an element, move the remaining elements to fill the gap.
Definition: DynamicListI.H:472
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:272
void reserve(const label)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:140
void setSize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:163
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:118
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
Definition: DynamicListI.H:283
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:292
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:61
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:240
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:231
label capacity() const
Size of the underlying storage.
Definition: DynamicListI.H:109
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:224
void transfer(List< T > &)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:259
Namespace for OpenFOAM.
void operator=(const T &)
Assignment of all addressed entries to the given value.
Definition: DynamicListI.H:384