DynamicField.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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::DynamicField
29
30Description
31 Dynamically sized Field.
32
33SourceFiles
34 DynamicFieldI.H
35
36\*---------------------------------------------------------------------------*/
37
38#ifndef Foam_DynamicField_H
39#define Foam_DynamicField_H
40
41#include "Field.H"
42#include "DynamicList.H"
43
44// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45
46namespace Foam
47{
48
49// Forward Declarations
50template<class T, int SizeMin> class DynamicField;
51
52template<class T, int SizeMin>
54
55template<class T, int SizeMin>
57
58/*---------------------------------------------------------------------------*\
59 Class DynamicField Declaration
60\*---------------------------------------------------------------------------*/
61
62template<class T, int SizeMin=64>
63class DynamicField
64:
65 public Field<T>
66{
67 static_assert(SizeMin > 0, "Invalid min size parameter");
68
69 // Private Data
70
71 //- The capacity (allocated size) of the underlying field.
72 label capacity_;
73
74
75 // Private Member Functions
76
77 //- Copy assignment from another list
78 template<class ListType>
79 inline void assignDynList(const ListType& list);
80
81 //- Alter the size of the underlying storage
82 // The 'nocopy' option will not attempt to recover old content
83 inline void doCapacity(const bool nocopy, const label len);
84
85 //- Reserve allocation space for at least this size.
86 // Never shrinks the allocated size, use setCapacity() for that.
87 // The 'nocopy' option will not attempt to recover old content
88 inline void doReserve(const bool nocopy, const label len);
89
90 //- Reserve allocation space for at least this size.
91 // Never shrinks the allocated size, use setCapacity() for that.
92 // The 'nocopy' option will not attempt to recover old content
93 inline void doResize(const bool nocopy, const label len);
94
95
96public:
97
98 // Static Member Functions
99
100 //- Return a null field
101 inline static const DynamicField<T, SizeMin>& null()
102 {
103 return NullObjectRef<DynamicField<T, SizeMin>>();
104 }
105
106
107 // Constructors
108
109 //- Default construct, an empty field without allocation.
110 inline constexpr DynamicField() noexcept;
111
112 //- Construct empty field with given reserve size.
113 inline explicit DynamicField(const label len);
114
115 //- Construct given size and initial value
116 inline DynamicField(const label len, const T& val);
117
118 //- Construct given size and initial value of zero
119 inline DynamicField(const label len, const Foam::zero);
120
121 //- Copy construct
122 inline DynamicField(const DynamicField<T, SizeMin>& list);
123
124 //- Copy construct with different sizing parameters
125 template<int AnySizeMin>
126 inline DynamicField(const DynamicField<T, AnySizeMin>& list);
127
128 //- Copy construct from UList. Size set to UList size.
129 // Also constructs from DynamicField with different sizing parameters.
130 inline explicit DynamicField(const UList<T>& list);
131
132 //- Copy construct from IndirectList
133 template<class Addr>
134 inline explicit DynamicField(const IndirectListBase<T, Addr>& list);
135
136 //- Move construct from List contents
137 inline explicit DynamicField(List<T>&& content);
138
139 //- Move construct from dynamic Field contents
140 inline DynamicField(DynamicField<T, SizeMin>&& content);
141
142 //- Move construct with different sizing parameters
143 template<int AnySizeMin>
144 inline DynamicField(DynamicField<T, AnySizeMin>&& content);
145
146 //- Move construct from DynamicList
147 template<int AnySizeMin>
148 inline DynamicField(DynamicList<T, AnySizeMin>&& list);
149
150 //- Construct by 1 to 1 mapping from the given field
151 inline DynamicField
152 (
153 const UList<T>& mapF,
154 const labelUList& mapAddressing
155 );
156
157 //- Construct by interpolative mapping from the given field
158 inline DynamicField
159 (
160 const UList<T>& mapF,
161 const labelListList& mapAddressing,
162 const scalarListList& weights
163 );
164
165 //- Construct by mapping from the given field
166 inline DynamicField
167 (
168 const UList<T>& mapF,
169 const FieldMapper& map
170 );
171
172 //- Construct from Istream. Size set to size of list read.
173 inline explicit DynamicField(Istream& is);
174
175 //- Clone
176 inline tmp<DynamicField<T, SizeMin>> clone() const;
177
178
179 // Member Functions
180
181 // Access
182
183 //- Normal lower capacity limit - the SizeMin template parameter
184 static constexpr label min_size() noexcept { return SizeMin; }
185
186 //- Size of the underlying storage.
187 inline label capacity() const noexcept;
188
189 //- Number of contiguous bytes of the underlying storage.
190 // \note Only meaningful for contiguous data
191 inline std::streamsize capacity_bytes() const noexcept;
192
193
194 // Sizing
195
196 //- Alter the size of the underlying storage.
197 // The addressed size will be truncated if needed to fit, but will
198 // remain otherwise untouched.
199 // Use this or reserve() in combination with append().
200 inline void setCapacity(const label len);
201
202 //- Alter the size of the underlying storage,
203 //- \em without retaining old content.
204 // The addressed size will be truncated if needed to fit, but will
205 // remain otherwise untouched.
206 inline void setCapacity_nocopy(const label len);
207
208 //- Change the value for the list capacity directly (ADVANCED, UNSAFE)
209 //- Does not perform any memory management or resizing.
210 inline void setCapacity_unsafe(const label len) noexcept;
211
212 //- Reserve allocation space for at least this size, allocating new
213 //- space if required and \em retaining old content.
214 // Never shrinks the allocated size, use setCapacity() for that.
215 inline void reserve(const label len);
216
217 //- Reserve allocation space for at least this size, allocating new
218 //- space if required \em without retaining old content.
219 // Never shrinks the allocated size, use setCapacity() for that.
220 inline void reserve_nocopy(const label len);
221
222 //- Alter addressable list size, allocating new space if required
223 //- while \em recovering old content.
224 // If no reallocation is required, the contents remain untouched.
225 // Otherwise new entries will be uninitialized.
226 // Use this to resize the list prior to using the operator[] for
227 // setting values (as per List usage).
228 inline void resize(const label len);
229
230 //- Alter addressable size and fill new space with constant value
231 inline void resize(const label len, const T& val);
232
233 //- Alter addressable list size, allocating new space if required
234 //- \em without necessarily recovering old content.
235 // If no reallocation is required, the contents remain untouched.
236 // Otherwise all entries will be uninitialized.
237 inline void resize_nocopy(const label len);
238
239 //- Alias for resize()
240 void setSize(const label n) { this->resize(n); }
241
242 //- Alias for resize()
243 void setSize(const label n, const T& val) { this->resize(n, val); }
244
245 //- Clear the addressed list, i.e. set the size to zero.
246 // Allocated size does not change
247 inline void clear() noexcept;
248
249 //- Clear the list and delete storage.
250 inline void clearStorage();
251
252 //- Expand the addressable size to fit the allocated capacity.
253 // Returns the previous addressable size.
254 inline label expandStorage() noexcept;
255
256 //- Shrink the allocated space to the number of elements used.
257 inline void shrinkStorage();
258
259 //- Shrink the allocated space to the number of elements used.
260 // Returns a reference to the DynamicField.
261 inline DynamicField<T, SizeMin>& shrink();
262
263
264 // Edit
265
266 //- Swap content, independent of sizing parameter
267 template<int AnySizeMin>
268 inline void swap(DynamicField<T, AnySizeMin>& other);
269
270 //- Swap content with DynamicList, independent of sizing parameter
271 template<int AnySizeMin>
272 inline void swap(DynamicList<T, AnySizeMin>& other);
273
274 //- Transfer the parameter contents into this
275 inline void transfer(List<T>& list);
276
277 //- Transfer the parameter contents into this
278 template<int AnySizeMin>
279 inline void transfer(DynamicList<T, AnySizeMin>& list);
280
281 //- Transfer the parameter contents into this
282 template<int AnySizeMin>
283 inline void transfer(DynamicField<T, AnySizeMin>& list);
284
285 //- Append an element at the end of the list
286 inline void append(const T& val);
287
288 //- Move append an element
289 inline void append(T&& val);
290
291 //- Append a List at the end of this list
292 inline void append(const UList<T>& list);
293
294 //- Remove and return the top element
295 inline T remove();
296
297
298 // Reading/writing
299
300 //- Read from Istream, discarding existing contents
301 // Uses a DynamicList::readList internally
302 inline Istream& readList(Istream& is);
303
304
305 // Member Operators
306
307 //- Return non-const access to an element, resizing list if needed
308 inline T& operator()(const label i);
309
310 //- Assign addressed entries to the given value
311 inline void operator=(const T& val);
312
313 //- Assign addressed entries to zero
314 inline void operator=(const Foam::zero);
315
316 //- Copy assignment
317 inline void operator=(const UList<T>& list);
318
319 //- Copy assignment
320 inline void operator=(const DynamicField<T, SizeMin>& list);
321
322 //- Move assignment
323 inline void operator=(List<T>&& list);
324
325 //- Move assignment
326 template<int AnySizeMin>
327 inline void operator=(DynamicList<T, AnySizeMin>&& list);
328
329 //- Move assignment
330 inline void operator=(DynamicField<T, SizeMin>&& list);
331
332 //- Move assignment
333 template<int AnySizeMin>
334 inline void operator=(DynamicField<T, AnySizeMin>&& list);
335
336
337 // IOstream Operators
338
339 //- Use the readList() method to read contents from Istream.
340 friend Istream& operator>> <T, SizeMin>
341 (
342 Istream& is,
343 DynamicField<T, SizeMin>& rhs
344 );
345
346 //- Write to Ostream
347 friend Ostream& operator<< <T, SizeMin>
348 (
349 Ostream& os,
350 const DynamicField<T, SizeMin>& rhs
351 );
352};
353
354
355// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
356
357} // End namespace Foam
358
359// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
360
361#include "DynamicFieldI.H"
362
363// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
364
365#endif
366
367// ************************************************************************* //
label n
Dynamically sized Field.
Definition: DynamicField.H:65
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
T remove()
Remove and return the top element.
void swap(DynamicField< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
void transfer(List< T > &list)
Transfer the parameter contents into this.
void setSize(const label n)
Alias for resize()
Definition: DynamicField.H:239
void resize_nocopy(const label len)
void setCapacity_nocopy(const label len)
void reserve_nocopy(const label len)
void setCapacity_unsafe(const label len) noexcept
std::streamsize capacity_bytes() const noexcept
Number of contiguous bytes of the underlying storage.
void append(const T &val)
Append an element at the end of the list.
void clearStorage()
Clear the list and delete storage.
static const DynamicField< T, SizeMin > & null()
Return a null field.
Definition: DynamicField.H:100
label capacity() const noexcept
Size of the underlying storage.
void setSize(const label n, const T &val)
Alias for resize()
Definition: DynamicField.H:242
void shrinkStorage()
Shrink the allocated space to the number of elements used.
tmp< DynamicField< T, SizeMin > > clone() const
Clone.
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
constexpr DynamicField() noexcept
Default construct, an empty field without allocation.
static constexpr label min_size() noexcept
Normal lower capacity limit - the SizeMin template parameter.
Definition: DynamicField.H:183
DynamicField< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
void resize(const label len)
void reserve(const label len)
void setCapacity(const label len)
Alter the size of the underlying storage.
Istream & readList(Istream &is)
Read from Istream, discarding existing contents.
friend Ostream & operator(Ostream &os, const DynamicField< T, SizeMin > &rhs)
Write to Ostream.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:50
Generic templated field type.
Definition: Field.H:82
tmp< Field< T > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:605
void map(const UList< T > &mapF, const labelUList &mapAddressing)
1 to 1 map from the given field
Definition: Field.C:240
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
A class for managing temporary objects.
Definition: tmp.H:65
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition: Scalar.H:223