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-2023 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 PtrList 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  //- Return element const pointer
176  inline T* operator()(const label);
177 
178 
179  // STL type definitions
180 
181  //- Type of values the UPtrList contains
182  typedef T value_type;
183 
184  //- Type that can be used for storing into UPtrList::value_type objects
185  typedef T& reference;
186 
187  //- Type that can be used for storing into constant UPtrList::value_type
188  // objects
189  typedef const T& const_reference;
190 
191 
192  // STL iterator
193  // Random access iterator for traversing UPtrList
194 
195  class iterator;
197  friend class iterator;
198 
199  //- An STL iterator
200  class iterator
201  {
202  T** ptr_;
203 
204  public:
205 
206  friend class const_iterator;
207 
208  //- Construct for a given UPtrList entry
209  inline iterator(T**);
210 
211  // Member Operators
212 
213  inline bool operator==(const iterator&) const;
214  inline bool operator!=(const iterator&) const;
215 
216  inline T& operator*();
217  inline T& operator()();
218 
219  inline iterator operator++();
220  inline iterator operator++(const int);
221 
222  inline iterator operator--();
223  inline iterator operator--(const int);
224 
225  inline iterator operator+=(const label);
226  inline iterator operator-=(const label);
227 
228  inline iterator operator+(const label) const;
229  inline iterator operator-(const label) const;
230 
231  inline label operator-(const iterator&) const;
232 
233  inline T& operator[](const label);
234 
235  inline bool operator<(const iterator&) const;
236  inline bool operator>(const iterator&) const;
237 
238  inline bool operator<=(const iterator&) const;
239  inline bool operator>=(const iterator&) const;
240  };
241 
242  //- Return an iterator to begin traversing the UPtrList
243  inline iterator begin();
244 
245  //- Return an iterator to end traversing the UPtrList
246  inline iterator end();
247 
248 
249  // STL const_iterator
250  // Random access iterator for traversing UPtrList
251 
252  //- An STL-conforming const_iterator
253  class const_iterator
254  {
255  const T* const* ptr_;
256 
257  public:
258 
259  //- Construct for a given UPtrList entry
260  inline const_iterator(const T* const*);
261 
262  //- Construct from an iterator
263  inline const_iterator(const iterator&);
264 
265 
266  // Member Operators
267 
268  inline bool operator==(const const_iterator&) const;
269  inline bool operator!=(const const_iterator&) const;
270 
271  typedef const T& Tref;
272  inline Tref operator*();
273  inline Tref operator()();
274 
275  inline const_iterator operator++();
276  inline const_iterator operator++(const int);
277 
278  inline const_iterator operator--();
279  inline const_iterator operator--(const int);
280 
281  inline const_iterator operator+=(const label);
282  inline const_iterator operator-=(const label);
283 
284  inline const_iterator operator+(const label) const;
285  inline const_iterator operator-(const label) const;
286 
287  inline label operator-(const const_iterator&) const;
288 
289  inline const T& operator[](const label);
290 
291  inline bool operator<(const const_iterator&) const;
292  inline bool operator>(const const_iterator&) const;
293 
294  inline bool operator<=(const const_iterator&) const;
295  inline bool operator>=(const const_iterator&) const;
296  };
297 
298  //- Return an const_iterator to begin traversing the UPtrList
299  inline const_iterator cbegin() const;
300 
301  //- Return an const_iterator to end traversing the UPtrList
302  inline const_iterator cend() const;
303 
304  //- Return an const_iterator to begin traversing the UPtrList
305  inline const_iterator begin() const;
306 
307  //- Return an const_iterator to end traversing the UPtrList
308  inline const_iterator end() const;
309 
310 
311  // IOstream operator
312 
313  //- Write UPtrList to Ostream
314  friend Ostream& operator<< <T>(Ostream&, const UPtrList<T>&);
315 };
316 
317 
318 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
319 
320 } // End namespace Foam
321 
322 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
323 
324 #include "UPtrListI.H"
325 
326 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
327 
328 #ifdef NoRepository
329  #include "UPtrList.C"
330 #endif
331 
332 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
333 
334 #endif
335 
336 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
An STL-conforming const_iterator.
Definition: UPtrList.H:253
const_iterator operator-(const label) const
Definition: UPtrListI.H:430
bool operator!=(const const_iterator &) const
Definition: UPtrListI.H:341
bool operator>(const const_iterator &) const
Definition: UPtrListI.H:466
bool operator<=(const const_iterator &) const
Definition: UPtrListI.H:476
const T & operator[](const label)
Definition: UPtrListI.H:448
bool operator>=(const const_iterator &) const
Definition: UPtrListI.H:486
const_iterator operator--()
Definition: UPtrListI.H:384
bool operator==(const const_iterator &) const
Definition: UPtrListI.H:331
const_iterator operator+=(const label)
Definition: UPtrListI.H:403
bool operator<(const const_iterator &) const
Definition: UPtrListI.H:456
const_iterator(const T *const *)
Construct for a given UPtrList entry.
Definition: UPtrListI.H:316
const_iterator operator+(const label) const
Definition: UPtrListI.H:421
const_iterator operator-=(const label)
Definition: UPtrListI.H:412
const_iterator operator++()
Definition: UPtrListI.H:365
An STL iterator.
Definition: UPtrList.H:200
bool operator!=(const iterator &) const
Definition: UPtrListI.H:158
bool operator>(const iterator &) const
Definition: UPtrListI.H:277
iterator operator-(const label) const
Definition: UPtrListI.H:245
T & operator[](const label)
Definition: UPtrListI.H:263
bool operator==(const iterator &) const
Definition: UPtrListI.H:151
bool operator<(const iterator &) const
Definition: UPtrListI.H:270
iterator operator+(const label) const
Definition: UPtrListI.H:236
iterator operator-=(const label)
Definition: UPtrListI.H:227
bool operator<=(const iterator &) const
Definition: UPtrListI.H:284
bool operator>=(const iterator &) const
Definition: UPtrListI.H:291
iterator operator+=(const label)
Definition: UPtrListI.H:218
iterator(T **)
Construct for a given UPtrList entry.
Definition: UPtrListI.H:144
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:66
iterator begin()
Return an iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:299
UPtrList< T2 > convert()
Convert to list of different pointer type.
T & first()
Return reference to the first element of the list.
Definition: UPtrListI.H:43
T value_type
Type of values the UPtrList contains.
Definition: UPtrList.H:181
bool set(const label) const
Is element set.
Definition: UPtrListI.H:78
void transfer(UPtrList< T > &)
Transfer the contents of the argument UPtrList into this.
Definition: UPtrList.C:87
iterator end()
Return an iterator to end traversing the UPtrList.
Definition: UPtrListI.H:307
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
void resize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrListI.H:71
bool empty() const
Return true if the UPtrList is empty (ie, size() is zero)
Definition: UPtrListI.H:36
const T & operator[](const label) const
Return element const reference.
Definition: UPtrListI.H:96
T & reference
Type that can be used for storing into UPtrList::value_type objects.
Definition: UPtrList.H:184
UPtrList()
Null Constructor.
Definition: UPtrList.C:31
const_iterator cbegin() const
Return an const_iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:512
const_iterator cend() const
Return an const_iterator to end traversing the UPtrList.
Definition: UPtrListI.H:520
void reorder(const labelUList &oldToNew)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:94
void shuffle(const labelUList &newToOld)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:142
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:80
const T * operator()(const label) const
Return element const pointer.
Definition: UPtrListI.H:128
void setSize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrList.C:54
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:57
const T & const_reference
Type that can be used for storing into constant UPtrList::value_type.
Definition: UPtrList.H:188
Namespace for OpenFOAM.
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 writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, directionInfo &)
Ostream & operator<<(Ostream &, const ensightPart &)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)