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-2020 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  DynamicField.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef DynamicField_H
40 #define DynamicField_H
41 
42 #include "Field.H"
43 #include <type_traits>
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward declarations
51 
52 template<class T, int SizeMin> class DynamicField;
53 
54 template<class T, int SizeMin>
55 Ostream& operator<<
56 (
57  Ostream& os,
59 );
60 
61 template<class T, int SizeMin>
62 Istream& operator>>
63 (
64  Istream& is,
66 );
67 
68 
69 /*---------------------------------------------------------------------------*\
70  Class DynamicField Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 template<class T, int SizeMin=64>
74 class DynamicField
75 :
76  public Field<T>
77 {
78  static_assert(SizeMin > 0, "Invalid min size parameter");
79 
80  // Private data
81 
82  //- The capacity (allocated size) of the underlying field.
83  label capacity_;
84 
85 
86  // Private Member Functions
87 
88  //- Copy assignment from another list
89  template<class ListType>
90  inline void assignDynField(const ListType& list);
91 
92 public:
93 
94  // Static Member Functions
95 
96  //- Return a null field
97  inline static const DynamicField<T, SizeMin>& null()
98  {
99  return NullObjectRef<DynamicField<T, SizeMin>>();
100  }
101 
102 
103  // Constructors
104 
105  //- Construct null
106  inline constexpr DynamicField() noexcept;
107 
108  //- Construct empty field with given reserve size.
109  inline explicit DynamicField(const label len);
110 
111  //- Construct given size and initial value
112  inline DynamicField(const label len, const T& val);
113 
114  //- Construct given size and initial value of zero
115  inline DynamicField(const label len, const Foam::zero);
116 
117  //- Copy construct
118  inline DynamicField(const DynamicField<T, SizeMin>& list);
119 
120  //- Copy construct with different sizing parameters
121  template<int AnySizeMin>
122  inline DynamicField(const DynamicField<T, AnySizeMin>& list);
123 
124  //- Copy construct from UList. Size set to UList size.
125  // Also constructs from DynamicField with different sizing parameters.
126  inline explicit DynamicField(const UList<T>& list);
127 
128  //- Copy construct from IndirectList
129  template<class Addr>
130  inline explicit DynamicField(const IndirectListBase<T, Addr>& list);
131 
132  //- Move construct from List contents
133  inline explicit DynamicField(List<T>&& content);
134 
135  //- Move construct from dynamic Field contents
136  inline DynamicField(DynamicField<T, SizeMin>&& content);
137 
138  //- Move construct with different sizing parameters
139  template<int AnySizeMin>
140  inline DynamicField(DynamicField<T, AnySizeMin>&& content);
141 
142  //- Construct by 1 to 1 mapping from the given field
143  inline DynamicField
144  (
145  const UList<T>& mapF,
146  const labelUList& mapAddressing
147  );
148 
149  //- Construct by interpolative mapping from the given field
150  inline DynamicField
151  (
152  const UList<T>& mapF,
153  const labelListList& mapAddressing,
154  const scalarListList& weights
155  );
156 
157  //- Construct by mapping from the given field
158  inline DynamicField
159  (
160  const UList<T>& mapF,
161  const FieldMapper& map
162  );
163 
164  //- Construct from Istream. Size set to size of list read.
165  explicit DynamicField(Istream& is);
166 
167  //- Clone
168  tmp<DynamicField<T, SizeMin>> clone() const;
169 
170 
171  // Member Functions
172 
173  // Access
174 
175  //- Size of the underlying storage.
176  inline label capacity() const noexcept;
177 
178 
179  // Edit
180 
181  //- Alter the size of the underlying storage.
182  // The addressed size will be truncated if needed to fit, but will
183  // remain otherwise untouched.
184  // Use this or reserve() in combination with append().
185  inline void setCapacity(const label nElem);
186 
187  //- Alter the addressed list size.
188  // New space will be allocated if required.
189  // Use this to resize the list prior to using the operator[] for
190  // setting values (as per List usage).
191  inline void setSize(const label nElem);
192 
193  //- Alter the addressed list size and fill new space with a constant.
194  inline void setSize(const label nElem, const T& val);
195 
196  //- Alter the addressed list size.
197  // New space will be allocated if required.
198  // Use this to resize the list prior to using the operator[] for
199  // setting values (as per List usage).
200  inline void resize(const label nElem);
201 
202  //- Alter the addressed list size and fill new space with a
203  // constant.
204  inline void resize(const label nElem, const T& val);
205 
206  //- Reserve allocation space for at least this size.
207  // Never shrinks the allocated size, use setCapacity() for that.
208  inline void reserve(const label nElem);
209 
210  //- Clear the addressed list, i.e. set the size to zero.
211  // Allocated size does not change
212  inline void clear();
213 
214  //- Clear the list and delete storage.
215  inline void clearStorage();
216 
217  //- Expand the addressable size to fit the allocated capacity.
218  // Returns the previous addressable size.
219  inline label expandStorage();
220 
221  //- Shrink the allocated space to the number of elements used.
222  // Returns a reference to the DynamicField.
223  inline DynamicField<T, SizeMin>& shrink();
224 
225  //- Swap content with any sized DynamicField
226  template<int AnySizeMin>
227  inline void swap(DynamicField<T, AnySizeMin>& list);
228 
229  //- Transfer the parameter contents into this
230  inline void transfer(List<T>& list);
231 
232  //- Transfer the parameter contents into this
233  template<int AnySizeMin>
234  inline void transfer(DynamicList<T, AnySizeMin>& list);
235 
236  //- Transfer the parameter contents into this
237  template<int AnySizeMin>
238  inline void transfer(DynamicField<T, AnySizeMin>& list);
239 
240 
241  //- Append an element at the end of the list
242  inline DynamicField<T, SizeMin>&
243  append(const T& val);
244 
245  //- Append a List at the end of this list
246  inline DynamicField<T, SizeMin>&
247  append(const UList<T>& list);
248 
249  //- Remove and return the top element
250  inline T remove();
251 
252 
253  // Member Operators
254 
255  //- Return non-const access to an element, resizing list if necessary
256  inline T& operator()(const label i);
257 
258  //- Assign addressed entries to the given value
259  inline void operator=(const T& val);
260 
261  //- Copy assignment
262  inline void operator=(const UList<T>& list);
263 
264  //- Copy assignment
265  inline void operator=(const DynamicField<T, SizeMin>& list);
266 
267  //- Move assignment
268  inline void operator=(List<T>&& list);
269 
270  //- Move assignment
271  inline void operator=(DynamicField<T, SizeMin>&& list);
272 
273  //- Move assignment
274  template<int AnySizeMin>
275  inline void operator=(DynamicField<T, AnySizeMin>&& list);
276 
277 };
278 
279 
280 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281 
282 } // End namespace Foam
283 
284 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
285 
286 #include "DynamicFieldI.H"
287 
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 
290 #ifdef NoRepository
291  #include "DynamicField.C"
292 #endif
293 
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 
296 #endif
297 
298 // ************************************************************************* //
Foam::DynamicField::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicFieldI.H:350
Foam::DynamicField::transfer
void transfer(List< T > &list)
Transfer the parameter contents into this.
Definition: DynamicFieldI.H:428
Foam::DynamicField::setSize
void setSize(const label nElem)
Alter the addressed list size.
Definition: DynamicFieldI.H:284
Foam::DynamicField::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: DynamicFieldI.H:357
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::DynamicField::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicFieldI.H:226
Foam::FieldMapper
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:49
DynamicField.C
Foam::DynamicField
Dynamically sized Field.
Definition: DynamicField.H:51
Foam::DynamicField::shrink
DynamicField< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicFieldI.H:378
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Field.H
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::DynamicField::DynamicField
constexpr DynamicField() noexcept
Construct null.
Definition: DynamicFieldI.H:60
Foam::DynamicField::swap
void swap(DynamicField< T, AnySizeMin > &list)
Swap content with any sized DynamicField.
Definition: DynamicFieldI.H:398
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::DynamicField::clone
tmp< DynamicField< T, SizeMin > > clone() const
Clone.
Definition: DynamicField.C:43
Foam::DynamicField::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: DynamicFieldI.H:256
Foam::DynamicField::remove
T remove()
Remove and return the top element.
Definition: DynamicFieldI.H:520
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::DynamicField::expandStorage
label expandStorage()
Expand the addressable size to fit the allocated capacity.
Definition: DynamicFieldI.H:365
Foam::UList
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:103
Foam::DynamicField::setCapacity
void setCapacity(const label nElem)
Alter the size of the underlying storage.
Definition: DynamicFieldI.H:234
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::DynamicField::append
DynamicField< T, SizeMin > & append(const T &val)
Append an element at the end of the list.
Definition: DynamicFieldI.H:483
Foam::DynamicField::resize
void resize(const label nElem)
Alter the addressed list size.
Definition: DynamicFieldI.H:330
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62