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-2019 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 
63 template<class T> class PtrList;
64 template<class T> class UPtrList;
65 
66 template<class T> Ostream& operator<<(Ostream& os, const UPtrList<T>& list);
67 
68 
69 /*---------------------------------------------------------------------------*\
70  Class UPtrList Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 template<class T>
74 class UPtrList
75 {
76 protected:
77 
78  // Protected Member Data
79 
80  //- The list of pointers
82 
83 
84  // Constructors
85 
86  //- Low-level move construct
87  inline UPtrList(Detail::PtrListDetail<T>&& ptrs);
88 
89 
90 public:
91 
92  // STL type definitions
93 
94  //- Type of values the list contains
95  typedef T value_type;
96 
97  //- A non-const reference to the value_type
98  typedef T& reference;
99 
100  //- A const reference to the value_type
101  typedef const T& const_reference;
102 
103  //- Random-access iterator with non-const access
104  class iterator;
105 
106  //- Random-access iterator with const access
107  class const_iterator;
108 
109 
110  // Constructors
111 
112  //- Construct null
113  inline constexpr UPtrList() noexcept;
114 
115  //- Construct with specified size, each element initialized to nullptr
116  inline explicit UPtrList(const label len);
117 
118  //- Copy construct (shallow copies addresses)
119  inline UPtrList(const UPtrList<T>& list);
120 
121  //- Move construct
122  inline UPtrList(UPtrList<T>&& list);
123 
124  //- Construct as shallow copy or re-use as specified
125  inline UPtrList(UPtrList<T>& list, bool reuse);
126 
127  //- Shallow copy from PtrList.
128  // The argument is non-const to reflect that the UPtrList can change
129  // the values (but not the addresses) of the original list.
130  explicit UPtrList(PtrList<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 
161  // Edit
162 
163  //- Set list size to zero.
164  inline void clear();
165 
166  //- Reset size of list.
167  // New entries are initialized to nullptr.
168  inline void resize(const label newLen);
169 
170  //- Same as resize()
171  inline void setSize(const label newLen);
172 
173  //- Squeeze out intermediate nullptr entries in the list of pointers
174  // \return the number of non-null entries
175  label squeezeNull();
176 
177  //- Append an element to the end of the list
178  inline void append(T* ptr);
179 
180  //- Swap content
181  inline void swap(UPtrList<T>& list);
182 
183  //- Transfer contents into this list and annul the argument
184  inline void transfer(UPtrList<T>& list);
185 
186  //- Return const pointer to element (if set) or nullptr.
187  // The return value can be tested as a bool.
188  inline const T* set(const label i) const;
189 
190  //- Set element to specified pointer and return the old list element,
191  //- which can be a nullptr.
192  // No-op if the new pointer value is identical to the current content.
193  inline T* set(const label i, T* ptr);
194 
195  //- Reorder elements.
196  //- Reordering must be unique (ie, shuffle).
197  // Optionally verify that all pointers have been set.
198  void reorder(const labelUList& oldToNew, const bool testNull = true);
199 
200  //- Reorder elements according to new order mapping (newToOld).
201  //- Reordering must be unique (ie, shuffle).
202  // Optionally verify that all pointers have been set.
203  void sortOrder(const labelUList& order, const bool testNull = true);
204 
205 
206  // Member Operators
207 
208  //- Return const reference to the element
209  inline const T& operator[](const label i) const;
210 
211  //- Return reference to the element
212  inline T& operator[](const label i);
213 
214  //- Return const pointer to the element
215  inline const T* operator()(const label i) const;
216 
217  //- Copy assignment (shallow copies addresses)
218  inline void operator=(const UPtrList<T>& list);
219 
220  //- Move assignment
221  inline void operator=(UPtrList<T>&& list);
222 
223 
224  // Iterators
225 
226  //- Random-access iterator with non-const access
227  class iterator
228  {
229  T** ptr_;
230 
231  public:
232 
233  using iterator_category = std::random_access_iterator_tag;
234  using value_type = T;
236  using pointer = T*;
237  using reference = T&;
238  friend class const_iterator;
239 
240  //- Construct for a given entry
241  inline iterator(T** ptr);
242 
243  // Member functions
244 
245  //- Return pointer, can be nullptr.
246  inline pointer get() const;
247 
248  // Member operators
249 
250  inline bool operator==(const iterator& iter) const;
251  inline bool operator!=(const iterator& iter) const;
252 
253  inline pointer operator->() const;
254  inline reference operator*() const;
255 
256  inline reference operator()() const;
257 
258  // Forward iteration
259  inline iterator& operator++();
260  inline iterator operator++(int);
261 
262  inline iterator& operator--();
263  inline iterator operator--(int);
264 
265  // Random-access
266  inline iterator& operator+=(difference_type n);
267  inline iterator& operator-=(difference_type n);
268  inline iterator operator+(difference_type n) const;
269  inline iterator operator-(difference_type n) const;
270 
271  inline difference_type operator-(const iterator& iter) const;
272 
273  inline reference operator[](difference_type n) const;
274 
275  inline bool operator<(const iterator& iter) const;
276  inline bool operator>(const iterator& iter) const;
277 
278  inline bool operator<=(const iterator& iter) const;
279  inline bool operator>=(const iterator& iter) const;
280  };
281 
282 
283  //- Random-access iterator with const access
284  class const_iterator
285  {
286  const T* const* ptr_;
287 
288  public:
289 
290  using iterator_category = std::random_access_iterator_tag;
291  using value_type = const T;
293  using pointer = const T*;
294  using reference = const T&;
295 
296  //- Construct for a given entry
297  inline const_iterator(const T* const* ptr);
298 
299  //- Copy construct from non-const iterator
300  inline const_iterator(const iterator& iter);
301 
302  // Member functions
303 
304  //- Return pointer, can be nullptr.
305  inline pointer get() const;
306 
307  // Member operators
308 
309  inline bool operator==(const const_iterator& iter) const;
310  inline bool operator!=(const const_iterator& iter) const;
311 
312  inline pointer operator->() const;
313  inline reference operator*() const;
314 
315  inline reference operator()() const;
316 
317  // Forward iteration
318  inline const_iterator& operator++();
319  inline const_iterator operator++(int);
320 
321  inline const_iterator& operator--();
322  inline const_iterator operator--(int);
323 
324  // Random-access
329 
330  inline difference_type operator-(const const_iterator& iter) const;
331 
332  inline reference operator[](difference_type n) const;
333 
334  inline bool operator<(const const_iterator& iter) const;
335  inline bool operator>(const const_iterator& iter) const;
336 
337  inline bool operator<=(const const_iterator& iter) const;
338  inline bool operator>=(const const_iterator& iter) const;
339  };
340 
341 
342  //- Return an iterator to begin traversing the UPtrList
343  inline iterator begin();
344 
345  //- Return an iterator to end traversing the UPtrList
346  inline iterator end();
347 
348  //- Return an const_iterator to begin traversing the UPtrList
349  inline const_iterator cbegin() const;
350 
351  //- Return an const_iterator to end traversing the UPtrList
352  inline const_iterator cend() const;
353 
354  //- Return an const_iterator to begin traversing the UPtrList
355  inline const_iterator begin() const;
356 
357  //- Return an const_iterator to end traversing the UPtrList
358  inline const_iterator end() const;
359 
360 
361  // IOstream operator
362 
363  //- Write UPtrList to Ostream
364  friend Ostream& operator<< <T>
365  (
366  Ostream& os,
367  const UPtrList<T>& list
368  );
369 
370 };
371 
372 
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 
375 } // End namespace Foam
376 
377 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
378 
379 #include "UPtrListI.H"
380 
381 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
382 
383 #ifdef NoRepository
384  #include "UPtrList.C"
385 #endif
386 
387 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
388 
389 #endif
390 
391 // ************************************************************************* //
Foam::UPtrList::const_iterator::operator-
const_iterator operator-(difference_type n) const
Definition: UPtrListI.H:534
Foam::UPtrList::const_iterator::operator==
bool operator==(const const_iterator &iter) const
Definition: UPtrListI.H:429
Foam::UPtrList::iterator::value_type
T value_type
Definition: UPtrList.H:233
Foam::UPtrList::size
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:90
Foam::UPtrList::first
T & first()
Return reference to the first element of the list.
Definition: UPtrListI.H:125
Foam::UPtrList::const_iterator::operator<
bool operator<(const const_iterator &iter) const
Definition: UPtrListI.H:558
Foam::UPtrList::iterator::difference_type
label difference_type
Definition: UPtrList.H:234
Foam::UPtrList::begin
iterator begin()
Return an iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:600
Foam::UPtrList::clear
void clear()
Set list size to zero.
Definition: UPtrListI.H:104
Foam::UPtrList::swap
void swap(UPtrList< T > &list)
Swap content.
Definition: UPtrListI.H:111
Foam::UPtrList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: UPtrListI.H:167
Foam::UPtrList::const_iterator::operator+=
const_iterator & operator+=(difference_type n)
Definition: UPtrListI.H:508
Foam::UPtrList::const_iterator::operator++
const_iterator & operator++()
Definition: UPtrListI.H:470
Foam::UPtrList::ptrs_
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:80
Foam::operator-
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Foam::UPtrList::reference
T & reference
A non-const reference to the value_type.
Definition: UPtrList.H:97
Foam::UPtrList::const_iterator::pointer
const T * pointer
Definition: UPtrList.H:292
Foam::UPtrList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr.
Definition: UPtrListI.H:176
Foam::UPtrList::iterator::pointer
T * pointer
Definition: UPtrList.H:235
Foam::UPtrList::const_iterator::value_type
const T value_type
Definition: UPtrList.H:290
Foam::UPtrList::const_iterator::operator()
reference operator()() const
Definition: UPtrListI.H:462
Foam::UPtrList::const_iterator::operator*
reference operator*() const
Definition: UPtrListI.H:455
Foam::UPtrList::const_iterator
Random-access iterator with const access.
Definition: UPtrList.H:283
Foam::UPtrList::const_iterator::operator>
bool operator>(const const_iterator &iter) const
Definition: UPtrListI.H:568
Foam::UPtrList::transfer
void transfer(UPtrList< T > &list)
Transfer contents into this list and annul the argument.
Definition: UPtrListI.H:118
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:235
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::UPtrList::resize
void resize(const label newLen)
Reset size of list.
Definition: UPtrListI.H:153
Foam::UPtrList::value_type
T value_type
Type of values the list contains.
Definition: UPtrList.H:94
Foam::UPtrList::const_iterator::operator>=
bool operator>=(const const_iterator &iter) const
Definition: UPtrListI.H:588
Foam::label
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:62
Foam::UPtrList::iterator::reference
T & reference
Definition: UPtrList.H:236
UPtrListI.H
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::UPtrList::const_iterator::operator!=
bool operator!=(const const_iterator &iter) const
Definition: UPtrListI.H:439
Foam::UPtrList::iterator
Random-access iterator with non-const access.
Definition: UPtrList.H:226
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:63
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:65
Foam::UPtrList::const_iterator::get
pointer get() const
Return pointer, can be nullptr.
Definition: UPtrListI.H:421
Foam::UPtrList::operator
friend Ostream & operator(Ostream &os, const UPtrList< T > &list)
Write UPtrList to Ostream.
Foam::UPtrList::UPtrList
constexpr UPtrList() noexcept
Construct null.
Definition: UPtrListI.H:32
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:489
PtrListDetail.H
Foam::UPtrList::iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: UPtrList.H:232
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::UPtrList::const_iterator::reference
const T & reference
Definition: UPtrList.H:293
Foam::UPtrList::const_iterator::operator<=
bool operator<=(const const_iterator &iter) const
Definition: UPtrListI.H:578
Foam::UPtrList::const_iterator::operator+
const_iterator operator+(difference_type n) const
Definition: UPtrListI.H:526
Foam::UPtrList::cend
const_iterator cend() const
Return an const_iterator to end traversing the UPtrList.
Definition: UPtrListI.H:624
Foam::operator<
bool operator<(const Instant< T1 > &a, const Instant< T2 > &b)
Compare instant values for less-than.
Definition: Instant.C:90
Foam::UPtrList::end
iterator end()
Return an iterator to end traversing the UPtrList.
Definition: UPtrListI.H:608
Foam::UPtrList::const_iterator::operator[]
reference operator[](difference_type n) const
Definition: UPtrListI.H:550
Foam::UPtrList::last
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:139
Foam::operator+
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::UPtrList::squeezeNull
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers.
Definition: UPtrList.C:45
Foam::operator*
tmp< faMatrix< Type > > operator*(const areaScalarField &, const faMatrix< Type > &)
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:517
Foam::operator>
bool operator>(const Instant< T1 > &a, const Instant< T2 > &b)
Compare instant values for greater-than.
Definition: Instant.C:97
Foam::UPtrList::operator[]
const T & operator[](const label i) const
Return const reference to the element.
Definition: UPtrListI.H:198
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:291
Foam::UPtrList::const_iterator::operator->
pointer operator->() const
Definition: UPtrListI.H:448
Foam::UPtrList::const_iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: UPtrList.H:289
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.
Definition: UPtrListI.H:232
Foam::UPtrList::const_reference
const typedef T & const_reference
A const reference to the value_type.
Definition: UPtrList.H:100
Foam::operator<=
bool operator<=(const Pair< T > &a, const Pair< T > &b)
Definition: Pair.H:249
Foam::operator>=
bool operator>=(const Pair< T > &a, const Pair< T > &b)
Definition: Pair.H:263
Foam::UPtrList::cbegin
const_iterator cbegin() const
Return an const_iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:616
Foam::UPtrList::empty
bool empty() const noexcept
True if the list is empty (ie, size() is zero)
Definition: UPtrListI.H:97
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
UPtrList.C
Foam::UPtrList::const_iterator::const_iterator
const_iterator(const T *const *ptr)
Construct for a given entry.
Definition: UPtrListI.H:407
Foam::UPtrList::setSize
void setSize(const label newLen)
Same as resize()
Definition: UPtrListI.H:160