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