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-2018 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  //- Construct by transferring the parameter contents
115  explicit inline DynamicField(const Xfer<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  //- Construct by mapping from the given field
133  inline DynamicField
134  (
135  const UList<T>& mapF,
136  const FieldMapper& map
137  );
138 
139  //- Construct copy
141 
142  //- Construct by transferring the Field contents
143  inline DynamicField
144  (
146  );
147 
148  //- Construct from Istream. Size set to size of list read.
149  explicit DynamicField(Istream&);
150 
151  //- Clone
153 
154 
155  // Member Functions
156 
157  // Access
158 
159  //- Size of the underlying storage.
160  inline label capacity() const;
161 
162  // Edit
163 
164  //- Alter the size of the underlying storage.
165  // The addressed size will be truncated if needed to fit, but will
166  // remain otherwise untouched.
167  // Use this or reserve() in combination with append().
168  inline void setCapacity(const label);
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 setSize(const label);
175 
176  //- Alter the addressed list size and fill new space with a
177  // constant.
178  inline void setSize(const label, const T&);
179 
180  //- Alter the addressed list size.
181  // New space will be allocated if required.
182  // Use this to resize the list prior to using the operator[] for
183  // setting values (as per List usage).
184  inline void resize(const label);
185 
186  //- Alter the addressed list size and fill new space with a
187  // constant.
188  inline void resize(const label, const T&);
189 
190  //- Reserve allocation space for at least this size.
191  // Never shrinks the allocated size, use setCapacity() for that.
192  inline void reserve(const label);
193 
194  //- Clear the addressed list, i.e. set the size to zero.
195  // Allocated size does not change
196  inline void clear();
197 
198  //- Clear the list and delete storage.
199  inline void clearStorage();
200 
201  //- Shrink the allocated space to the number of elements used.
202  // Returns a reference to the DynamicField.
204 
205  //- Transfer contents to the Xfer container as a plain List
206  inline Xfer<List<T>> xfer();
207 
208 
209  // Member Operators
210 
211  //- Append an element at the end of the list
213  (
214  const T&
215  );
216 
217  //- Append a List at the end of this list
219  (
220  const UList<T>&
221  );
222 
223  //- Remove and return the top element
224  inline T remove();
225 
226  //- Return non-const access to an element, resizing list if
227  // necessary
228  inline T& operator()(const label);
229 
230  //- Assignment of all addressed entries to the given value
231  inline void operator=(const T&);
232 
233  //- Assignment to DynamicField
234  inline void operator=
235  (
237  );
238 
239  //- Assignment to UList
240  inline void operator=(const UList<T>&);
241 };
242 
243 
244 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245 
246 } // End namespace Foam
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
250 #include "DynamicFieldI.H"
251 
252 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
253 
254 #ifdef NoRepository
255  #include "DynamicField.C"
256 #endif
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 #endif
261 
262 // ************************************************************************* //
DynamicField< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
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:60
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.
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:45
Pre-declare SubField and related Field type.
Definition: Field.H:57
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:61
void map(const UList< T > &mapF, const labelUList &mapAddressing)
1 to 1 map from the given field
Definition: Field.C:358
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:717
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
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.