UPtrList.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-2022 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::UPtrList
26 
27 Description
28  A templated 1D list of pointers to objects of type <T>, where the
29  size of the array is known and used for subscript bounds checking, etc.
30 
31  The element operator [] returns a reference to the object rather than a
32  pointer. Storage is not allocated during construction or use but is
33  supplied to the constructor as an argument.
34 
35 SourceFiles
36  UPtrList.C
37  UPtrListIO.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef UPtrList_H
42 #define UPtrList_H
43 
44 #include "List.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 // Forward declaration of friend classes
52 template<class T> class PtrList;
53 
54 // Forward declaration of friend functions and operators
55 template<class T> class UPtrList;
56 template<class T> void writeEntry(Ostream& os, const UPtrList<T>&);
57 template<class T> Istream& operator>>(Istream&, UPtrList<T>&);
58 template<class T> Ostream& operator<<(Ostream&, const UPtrList<T>&);
59 
60 
61 /*---------------------------------------------------------------------------*\
62  Class UPtrList Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<class T>
66 class UPtrList
67 {
68  // Private Data
69 
70  List<T*> ptrs_;
71 
72 
73 public:
74 
75  // Related types
76 
77  //- Declare friendship with the UPtrList class
78  friend class PtrList<T>;
79 
80 
81  // Constructors
82 
83  //- Null Constructor
84  UPtrList();
85 
86  //- Construct with size specified
87  explicit UPtrList(const label);
88 
89  //- Construct as copy or re-use as specified
90  UPtrList(UPtrList<T>&, bool reuse);
91 
92 
93  // Member Functions
94 
95  // Access
96 
97  //- Return the number of elements in the UPtrList
98  inline label size() const;
99 
100  //- Return true if the UPtrList is empty (ie, size() is zero)
101  inline bool empty() const;
102 
103  //- Return reference to the first element of the list
104  inline T& first();
105 
106  //- Return reference to first element of the list
107  inline const T& first() const;
108 
109  //- Return reference to the last element of the list
110  inline T& last();
111 
112  //- Return reference to the last element of the list
113  inline const T& last() const;
114 
115 
116  // Edit
117 
118  //- Reset size of UPtrList. This can only be used to set the size
119  // of an empty UPtrList, extend a UPtrList, remove entries from
120  // the end of a UPtrList
121  void setSize(const label);
122 
123  //- Reset size of UPtrList. This can only be used to set the size
124  // of an empty UPtrList, extend a UPtrList, remove entries from
125  // the end of a UPtrList
126  inline void resize(const label);
127 
128  //- Clear the UPtrList, i.e. set size to zero
129  void clear();
130 
131  //- Transfer the contents of the argument UPtrList into this
132  // UPtrList and annul the argument list
133  void transfer(UPtrList<T>&);
134 
135  //- Is element set
136  inline bool set(const label) const;
137 
138  //- Set element. Return old element (can be nullptr).
139  // No checks on new element
140  inline T* set(const label, T*);
141 
142  //- Reorders elements. Ordering does not have to be done in
143  // ascending or descending order. Reordering has to be unique.
144  // (is shuffle)
145  void reorder(const labelUList& oldToNew);
146 
147  //- Reorders elements. Ordering does not have to be done in
148  // ascending or descending order. Reordering has to be unique.
149  // Note: can create unset elements
150  void shuffle(const labelUList& newToOld);
151 
152 
153  // Conversion
154 
155  //- Convert to list of different pointer type
156  template<class T2>
158 
159  //- Convert to list of different pointer type
160  template<class T2>
161  UPtrList<const T2> convert() const;
162 
163 
164  // Member Operators
165 
166  //- Return element const reference
167  inline const T& operator[](const label) const;
168 
169  //- Return element reference
170  inline T& operator[](const label);
171 
172  //- Return element const pointer
173  inline const T* operator()(const label) const;
174 
175 
176  // STL type definitions
177 
178  //- Type of values the UPtrList contains
179  typedef T value_type;
180 
181  //- Type that can be used for storing into UPtrList::value_type objects
182  typedef T& reference;
183 
184  //- Type that can be used for storing into constant UPtrList::value_type
185  // objects
186  typedef const T& const_reference;
187 
188 
189  // STL iterator
190  // Random access iterator for traversing UPtrList
191 
192  class iterator;
193  class const_iterator;
194  friend class iterator;
195 
196  //- An STL iterator
197  class iterator
198  {
199  T** ptr_;
200 
201  public:
203  friend class const_iterator;
204 
205  //- Construct for a given UPtrList entry
206  inline iterator(T**);
207 
208  // Member Operators
209 
210  inline bool operator==(const iterator&) const;
211  inline bool operator!=(const iterator&) const;
212 
213  inline T& operator*();
214  inline T& operator()();
215 
216  inline iterator operator++();
217  inline iterator operator++(const int);
218 
219  inline iterator operator--();
220  inline iterator operator--(const int);
221 
222  inline iterator operator+=(const label);
223  inline iterator operator-=(const label);
224 
225  inline iterator operator+(const label) const;
226  inline iterator operator-(const label) const;
227 
228  inline label operator-(const iterator&) const;
229 
230  inline T& operator[](const label);
231 
232  inline bool operator<(const iterator&) const;
233  inline bool operator>(const iterator&) const;
234 
235  inline bool operator<=(const iterator&) const;
236  inline bool operator>=(const iterator&) const;
237  };
238 
239  //- Return an iterator to begin traversing the UPtrList
240  inline iterator begin();
241 
242  //- Return an iterator to end traversing the UPtrList
243  inline iterator end();
244 
245 
246  // STL const_iterator
247  // Random access iterator for traversing UPtrList
248 
249  //- An STL-conforming const_iterator
250  class const_iterator
251  {
252  const T* const* ptr_;
253 
254  public:
255 
256  //- Construct for a given UPtrList entry
257  inline const_iterator(const T* const*);
258 
259  //- Construct from an iterator
260  inline const_iterator(const iterator&);
261 
262 
263  // Member Operators
264 
265  inline bool operator==(const const_iterator&) const;
266  inline bool operator!=(const const_iterator&) const;
268  typedef const T& Tref;
269  inline Tref operator*();
270  inline Tref operator()();
271 
272  inline const_iterator operator++();
273  inline const_iterator operator++(const int);
274 
275  inline const_iterator operator--();
276  inline const_iterator operator--(const int);
277 
278  inline const_iterator operator+=(const label);
279  inline const_iterator operator-=(const label);
280 
281  inline const_iterator operator+(const label) const;
282  inline const_iterator operator-(const label) const;
283 
284  inline label operator-(const const_iterator&) const;
285 
286  inline const T& operator[](const label);
287 
288  inline bool operator<(const const_iterator&) const;
289  inline bool operator>(const const_iterator&) const;
290 
291  inline bool operator<=(const const_iterator&) const;
292  inline bool operator>=(const const_iterator&) const;
293  };
294 
295  //- Return an const_iterator to begin traversing the UPtrList
296  inline const_iterator cbegin() const;
297 
298  //- Return an const_iterator to end traversing the UPtrList
299  inline const_iterator cend() const;
300 
301  //- Return an const_iterator to begin traversing the UPtrList
302  inline const_iterator begin() const;
303 
304  //- Return an const_iterator to end traversing the UPtrList
305  inline const_iterator end() const;
306 
307 
308  // IOstream operator
309 
310  //- Write UPtrList to Ostream
311  friend Ostream& operator<< <T>(Ostream&, const UPtrList<T>&);
312 };
313 
314 
315 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
316 
317 } // End namespace Foam
318 
319 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
320 
321 #include "UPtrListI.H"
322 
323 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
324 
325 #ifdef NoRepository
326  #include "UPtrList.C"
327 #endif
328 
329 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330 
331 #endif
332 
333 // ************************************************************************* //
iterator operator-=(const label)
Definition: UPtrListI.H:220
void resize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrListI.H:71
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
iterator(T **)
Construct for a given UPtrList entry.
Definition: UPtrListI.H:137
T & reference
Type that can be used for storing into UPtrList::value_type objects.
Definition: UPtrList.H:181
iterator operator-(const label) const
Definition: UPtrListI.H:238
T & operator[](const label)
Definition: UPtrListI.H:256
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
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
UPtrList()
Null Constructor.
Definition: UPtrList.C:31
const T & operator[](const label) const
Return element const reference.
Definition: UPtrListI.H:96
bool empty() const
Return true if the UPtrList is empty (ie, size() is zero)
Definition: UPtrListI.H:36
iterator end()
Return an iterator to end traversing the UPtrList.
Definition: UPtrListI.H:300
bool operator>(const iterator &) const
Definition: UPtrListI.H:270
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:57
An STL-conforming const_iterator.
Definition: UPtrList.H:249
friend class iterator
Definition: UPtrList.H:192
An STL iterator.
Definition: UPtrList.H:196
bool operator<(const iterator &) const
Definition: UPtrListI.H:263
void setSize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrList.C:54
bool operator<=(const iterator &) const
Definition: UPtrListI.H:277
T value_type
Type of values the UPtrList contains.
Definition: UPtrList.H:178
const T & const_reference
Type that can be used for storing into constant UPtrList::value_type.
Definition: UPtrList.H:185
iterator operator+=(const label)
Definition: UPtrListI.H:211
Istream & operator>>(Istream &, directionInfo &)
iterator operator+(const label) const
Definition: UPtrListI.H:229
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:54
void shuffle(const labelUList &newToOld)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:142
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
bool operator>=(const iterator &) const
Definition: UPtrListI.H:284
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
bool operator==(const iterator &) const
Definition: UPtrListI.H:144
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
UPtrList< T2 > convert()
Convert to list of different pointer type.
iterator begin()
Return an iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:292
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
friend class const_iterator
Definition: UPtrList.H:202
void transfer(UPtrList< T > &)
Transfer the contents of the argument UPtrList into this.
Definition: UPtrList.C:87
bool operator!=(const iterator &) const
Definition: UPtrListI.H:151
const_iterator cend() const
Return an const_iterator to end traversing the UPtrList.
Definition: UPtrListI.H:513
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:80
const_iterator cbegin() const
Return an const_iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:505
const T * operator()(const label) const
Return element const pointer.
Definition: UPtrListI.H:128
Namespace for OpenFOAM.
void reorder(const labelUList &oldToNew)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:94
T & first()
Return reference to the first element of the list.
Definition: UPtrListI.H:43