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-2023 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 
33 SourceFiles
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 
46 namespace Foam
47 {
48 
49 // Forward Declarations
50 template<class T, int SizeMin> class DynamicField;
51 
52 template<class T, int SizeMin>
54 
55 template<class T, int SizeMin>
56 inline Ostream& operator<<(Ostream&, const DynamicField<T, SizeMin>&);
57 
58 /*---------------------------------------------------------------------------*\
59  Class DynamicField Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 template<class T, int SizeMin=64>
63 class 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 doAssignDynList(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 
96 public:
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;
112  //- Construct empty field with given initial capacity
113  inline explicit DynamicField(const label initialCapacity);
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) noexcept;
138 
139  //- Move construct from dynamic Field contents
141 
142  //- Move construct with different sizing parameters
143  template<int AnySizeMin>
145 
146  //- Move construct from DynamicList
147  template<int AnySizeMin>
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  // Capacity
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  label capacity() const noexcept { return capacity_; }
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 push_back().
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  //- Shrink the allocated space to the number of elements used.
253  inline void shrink_to_fit();
254 
255  //- Shrink the internal bookkeeping of the allocated space to the
256  //- number of addressed elements without affecting allocation.
257  // \note when empty() it will delete any allocated memory.
258  inline void shrink_unsafe();
259 
260  //- Calls shrink_to_fit() and returns a reference to the DynamicField.
261  inline DynamicField<T, SizeMin>& shrink();
262 
263 
264  // Edit
265 
266  //- Swap with plain List content. Implies shrink_to_fit().
267  inline void swap(List<T>& list);
268 
269  //- Swap content, independent of sizing parameter
270  template<int AnySizeMin>
271  inline void swap(DynamicField<T, AnySizeMin>& other);
272 
273  //- Swap content with DynamicList, independent of sizing parameter
274  template<int AnySizeMin>
275  inline void swap(DynamicList<T, AnySizeMin>& other);
276 
277  //- Transfer the parameter contents into this
278  inline void transfer(List<T>& list);
279 
280  //- Transfer the parameter contents into this
281  template<int AnySizeMin>
282  inline void transfer(DynamicList<T, AnySizeMin>& list);
283 
284  //- Transfer the parameter contents into this
285  template<int AnySizeMin>
286  inline void transfer(DynamicField<T, AnySizeMin>& list);
287 
288  //- Construct an element at the end of the list,
289  //- return reference to the new list element
290  template<class... Args>
291  inline T& emplace_back(Args&&... args);
292 
293  //- Copy append an element at the end of the list
294  inline void push_back(const T& val);
295 
296  //- Move append an element
297  inline void push_back(T&& val);
298 
299  //- Copy append another list to the end of this list
300  inline void push_back(const UList<T>& list);
301 
302  //- Move append another list to the end of this list
303  inline void push_back(List<T>&& list);
304 
305  //- Reduce size by 1 or more elements. Can be called on an empty list.
306  inline void pop_back(label n = 1);
307 
308 
309  // Reading/writing
310 
311  //- Read from Istream, discarding existing contents
312  // Uses a DynamicList::readList internally
313  inline Istream& readList(Istream& is);
314 
315 
316  // Assignment
317 
318  //- Assign addressed entries to the given value
319  inline void operator=(const T& val);
320 
321  //- Assign addressed entries to zero
322  inline void operator=(const Foam::zero);
323 
324  //- Copy assignment
325  inline void operator=(const UList<T>& list);
326 
327  //- Copy assignment
328  inline void operator=(const DynamicField<T, SizeMin>& list);
329 
330  //- Copy assign from IndirectList
331  template<class Addr>
332  inline void operator=(const IndirectListBase<T, Addr>& rhs);
333 
334  //- Move assignment
335  inline void operator=(List<T>&& list);
336 
337  //- Move assignment
338  template<int AnySizeMin>
339  inline void operator=(DynamicList<T, AnySizeMin>&& list);
340 
341  //- Move assignment
342  inline void operator=(DynamicField<T, SizeMin>&& list);
343 
344  //- Move assignment
345  template<int AnySizeMin>
346  inline void operator=(DynamicField<T, AnySizeMin>&& list);
347 
348 
349  // Member Operators
350 
351  //- Return non-const access to an element, resizing list if needed
352  inline T& operator()(const label i);
353 
354 
355  // IOstream Operators
356 
357  //- Use the readList() method to read contents from Istream.
358  friend Istream& operator>> <T, SizeMin>
359  (
360  Istream& is,
361  DynamicField<T, SizeMin>& rhs
362  );
363 
364  //- Write to Ostream
365  friend Ostream& operator<< <T, SizeMin>
366  (
367  Ostream& os,
368  const DynamicField<T, SizeMin>& rhs
369  );
370 
371 
372  // Housekeeping
373 
374  //- Append an element at the end of the list
375  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
376  void append(const T& val) { this->push_back(val); }
377 
378  //- Move append an element
379  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
380  void append(T&& val) { this->push_back(std::move(val)); }
381 
382  //- Append a List at the end of this list
383  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
384  void append(const UList<T>& list) { this->push_back(list); }
385 };
386 
387 
388 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
389 
390 } // End namespace Foam
391 
392 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
393 
394 #include "DynamicFieldI.H"
395 
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397 
398 #endif
399 
400 // ************************************************************************* //
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
DynamicField< T, SizeMin > & shrink()
Calls shrink_to_fit() and returns a reference to the DynamicField.
void push_back(const T &val)
Copy append an element at the end of the list.
constexpr DynamicField() noexcept
Default construct, an empty field without allocation.
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.
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...
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicField.H:236
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:62
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.
Definition: DynamicField.H:45
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:303
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
static constexpr label min_size() noexcept
Normal lower capacity limit - the SizeMin template parameter.
Definition: DynamicField.H:231
static const DynamicField< T, SizeMin > & null()
Return a null field.
Definition: DynamicField.H:111
constexpr UList() noexcept
Default construct, zero-sized and nullptr.
Definition: UListI.H:28
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:520
tmp< Field< T > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:711
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
constexpr List() noexcept
Default construct.
Definition: ListI.H:116
void setSize(const label n)
Alias for resize()
Definition: DynamicField.H:316
Namespace for OpenFOAM.
friend Ostream & operator(Ostream &os, const DynamicField< T, SizeMin > &rhs)
Write to Ostream.