DynamicField.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 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::DynamicField
26 
27 Description
28  Dynamically sized Field.
29 
30 SourceFiles
31  DynamicFieldI.H
32  DynamicField.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef DynamicField_H
37 #define DynamicField_H
38 
39 #include "Field.H"
40 #include "DynamicFieldFwd.H"
41 #include <type_traits>
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // Forward declaration of friend functions and operators
49 
50 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
51 Ostream& operator<<
52 (
53  Ostream&,
55 );
56 
57 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
58 Istream& operator>>
59 (
60  Istream&,
62 );
63 
64 
65 /*---------------------------------------------------------------------------*\
66  Class DynamicField Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
70 class DynamicField
71 :
72  public Field<T>
73 {
74  static_assert
75  (
76  (SizeInc || SizeMult) && SizeDiv,
77  "Avoid invalid sizing parameters"
78  );
79 
80  // Private Data
81 
82  //- The capacity (allocated size) of the underlying field.
83  label capacity_;
84 
85 
86 public:
87 
88  // Static Member Functions
89 
90  //- Return a null field
91  inline static const DynamicField<T, SizeInc, SizeMult, SizeDiv>& null()
92  {
93  return *reinterpret_cast
94  <
96  >(0);
97  }
98 
99 
100  // Constructors
101 
102  //- Construct null
103  inline DynamicField();
104 
105  //- Construct given size.
106  explicit inline DynamicField(const label);
107 
108  //- Construct given size and initial value
109  inline DynamicField(const label, const T&);
110 
111  //- Construct given size and initialised to zero
112  inline DynamicField(const label, const zero);
113 
114  //- Construct from UList. Size set to UList size.
115  // Also constructs from DynamicField with different sizing parameters.
116  explicit inline DynamicField(const UList<T>&);
117 
118  //- Move constructor transferring the list contents
119  explicit inline DynamicField(List<T>&&);
120 
121  //- Construct by 1 to 1 mapping from the given field
122  inline DynamicField
123  (
124  const UList<T>& mapF,
125  const labelList& mapAddressing
126  );
127 
128  //- Construct by interpolative mapping from the given field
129  inline DynamicField
130  (
131  const UList<T>& mapF,
132  const labelListList& mapAddressing,
133  const scalarListList& weights
134  );
135 
136  //- Copy constructor
138 
139  //- Copy constructor or reuse as specified
140  inline DynamicField
141  (
143  const bool
144  );
145 
146  //- Move constructor
148 
149  //- Construct from Istream. Size set to size of list read.
150  explicit DynamicField(Istream&);
151 
152  //- Construct from a dictionary entry
153  DynamicField(const word& keyword, const dictionary&, const label size);
154 
155  //- Construct from a dictionary entry with unit conversion
157  (
158  const word& keyword,
159  const unitConversion&,
160  const dictionary&,
161  const label size
162  );
163 
164  //- Clone
166 
167 
168  // Member Functions
169 
170  // Access
171 
172  //- Size of the underlying storage.
173  inline label capacity() const;
174 
175 
176  // Edit
177 
178  //- Alter the size of the underlying storage.
179  // The addressed size will be truncated if needed to fit, but will
180  // remain otherwise untouched.
181  // Use this or reserve() in combination with append().
182  inline void setCapacity(const label);
183 
184  //- Alter the addressed list size.
185  // New space will be allocated if required.
186  // Use this to resize the list prior to using the operator[] for
187  // setting values (as per List usage).
188  inline void setSize(const label);
189 
190  //- Alter the addressed list size and fill new space with a
191  // constant.
192  inline void setSize(const label, const T&);
193 
194  //- Alter the addressed list size.
195  // New space will be allocated if required.
196  // Use this to resize the list prior to using the operator[] for
197  // setting values (as per List usage).
198  inline void resize(const label);
199 
200  //- Alter the addressed list size and fill new space with a
201  // constant.
202  inline void resize(const label, const T&);
203 
204  //- Reserve allocation space for at least this size.
205  // Never shrinks the allocated size, use setCapacity() for that.
206  inline void reserve(const label);
207 
208  //- Clear the addressed list, i.e. set the size to zero.
209  // Allocated size does not change
210  inline void clear();
211 
212  //- Clear the list and delete storage.
213  inline void clearStorage();
214 
215  //- Reset the field values to the given field
216  // Equivalent to operator=
218 
219  //- Reset the field values to the given field
220  // Equivalent to operator=
221  void reset(const UList<T>&);
222 
223  //- Shrink the allocated space to the number of elements used.
224  // Returns a reference to the DynamicField.
226 
227 
228  // Member Operators
229 
230  //- Append an element at the end of the list
232  (
233  const T&
234  );
235 
236  //- Append a List at the end of this list
238  (
239  const UList<T>&
240  );
241 
242  //- Remove and return the top element
243  inline T remove();
244 
245  //- Return non-const access to an element, resizing list if
246  // necessary
247  inline T& operator()(const label);
248 
249  //- Assignment of all addressed entries to the given value
250  inline void operator=(const T&);
251 
252  //- Assignment operator
253  inline void operator=
254  (
256  );
257 
258  //- Move assignment operator
259  inline void operator=
260  (
262  );
263 
264  //- Assignment to UList
265  inline void operator=(const UList<T>&);
266 
267  //- Move assignment to List
268  inline void operator=(List<T>&&);
269 
270 
271  // IOstream Operators
272 
273  // Write DynamicField to Ostream.
274  friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv>
275  (
276  Ostream&,
278  );
279 
280  //- Read from Istream, discarding contents of existing DynamicField.
281  friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv>
282  (
283  Istream&,
285  );
286 };
287 
288 
289 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
290 
291 } // End namespace Foam
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 #include "DynamicFieldI.H"
296 
297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
298 
299 #ifdef NoRepository
300  #include "DynamicField.C"
301 #endif
302 
303 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
304 
305 #endif
306 
307 // ************************************************************************* //
Dynamically sized Field.
Definition: DynamicField.H:72
T remove()
Remove and return the top element.
DynamicField< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
void setCapacity(const label)
Alter the size of the underlying storage.
void resize(const label)
Alter the addressed list size.
tmp< DynamicField< T, SizeInc, SizeMult, SizeDiv > > clone() const
Clone.
Definition: DynamicField.C:67
label capacity() const
Size of the underlying storage.
DynamicField()
Construct null.
Definition: DynamicFieldI.H:29
DynamicField< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
void operator=(const T &)
Assignment of all addressed entries to the given value.
void reserve(const label)
Reserve allocation space for at least this size.
void clearStorage()
Clear the list and delete storage.
T & operator()(const label)
Return non-const access to an element, resizing list if.
void clear()
Clear the addressed list, i.e. set the size to zero.
void setSize(const label)
Alter the addressed list size.
void reset(const DynamicField< T, SizeInc, SizeMult, SizeDiv > &)
Reset the field values to the given field.
Pre-declare SubField and related Field type.
Definition: Field.H:83
tmp< Field< T > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:546
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:91
label size() const
Return the number of elements in the UList.
Definition: ListI.H:171
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
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:74
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
A class for managing temporary objects.
Definition: tmp.H:55
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
A class for handling words, derived from string.
Definition: word.H:62
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:50
Namespace for OpenFOAM.
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