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-2023 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 value for all elements.
124  inline DynamicList(const label len, const T& val);
125 
126  //- Construct with given size initializing all elements to zero
127  inline DynamicList(const label len, const Foam::zero);
128 
129  //- Copy construct.
130  inline DynamicList(const DynamicList<T, SizeMin>& lst);
131 
132  //- Copy construct from DynamicList with different sizing parameters
133  template<int AnySizeMin>
134  inline DynamicList(const DynamicList<T, AnySizeMin>& lst);
135 
136  //- Construct from UList. Size set to UList size.
137  // Also constructs from DynamicList with different sizing parameters.
138  inline explicit DynamicList(const UList<T>& lst);
139 
140  //- Copy construct subset of list
141  inline DynamicList(const UList<T>& list, const labelUList& indices);
142 
143  //- Construct from a FixedList
144  template<unsigned N>
145  inline explicit DynamicList(const FixedList<T, N>& lst);
146 
147  //- Construct from an initializer list. Size set to list size.
148  inline explicit DynamicList(std::initializer_list<T> lst);
149 
150  //- Construct from IndirectList. Size set to addressing size.
151  template<class Addr>
152  inline explicit DynamicList(const IndirectListBase<T, Addr>& lst);
153 
154  //- Move construct.
156 
157  //- Move construct with different sizing parameters
158  template<int AnySizeMin>
160 
161  //- Move construct from List
162  inline DynamicList(List<T>&& list) noexcept;
163 
164  //- Construct from Istream. Size set to size of list read.
165  explicit DynamicList(Istream& is);
166 
167 
168  // Member Functions
169 
170  // Capacity
171 
172  //- Normal lower capacity limit - the SizeMin template parameter
173  static constexpr label min_size() noexcept { return SizeMin; }
174 
175  //- Size of the underlying storage.
176  label capacity() const noexcept { return capacity_; }
177 
178  //- Number of contiguous bytes of the underlying storage.
179  // \note Only meaningful for contiguous data
180  inline std::streamsize capacity_bytes() const noexcept;
181 
182 
183  // Sizing
184 
185  //- Alter the size of the underlying storage.
186  // The addressed size will be truncated if needed to fit, but will
187  // remain otherwise untouched.
188  // Use this or reserve() in combination with push_back().
189  inline void setCapacity(const label len);
190 
191  //- Alter the size of the underlying storage,
192  //- \em without retaining old content.
193  // The addressed size will be truncated if needed to fit, but will
194  // remain otherwise untouched.
195  inline void setCapacity_nocopy(const label len);
196 
197  //- Change the value for the list capacity directly (ADVANCED, UNSAFE)
198  //- Does not perform any memory management or resizing.
199  inline void setCapacity_unsafe(const label len) noexcept;
200 
201  //- Reserve allocation space for at least this size, allocating new
202  //- space if required and \em retaining old content.
203  // Never shrinks the allocated size, use setCapacity() for that.
204  inline void reserve(const label len);
205 
206  //- Reserve allocation space for at least this size, allocating new
207  //- space if required \em without retaining old content.
208  // Never shrinks the allocated size, use setCapacity() for that.
209  inline void reserve_nocopy(const label len);
210 
211  //- Alter addressable list size, allocating new space if required
212  //- while \em recovering old content.
213  // If no reallocation is required, the contents remain untouched.
214  // Otherwise new entries will be uninitialized.
215  // Use this to resize the list prior to using the operator[] for
216  // setting values (as per List usage).
217  inline void resize(const label len);
218 
219  //- Alter addressable size and fill \em new entries with constant value
220  inline void resize(const label len, const T& val);
221 
222  //- Alter addressable size and set val for \em all addressed entries
223  inline void resize_fill(const label len, const T& val);
224 
225  //- Alter addressable list size, allocating new space if required
226  //- \em without necessarily recovering old content.
227  // If no reallocation is required, the contents remain untouched.
228  // Otherwise all entries will be uninitialized.
229  inline void resize_nocopy(const label len);
230 
231  //- Same as resize()
232  void setSize(const label n) { this->resize(n); }
233 
234  //- Same as resize()
235  void setSize(const label n, const T& val) { this->resize(n, val); }
236 
237  //- Clear the addressed list, i.e. set the size to zero.
238  // Allocated size does not change
239  inline void clear() noexcept;
240 
241  //- Clear the list and delete storage.
242  inline void clearStorage();
243 
244  //- Shrink the allocated space to the number of elements used.
245  inline void shrink_to_fit();
246 
247  //- Shrink the internal bookkeeping of the allocated space to the
248  //- number of addressed elements without affecting allocation.
249  // \note when empty() it will delete any allocated memory.
250  inline void shrink_unsafe();
251 
252  //- Calls shrink_to_fit() and returns a reference to the DynamicList.
253  inline DynamicList<T, SizeMin>& shrink();
254 
255 
256  // Edit
257 
258  //- Swap with plain List content. Implies shrink_to_fit().
259  inline void swap(List<T>& list);
260 
261  //- Swap content, independent of sizing parameter
262  template<int AnySizeMin>
263  inline void swap(DynamicList<T, AnySizeMin>& other) noexcept;
264 
265  //- Transfer contents of the argument List into this.
266  inline void transfer(List<T>& list);
267 
268  //- Transfer contents of any sized DynamicList into this.
269  template<int AnySizeMin>
270  inline void transfer(DynamicList<T, AnySizeMin>& list);
271 
272  //- Construct an element at the end of the list,
273  //- return reference to the new list element
274  template<class... Args>
275  inline T& emplace_back(Args&&... args);
276 
277  //- Copy append an element to the end of this list.
278  inline void push_back(const T& val);
279 
280  //- Move append an element
281  inline void push_back(T&& val);
282 
283  //- Copy append another list to the end of this list.
284  inline void push_back(const UList<T>& list);
285 
286  //- Copy append a FixedList to the end of this list.
287  template<unsigned N>
288  inline void push_back(const FixedList<T, N>& list);
289 
290  //- Copy append an initializer list at the end of this list.
291  inline void push_back(std::initializer_list<T> list);
292 
293  //- Copy append an IndirectList at the end of this list
294  template<class Addr>
295  inline void push_back(const IndirectListBase<T, Addr>& lst);
296 
297  //- Move append list
298  inline void push_back(List<T>&& list);
299 
300  //- Move append list
301  template<int AnySizeMin>
302  inline void push_back(DynamicList<T, AnySizeMin>&& list);
303 
304  //- Append an element if not already in the list.
305  // \return the change in list length
306  inline label push_uniq(const T& val);
307 
308  //- Reduce size by 1 or more elements. Can be called on an empty list.
309  inline void pop_back(label n = 1);
311 
312  // Edit
313 
314  //- Remove and return the last element. Fatal on an empty list.
315  inline T remove();
316 
317  //- Remove and return the specified element. Fatal on an empty list.
318  // With fast=true (operates in constant time), the place of the
319  // removed element is swapped with the last one in the list, which
320  // changes the ordering.
321  // With fast=false (operates in linear time), the elements
322  // are swapped down in the list to preserve ordering.
323  inline T remove(const label idx, const bool fast=false);
324 
325  //- Remove a (start,size) subset from the list.
326  // The range is subsetted with the list size itself to ensure
327  // result always addresses a valid section of the list.
328  // Remaining elements are moved down.
329  inline label remove(const labelRange& range);
330 
331  //- Remove a (start,size) subset from the list.
332  inline label remove(std::initializer_list<label> start_size);
333 
334  //- Retain a (start,size) subset from the list.
335  // The range is subsetted with the list size itself to ensure
336  // result always addresses a valid section of the list.
337  inline label subset(const labelRange& range);
338 
339  //- Retain a (start,size) subset from List.
340  inline label subset(std::initializer_list<label> start_size);
341 
342 
343  // Member Operators
344 
345  //- Return non-const access to an element, resizing list if needed
346  inline T& operator()(const label i);
347 
348  //- Assignment of all addressed entries to the given value
349  inline void operator=(const T& val);
350 
351  //- Assignment of all entries to zero
352  inline void operator=(const Foam::zero);
353 
354  //- Assignment to UList
355  inline void operator=(const UList<T>& lst);
356 
357  //- Assignment to FixedList
358  template<unsigned N>
359  inline void operator=(const FixedList<T, N>& lst);
360 
361  //- Assignment to DynamicList
362  inline void operator=(const DynamicList<T, SizeMin>& lst);
363 
364  //- Assignment from DynamicList with different sizing parameters
365  template<int AnySizeMin>
366  inline void operator=(const DynamicList<T, AnySizeMin>& lst);
367 
368  //- Assignment from initializer list
369  inline void operator=(std::initializer_list<T> lst);
370 
371  //- Assignment from IndirectList
372  template<class Addr>
373  inline void operator=(const IndirectListBase<T, Addr>& lst);
374 
375  //- Move assignment
376  inline void operator=(List<T>&& lst);
377 
378  //- Move assignment
379  inline void operator=(DynamicList<T, SizeMin>&& lst);
380 
381  //- Move assignment
382  template<int AnySizeMin>
383  inline void operator=(DynamicList<T, AnySizeMin>&& lst);
384 
385 
386  // Reading/writing
387 
388  //- Read from Istream, discarding existing contents
389  Istream& readList(Istream& is);
390 
391 
392  // IOstream Operators
393 
394  //- Use the readList() method to read contents from Istream.
395  friend Istream& operator>> <T, SizeMin>
396  (
397  Istream& is,
398  DynamicList<T, SizeMin>& list
399  );
400 
401  //- Write to Ostream
402  friend Ostream& operator<< <T, SizeMin>
403  (
404  Ostream& os,
405  const DynamicList<T, SizeMin>& list
406  );
407 
408 
409  // Housekeeping
410 
411  //- Copy append an element to the end of this list.
412  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
413  void append(const T& val) { this->push_back(val); }
414 
415  //- Move append an element
416  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
417  void append(T&& val) { this->push_back(std::move(val)); }
418 
419  //- Append another list to the end of this list.
420  void append(const UList<T>& list) { this->push_back(list); }
421 
422  //- Append a FixedList to the end of this list.
423  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
424  template<unsigned N>
425  void append(const FixedList<T, N>& list) { this->push_back(list); }
426 
427  //- Append an initializer list at the end of this list.
428  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
429  void append(std::initializer_list<T> list) { this->push_back(list); }
430 
431  //- Append a IndirectList at the end of this list
432  template<class Addr>
433  void append(const IndirectListBase<T, Addr>& list)
434  {
435  this->push_back(list);
436  }
437 
438  //- Move append list
439  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
440  void append(List<T>&& list) { this->push_back(std::move(list)); }
441 
442  //- Move append list
443  template<int AnySizeMin>
444  void append(DynamicList<T, AnySizeMin>&& list)
445  {
446  this->push_back(std::move(list));
447  }
448 
449  //- Same as push_uniq()
450  FOAM_DEPRECATED_STRICT(2022-10, "push_uniq()")
451  label appendUniq(const T& val) { return this->push_uniq(val); }
452 };
453 
454 
455 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
456 
457 //- Read List contents from Istream
458 template<class T, int SizeMin>
459 Istream& operator>>(Istream& is, DynamicList<T, SizeMin>& list)
460 {
461  return list.readList(is);
462 }
463 
464 
465 //- Write List to Ostream, as per UList::writeList() with default length.
466 template<class T, int SizeMin>
467 Ostream& operator<<(Ostream& os, const DynamicList<T, SizeMin>& list)
468 {
469  return (os << static_cast<const UList<T>&>(list));
470 }
471 
472 
473 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
474 
475 //- Hashing for List data
476 template<class T, int SizeMin>
477 struct Hash<DynamicList<T, SizeMin>> : List<T>::hasher {};
478 
479 } // End namespace Foam
480 
481 
482 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
483 
484 namespace Foam
485 {
486 
487 //- Exchange contents of lists - see DynamicList::swap().
488 // Works for lists with dissimilar SizeMin template parameters.
489 // If the SizeMin template parameters are identical, a regular std::swap
490 // works (since DynamicList is MoveConstructible and MoveAssignable)
491 template<class T, int SizeMinA, int SizeMinB>
492 inline void Swap(DynamicList<T, SizeMinA>& a, DynamicList<T, SizeMinB>& b)
493 {
494  a.swap(b);
495 }
496 
497 } // End namespace Foam
498 
499 
500 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
501 
502 #include "DynamicListI.H"
503 
504 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
505 
506 #ifdef NoRepository
507  #include "DynamicList.C"
508  #include "DynamicListIO.C"
509 #endif
510 
511 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
512 
513 #endif
514 
515 // ************************************************************************* //
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:343
constexpr DynamicList() noexcept
Default construct, an empty list without allocation.
Definition: DynamicListI.H:131
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:434
void resize(const label len)
Alter addressable list size, allocating new space if required while recovering old content...
Definition: DynamicListI.H:353
label appendUniq(const T &val)
Same as push_uniq()
Definition: DynamicList.H:642
void setCapacity(const label len)
Alter the size of the underlying storage.
Definition: DynamicListI.H:303
void setSize(const label n)
Same as resize()
Definition: DynamicList.H:310
void resize_fill(const label len, const T &val)
Alter addressable size and set val for all addressed entries.
Definition: DynamicListI.H:363
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: DynamicListI.H:701
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:420
friend class List< T >
Declare friendship with the List class.
Definition: UList.H:219
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicList.H:225
static constexpr label min_size() noexcept
Normal lower capacity limit - the SizeMin template parameter.
Definition: DynamicList.H:220
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:246
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:538
Istream & operator>>(Istream &, directionInfo &)
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: DynamicList.H:584
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition: stdFoam.H:55
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:333
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: Scalar.H:258
void setCapacity_unsafe(const label len) noexcept
Change the value for the list capacity directly (ADVANCED, UNSAFE) Does not perform any memory manage...
Definition: DynamicListI.H:323
DynamicList< T, SizeMin > & shrink()
Calls shrink_to_fit() and returns a reference to the DynamicList.
Definition: DynamicListI.H:447
OBJstream os(runTime.globalPath()/outputName)
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:405
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:555
void swap(List< T > &list)
Swap with plain List content. Implies shrink_to_fit().
Definition: DynamicListI.H:456
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:686
T remove()
Remove and return the last element. Fatal on an empty list.
Definition: DynamicListI.H:715
constexpr UList() noexcept
Default construct, zero-sized and nullptr.
Definition: UListI.H:28
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
Definition: DynamicList.H:692
void transfer(List< T > &list)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:504
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:412
label subset(const labelRange &range)
Retain a (start,size) subset from the list.
Definition: DynamicListI.H:779
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:375
Foam::argList args(argc, argv)
constexpr List() noexcept
Default construct.
Definition: ListI.H:116
void setCapacity_nocopy(const label len)
Alter the size of the underlying storage, without retaining old content.
Definition: DynamicListI.H:313
Namespace for OpenFOAM.