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-2021 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 DynamicField_H
39 #define 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>
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 assignDynList(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;
111 
112  //- Construct empty field with given reserve size.
113  inline explicit DynamicField(const label len);
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);
138 
139  //- Move construct from dynamic Field contents
140  inline DynamicField(DynamicField<T, SizeMin>&& content);
141 
142  //- Move construct with different sizing parameters
143  template<int AnySizeMin>
144  inline DynamicField(DynamicField<T, AnySizeMin>&& content);
145 
146  //- Move construct from DynamicList
147  template<int AnySizeMin>
148  inline DynamicField(DynamicList<T, AnySizeMin>&& list);
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  // Access
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  inline label capacity() const noexcept;
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 append().
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  //- Expand the addressable size to fit the allocated capacity.
253  // Returns the previous addressable size.
254  inline label expandStorage() noexcept;
255 
256  //- Shrink the allocated space to the number of elements used.
257  inline void shrinkStorage();
258 
259  //- Shrink the allocated space to the number of elements used.
260  // Returns a reference to the DynamicField.
261  inline DynamicField<T, SizeMin>& shrink();
262 
263 
264  // Edit
265 
266  //- Swap content, independent of sizing parameter
267  template<int AnySizeMin>
268  inline void swap(DynamicField<T, AnySizeMin>& other);
269 
270  //- Swap content with DynamicList, independent of sizing parameter
271  template<int AnySizeMin>
272  inline void swap(DynamicList<T, AnySizeMin>& other);
273 
274  //- Transfer the parameter contents into this
275  inline void transfer(List<T>& list);
276 
277  //- Transfer the parameter contents into this
278  template<int AnySizeMin>
279  inline void transfer(DynamicList<T, AnySizeMin>& list);
280 
281  //- Transfer the parameter contents into this
282  template<int AnySizeMin>
283  inline void transfer(DynamicField<T, AnySizeMin>& list);
284 
285  //- Append an element at the end of the list
286  inline DynamicField<T, SizeMin>& append(const T& val);
287 
288  //- Move append an element
289  inline DynamicField<T, SizeMin>& append(T&& val);
290 
291  //- Append a List at the end of this list
292  inline DynamicField<T, SizeMin>&
293  append(const UList<T>& list);
294 
295  //- Remove and return the top element
296  inline T remove();
297 
298 
299  // Reading/writing
300 
301  //- Read from Istream, discarding existing contents
302  // Uses a DynamicList::readList internally
303  inline Istream& readList(Istream& is);
304 
305 
306  // Member Operators
307 
308  //- Return non-const access to an element, resizing list if needed
309  inline T& operator()(const label i);
310 
311  //- Assign addressed entries to the given value
312  inline void operator=(const T& val);
313 
314  //- Assign addressed entries to zero
315  inline void operator=(const Foam::zero);
316 
317  //- Copy assignment
318  inline void operator=(const UList<T>& list);
319 
320  //- Copy assignment
321  inline void operator=(const DynamicField<T, SizeMin>& list);
322 
323  //- Move assignment
324  inline void operator=(List<T>&& list);
325 
326  //- Move assignment
327  template<int AnySizeMin>
328  inline void operator=(DynamicList<T, AnySizeMin>&& list);
329 
330  //- Move assignment
331  inline void operator=(DynamicField<T, SizeMin>&& list);
332 
333  //- Move assignment
334  template<int AnySizeMin>
335  inline void operator=(DynamicField<T, AnySizeMin>&& list);
336 
337 
338  // IOstream Operators
339 
340  //- Use the readList() method to read contents from Istream.
341  friend Istream& operator>> <T, SizeMin>
342  (
343  Istream& is,
344  DynamicField<T, SizeMin>& rhs
345  );
346 
347  //- Write to Ostream
348  friend Ostream& operator<< <T, SizeMin>
349  (
350  Ostream& os,
351  const DynamicField<T, SizeMin>& rhs
352  );
353 };
354 
355 
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
357 
358 } // End namespace Foam
359 
360 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
361 
362 #include "DynamicFieldI.H"
363 
364 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
365 
366 #endif
367 
368 // ************************************************************************* //
Foam::DynamicField::transfer
void transfer(List< T > &list)
Transfer the parameter contents into this.
Definition: DynamicFieldI.H:532
Foam::DynamicField::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: DynamicFieldI.H:438
Foam::DynamicField::resize
void resize(const label len)
Definition: DynamicFieldI.H:393
Foam::DynamicField::setCapacity_nocopy
void setCapacity_nocopy(const label len)
Definition: DynamicFieldI.H:353
Foam::DynamicField::setSize
void setSize(const label n)
Alias for resize()
Definition: DynamicField.H:239
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::operator
friend Ostream & operator(Ostream &os, const DynamicField< T, SizeMin > &rhs)
Write to Ostream.
Foam::DynamicField::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicFieldI.H:327
Foam::FieldMapper
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:49
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::DynamicField::setSize
void setSize(const label n, const T &val)
Alias for resize()
Definition: DynamicField.H:242
Foam::DynamicField::swap
void swap(DynamicField< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
Definition: DynamicFieldI.H:485
Foam::DynamicField
Dynamically sized Field.
Definition: DynamicField.H:49
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::DynamicField::shrinkStorage
void shrinkStorage()
Shrink the allocated space to the number of elements used.
Definition: DynamicFieldI.H:458
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::DynamicField::shrink
DynamicField< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicFieldI.H:475
Foam::DynamicField::setCapacity
void setCapacity(const label len)
Alter the size of the underlying storage.
Definition: DynamicFieldI.H:343
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
Foam::DynamicField::expandStorage
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
Definition: DynamicFieldI.H:446
Foam::DynamicField::DynamicField
constexpr DynamicField() noexcept
Default construct, an empty field without allocation.
Definition: DynamicFieldI.H:132
Foam::DynamicField::reserve_nocopy
void reserve_nocopy(const label len)
Definition: DynamicFieldI.H:383
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::DynamicField::readList
Istream & readList(Istream &is)
Read from Istream, discarding existing contents.
Definition: DynamicFieldI.H:767
Foam::DynamicField::clone
tmp< DynamicField< T, SizeMin > > clone() const
Clone.
Definition: DynamicFieldI.H:318
Foam::DynamicField::remove
T remove()
Remove and return the top element.
Definition: DynamicFieldI.H:640
Foam::DynamicField::min_size
static constexpr label min_size() noexcept
Normal lower capacity limit - the SizeMin template parameter.
Definition: DynamicField.H:183
Foam::DynamicField::setCapacity_unsafe
void setCapacity_unsafe(const label len) noexcept
Definition: DynamicFieldI.H:363
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::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::reserve
void reserve(const label len)
Definition: DynamicFieldI.H:373
DynamicList.H
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::DynamicField::resize_nocopy
void resize_nocopy(const label len)
Definition: DynamicFieldI.H:403
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:587
Foam::DynamicField::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicFieldI.H:431
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62
Foam::DynamicField::capacity_bytes
std::streamsize capacity_bytes() const noexcept
Number of contiguous bytes of the underlying storage.
Definition: DynamicFieldI.H:335