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