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