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-2021 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 DynamicList_H
46 #define 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>
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 assignDynList(const ListType& list);
93 
94  //- Alter the size of the underlying storage
95  // The 'nocopy' option will not attempt to recover old content
96  inline void doCapacity(const bool nocopy, const label len);
97 
98  //- Reserve allocation space for at least this size.
99  // Never shrinks the allocated size, use setCapacity() for that.
100  // The 'nocopy' option will not attempt to recover old content
101  inline void doReserve(const bool nocopy, const label len);
102 
103  //- Reserve allocation space for at least this size.
104  // Never shrinks the allocated size, use setCapacity() for that.
105  // The 'nocopy' option will not attempt to recover old content
106  inline void doResize(const bool nocopy, const label len);
107 
108 
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  //- Construct from a FixedList
137  template<unsigned N>
138  inline explicit DynamicList(const FixedList<T, N>& lst);
139 
140  //- Construct from an initializer list. Size set to list size.
141  inline explicit DynamicList(std::initializer_list<T> lst);
142 
143  //- Construct from IndirectList. Size set to addressing size.
144  template<class Addr>
145  inline explicit DynamicList(const IndirectListBase<T, Addr>& lst);
146 
147  //- Move construct.
148  inline DynamicList(DynamicList<T, SizeMin>&& lst);
149 
150  //- Move construct with different sizing parameters
151  template<int AnySizeMin>
152  inline DynamicList(DynamicList<T, AnySizeMin>&& lst);
153 
154  //- Move construct from List
155  inline DynamicList(List<T>&& lst);
156 
157  //- Move construct from SortableList
158  DynamicList(SortableList<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  inline label capacity() const noexcept;
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 append().
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);
214 
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  //- Transfer contents of the argument SortableList into this.
263  inline void transfer(SortableList<T>& list);
264 
265  //- Append an element to the end of this list.
266  inline DynamicList<T, SizeMin>& append(const T& val);
267 
268  //- Move append an element
269  inline DynamicList<T, SizeMin>& append(T&& val);
270 
271  //- Append another list to the end of this list.
272  inline DynamicList<T, SizeMin>& append(const UList<T>& lst);
273 
274  //- Append a FixedList to the end of this list.
275  template<unsigned N>
276  inline DynamicList<T, SizeMin>&
277  append(const FixedList<T, N>& lst);
278 
279  //- Append an initializer list at the end of this list.
280  inline DynamicList<T, SizeMin>&
281  append(std::initializer_list<T> lst);
282 
283  //- Append a IndirectList at the end of this list
284  template<class Addr>
285  inline DynamicList<T, SizeMin>&
286  append(const IndirectListBase<T, Addr>& lst);
287 
288  //- Move append list
289  inline DynamicList<T, SizeMin>& append(List<T>&& lst);
290 
291  //- Move append list
292  inline DynamicList<T, SizeMin>&
293  append(DynamicList<T, SizeMin>&& lst);
294 
295  //- Move append list
296  template<int AnySizeMin>
297  inline DynamicList<T, SizeMin>&
298  append(DynamicList<T, AnySizeMin>&& lst);
299 
300  //- Move append list
301  inline DynamicList<T, SizeMin>&
302  append(SortableList<T>&& lst);
303 
304  //- Append an element if not already in the list.
305  // \return the change in list length
306  inline label appendUniq(const T& val);
307 
308  //- Remove and return the last element. Fatal on an empty list.
309  inline T remove();
310 
311  //- Remove and return the specified element. Fatal on an empty list.
312  // With fast=true (operates in constant time), the place of the
313  // removed element is swapped with the last one in the list, which
314  // changes the ordering.
315  // With fast=false (operates in linear time), the elements
316  // are swapped down in the list to preserve ordering.
317  inline T remove(const label idx, const bool fast=false);
318 
319  //- Remove a (start,size) subset from the list.
320  // The range is subsetted with the list size itself to ensure
321  // result always addresses a valid section of the list.
322  // Remaining elements are moved down.
323  inline label remove(const labelRange& range);
324 
325  //- Remove a (start,size) subset from the list.
326  inline label remove(std::initializer_list<label> start_size);
327 
328  //- Retain a (start,size) subset from the list.
329  // The range is subsetted with the list size itself to ensure
330  // result always addresses a valid section of the list.
331  // Remaining elements are moved down.
332  inline label subset(const labelRange& range);
333 
334  //- Retain a (start,size) subset from List.
335  inline label subset(std::initializer_list<label> start_size);
336 
337 
338  // Member Operators
339 
340  //- Return non-const access to an element, resizing list if needed
341  inline T& operator()(const label i);
342 
343  //- Assignment of all addressed entries to the given value
344  inline void operator=(const T& val);
345 
346  //- Assignment of all entries to zero
347  inline void operator=(const Foam::zero);
348 
349  //- Assignment to UList
350  inline void operator=(const UList<T>& lst);
351 
352  //- Assignment to FixedList
353  template<unsigned N>
354  inline void operator=(const FixedList<T, N>& lst);
355 
356  //- Assignment to DynamicList
357  inline void operator=(const DynamicList<T, SizeMin>& lst);
358 
359  //- Assignment from DynamicList with different sizing parameters
360  template<int AnySizeMin>
361  inline void operator=(const DynamicList<T, AnySizeMin>& lst);
362 
363  //- Assignment from initializer list
364  inline void operator=(std::initializer_list<T> lst);
365 
366  //- Assignment from IndirectList
367  template<class Addr>
368  inline void operator=(const IndirectListBase<T, Addr>& lst);
369 
370  //- Move assignment
371  inline void operator=(List<T>&& lst);
372 
373  //- Move assignment
374  inline void operator=(DynamicList<T, SizeMin>&& lst);
375 
376  //- Move assignment
377  template<int AnySizeMin>
378  inline void operator=(DynamicList<T, AnySizeMin>&& lst);
379 
380  //- Move assignment
381  inline void operator=(SortableList<T>&& lst);
382 
383 
384  // Reading/writing
385 
386  //- Read from Istream, discarding existing contents
387  Istream& readList(Istream& is);
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 
408 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
409 
410 //- Read List contents from Istream
411 template<class T, int SizeMin>
412 Istream& operator>>(Istream& is, DynamicList<T, SizeMin>& list)
413 {
414  return list.readList(is);
415 }
416 
417 
418 //- Write List to Ostream, as per UList::writeList() with default length.
419 template<class T, int SizeMin>
421 {
422  return (os << static_cast<const UList<T>&>(list));
423 }
424 
425 
426 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
427 
428 // Exchange contents of lists - see DynamicList::swap().
429 template<class T, int SizeMinA, int SizeMinB>
431 {
432  a.swap(b);
433 }
434 
435 
436 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
437 
438 //- Hashing for List data
439 template<class T, int SizeMin>
440 struct Hash<DynamicList<T, SizeMin>> : List<T>::hasher {};
441 
442 
443 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
444 
445 } // End namespace Foam
446 
447 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
448 
449 #include "DynamicListI.H"
450 
451 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
452 
453 #ifdef NoRepository
454  #include "DynamicList.C"
455  #include "DynamicListIO.C"
456 #endif
457 
458 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
459 
460 #endif
461 
462 // ************************************************************************* //
Foam::DynamicList::resize
void resize(const label len)
Definition: DynamicListI.H:353
Foam::DynamicList::subset
label subset(const labelRange &range)
Retain a (start,size) subset from the list.
Definition: DynamicListI.H:768
Foam::DynamicList::setCapacity_unsafe
void setCapacity_unsafe(const label len) noexcept
Definition: DynamicListI.H:323
Foam::DynamicList::DynamicList
constexpr DynamicList() noexcept
Default construct, an empty list without allocation.
Definition: DynamicListI.H:138
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
List.H
Foam::DynamicList::setCapacity_nocopy
void setCapacity_nocopy(const label len)
Definition: DynamicListI.H:313
Foam::DynamicList::swap
void swap(DynamicList< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
Definition: DynamicListI.H:444
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
DynamicListI.H
Foam::DynamicList::setSize
void setSize(const label n, const T &val)
Same as resize()
Definition: DynamicList.H:227
Foam::DynamicList::shrinkStorage
void shrinkStorage()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:418
Foam::DynamicList::capacity_bytes
std::streamsize capacity_bytes() const noexcept
Number of contiguous bytes of the underlying storage.
Definition: DynamicListI.H:295
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::DynamicList::operator
friend Ostream & operator(Ostream &os, const DynamicList< T, SizeMin > &list)
Write to Ostream.
Foam::DynamicList::reserve
void reserve(const label len)
Definition: DynamicListI.H:333
Foam::DynamicList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicListI.H:287
Foam::DynamicList::shrink
DynamicList< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:434
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
Foam::DynamicList::expandStorage
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
Definition: DynamicListI.H:406
Foam::DynamicList::setCapacity
void setCapacity(const label len)
Alter the size of the underlying storage.
Definition: DynamicListI.H:303
Foam::DynamicList::readList
Istream & readList(Istream &is)
Read from Istream, discarding existing contents.
Definition: DynamicListIO.C:50
DynamicListIO.C
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:511
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
DynamicList.C
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:55
Foam::DynamicList::setSize
void setSize(const label n)
Same as resize()
Definition: DynamicList.H:224
Foam::DynamicList::reserve_nocopy
void reserve_nocopy(const label len)
Definition: DynamicListI.H:343
os
OBJstream os(runTime.globalPath()/outputName)
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:60
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::UList
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:103
Foam::DynamicList::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:398
Foam::DynamicList::remove
T remove()
Remove and return the last element. Fatal on an empty list.
Definition: DynamicListI.H:704
Foam::DynamicList::transfer
void transfer(List< T > &list)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:464
Foam::DynamicList::resize_nocopy
void resize_nocopy(const label len)
Definition: DynamicListI.H:363
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
N
const Vector< label > N(dict.get< Vector< label >>("N"))
Foam::DynamicList::appendUniq
label appendUniq(const T &val)
Append an element if not already in the list.
Definition: DynamicListI.H:689
Foam::DynamicList::min_size
static constexpr label min_size() noexcept
Normal lower capacity limit - the SizeMin template parameter.
Definition: DynamicList.H:168
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62