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-2019 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 <type_traits>
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 // Forward declaration of friend functions and operators
48 
49 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
50 class DynamicField;
51 
52 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
53 Ostream& operator<<
54 (
55  Ostream&,
57 );
58 
59 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
60 Istream& operator>>
61 (
62  Istream&,
64 );
65 
66 
67 /*---------------------------------------------------------------------------*\
68  Class DynamicField Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 template<class T, unsigned SizeInc=0, unsigned SizeMult=2, unsigned SizeDiv=1>
72 class DynamicField
73 :
74  public Field<T>
75 {
76  static_assert
77  (
78  (SizeInc || SizeMult) && SizeDiv,
79  "Avoid invalid sizing parameters"
80  );
81 
82  // Private Data
83 
84  //- The capacity (allocated size) of the underlying field.
85  label capacity_;
86 
87 
88 public:
89 
90  // Static Member Functions
91 
92  //- Return a null field
94  {
95  return *reinterpret_cast
96  <
98  >(0);
99  }
100 
101 
102  // Constructors
103 
104  //- Construct null
105  inline DynamicField();
106 
107  //- Construct given size.
108  explicit inline DynamicField(const label);
109 
110  //- Construct from UList. Size set to UList size.
111  // Also constructs from DynamicField with different sizing parameters.
112  explicit inline DynamicField(const UList<T>&);
113 
114  //- Move constructor transferring the list contents
115  explicit inline DynamicField(List<T>&&);
116 
117  //- Construct by 1 to 1 mapping from the given field
118  inline DynamicField
119  (
120  const UList<T>& mapF,
121  const labelList& mapAddressing
122  );
123 
124  //- Construct by interpolative mapping from the given field
125  inline DynamicField
126  (
127  const UList<T>& mapF,
128  const labelListList& mapAddressing,
129  const scalarListList& weights
130  );
131 
132  //- Copy constructor
134 
135  //- Move constructor
137 
138  //- Construct from Istream. Size set to size of list read.
139  explicit DynamicField(Istream&);
140 
141  //- Clone
143 
144 
145  // Member Functions
146 
147  // Access
148 
149  //- Size of the underlying storage.
150  inline label capacity() const;
151 
152  // Edit
153 
154  //- Alter the size of the underlying storage.
155  // The addressed size will be truncated if needed to fit, but will
156  // remain otherwise untouched.
157  // Use this or reserve() in combination with append().
158  inline void setCapacity(const label);
159 
160  //- Alter the addressed list size.
161  // New space will be allocated if required.
162  // Use this to resize the list prior to using the operator[] for
163  // setting values (as per List usage).
164  inline void setSize(const label);
165 
166  //- Alter the addressed list size and fill new space with a
167  // constant.
168  inline void setSize(const label, const T&);
169 
170  //- Alter the addressed list size.
171  // New space will be allocated if required.
172  // Use this to resize the list prior to using the operator[] for
173  // setting values (as per List usage).
174  inline void resize(const label);
175 
176  //- Alter the addressed list size and fill new space with a
177  // constant.
178  inline void resize(const label, const T&);
179 
180  //- Reserve allocation space for at least this size.
181  // Never shrinks the allocated size, use setCapacity() for that.
182  inline void reserve(const label);
183 
184  //- Clear the addressed list, i.e. set the size to zero.
185  // Allocated size does not change
186  inline void clear();
187 
188  //- Clear the list and delete storage.
189  inline void clearStorage();
190 
191  //- Shrink the allocated space to the number of elements used.
192  // Returns a reference to the DynamicField.
194 
195 
196  // Member Operators
197 
198  //- Append an element at the end of the list
200  (
201  const T&
202  );
203 
204  //- Append a List at the end of this list
206  (
207  const UList<T>&
208  );
209 
210  //- Remove and return the top element
211  inline T remove();
212 
213  //- Return non-const access to an element, resizing list if
214  // necessary
215  inline T& operator()(const label);
216 
217  //- Assignment of all addressed entries to the given value
218  inline void operator=(const T&);
219 
220  //- Assignment operator
221  inline void operator=
222  (
224  );
225 
226  //- Move assignment operator
227  inline void operator=
228  (
230  );
231 
232  //- Assignment to UList
233  inline void operator=(const UList<T>&);
234 
235  //- Move assignment to List
236  inline void operator=(List<T>&&);
237 };
238 
239 
240 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241 
242 } // End namespace Foam
243 
244 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245 
246 #include "DynamicFieldI.H"
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
250 #ifdef NoRepository
251  #include "DynamicField.C"
252 #endif
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 #endif
257 
258 // ************************************************************************* //
DynamicField< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
DynamicField()
Construct null.
Definition: DynamicFieldI.H:29
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
void setCapacity(const label)
Alter the size of the underlying storage.
void resize(const label)
Alter the addressed list size.
label capacity() const
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: HashTable.H:59
void clear()
Clear the addressed list, i.e. set the size to zero.
void operator=(const T &)
Assignment of all addressed entries to the given value.
Pre-declare SubField and related Field type.
Definition: Field.H:56
T & operator()(const label)
Return non-const access to an element, resizing list if.
void reserve(const label)
Reserve allocation space for at least this size.
Dynamically sized Field.
Definition: DynamicField.H:49
static const DynamicField< T, SizeInc, SizeMult, SizeDiv > & null()
Return a null field.
Definition: DynamicField.H:92
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:60
tmp< DynamicField< T, SizeInc, SizeMult, SizeDiv > > clone() const
Clone.
Definition: DynamicField.C:40
void clearStorage()
Clear the list and delete storage.
void setSize(const label)
Alter the addressed list size.
tmp< Field< T > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:520
A class for managing temporary objects.
Definition: PtrList.H:53
DynamicField< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Namespace for OpenFOAM.