Loading...
Searching...
No Matches
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-2025 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>
60
61template<class T, int SizeMin>
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 doAssignDynList(const ListType& list);
93
94 //- Alter the size of the underlying storage,
95 //- retaining the first count elements.
96 inline void doCapacity_copy(label count, const label len);
97
98 //- Reserve allocation space for at least this size,
99 //- retaining the first count elements.
100 // Never shrinks the allocated size, use setCapacity() for that.
101 inline void doReserve_copy(label count, const label len);
102
103 //- Read List from Istream between '(' and ')' delimiters.
104 //- The size is not known a priori.
105 bool readBracketList(Istream& is);
106
107
108public:
109
110 // Constructors
111
112 //- Default construct, an empty list without allocation.
113 inline constexpr DynamicList() noexcept;
114
115 //- Construct an empty list with given initial capacity
116 inline explicit DynamicList(const label initialCapacity);
117
118 //- Construct with given size and capacity
119 inline explicit DynamicList(std::pair<label,label> sizing);
120
121 //- Construct with given size and value for all elements.
122 inline DynamicList(const label len, const T& val);
123
124 //- Construct with given size initializing all elements to zero
125 inline DynamicList(const label len, Foam::zero);
126
127 //- Copy construct.
128 inline DynamicList(const DynamicList<T, SizeMin>& lst);
129
130 //- Copy construct from DynamicList with different sizing parameters
131 template<int AnySizeMin>
132 inline DynamicList(const DynamicList<T, AnySizeMin>& lst);
133
134 //- Copy construct from UList. Size set to UList size.
135 // Also constructs from DynamicList with different sizing parameters.
136 inline explicit DynamicList(const UList<T>& lst);
137
138 //- Copy construct subset of list
139 inline DynamicList(const UList<T>& list, const labelUList& indices);
141 //- Copy construct from a FixedList
142 template<unsigned N>
143 inline explicit DynamicList(const FixedList<T, N>& lst);
144
145 //- Copy construct from an initializer list. Size set to list size.
146 inline explicit DynamicList(std::initializer_list<T> lst);
147
148 //- Copy construct from IndirectList. Size set to addressing size.
149 template<class Addr>
150 inline explicit DynamicList(const IndirectListBase<T, Addr>& lst);
151
152 //- Move construct.
153 inline DynamicList(DynamicList<T, SizeMin>&& list) noexcept;
154
155 //- Move construct with different sizing parameters
156 template<int AnySizeMin>
157 inline DynamicList(DynamicList<T, AnySizeMin>&& list) noexcept;
158
159 //- Move construct from List
160 inline DynamicList(List<T>&& list) noexcept;
161
162 //- Construct from Istream. Size set to size of list read.
163 explicit DynamicList(Istream& is);
164
165
166 //- Destructor, sync allocated size before List destruction
169
170 // Member Functions
171
172 // Capacity
173
174 //- Normal lower capacity limit - the SizeMin template parameter
175 static constexpr label min_size() noexcept { return SizeMin; }
176
177 //- Size of the underlying storage.
178 label capacity() const noexcept { return capacity_; }
180 //- Number of contiguous bytes of the underlying storage.
181 // \note Only meaningful for contiguous data
182 inline std::streamsize capacity_bytes() const noexcept;
183
184
185 // Sizing
186
187 //- Alter the size of the underlying storage.
188 // The addressed size will be truncated if needed to fit, but will
189 // remain otherwise untouched.
190 // Use this or reserve() in combination with push_back().
191 inline void setCapacity(const label len);
192
193 //- Alter the size of the underlying storage,
194 //- \em without retaining old content.
195 // The addressed size will be truncated if needed to fit, but will
196 // remain otherwise untouched.
197 inline void setCapacity_nocopy(const label len);
198
199 //- Change the value for the list capacity directly (ADVANCED, UNSAFE)
200 //- Does not perform any memory management or resizing.
201 void setCapacity_unsafe(label len) noexcept { capacity_ = len; }
202
203 //- Reserve allocation space for at least this size, allocating new
204 //- space if required and \em retaining old content.
205 // Never shrinks the allocated size, use setCapacity() for that.
206 inline void reserve(const label len);
207
208 //- Reserve allocation space for at least this size, allocating new
209 //- space if required \em without retaining old content.
210 // Never shrinks the allocated size, use setCapacity() for that.
211 inline void reserve_nocopy(const label len);
213 //- Reserve allocation space for at least this size, allocating new
214 //- space if required and \em retaining old content.
215 //- If allocation is required, uses the specified size
216 //- without any other resizing logic.
217 inline void reserve_exact(const label len);
218
219 //- Alter addressable list size, allocating new space if required
220 //- while \em recovering old content.
221 // If no reallocation is required, the contents remain untouched.
222 // Otherwise new entries will be uninitialized.
223 // Use this to resize the list prior to using the operator[] for
224 // setting values (as per List usage).
225 inline void resize(const label len);
226
227 //- Alter addressable size and fill \em new entries with constant value
228 inline void resize(const label len, const T& val);
229
230 //- Alter addressable size, retaining the first count contents.
231 // \note Only uses a limited number of internal checks.
232 void resize_copy(label count, const label len);
233
234 //- Alter addressable size and set val for \em all addressed entries
235 inline void resize_fill(const label len, const T& val);
236
237 //- Alter addressable list size, allocating new space if required
238 //- \em without necessarily recovering old content.
239 // If no reallocation is required, the contents remain untouched.
240 // Otherwise all entries will be uninitialized.
241 inline void resize_nocopy(const label len);
242
243 //- Clear the addressed list, i.e. set the size to zero.
244 // Allocated size does not change
245 inline void clear() noexcept;
247 //- Clear the list and delete storage.
248 inline void clearStorage();
249
250 //- Shrink the allocated space to the number of elements used.
251 inline void shrink_to_fit();
252
253
254 // Edit
256 //- Swap with plain List content. Implies shrink_to_fit().
257 inline void swap(List<T>& other);
258
259 //- Swap content, independent of sizing parameter
260 template<int AnySizeMin>
261 inline void swap(DynamicList<T, AnySizeMin>& other) noexcept;
262
263 //- Transfer contents of the argument List into this.
264 inline void transfer(List<T>& list);
265
266 //- Transfer contents of any DynamicList into this.
267 template<int AnySizeMin>
268 inline void transfer(DynamicList<T, AnySizeMin>& list);
270 //- Construct an element at the end of the list,
271 //- return reference to the new list element
272 template<class... Args>
273 inline T& emplace_back(Args&&... args);
274
275 //- Copy append an element to the end of this list.
276 inline void push_back(const T& val);
278 //- Move append an element
279 inline void push_back(T&& val);
280
281 //- Copy append another list to the end of this list.
282 inline void push_back(const UList<T>& list);
283
284 //- Copy append a FixedList to the end of this list.
285 template<unsigned N>
286 inline void push_back(const FixedList<T, N>& list);
287
288 //- Copy append an initializer list at the end of this list.
289 inline void push_back(std::initializer_list<T> list);
290
291 //- Copy append an IndirectList at the end of this list
292 template<class Addr>
293 inline void push_back(const IndirectListBase<T, Addr>& lst);
294
295 //- Move append another list to the end of this list
296 inline void push_back(List<T>&& list);
297
298 //- Move append list
299 template<int AnySizeMin>
300 inline void push_back(DynamicList<T, AnySizeMin>&& list);
302 //- Append an element if not already in the list.
303 // \return the change in list length
304 inline label push_uniq(const T& val);
305
306 //- Reduce size by 1 or more elements. Can be called on an empty list.
307 inline void pop_back(label n = 1);
309
310 // Edit
311
312 //- Remove and return the last element. Fatal on an empty list.
313 inline T remove();
314
315 //- Remove and return the specified element. Fatal on an empty list.
316 // With fast=true (operates in constant time), the place of the
317 // removed element is swapped with the last one in the list, which
318 // changes the ordering.
319 // With fast=false (operates in linear time), the elements
320 // are swapped down in the list to preserve ordering.
321 inline T remove(const label idx, const bool fast=false);
323 //- Remove a (start,size) subset from the list.
324 // The range is subsetted with the list size itself to ensure
325 // result always addresses a valid section of the list.
326 // Remaining elements are moved down.
327 inline label remove(const labelRange& range);
328
329 //- Remove a (start,size) subset from the list.
330 inline label remove(std::initializer_list<label> start_size);
331
332 //- Retain a (start,size) subset from the list.
333 // The range is subsetted with the list size itself to ensure
334 // result always addresses a valid section of the list.
335 inline label subset(const labelRange& range);
336
337 //- Retain a (start,size) subset from List.
338 inline label subset(std::initializer_list<label> start_size);
340
341 // Member Operators
342
343 //- Return non-const access to an element, resizing list if needed
344 inline T& operator()(const label i);
345
346 //- Assign addressed entries to the given value
347 inline void operator=(const T& val);
348
349 //- Assign addressed entries to zero
350 inline void operator=(Foam::zero);
351
352 //- Assignment to UList
353 inline void operator=(const UList<T>& lst);
354
355 //- Assignment to FixedList
356 template<unsigned N>
357 inline void operator=(const FixedList<T, N>& lst);
359 //- Assignment to DynamicList
360 inline void operator=(const DynamicList<T, SizeMin>& lst);
361
362 //- Assignment from DynamicList with different sizing parameters
363 template<int AnySizeMin>
364 inline void operator=(const DynamicList<T, AnySizeMin>& lst);
365
366 //- Assignment from initializer list
367 inline void operator=(std::initializer_list<T> lst);
368
369 //- Assignment from IndirectList
370 template<class Addr>
371 inline void operator=(const IndirectListBase<T, Addr>& lst);
372
373 //- Move assignment
374 inline void operator=(List<T>&& lst);
375
376 //- Move assignment
377 inline void operator=(DynamicList<T, SizeMin>&& lst);
378
379 //- Move assignment
380 template<int AnySizeMin>
381 inline void operator=(DynamicList<T, AnySizeMin>&& lst);
382
383
384 // Reading/writing
385
386 //- Read from Istream, discarding existing contents
388
389
390 // IOstream Operators
391
392 //- Use the readList() method to read contents from Istream.
393 friend Istream& operator>> <T, SizeMin>
394 (
395 Istream& is,
396 DynamicList<T, SizeMin>& list
397 );
398
399 //- Write to Ostream
400 friend Ostream& operator<< <T, SizeMin>
401 (
402 Ostream& os,
403 const DynamicList<T, SizeMin>& list
404 );
405
406
407 // Housekeeping
409 //- Alias for resize()
410 void setSize(const label n) { this->resize(n); }
411
412 //- Alias for resize()
413 void setSize(const label n, const T& val) { this->resize(n, val); }
415 //- Calls shrink_to_fit() and returns a reference to the DynamicList.
416 //FOAM_DEPRECATED_FOR(2025-04, "shrink_to_fit()")
418 {
419 this->shrink_to_fit();
420 return *this;
422
423 //- Copy append an element to the end of this list.
424 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
425 void append(const T& val) { this->push_back(val); }
427 //- Move append an element
428 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
429 void append(T&& val) { this->push_back(std::move(val)); }
430
431 //- Append another list to the end of this list.
432 void append(const UList<T>& list) { this->push_back(list); }
433
434 //- Append a FixedList to the end of this list.
435 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
436 template<unsigned N>
437 void append(const FixedList<T, N>& list) { this->push_back(list); }
438
439 //- Append an initializer list at the end of this list.
440 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
441 void append(std::initializer_list<T> list) { this->push_back(list); }
442
443 //- Append a IndirectList at the end of this list
444 template<class Addr>
446 {
447 this->push_back(list);
448 }
449
450 //- Move append list
451 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
452 void append(List<T>&& list) { this->push_back(std::move(list)); }
453
454 //- Move append list
455 template<int AnySizeMin>
457 {
458 this->push_back(std::move(list));
460
461 //- Same as push_uniq()
462 FOAM_DEPRECATED_FOR(2022-10, "push_uniq()")
463 label appendUniq(const T& val) { return this->push_uniq(val); }
464};
465
466
467// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
468
469//- Read List contents from Istream
470template<class T, int SizeMin>
473 return list.readList(is);
474}
475
476
477//- Write List to Ostream, as per UList::writeList() with default length.
478template<class T, int SizeMin>
481 return (os << static_cast<const UList<T>&>(list));
482}
483
484
485// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
486
487//- Hashing for List data
488template<class T, int SizeMin>
489struct Hash<DynamicList<T, SizeMin>> : List<T>::hasher {};
491} // End namespace Foam
492
493
494// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
496namespace Foam
497{
498
499//- Exchange contents of lists - see DynamicList::swap().
500// Works for lists with dissimilar SizeMin template parameters.
501// If the SizeMin template parameters are identical, a regular std::swap
502// works (since DynamicList is MoveConstructible and MoveAssignable)
503template<class T, int SizeMinA, int SizeMinB>
505{
506 a.swap(b);
507}
508
509} // End namespace Foam
510
511
512// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
513
514#include "DynamicListI.H"
515
516// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
518#ifdef NoRepository
519 #include "DynamicList.C"
520 #include "DynamicListIO.C"
521#endif
522
523// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
524
525#endif
526
527// ************************************************************************* //
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:68
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
constexpr DynamicList() noexcept
Default construct, an empty list without allocation.
void transfer(List< float > &list)
void append(std::initializer_list< T > list)
Append an initializer list at the end of this list.
void setSize(const label n)
void pop_back(label n=1)
label appendUniq(const float &val)
void resize_nocopy(const label len)
Alter addressable list size, allocating new space if required without necessarily recovering old cont...
void setCapacity_nocopy(const label len)
float & emplace_back(Args &&... args)
void setCapacity_unsafe(label len) noexcept
void append(DynamicList< T, AnySizeMin > &&list)
Move append list.
void append(const IndirectListBase< T, Addr > &list)
Append a IndirectList at the end of this list.
void reserve_nocopy(const label len)
Reserve allocation space for at least this size, allocating new space if required without retaining o...
void reserve_exact(const label len)
Reserve allocation space for at least this size, allocating new space if required and retaining old c...
std::streamsize capacity_bytes() const noexcept
Number of contiguous bytes of the underlying storage.
DynamicList(const DynamicList< T, SizeMin > &lst)
Copy construct.
void resize_copy(label count, const label len)
Alter addressable size, retaining the first count contents.
void append(const T &val)
Copy append an element to the end of this list.
void resize(const label len, const T &val)
Alter addressable size and fill new entries with constant value.
void append(List< T > &&list)
Move append list.
~DynamicList()
Destructor, sync allocated size before List destruction.
void swap(List< float > &other)
void append(T &&val)
Move append an element.
label push_uniq(const float &val)
void setSize(const label n, const T &val)
Alias for resize().
void append(const FixedList< T, N > &list)
Append a FixedList to the end of this list.
void push_back(const float &val)
DynamicList< T, SizeMin > & shrink()
Calls shrink_to_fit() and returns a reference to the DynamicList.
static constexpr label min_size() noexcept
Normal lower capacity limit - the SizeMin template parameter.
label capacity() const noexcept
Size of the underlying storage.
friend Ostream & operator(Ostream &os, const DynamicList< float, 16 > &list)
void resize(const label len)
Alter addressable list size, allocating new space if required while recovering old content.
void reserve(const label len)
Reserve allocation space for at least this size, allocating new space if required and retaining old c...
label subset(const labelRange &range)
void resize_fill(const label len, const T &val)
Alter addressable size and set val for all addressed entries.
void setCapacity(const label len)
Istream & readList(Istream &is)
void append(const UList< T > &list)
Append another list to the end of this list.
DynamicList(const UList< T > &lst)
Copy construct from UList. Size set to UList size.
A 1D vector of objects of type <T> with a fixed length <N>.
Definition FixedList.H:73
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: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:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
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:89
SubList< T > slice(const label pos, label len=-1)
Return SubList slice (non-const access) - no range checking.
Definition SubList.H:258
void setAddressableSize(const label n) noexcept
Set addressed size to be inconsistent with allocated storage.
Definition UListI.H:517
A range or interval of labels defined by a start and a size.
Definition labelRange.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition scalarImpl.H:265
UList< label > labelUList
A UList of labels.
Definition UList.H:75
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)
volScalarField & b
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition Hash.H:48
const Vector< label > N(dict.get< Vector< label > >("N"))