All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-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::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  // Member Operators
154 
155  //- Return element const reference
156  inline const T& operator[](const label) const;
157 
158  //- Return element reference
159  inline T& operator[](const label);
160 
161  //- Return element const pointer
162  inline const T* operator()(const label) const;
163 
164 
165  // STL type definitions
166 
167  //- Type of values the UPtrList contains
168  typedef T value_type;
169 
170  //- Type that can be used for storing into UPtrList::value_type objects
171  typedef T& reference;
172 
173  //- Type that can be used for storing into constant UPtrList::value_type
174  // objects
175  typedef const T& const_reference;
176 
177 
178  // STL iterator
179  // Random access iterator for traversing UPtrList
180 
181  class iterator;
182  class const_iterator;
183  friend class iterator;
184 
185  //- An STL iterator
186  class iterator
187  {
188  T** ptr_;
189 
190  public:
192  friend class const_iterator;
193 
194  //- Construct for a given UPtrList entry
195  inline iterator(T**);
196 
197  // Member Operators
198 
199  inline bool operator==(const iterator&) const;
200  inline bool operator!=(const iterator&) const;
201 
202  inline T& operator*();
203  inline T& operator()();
204 
205  inline iterator operator++();
206  inline iterator operator++(const int);
207 
208  inline iterator operator--();
209  inline iterator operator--(const int);
210 
211  inline iterator operator+=(const label);
212  inline iterator operator-=(const label);
213 
214  inline iterator operator+(const label) const;
215  inline iterator operator-(const label) const;
216 
217  inline label operator-(const iterator&) const;
218 
219  inline T& operator[](const label);
220 
221  inline bool operator<(const iterator&) const;
222  inline bool operator>(const iterator&) const;
223 
224  inline bool operator<=(const iterator&) const;
225  inline bool operator>=(const iterator&) const;
226  };
227 
228  //- Return an iterator to begin traversing the UPtrList
229  inline iterator begin();
230 
231  //- Return an iterator to end traversing the UPtrList
232  inline iterator end();
233 
234 
235  // STL const_iterator
236  // Random access iterator for traversing UPtrList
237 
238  //- An STL-conforming const_iterator
239  class const_iterator
240  {
241  const T* const* ptr_;
242 
243  public:
244 
245  //- Construct for a given UPtrList entry
246  inline const_iterator(const T* const*);
247 
248  //- Construct from an iterator
249  inline const_iterator(const iterator&);
250 
251 
252  // Member Operators
253 
254  inline bool operator==(const const_iterator&) const;
255  inline bool operator!=(const const_iterator&) const;
257  typedef const T& Tref;
258  inline Tref operator*();
259  inline Tref operator()();
260 
261  inline const_iterator operator++();
262  inline const_iterator operator++(const int);
263 
264  inline const_iterator operator--();
265  inline const_iterator operator--(const int);
266 
267  inline const_iterator operator+=(const label);
268  inline const_iterator operator-=(const label);
269 
270  inline const_iterator operator+(const label) const;
271  inline const_iterator operator-(const label) const;
272 
273  inline label operator-(const const_iterator&) const;
274 
275  inline const T& operator[](const label);
276 
277  inline bool operator<(const const_iterator&) const;
278  inline bool operator>(const const_iterator&) const;
279 
280  inline bool operator<=(const const_iterator&) const;
281  inline bool operator>=(const const_iterator&) const;
282  };
283 
284  //- Return an const_iterator to begin traversing the UPtrList
285  inline const_iterator cbegin() const;
286 
287  //- Return an const_iterator to end traversing the UPtrList
288  inline const_iterator cend() const;
289 
290  //- Return an const_iterator to begin traversing the UPtrList
291  inline const_iterator begin() const;
292 
293  //- Return an const_iterator to end traversing the UPtrList
294  inline const_iterator end() const;
295 
296 
297  // IOstream operator
298 
299  //- Write UPtrList to Ostream
300  friend Ostream& operator<< <T>(Ostream&, const UPtrList<T>&);
301 };
302 
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 } // End namespace Foam
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 #include "UPtrListI.H"
311 
312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
313 
314 #ifdef NoRepository
315  #include "UPtrList.C"
316 #endif
317 
318 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
319 
320 #endif
321 
322 // ************************************************************************* //
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
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
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:170
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:238
friend class iterator
Definition: UPtrList.H:181
An STL iterator.
Definition: UPtrList.H:185
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:167
const T & const_reference
Type that can be used for storing into constant UPtrList::value_type.
Definition: UPtrList.H:174
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
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:191
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