Loading...
Searching...
No Matches
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-2025 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 Similar to DynamicList, but inheriting from a Field instead of a List.
33
34SourceFiles
35 DynamicFieldI.H
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef Foam_DynamicField_H
40#define Foam_DynamicField_H
41
42#include "Field.H"
43#include "DynamicList.H"
44
45// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46
47namespace Foam
48{
50// Forward Declarations
51template<class T, int SizeMin> class DynamicField;
53template<class T, int SizeMin>
55
56template<class T, int SizeMin>
58
59/*---------------------------------------------------------------------------*\
60 Class DynamicField Declaration
61\*---------------------------------------------------------------------------*/
62
63template<class T, int SizeMin=64>
64class DynamicField
65:
66 public Field<T>
67{
68 static_assert(SizeMin > 0, "Invalid min size parameter");
69
70 // Private Data
71
72 //- The capacity (allocated size) of the underlying field.
73 label capacity_;
74
75
76 // Private Member Functions
77
78 //- Copy assignment from another list
79 template<class ListType>
80 inline void doAssignDynList(const ListType& list);
81
82 //- Alter the size of the underlying storage,
83 //- retaining the first count elements.
84 inline void doCapacity_copy(label count, const label len);
85
86 //- Reserve allocation space for at least this size,
87 //- retaining the first count elements.
88 // Never shrinks the allocated size, use setCapacity() for that.
89 inline void doReserve_copy(label count, const label len);
90
91
92public:
93
94 // Constructors
95
96 //- Default construct, an empty field without allocation.
97 inline constexpr DynamicField() noexcept;
98
99 //- Construct empty field with given initial capacity
100 inline explicit DynamicField(const label initialCapacity);
101
102 //- Construct with given size and capacity
103 inline explicit DynamicField(std::pair<label,label> sizing);
104
105 //- Construct given size and initial value
106 inline DynamicField(const label len, const T& val);
107
108 //- Construct given size and initial value of zero
109 inline DynamicField(const label len, Foam::zero);
110
111 //- Copy construct
112 inline DynamicField(const DynamicField<T, SizeMin>& list);
114 //- Copy construct with different sizing parameters
115 template<int AnySizeMin>
116 inline DynamicField(const DynamicField<T, AnySizeMin>& list);
117
118 //- Copy construct from UList. Size set to UList size.
119 // Also constructs from DynamicField with different sizing parameters.
120 inline explicit DynamicField(const UList<T>& list);
121
122 //- Copy construct from IndirectList
123 template<class Addr>
124 inline explicit DynamicField(const IndirectListBase<T, Addr>& list);
125
126 //- Move construct from List contents
127 inline explicit DynamicField(List<T>&& content) noexcept;
129 //- Move construct from dynamic Field contents
130 inline DynamicField(DynamicField<T, SizeMin>&& content) noexcept;
131
132 //- Move construct with different sizing parameters
133 template<int AnySizeMin>
134 inline DynamicField(DynamicField<T, AnySizeMin>&& content) noexcept;
135
136 //- Move construct from DynamicList
137 template<int AnySizeMin>
138 inline DynamicField(DynamicList<T, AnySizeMin>&& content) noexcept;
139
140 //- Copy or move construct from DynamicField
141 template<int AnySizeMin>
142 inline DynamicField(DynamicField<T, AnySizeMin>& content, bool reuse);
143
144 //- Copy or move construct from DynamicList
145 template<int AnySizeMin>
146 inline DynamicField(DynamicList<T, AnySizeMin>& content, bool reuse);
148 //- Copy or move construct from List
149 inline DynamicField(List<T>& content, bool reuse);
150
151 //- Construct by 1 to 1 mapping from the given field
153 (
154 const UList<T>& mapF,
155 const labelUList& mapAddressing
156 );
158 //- Construct by interpolative mapping from the given field
159 inline DynamicField
160 (
161 const UList<T>& mapF,
162 const labelListList& mapAddressing,
163 const scalarListList& weights
164 );
165
166 //- Construct by mapping from the given field
167 inline DynamicField
168 (
169 const UList<T>& mapF,
170 const FieldMapper& map
171 );
172
173 //- Construct from Istream. Size set to size of list read.
174 inline explicit DynamicField(Istream& is);
176 //- Clone
177 inline tmp<DynamicField<T, SizeMin>> clone() const;
178
179
180 //- Destructor, sync allocated size before list destruction
182
183
184 // Member Functions
185
186 // Capacity
187
188 //- Normal lower capacity limit - the SizeMin template parameter
189 static constexpr label min_size() noexcept { return SizeMin; }
190
191 //- Size of the underlying storage.
192 label capacity() const noexcept { return capacity_; }
193
194 //- Number of contiguous bytes of the underlying storage.
195 // \note Only meaningful for contiguous data
196 inline std::streamsize capacity_bytes() const noexcept;
197
198
199 // Sizing
201 //- Alter the size of the underlying storage.
202 // The addressed size will be truncated if needed to fit, but will
203 // remain otherwise untouched.
204 // Use this or reserve() in combination with push_back().
205 inline void setCapacity(const label len);
206
207 //- Alter the size of the underlying storage,
208 //- \em without retaining old content.
209 // The addressed size will be truncated if needed to fit, but will
210 // remain otherwise untouched.
211 inline void setCapacity_nocopy(const label len);
212
213 //- Change the value for the list capacity directly (ADVANCED, UNSAFE)
214 //- Does not perform any memory management or resizing.
215 void setCapacity_unsafe(label len) noexcept { capacity_ = len; }
216
217 //- Reserve allocation space for at least this size, allocating new
218 //- space if required and \em retaining old content.
219 // Never shrinks the allocated size, use setCapacity() for that.
220 inline void reserve(const label len);
221
222 //- Reserve allocation space for at least this size, allocating new
223 //- space if required \em without retaining old content.
224 // Never shrinks the allocated size, use setCapacity() for that.
225 inline void reserve_nocopy(const label len);
226
227 //- Reserve allocation space for at least this size, allocating new
228 //- space if required and \em retaining old content.
229 //- If allocation is required, uses the specified size
230 //- without any other resizing logic.
231 inline void reserve_exact(const label len);
232
233 //- Alter addressable list size, allocating new space if required
234 //- while \em recovering old content.
235 // If no reallocation is required, the contents remain untouched.
236 // Otherwise new entries will be uninitialized.
237 // Use this to resize the list prior to using the operator[] for
238 // setting values (as per List usage).
239 inline void resize(const label len);
241 //- Alter addressable size and fill \em new entries with constant value
242 inline void resize(const label len, const T& val);
243
244 //- Alter addressable size, retaining the first count contents.
245 // \note Only uses a limited number of internal checks.
246 inline void resize_copy(label count, const label len);
247
248 //- Alter addressable size and set val for \em all addressed entries
249 inline void resize_fill(const label len, const T& val);
250
251 //- Alter addressable list size, allocating new space if required
252 //- \em without necessarily recovering old content.
253 // If no reallocation is required, the contents remain untouched.
254 // Otherwise all entries will be uninitialized.
255 inline void resize_nocopy(const label len);
256
257 //- Clear the addressed list, i.e. set the size to zero.
258 // Allocated size does not change
259 inline void clear() noexcept;
260
261 //- Clear the list and delete storage.
262 inline void clearStorage();
263
264 //- Shrink the allocated space to the number of elements used.
265 inline void shrink_to_fit();
266
267
268 // Edit
269
270 //- Swap with plain List content. Implies shrink_to_fit().
271 inline void swap(List<T>& other);
272
273 //- Swap content, independent of sizing parameter
274 template<int AnySizeMin>
275 inline void swap(DynamicField<T, AnySizeMin>& other) noexcept;
276
277 //- Swap content with DynamicList, independent of sizing parameter
278 template<int AnySizeMin>
279 inline void swap(DynamicList<T, AnySizeMin>& other) noexcept;
280
281 //- Transfer the parameter contents into this
282 inline void transfer(List<T>& list);
283
284 //- Transfer the parameter contents into this
285 template<int AnySizeMin>
286 inline void transfer(DynamicList<T, AnySizeMin>& list);
288 //- Transfer the parameter contents into this
289 template<int AnySizeMin>
290 inline void transfer(DynamicField<T, AnySizeMin>& list);
291
292 //- Construct an element at the end of the list,
293 //- return reference to the new list element
294 template<class... Args>
295 inline T& emplace_back(Args&&... args);
296
297 //- Copy append an element to the end of the list
298 inline void push_back(const T& val);
299
300 //- Move append an element
301 inline void push_back(T&& val);
302
303 //- Copy append another list to the end of this list
304 inline void push_back(const UList<T>& list);
305
306 //- Move append another list to the end of this list
307 inline void push_back(List<T>&& list);
308
309 //- Reduce size by 1 or more elements. Can be called on an empty list.
310 inline void pop_back(label n = 1);
311
312
313 // Reading/writing
315 //- Read from Istream, discarding existing contents
316 // Uses a DynamicList::readList internally
317 inline Istream& readList(Istream& is);
318
320 // Assignment
321
322 //- Assign addressed entries to the given value
323 inline void operator=(const T& val);
324
325 //- Assign addressed entries to zero
326 inline void operator=(Foam::zero);
327
328 //- Copy assignment
329 inline void operator=(const UList<T>& list);
330
331 //- Copy assignment
332 inline void operator=(const DynamicField<T, SizeMin>& list);
333
334 //- Copy assign from IndirectList
335 template<class Addr>
336 inline void operator=(const IndirectListBase<T, Addr>& rhs);
337
338 //- Move assignment
339 inline void operator=(List<T>&& list);
341 //- Move assignment
342 template<int AnySizeMin>
343 inline void operator=(DynamicList<T, AnySizeMin>&& list);
344
345 //- Move assignment
346 inline void operator=(DynamicField<T, SizeMin>&& list);
348 //- Move assignment
349 template<int AnySizeMin>
350 inline void operator=(DynamicField<T, AnySizeMin>&& list);
351
353 // Member Operators
354
355 //- Return non-const access to an element, resizing list if needed
356 inline T& operator()(const label i);
358
359 // IOstream Operators
360
361 //- Use the readList() method to read contents from Istream.
362 friend Istream& operator>> <T, SizeMin>
363 (
364 Istream& is,
365 DynamicField<T, SizeMin>& rhs
366 );
367
368 //- Write to Ostream
369 friend Ostream& operator<< <T, SizeMin>
370 (
372 const DynamicField<T, SizeMin>& rhs
373 );
374
375
376 // Housekeeping
378 //- Alias for resize()
379 void setSize(const label n) { this->resize(n); }
380
381 //- Alias for resize()
382 void setSize(const label n, const T& val) { this->resize(n, val); }
383
384 //- Calls shrink_to_fit() and returns a reference to the DynamicField.
385 //FOAM_DEPRECATED_FOR(2025-04, "shrink_to_fit()")
387 {
389 return *this;
390 }
391
392 //- Append an element at the end of the list
393 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
394 void append(const T& val) { this->push_back(val); }
395
396 //- Move append an element
397 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
398 void append(T&& val) { this->push_back(std::move(val)); }
399
400 //- Append a List at the end of this list
401 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
402 void append(const UList<T>& list) { this->push_back(list); }
403};
404
405
406// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
407
408} // End namespace Foam
409
410// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
412#include "DynamicFieldI.H"
413
414// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
415
416#endif
417
418// ************************************************************************* //
label n
Dynamically sized Field. Similar to DynamicList, but inheriting from a Field instead of a List.
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
void transfer(List< Type > &list)
void setSize(const label n)
void pop_back(label n=1)
void resize_nocopy(const label len)
Alter addressable list size, allocating new space if required without necessarily recovering old cont...
void setCapacity_nocopy(const label len)
Type & emplace_back(Args &&... args)
void setCapacity_unsafe(label len) noexcept
void reserve_nocopy(const label len)
Reserve allocation space for at least this size, allocating new space if required without retaining o...
~DynamicField()
Destructor, sync allocated size before list destruction.
void reserve_exact(const label len)
Reserve allocation space for at least this size, allocating new space if required and retaining old c...
std::streamsize capacity_bytes() const noexcept
Number of contiguous bytes of the underlying storage.
void resize_copy(label count, const label len)
Alter addressable size, retaining the first count contents.
void append(const T &val)
Append an element at the end of the list.
void resize(const label len, const T &val)
Alter addressable size and fill new entries with constant value.
void swap(List< Type > &other)
void append(T &&val)
Move append an element.
void setSize(const label n, const T &val)
Alias for resize().
tmp< DynamicField< Type, 64 > > clone() const
void push_back(const Type &val)
constexpr DynamicField() noexcept
Default construct, an empty field without allocation.
static constexpr label min_size() noexcept
Normal lower capacity limit - the SizeMin template parameter.
label capacity() const noexcept
Size of the underlying storage.
DynamicField(const DynamicField< T, AnySizeMin > &list)
Copy construct with different sizing parameters.
void resize(const label len)
Alter addressable list size, allocating new space if required while recovering old content.
void reserve(const label len)
Reserve allocation space for at least this size, allocating new space if required and retaining old c...
DynamicField< T, SizeMin > & shrink()
Calls shrink_to_fit() and returns a reference to the DynamicField.
void resize_fill(const label len, const T &val)
Alter addressable size and set val for all addressed entries.
void setCapacity(const label len)
Istream & readList(Istream &is)
void append(const UList< T > &list)
Append a List at the end of this list.
friend Ostream & operator(Ostream &os, const DynamicField< Type, 64 > &rhs)
DynamicField(List< T > &&content) noexcept
Move construct from List contents.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
Abstract base class to hold the Field mapping addressing and weights.
Definition FieldMapper.H:44
tmp< Field< Type > > T() const
Return the field transpose (only defined for second rank tensors).
Definition Field.C:745
constexpr Field() noexcept
void map(const UList< Type > &mapF, const labelUList &mapAddressing)
1 to 1 map from the given field
Definition Field.C:302
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:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
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:89
void setAddressableSize(const label n) noexcept
Set addressed size to be inconsistent with allocated storage.
Definition UListI.H:517
A class for managing temporary objects.
Definition tmp.H:75
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
List< scalarList > scalarListList
List of scalarList.
Definition scalarList.H:35
List< labelList > labelListList
List of labelList.
Definition labelList.H:38
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265
UList< label > labelUList
A UList of labels.
Definition UList.H:75
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)
A non-counting (dummy) refCount.
Definition refCount.H:55