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 | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::DynamicList
26 
27 Description
28  A 1D vector of objects of type <T> that resizes itself as necessary to
29  accept the new objects.
30 
31  Internal storage is a compact array and the list can be shrunk to compact
32  storage. The increase of list size is controlled by three template
33  parameters, which allows the list storage to either increase by the given
34  increment or by the given multiplier and divider (allowing non-integer
35  multiples).
36 
37 SourceFiles
38  DynamicListI.H
39  DynamicList.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef DynamicList_H
44 #define DynamicList_H
45 
46 #include "List.H"
47 #include "StaticAssert.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward declaration of friend functions and operators
55 
56 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
57 class DynamicList;
58 
59 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
60 Ostream& operator<<
61 (
62  Ostream&,
64 );
65 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
66 Istream& operator>>
67 (
68  Istream&,
70 );
71 
72 
73 /*---------------------------------------------------------------------------*\
74  Class DynamicList Declaration
75 \*---------------------------------------------------------------------------*/
76 
77 template<class T, unsigned SizeInc=0, unsigned SizeMult=2, unsigned SizeDiv=1>
78 class DynamicList
79 :
80  public List<T>
81 {
82  //- Avoid invalid sizing parameters
83  StaticAssert((SizeInc || SizeMult) && SizeDiv);
84 
85  // Private data
86 
87  //- The capacity (allocated size) of the underlying list.
89 
90 
91 public:
92 
93  // Related types
94 
95  //- Declare friendship with the List class
96  friend class List<T>;
97 
98 
99  // Constructors
100 
101  //- Construct null
102  inline DynamicList();
103 
104  //- Construct given size.
105  explicit inline DynamicList(const label);
106 
107  //- Construct copy.
109 
110  //- Construct from UList. Size set to UList size.
111  // Also constructs from DynamicList with different sizing parameters.
112  explicit inline DynamicList(const UList<T>&);
113 
114  //- Construct from UIndirectList. Size set to UIndirectList size.
115  explicit inline DynamicList(const UIndirectList<T>&);
116 
117  //- Construct by transferring the parameter contents
118  explicit inline DynamicList(const Xfer<List<T> >&);
119 
120  //- Construct from Istream. Size set to size of list read.
121  explicit DynamicList(Istream&);
122 
123 
124  // Member Functions
125 
126  // Access
127 
128  //- Size of the underlying storage.
129  inline label capacity() const;
130 
131  // Edit
132 
133  //- Alter the size of the underlying storage.
134  // The addressed size will be truncated if needed to fit, but will
135  // remain otherwise untouched.
136  // Use this or reserve() in combination with append().
137  inline void setCapacity(const label);
138 
139  //- Alter the addressed list size.
140  // New space will be allocated if required.
141  // Use this to resize the list prior to using the operator[] for
142  // setting values (as per List usage).
143  inline void setSize(const label);
144 
145  //- Alter the addressed list size and fill new space with a
146  // constant.
147  inline void setSize(const label, const T&);
148 
149  //- Alter the addressed list size.
150  // New space will be allocated if required.
151  // Use this to resize the list prior to using the operator[] for
152  // setting values (as per List usage).
153  inline void resize(const label);
154 
155  //- Alter the addressed list size and fill new space with a
156  // constant.
157  inline void resize(const label, const T&);
158 
159  //- Reserve allocation space for at least this size.
160  // Never shrinks the allocated size, use setCapacity() for that.
161  inline void reserve(const label);
162 
163  //- Clear the addressed list, i.e. set the size to zero.
164  // Allocated size does not change
165  inline void clear();
166 
167  //- Clear the list and delete storage.
168  inline void clearStorage();
169 
170  //- Shrink the allocated space to the number of elements used.
171  // Returns a reference to the DynamicList.
173 
174  //- Transfer contents of the argument List into this.
175  inline void transfer(List<T>&);
176 
177  //- Transfer contents of the argument DynamicList into this.
179 
180  //- Transfer contents to the Xfer container as a plain List
181  inline Xfer<List<T> > xfer();
182 
183 
184  // Member Operators
185 
186  //- Append an element at the end of the list
188  (
189  const T&
190  );
191 
192  //- Append a List at the end of this list
194  (
195  const UList<T>&
196  );
197 
198  //- Append a UIndirectList at the end of this list
200  (
201  const UIndirectList<T>&
202  );
203 
204  //- Remove and return the top element
205  inline T remove();
206 
207  //- Return non-const access to an element, resizing list if
208  // necessary
209  inline T& operator()(const label);
210 
211  //- Assignment of all addressed entries to the given value
212  inline void operator=(const T&);
213 
214  //- Assignment from DynamicList
215  inline void operator=
216  (
218  );
219 
220  //- Assignment from UList
221  inline void operator=(const UList<T>&);
222 
223  //- Assignment from UIndirectList
224  inline void operator=(const UIndirectList<T>&);
225 
226 
227 
228  // IOstream operators
229 
230  // Write DynamicList to Ostream.
231  friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv>
232  (
233  Ostream&,
235  );
236 
237  //- Read from Istream, discarding contents of existing DynamicList.
238  friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv>
239  (
240  Istream&,
242  );
243 };
244 
245 
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 
248 } // End namespace Foam
249 
250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251 
252 #include "DynamicListI.H"
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 #ifdef NoRepository
257 # include "DynamicList.C"
258 #endif
259 
260 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261 
262 #endif
263 
264 // ************************************************************************* //
Foam::DynamicList::resize
void resize(const label)
Alter the addressed list size.
Foam::DynamicList::xfer
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
Definition: DynamicListI.H:301
List.H
Foam::DynamicList::capacity
label capacity() const
Size of the underlying storage.
Definition: DynamicListI.H:100
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
DynamicListI.H
Foam::Xfer
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
Foam::DynamicList::operator()
T & operator()(const label)
Return non-const access to an element, resizing list if.
Definition: DynamicListI.H:387
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:242
Foam::DynamicList::setCapacity
void setCapacity(const label)
Alter the size of the underlying storage.
Definition: DynamicListI.H:109
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
DynamicList.C
Foam::DynamicList::StaticAssert
StaticAssert((SizeInc||SizeMult) &&SizeDiv)
Avoid invalid sizing parameters.
Foam::DynamicList::shrink
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:258
Foam::DynamicList::operator=
void operator=(const T &)
Assignment of all addressed entries to the given value.
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
StaticAssert.H
Foam::DynamicList::capacity_
label capacity_
The capacity (allocated size) of the underlying list.
Definition: DynamicList.H:87
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:59
Foam::UList< T >
Foam::DynamicList::transfer
void transfer(List< T > &)
Transfer contents of the argument List into this.
Foam::DynamicList::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:249
Foam::DynamicList::remove
T remove()
Remove and return the top element.
Definition: DynamicListI.H:365
Foam::DynamicList::reserve
void reserve(const label)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:130
Foam::UIndirectList< T >
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::DynamicList::setSize
void setSize(const label)
Alter the addressed list size.
Foam::DynamicList::DynamicList
DynamicList()
Construct null.
Definition: DynamicListI.H:29