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-2020 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  DynamicListI.H
40  DynamicList.C
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #ifndef DynamicList_H
45 #define DynamicList_H
46 
47 #include "List.H"
48 #include <type_traits>
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>
59 Ostream& operator<<
60 (
61  Ostream& os,
62  const DynamicList<T, SizeMin>& lst
63 );
64 
65 template<class T, int SizeMin>
66 Istream& operator>>
67 (
68  Istream& is,
70 );
71 
72 
73 /*---------------------------------------------------------------------------*\
74  Class DynamicList Declaration
75 \*---------------------------------------------------------------------------*/
76 
77 template<class T, int SizeMin = 16>
78 class DynamicList
79 :
80  public List<T>
81 {
82  static_assert(SizeMin > 0, "Invalid min size parameter");
83 
84  // Private Data
85 
86  //- The capacity (allocated size) of the underlying list.
87  label capacity_;
88 
89 
90  // Private Member Functions
91 
92  //- Remove elements in range
93  label removeElements(const labelRange& slice);
94 
95  //- Subset elements in range
96  label subsetElements(const labelRange& slice);
97 
98 
99 protected:
100 
101  // Protected Member Functions
102 
103  //- Copy assignment from another list
104  template<class ListType>
105  inline void assignDynList(const ListType& lst);
106 
107 public:
108 
109  // Related Types
110 
111  //- Declare friendship with the List class
112  friend class List<T>;
113 
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 reserve size.
121  inline explicit DynamicList(const label nElem);
122 
123  //- Construct with given size and value for all elements.
124  inline DynamicList(const label nElem, const T& val);
125 
126  //- Construct with given size initializing all elements to zero
127  inline DynamicList(const label nElem, const 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  //- Construct from a FixedList
141  template<unsigned N>
142  inline DynamicList(const FixedList<T, N>& lst);
143 
144  //- Construct given begin/end iterators.
145  // Uses std::distance to determine the size.
146  template<class InputIterator>
147  inline DynamicList(InputIterator begIter, InputIterator endIter);
148 
149  //- Construct from an initializer list. Size set to list size.
150  inline explicit DynamicList(std::initializer_list<T> lst);
151 
152  //- Construct from IndirectList. Size set to addressing size.
153  template<class Addr>
154  inline explicit DynamicList(const IndirectListBase<T, Addr>& lst);
155 
156  //- Move construct.
157  inline DynamicList(DynamicList<T, SizeMin>&& lst);
158 
159  //- Move construct with different sizing parameters
160  template<int AnySizeMin>
161  inline DynamicList(DynamicList<T, AnySizeMin>&& lst);
162 
163  //- Move construct from List
164  inline DynamicList(List<T>&& lst);
165 
166  //- Move construct from SortableList
167  DynamicList(SortableList<T>&& lst);
168 
169  //- Construct from Istream. Size set to size of list read.
170  explicit DynamicList(Istream& is);
171 
172 
173  // Member Functions
174 
175  // Access
176 
177  //- Normal lower capacity limit - the SizeMin template parameter
178  static constexpr label min_size() noexcept { return SizeMin; }
179 
180  //- Size of the underlying storage.
181  inline label capacity() const noexcept;
182 
183 
184  // Edit
185 
186  //- Alter the size of the underlying storage.
187  // The addressed size will be truncated if needed to fit, but will
188  // remain otherwise untouched.
189  // Use this or reserve() in combination with append().
190  inline void setCapacity(const label nElem);
191 
192  //- Alter addressable list size.
193  // New space will be allocated if required.
194  // Use this to resize the list prior to using the operator[] for
195  // setting values (as per List usage).
196  inline void setSize(const label nElem);
197 
198  //- Alter addressable list size and fill new space with constant.
199  inline void setSize(const label nElem, const T& val);
200 
201  //- Alter addressable list size.
202  // New space will be allocated if required.
203  // Use this to resize the list prior to using the operator[] for
204  // setting values (as per List usage).
205  inline void resize(const label nElem);
206 
207  //- Alter addressable list size and fill new space with constant.
208  inline void resize(const label nElem, const T& val);
209 
210  //- Reserve allocation space for at least this size.
211  // Never shrinks the allocated size, use setCapacity() for that.
212  inline void reserve(const label nElem);
213 
214  //- Clear the addressed list, i.e. set the size to zero.
215  // Allocated size does not change
216  inline void clear();
217 
218  //- Clear the list and delete storage.
219  inline void clearStorage();
220 
221  //- Expand the addressable size to fit the allocated capacity.
222  // Returns the previous addressable size.
223  inline label expandStorage();
224 
225  //- Shrink the allocated space to the number of elements used.
226  // Returns a reference to the DynamicList.
227  inline DynamicList<T, SizeMin>& shrink();
228 
229  //- Swap content with any sized DynamicList
230  template<int AnySizeMin>
231  inline void swap(DynamicList<T, AnySizeMin>& lst);
232 
233  //- Transfer contents of the argument List into this.
234  inline void transfer(List<T>& lst);
235 
236  //- Transfer contents of any sized DynamicList into this.
237  template<int AnySizeMin>
238  inline void transfer(DynamicList<T, AnySizeMin>& lst);
239 
240  //- Transfer contents of the argument SortableList into this.
241  inline void transfer(SortableList<T>& lst);
242 
243  //- Append an element to the end of this list.
244  inline DynamicList<T, SizeMin>& append(const T& val);
245 
246  //- Move append an element
247  inline DynamicList<T, SizeMin>& append(T&& val);
248 
249  //- Append another list to the end of this list.
250  inline DynamicList<T, SizeMin>& append(const UList<T>& lst);
251 
252  //- Append a FixedList to the end of this list.
253  template<unsigned N>
254  inline DynamicList<T, SizeMin>&
255  append(const FixedList<T, N>& lst);
256 
257  //- Append an initializer list at the end of this list.
258  inline DynamicList<T, SizeMin>&
259  append(std::initializer_list<T> lst);
260 
261  //- Append a IndirectList at the end of this list
262  template<class Addr>
263  inline DynamicList<T, SizeMin>&
264  append(const IndirectListBase<T, Addr>& lst);
265 
266  //- Move append list
267  inline DynamicList<T, SizeMin>& append(List<T>&& lst);
268 
269  //- Move append list
270  inline DynamicList<T, SizeMin>&
271  append(DynamicList<T, SizeMin>&& lst);
272 
273  //- Move append list
274  template<int AnySizeMin>
275  inline DynamicList<T, SizeMin>&
276  append(DynamicList<T, AnySizeMin>&& lst);
277 
278  //- Move append list
279  inline DynamicList<T, SizeMin>&
280  append(SortableList<T>&& lst);
281 
282  //- Remove and return the last element. Fatal on an empty list.
283  inline T remove();
284 
285  //- Remove and return the specified element. Fatal on an empty list.
286  // With fast=true (operates in constant time), the place of the
287  // removed element is swapped with the last one in the list, which
288  // changes the ordering.
289  // With fast=false (operates in linear time), the elements
290  // are swapped down in the list to preserve ordering.
291  inline T remove(const label idx, const bool fast=false);
292 
293  //- Remove a (start,size) subset from the list.
294  // The range is subsetted with the list size itself to ensure
295  // result always addresses a valid section of the list.
296  // Remaining elements are moved down.
297  inline label remove(const labelRange& range);
298 
299  //- Remove a (start,size) subset from the list.
300  inline label remove(std::initializer_list<label> start_size);
301 
302  //- Retain a (start,size) subset from the list.
303  // The range is subsetted with the list size itself to ensure
304  // result always addresses a valid section of the list.
305  // Remaining elements are moved down.
306  inline label subset(const labelRange& range);
307 
308  //- Retain a (start,size) subset from List.
309  inline label subset(std::initializer_list<label> start_size);
310 
311 
312  // Member Operators
313 
314  //- Return non-const access to an element,
315  //- resizing list if necessary
316  inline T& operator()(const label i);
317 
318  //- Assignment of all addressed entries to the given value
319  inline void operator=(const T& val);
320 
321  //- Assignment of all entries to zero
322  inline void operator=(const zero);
323 
324  //- Assignment to UList
325  inline void operator=(const UList<T>& lst);
326 
327  //- Assignment to FixedList
328  template<unsigned N>
329  inline void operator=(const FixedList<T, N>& lst);
330 
331  //- Assignment to DynamicList
332  inline void operator=(const DynamicList<T, SizeMin>& lst);
333 
334  //- Assignment from DynamicList with different sizing parameters
335  template<int AnySizeMin>
336  inline void operator=(const DynamicList<T, AnySizeMin>& lst);
337 
338  //- Assignment from initializer list
339  inline void operator=(std::initializer_list<T> lst);
340 
341  //- Assignment from IndirectList
342  template<class Addr>
343  inline void operator=(const IndirectListBase<T, Addr>& lst);
344 
345  //- Move assignment
346  inline void operator=(List<T>&& lst);
347 
348  //- Move assignment
349  inline void operator=(DynamicList<T, SizeMin>&& lst);
350 
351  //- Move assignment
352  template<int AnySizeMin>
353  inline void operator=(DynamicList<T, AnySizeMin>&& lst);
354 
355  //- Move assignment
356  inline void operator=(SortableList<T>&& lst);
357 
358 
359  // IOstream operators
360 
361  // Write DynamicList to Ostream.
362  friend Ostream& operator<< <T, SizeMin>
363  (
364  Ostream& os,
365  const DynamicList<T, SizeMin>& lst
366  );
367 
368  //- Read from Istream, discarding contents of existing DynamicList.
369  friend Istream& operator>> <T, SizeMin>
370  (
371  Istream& is,
372  DynamicList<T, SizeMin>& lst
373  );
374 };
375 
376 
377 // Global Functions
378 
379 // Exchange contents of lists - see DynamicList::swap().
380 template<class T, int SizeMin1, int SizeMin2>
381 inline void Swap(DynamicList<T, SizeMin1>& a, DynamicList<T, SizeMin2>& b);
382 
383 
384 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
385 
386 } // End namespace Foam
387 
388 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
389 
390 #include "DynamicListI.H"
391 
392 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
393 
394 #ifdef NoRepository
395  #include "DynamicList.C"
396 #endif
397 
398 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
399 
400 #endif
401 
402 // ************************************************************************* //
Foam::DynamicList::subset
label subset(const labelRange &range)
Retain a (start,size) subset from the list.
Definition: DynamicListI.H:715
Foam::DynamicList::DynamicList
constexpr DynamicList() noexcept
Default construct, an empty list without allocation.
Definition: DynamicListI.H:62
Foam::DynamicList::assignDynList
void assignDynList(const ListType &lst)
Copy assignment from another list.
Definition: DynamicListI.H:36
List.H
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::DynamicList::expandStorage
label expandStorage()
Expand the addressable size to fit the allocated capacity.
Definition: DynamicListI.H:363
Foam::DynamicList::swap
void swap(DynamicList< T, AnySizeMin > &lst)
Swap content with any sized DynamicList.
Definition: DynamicListI.H:396
Foam::DynamicList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicListI.H:224
Foam::DynamicList::shrink
DynamicList< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:376
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:909
Foam::DynamicList::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:254
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:472
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:348
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:58
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:63
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::DynamicList::setSize
void setSize(const label nElem)
Alter addressable list size.
Definition: DynamicListI.H:282
Foam::DynamicList::setCapacity
void setCapacity(const label nElem)
Alter the size of the underlying storage.
Definition: DynamicListI.H:232
Foam::DynamicList::resize
void resize(const label nElem)
Alter addressable list size.
Definition: DynamicListI.H:328
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: HashTable.H:102
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::transfer
void transfer(List< T > &lst)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:426
Foam::DynamicList::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:355
Foam::DynamicList::remove
T remove()
Remove and return the last element. Fatal on an empty list.
Definition: DynamicListI.H:651
Foam::DynamicList::operator
friend Ostream & operator(Ostream &os, const DynamicList< T, SizeMin > &lst)
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::min_size
static constexpr label min_size() noexcept
Normal lower capacity limit - the SizeMin template parameter.
Definition: DynamicList.H:177
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62