DynamicList.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2016-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::DynamicList
29
30Description
31 A 1D vector of objects of type <T> that resizes itself as necessary to
32 accept the new objects.
33
34 Internal storage is a compact array and the list can be shrunk to compact
35 storage. The increase of list size uses a doubling strategy, with the
36 SizeMin template parameter dictating a lower bound.
37
38SourceFiles
39 DynamicList.C
40 DynamicListI.H
41 DynamicListIO.C
42
43\*---------------------------------------------------------------------------*/
44
45#ifndef Foam_DynamicList_H
46#define Foam_DynamicList_H
47
48#include "List.H"
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51
52namespace Foam
53{
54
55// Forward Declarations
56template<class T, int SizeMin> class DynamicList;
57
58template<class T, int SizeMin>
59Istream& operator>>(Istream& is, DynamicList<T, SizeMin>& list);
60
61template<class T, int SizeMin>
62Ostream& operator<<(Ostream& os, const DynamicList<T, SizeMin>& list);
63
64
65/*---------------------------------------------------------------------------*\
66 Class DynamicList Declaration
67\*---------------------------------------------------------------------------*/
68
69template<class T, int SizeMin = 16>
70class DynamicList
71:
72 public List<T>
73{
74 static_assert(SizeMin > 0, "Invalid min size parameter");
75
76 // Private Data
77
78 //- The capacity (allocated size) of the underlying list.
79 label capacity_;
80
81
82 // Private Member Functions
83
84 //- Remove elements in range
85 label removeElements(const labelRange& slice);
86
87 //- Subset elements in range
88 label subsetElements(const labelRange& slice);
89
90 //- Copy assignment from another list
91 template<class ListType>
92 inline void assignDynList(const ListType& list);
93
94 //- Alter the size of the underlying storage
95 // The 'nocopy' option will not attempt to recover old content
96 inline void doCapacity(const bool nocopy, const label len);
97
98 //- Reserve allocation space for at least this size.
99 // Never shrinks the allocated size, use setCapacity() for that.
100 // The 'nocopy' option will not attempt to recover old content
101 inline void doReserve(const bool nocopy, const label len);
102
103 //- Reserve allocation space for at least this size.
104 // Never shrinks the allocated size, use setCapacity() for that.
105 // The 'nocopy' option will not attempt to recover old content
106 inline void doResize(const bool nocopy, const label len);
107
108
109public:
110
111 // Constructors
112
113 //- Default construct, an empty list without allocation.
114 inline constexpr DynamicList() noexcept;
115
116 //- Construct an empty list with given reserve size.
117 inline explicit DynamicList(const label len);
118
119 //- Construct with given size and value for all elements.
120 inline DynamicList(const label len, const T& val);
121
122 //- Construct with given size initializing all elements to zero
123 inline DynamicList(const label len, const Foam::zero);
124
125 //- Copy construct.
126 inline DynamicList(const DynamicList<T, SizeMin>& lst);
127
128 //- Copy construct from DynamicList with different sizing parameters
129 template<int AnySizeMin>
130 inline DynamicList(const DynamicList<T, AnySizeMin>& lst);
131
132 //- Construct from UList. Size set to UList size.
133 // Also constructs from DynamicList with different sizing parameters.
134 inline explicit DynamicList(const UList<T>& lst);
135
136 //- Construct from a FixedList
137 template<unsigned N>
138 inline explicit DynamicList(const FixedList<T, N>& lst);
139
140 //- Construct from an initializer list. Size set to list size.
141 inline explicit DynamicList(std::initializer_list<T> lst);
142
143 //- Construct from IndirectList. Size set to addressing size.
144 template<class Addr>
145 inline explicit DynamicList(const IndirectListBase<T, Addr>& lst);
146
147 //- Move construct.
149
150 //- Move construct with different sizing parameters
151 template<int AnySizeMin>
153
154 //- Move construct from List
155 inline DynamicList(List<T>&& lst);
156
157 //- Construct from Istream. Size set to size of list read.
158 explicit DynamicList(Istream& is);
159
160
161 // Member Functions
162
163 // Access
164
165 //- Normal lower capacity limit - the SizeMin template parameter
166 static constexpr label min_size() noexcept { return SizeMin; }
167
168 //- Size of the underlying storage.
169 inline label capacity() const noexcept;
170
171 //- Number of contiguous bytes of the underlying storage.
172 // \note Only meaningful for contiguous data
173 inline std::streamsize capacity_bytes() const noexcept;
174
175
176 // Sizing
177
178 //- Alter the size of the underlying storage.
179 // The addressed size will be truncated if needed to fit, but will
180 // remain otherwise untouched.
181 // Use this or reserve() in combination with append().
182 inline void setCapacity(const label len);
183
184 //- Alter the size of the underlying storage,
185 //- \em without retaining old content.
186 // The addressed size will be truncated if needed to fit, but will
187 // remain otherwise untouched.
188 inline void setCapacity_nocopy(const label len);
189
190 //- Change the value for the list capacity directly (ADVANCED, UNSAFE)
191 //- Does not perform any memory management or resizing.
192 inline void setCapacity_unsafe(const label len) noexcept;
193
194 //- Reserve allocation space for at least this size, allocating new
195 //- space if required and \em retaining old content.
196 // Never shrinks the allocated size, use setCapacity() for that.
197 inline void reserve(const label len);
198
199 //- Reserve allocation space for at least this size, allocating new
200 //- space if required \em without retaining old content.
201 // Never shrinks the allocated size, use setCapacity() for that.
202 inline void reserve_nocopy(const label len);
203
204 //- Alter addressable list size, allocating new space if required
205 //- while \em recovering old content.
206 // If no reallocation is required, the contents remain untouched.
207 // Otherwise new entries will be uninitialized.
208 // Use this to resize the list prior to using the operator[] for
209 // setting values (as per List usage).
210 inline void resize(const label len);
211
212 //- Alter addressable size and fill new entries with constant value
213 inline void resize(const label len, const T& val);
214
215 //- Alter addressable list size, allocating new space if required
216 //- \em without necessarily recovering old content.
217 // If no reallocation is required, the contents remain untouched.
218 // Otherwise all entries will be uninitialized.
219 inline void resize_nocopy(const label len);
220
221 //- Same as resize()
222 void setSize(const label n) { this->resize(n); }
223
224 //- Same as resize()
225 void setSize(const label n, const T& val) { this->resize(n, val); }
226
227 //- Clear the addressed list, i.e. set the size to zero.
228 // Allocated size does not change
229 inline void clear() noexcept;
230
231 //- Clear the list and delete storage.
232 inline void clearStorage();
233
234 //- Expand the addressable size to fit the allocated capacity.
235 // Returns the previous addressable size.
236 inline label expandStorage() noexcept;
237
238 //- Shrink the allocated space to the number of elements used.
239 inline void shrinkStorage();
240
241 //- Shrink the allocated space to the number of elements used.
242 // Returns a reference to the DynamicList.
243 inline DynamicList<T, SizeMin>& shrink();
244
245
246 // Edit
247
248 //- Swap content, independent of sizing parameter
249 template<int AnySizeMin>
250 inline void swap(DynamicList<T, AnySizeMin>& other);
251
252 //- Transfer contents of the argument List into this.
253 inline void transfer(List<T>& list);
254
255 //- Transfer contents of any sized DynamicList into this.
256 template<int AnySizeMin>
257 inline void transfer(DynamicList<T, AnySizeMin>& list);
258
259 //- Copy append an element to the end of this list.
260 inline void append(const T& val);
261
262 //- Move append an element
263 inline void append(T&& val);
264
265 //- Append another list to the end of this list.
266 inline void append(const UList<T>& lst);
267
268 //- Append a FixedList to the end of this list.
269 template<unsigned N>
270 inline void append(const FixedList<T, N>& lst);
271
272 //- Append an initializer list at the end of this list.
273 inline void append(std::initializer_list<T> lst);
274
275 //- Append a IndirectList at the end of this list
276 template<class Addr>
277 inline void append(const IndirectListBase<T, Addr>& lst);
278
279 //- Move append list
280 inline void append(List<T>&& lst);
281
282 //- Move append list
283 template<int AnySizeMin>
284 inline void append(DynamicList<T, AnySizeMin>&& list);
285
286 //- Append an element if not already in the list.
287 // \return the change in list length
288 inline label appendUniq(const T& val);
289
290 //- Remove and return the last element. Fatal on an empty list.
291 inline T remove();
292
293 //- Remove and return the specified element. Fatal on an empty list.
294 // With fast=true (operates in constant time), the place of the
295 // removed element is swapped with the last one in the list, which
296 // changes the ordering.
297 // With fast=false (operates in linear time), the elements
298 // are swapped down in the list to preserve ordering.
299 inline T remove(const label idx, const bool fast=false);
300
301 //- Remove a (start,size) subset from the list.
302 // The range is subsetted with the list size itself to ensure
303 // result always addresses a valid section of the list.
304 // Remaining elements are moved down.
305 inline label remove(const labelRange& range);
306
307 //- Remove a (start,size) subset from the list.
308 inline label remove(std::initializer_list<label> start_size);
309
310 //- Retain a (start,size) subset from the list.
311 // The range is subsetted with the list size itself to ensure
312 // result always addresses a valid section of the list.
313 // Remaining elements are moved down.
314 inline label subset(const labelRange& range);
315
316 //- Retain a (start,size) subset from List.
317 inline label subset(std::initializer_list<label> start_size);
318
319
320 // Member Operators
321
322 //- Return non-const access to an element, resizing list if needed
323 inline T& operator()(const label i);
324
325 //- Assignment of all addressed entries to the given value
326 inline void operator=(const T& val);
327
328 //- Assignment of all entries to zero
329 inline void operator=(const Foam::zero);
330
331 //- Assignment to UList
332 inline void operator=(const UList<T>& lst);
333
334 //- Assignment to FixedList
335 template<unsigned N>
336 inline void operator=(const FixedList<T, N>& lst);
337
338 //- Assignment to DynamicList
339 inline void operator=(const DynamicList<T, SizeMin>& lst);
340
341 //- Assignment from DynamicList with different sizing parameters
342 template<int AnySizeMin>
343 inline void operator=(const DynamicList<T, AnySizeMin>& lst);
344
345 //- Assignment from initializer list
346 inline void operator=(std::initializer_list<T> lst);
347
348 //- Assignment from IndirectList
349 template<class Addr>
350 inline void operator=(const IndirectListBase<T, Addr>& lst);
351
352 //- Move assignment
353 inline void operator=(List<T>&& lst);
354
355 //- Move assignment
356 inline void operator=(DynamicList<T, SizeMin>&& lst);
357
358 //- Move assignment
359 template<int AnySizeMin>
360 inline void operator=(DynamicList<T, AnySizeMin>&& lst);
361
362
363 // Reading/writing
364
365 //- Read from Istream, discarding existing contents
367
368
369 // IOstream Operators
370
371 //- Use the readList() method to read contents from Istream.
372 friend Istream& operator>> <T, SizeMin>
373 (
374 Istream& is,
375 DynamicList<T, SizeMin>& list
376 );
377
378 //- Write to Ostream
379 friend Ostream& operator<< <T, SizeMin>
380 (
381 Ostream& os,
382 const DynamicList<T, SizeMin>& list
383 );
384};
385
386
387// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
388
389//- Read List contents from Istream
390template<class T, int SizeMin>
391Istream& operator>>(Istream& is, DynamicList<T, SizeMin>& list)
392{
393 return list.readList(is);
394}
395
396
397//- Write List to Ostream, as per UList::writeList() with default length.
398template<class T, int SizeMin>
400{
401 return (os << static_cast<const UList<T>&>(list));
402}
403
404
405// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
406
407// Exchange contents of lists - see DynamicList::swap().
408template<class T, int SizeMinA, int SizeMinB>
410{
411 a.swap(b);
412}
413
414
415// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
416
417//- Hashing for List data
418template<class T, int SizeMin>
419struct Hash<DynamicList<T, SizeMin>> : List<T>::hasher {};
420
421
422// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
423
424} // End namespace Foam
425
426// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
427
428#include "DynamicListI.H"
429
430// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
431
432#ifdef NoRepository
433 #include "DynamicList.C"
434 #include "DynamicListIO.C"
435#endif
436
437// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
438
439#endif
440
441// ************************************************************************* //
scalar range
label n
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
constexpr DynamicList() noexcept
Default construct, an empty list without allocation.
Definition: DynamicListI.H:138
void swap(DynamicList< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
Definition: DynamicListI.H:444
DynamicList< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:434
T remove()
Remove and return the last element. Fatal on an empty list.
Definition: DynamicListI.H:655
void transfer(List< T > &list)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:467
void setSize(const label n)
Same as resize()
Definition: DynamicList.H:221
void resize_nocopy(const label len)
Definition: DynamicListI.H:363
void setCapacity_nocopy(const label len)
Definition: DynamicListI.H:313
void reserve_nocopy(const label len)
Definition: DynamicListI.H:343
void setCapacity_unsafe(const label len) noexcept
Definition: DynamicListI.H:323
std::streamsize capacity_bytes() const noexcept
Number of contiguous bytes of the underlying storage.
Definition: DynamicListI.H:295
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:503
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:398
label appendUniq(const T &val)
Append an element if not already in the list.
Definition: DynamicListI.H:640
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicListI.H:287
void setSize(const label n, const T &val)
Same as resize()
Definition: DynamicList.H:224
void shrinkStorage()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:418
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
Definition: DynamicListI.H:406
static constexpr label min_size() noexcept
Normal lower capacity limit - the SizeMin template parameter.
Definition: DynamicList.H:165
friend Ostream & operator(Ostream &os, const DynamicList< T, SizeMin > &list)
Write to Ostream.
void resize(const label len)
Definition: DynamicListI.H:353
void reserve(const label len)
Definition: DynamicListI.H:333
label subset(const labelRange &range)
Retain a (start,size) subset from the list.
Definition: DynamicListI.H:719
void setCapacity(const label len)
Alter the size of the underlying storage.
Definition: DynamicListI.H:303
Istream & readList(Istream &is)
Read from Istream, discarding existing contents.
Definition: DynamicListIO.C:50
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
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:94
SubList< T > slice(const label pos, label len=-1)
Return SubList slice (non-const access) - no range checking.
Definition: SubList.H:165
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition: Scalar.H:223
volScalarField & b
Definition: createFields.H:27
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:54
const Vector< label > N(dict.get< Vector< label > >("N"))