UPtrList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::UPtrList
29 
30 Description
31  A list of pointers to objects of type <T>, without allocation/deallocation
32  management of the pointers - this is to be done elsewhere.
33  The operator[] returns a reference to the object, not the pointer.
34 
35 Note
36  The class definition is such that it contains a list of pointers, but
37  itself does not inherit from a list of pointers since this would
38  wreak havoc later with inheritance resolution.
39 
40 See Also
41  Foam::PtrList
42  Foam::PtrDynList
43 
44 SourceFiles
45  UPtrListI.H
46  UPtrList.C
47 
48 \*---------------------------------------------------------------------------*/
49 
50 #ifndef UPtrList_H
51 #define UPtrList_H
52 
53 #include "PtrListDetail.H"
54 #include <iterator>
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 
61 // Forward Declarations
62 template<class T> class PtrList;
63 template<class T> class UPtrList;
64 template<class T> Ostream& operator<<(Ostream& os, const UPtrList<T>& list);
65 
66 /*---------------------------------------------------------------------------*\
67  Class UPtrList Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 template<class T>
71 class UPtrList
72 {
73 protected:
74 
75  // Protected Member Data
76 
77  //- The list of pointers
79 
80 
81  // Constructors
82 
83  //- Low-level move construct
84  inline UPtrList(Detail::PtrListDetail<T>&& ptrs);
85 
86 
87 public:
88 
89  // STL type definitions
90 
91  //- Type of values the list contains
92  typedef T value_type;
93 
94  //- A non-const reference to the value_type
95  typedef T& reference;
96 
97  //- A const reference to the value_type
98  typedef const T& const_reference;
99 
100  //- Random-access iterator with non-const access
101  class iterator;
102 
103  //- Random-access iterator with const access
104  class const_iterator;
105 
106 
107  // Constructors
108 
109  //- Default construct
110  inline constexpr UPtrList() noexcept;
111 
112  //- Construct with specified size, each element initialized to nullptr
113  inline explicit UPtrList(const label len);
114 
115  //- Copy construct (shallow copies addresses)
116  inline UPtrList(const UPtrList<T>& list);
117 
118  //- Move construct
119  inline UPtrList(UPtrList<T>&& list);
120 
121  //- Construct as shallow copy or re-use as specified
122  inline UPtrList(UPtrList<T>& list, bool reuse);
123 
124  //- Shallow copy from PtrList.
125  // The argument is non-const to reflect that the UPtrList can change
126  // the values (not the addresses) within the original list.
127  explicit UPtrList(PtrList<T>& list);
128 
129  //- Construct from UList of pointers
130  inline explicit UPtrList(const UList<T*>& list);
131 
132  //- Construct from UList, taking the address of each list element
133  // The argument is non-const to reflect that the UPtrList can change
134  // the values of the original list.
135  inline explicit UPtrList(UList<T>& list);
136 
137 
138  // Member Functions
139 
140  // Access
141 
142  //- The number of elements in the list
143  inline label size() const noexcept;
144 
145  //- True if the list is empty (ie, size() is zero)
146  inline bool empty() const noexcept;
147 
148  //- Return reference to the first element of the list
149  inline T& first();
150 
151  //- Return reference to first element of the list
152  inline const T& first() const;
153 
154  //- Return reference to the last element of the list
155  inline T& last();
156 
157  //- Return reference to the last element of the list
158  inline const T& last() const;
159 
160  //- Return pointer to element (can be nullptr),
161  // without bounds checking.
162  inline T* get(const label i);
163 
164  //- Return const pointer to element (can be nullptr),
165  // without bounds checking.
166  inline const T* get(const label i) const;
167 
168  //- Return const pointer to element (can be nullptr),
169  // without bounds checking - same as get().
170  // The return value can also be tested as a bool.
171  const T* set(const label i) const { return this->get(i); }
172 
173 
174  // Edit
175 
176  //- Set list size to zero.
177  inline void clear();
178 
179  //- Reset size of list.
180  // New entries are initialized to nullptr.
181  inline void resize(const label newLen);
182 
183  //- Same as resize()
184  inline void setSize(const label newLen);
185 
186  //- Squeeze out intermediate nullptr entries in the list of pointers
187  // \return the number of non-null entries
188  label squeezeNull();
189 
190  //- Append an element to the end of the list
191  inline void append(T* ptr);
192 
193  //- Swap content
194  inline void swap(UPtrList<T>& list);
195 
196  //- Transfer contents into this list and annul the argument
197  inline void transfer(UPtrList<T>& list);
198 
199  //- Set element to specified pointer and return the old list element,
200  //- which can be a nullptr.
201  // No-op if the new pointer value is identical to the current content.
202  inline T* set(const label i, T* ptr);
203 
204  //- Reorder elements.
205  //- Reordering must be unique (ie, shuffle).
206  // Optionally verify that all pointers have been set.
207  void reorder(const labelUList& oldToNew, const bool testNull = true);
208 
209  //- Reorder elements according to new order mapping (newToOld).
210  //- Reordering must be unique (ie, shuffle).
211  // Optionally verify that all pointers have been set.
212  void sortOrder(const labelUList& order, const bool testNull = true);
213 
214 
215  // Member Operators
216 
217  //- Return const reference to the element
218  inline const T& operator[](const label i) const;
219 
220  //- Return reference to the element
221  inline T& operator[](const label i);
222 
223  //- Return const pointer to the element - same as get().
224  inline const T* operator()(const label i) const;
225 
226  //- Copy assignment (shallow copies addresses)
227  inline void operator=(const UPtrList<T>& list);
228 
229  //- Move assignment
230  inline void operator=(UPtrList<T>&& list);
231 
232 
233  // Iterators
234 
235  //- Random-access iterator with non-const access
236  class iterator
237  {
238  T** ptr_;
239 
240  public:
241 
242  using iterator_category = std::random_access_iterator_tag;
243  using value_type = T;
244  using difference_type = label;
245  using pointer = T*;
246  using reference = T&;
247  friend class const_iterator;
248 
249  //- Construct for a given entry
250  inline iterator(T** ptr);
251 
252  // Member functions
253 
254  //- Return pointer, can be nullptr.
255  inline pointer get() const;
256 
257  // Member operators
258 
259  inline bool operator==(const iterator& iter) const;
260  inline bool operator!=(const iterator& iter) const;
261 
262  inline pointer operator->() const;
263  inline reference operator*() const;
264 
265  inline reference operator()() const;
266 
267  // Forward iteration
268  inline iterator& operator++();
269  inline iterator operator++(int);
270 
271  inline iterator& operator--();
272  inline iterator operator--(int);
273 
274  // Random-access
277  inline iterator operator+(difference_type n) const;
278  inline iterator operator-(difference_type n) const;
279 
280  inline difference_type operator-(const iterator& iter) const;
281 
282  inline reference operator[](difference_type n) const;
283 
284  inline bool operator<(const iterator& iter) const;
285  inline bool operator>(const iterator& iter) const;
286 
287  inline bool operator<=(const iterator& iter) const;
288  inline bool operator>=(const iterator& iter) const;
289  };
290 
291 
292  //- Random-access iterator with const access
293  class const_iterator
294  {
295  const T* const* ptr_;
296 
297  public:
298 
299  using iterator_category = std::random_access_iterator_tag;
300  using value_type = const T;
301  using difference_type = label;
302  using pointer = const T*;
303  using reference = const T&;
304 
305  //- Construct for a given entry
306  inline const_iterator(const T* const* ptr);
307 
308  //- Copy construct from non-const iterator
309  inline const_iterator(const iterator& iter);
310 
311  // Member functions
312 
313  //- Return pointer, can be nullptr.
314  inline pointer get() const;
315 
316  // Member operators
317 
318  inline bool operator==(const const_iterator& iter) const;
319  inline bool operator!=(const const_iterator& iter) const;
320 
321  inline pointer operator->() const;
322  inline reference operator*() const;
323 
324  inline reference operator()() const;
325 
326  // Forward iteration
327  inline const_iterator& operator++();
328  inline const_iterator operator++(int);
329 
330  inline const_iterator& operator--();
331  inline const_iterator operator--(int);
332 
333  // Random-access
338 
339  inline difference_type operator-(const const_iterator& iter) const;
340 
341  inline reference operator[](difference_type n) const;
342 
343  inline bool operator<(const const_iterator& iter) const;
344  inline bool operator>(const const_iterator& iter) const;
345 
346  inline bool operator<=(const const_iterator& iter) const;
347  inline bool operator>=(const const_iterator& iter) const;
348  };
349 
350 
351  //- Return an iterator to begin traversing the UPtrList
352  inline iterator begin();
353 
354  //- Return an iterator to end traversing the UPtrList
355  inline iterator end();
356 
357  //- Return an const_iterator to begin traversing the UPtrList
358  inline const_iterator cbegin() const;
359 
360  //- Return an const_iterator to end traversing the UPtrList
361  inline const_iterator cend() const;
362 
363  //- Return an const_iterator to begin traversing the UPtrList
364  inline const_iterator begin() const;
365 
366  //- Return an const_iterator to end traversing the UPtrList
367  inline const_iterator end() const;
368 
369 
370  // IOstream operator
371 
372  //- Write UPtrList to Ostream
373  friend Ostream& operator<< <T>
374  (
375  Ostream& os,
376  const UPtrList<T>& list
377  );
378 
379 };
380 
381 
382 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
383 
384 } // End namespace Foam
385 
386 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
387 
388 #include "UPtrListI.H"
389 
390 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
391 
392 #ifdef NoRepository
393  #include "UPtrList.C"
394 #endif
395 
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397 
398 #endif
399 
400 // ************************************************************************* //
Foam::UPtrList::const_iterator::operator-
const_iterator operator-(difference_type n) const
Definition: UPtrListI.H:548
Foam::UPtrList::const_iterator::operator==
bool operator==(const const_iterator &iter) const
Definition: UPtrListI.H:443
Foam::UPtrList::iterator::value_type
T value_type
Definition: UPtrList.H:242
Foam::UPtrList::size
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:97
Foam::UPtrList::first
T & first()
Return reference to the first element of the list.
Definition: UPtrListI.H:146
Foam::UPtrList::const_iterator::operator<
bool operator<(const const_iterator &iter) const
Definition: UPtrListI.H:572
Foam::UPtrList::iterator::difference_type
label difference_type
Definition: UPtrList.H:243
Foam::UPtrList::begin
iterator begin()
Return an iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:614
Foam::UPtrList::clear
void clear()
Set list size to zero.
Definition: UPtrListI.H:125
Foam::UPtrList::iterator::operator>=
bool operator>=(const iterator &iter) const
Definition: UPtrListI.H:412
Foam::UPtrList::swap
void swap(UPtrList< T > &list)
Swap content.
Definition: UPtrListI.H:132
Foam::UPtrList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: UPtrListI.H:188
Foam::UPtrList::const_iterator::operator+=
const_iterator & operator+=(difference_type n)
Definition: UPtrListI.H:522
Foam::UPtrList::const_iterator::operator++
const_iterator & operator++()
Definition: UPtrListI.H:484
Foam::UPtrList::ptrs_
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:77
Foam::UPtrList::reference
T & reference
A non-const reference to the value_type.
Definition: UPtrList.H:94
Foam::UPtrList::get
T * get(const label i)
Return pointer to element (can be nullptr),.
Definition: UPtrListI.H:111
Foam::UPtrList::const_iterator::pointer
const T * pointer
Definition: UPtrList.H:301
Foam::UPtrList::iterator::pointer
T * pointer
Definition: UPtrList.H:244
Foam::UPtrList::const_iterator::value_type
const T value_type
Definition: UPtrList.H:299
Foam::UPtrList::operator=
void operator=(const UPtrList< T > &list)
Copy assignment (shallow copies addresses)
Definition: UPtrListI.H:663
Foam::UPtrList::const_iterator::operator()
reference operator()() const
Definition: UPtrListI.H:476
Foam::UPtrList::const_iterator::operator*
reference operator*() const
Definition: UPtrListI.H:469
Foam::UPtrList::iterator::operator+
iterator operator+(difference_type n) const
Definition: UPtrListI.H:361
Foam::UPtrList::iterator::operator<=
bool operator<=(const iterator &iter) const
Definition: UPtrListI.H:405
Foam::UPtrList::const_iterator
Random-access iterator with const access.
Definition: UPtrList.H:292
Foam::UPtrList::iterator::get
pointer get() const
Return pointer, can be nullptr.
Definition: UPtrListI.H:262
Foam::UPtrList::const_iterator::operator>
bool operator>(const const_iterator &iter) const
Definition: UPtrListI.H:582
Foam::UPtrList::iterator::operator!=
bool operator!=(const iterator &iter) const
Definition: UPtrListI.H:276
Foam::UPtrList::transfer
void transfer(UPtrList< T > &list)
Transfer contents into this list and annul the argument.
Definition: UPtrListI.H:139
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::UPtrList::resize
void resize(const label newLen)
Reset size of list.
Definition: UPtrListI.H:174
Foam::UPtrList::value_type
T value_type
Type of values the list contains.
Definition: UPtrList.H:91
Foam::UPtrList::const_iterator::operator>=
bool operator>=(const const_iterator &iter) const
Definition: UPtrListI.H:602
Foam::UPtrList::iterator::reference
T & reference
Definition: UPtrList.H:245
UPtrListI.H
Foam::UPtrList::const_iterator::operator!=
bool operator!=(const const_iterator &iter) const
Definition: UPtrListI.H:453
Foam::UPtrList::iterator::operator()
reference operator()() const
Definition: UPtrListI.H:297
Foam::UPtrList::iterator
Random-access iterator with non-const access.
Definition: UPtrList.H:235
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:62
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
Foam::UPtrList::const_iterator::get
pointer get() const
Return pointer, can be nullptr.
Definition: UPtrListI.H:435
Foam::UPtrList::UPtrList
constexpr UPtrList() noexcept
Default construct.
Definition: UPtrListI.H:32
Foam::UPtrList::iterator::operator++
iterator & operator++()
Definition: UPtrListI.H:305
Foam::UPtrList::sortOrder
void sortOrder(const labelUList &order, const bool testNull=true)
Definition: UPtrList.C:129
Foam::UPtrList::const_iterator::operator--
const_iterator & operator--()
Definition: UPtrListI.H:503
PtrListDetail.H
Foam::UPtrList::iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: UPtrList.H:241
Foam::UPtrList::iterator::operator*
reference operator*() const
Definition: UPtrListI.H:290
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::UPtrList::const_iterator::reference
const T & reference
Definition: UPtrList.H:302
Foam::UPtrList::iterator::operator-
iterator operator-(difference_type n) const
Definition: UPtrListI.H:369
Foam::UPtrList::const_iterator::operator<=
bool operator<=(const const_iterator &iter) const
Definition: UPtrListI.H:592
Foam::UPtrList::const_iterator::operator+
const_iterator operator+(difference_type n) const
Definition: UPtrListI.H:540
Foam::UPtrList::iterator::operator->
pointer operator->() const
Definition: UPtrListI.H:283
Foam::UPtrList::iterator::operator>
bool operator>(const iterator &iter) const
Definition: UPtrListI.H:398
Foam::UPtrList::cend
const_iterator cend() const
Return an const_iterator to end traversing the UPtrList.
Definition: UPtrListI.H:638
Foam::UPtrList::iterator::iterator
iterator(T **ptr)
Construct for a given entry.
Definition: UPtrListI.H:255
Foam::UPtrList::iterator::operator==
bool operator==(const iterator &iter) const
Definition: UPtrListI.H:269
Foam::UPtrList::iterator::operator[]
reference operator[](difference_type n) const
Definition: UPtrListI.H:384
Foam::UPtrList::set
const T * set(const label i) const
Return const pointer to element (can be nullptr),.
Definition: UPtrList.H:170
Foam::UPtrList::end
iterator end()
Return an iterator to end traversing the UPtrList.
Definition: UPtrListI.H:622
Foam::UPtrList::const_iterator::operator[]
reference operator[](difference_type n) const
Definition: UPtrListI.H:564
Foam::UPtrList::last
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:160
Foam::UPtrList::squeezeNull
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers.
Definition: UPtrList.C:45
Foam::UList
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:103
Foam::UPtrList::const_iterator::operator-=
const_iterator & operator-=(difference_type n)
Definition: UPtrListI.H:531
Foam::UPtrList::operator[]
const T & operator[](const label i) const
Return const reference to the element.
Definition: UPtrListI.H:212
Foam::Detail::PtrListDetail
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:61
Foam::UPtrList::const_iterator::difference_type
label difference_type
Definition: UPtrList.H:300
Foam::UPtrList::const_iterator::operator->
pointer operator->() const
Definition: UPtrListI.H:462
Foam::UPtrList::const_iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: UPtrList.H:298
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::UPtrList::reorder
void reorder(const labelUList &oldToNew, const bool testNull=true)
Definition: UPtrList.C:70
Foam::UPtrList::operator()
const T * operator()(const label i) const
Return const pointer to the element - same as get().
Definition: UPtrListI.H:246
Foam::UPtrList::iterator::operator<
bool operator<(const iterator &iter) const
Definition: UPtrListI.H:391
Foam::UPtrList::const_reference
const typedef T & const_reference
A const reference to the value_type.
Definition: UPtrList.H:97
Foam::UPtrList::iterator::operator+=
iterator & operator+=(difference_type n)
Definition: UPtrListI.H:343
Foam::UPtrList::iterator::operator--
iterator & operator--()
Definition: UPtrListI.H:324
Foam::UPtrList::iterator::operator-=
iterator & operator-=(difference_type n)
Definition: UPtrListI.H:352
Foam::UPtrList::cbegin
const_iterator cbegin() const
Return an const_iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:630
Foam::UPtrList::empty
bool empty() const noexcept
True if the list is empty (ie, size() is zero)
Definition: UPtrListI.H:104
UPtrList.C
Foam::UPtrList::const_iterator::const_iterator
const_iterator(const T *const *ptr)
Construct for a given entry.
Definition: UPtrListI.H:421
Foam::UPtrList::setSize
void setSize(const label newLen)
Same as resize()
Definition: UPtrListI.H:181