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-2021 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  // Protected Member Functions
82 
83  //- Adjust addressable size
84  inline void setAddressableSize(const label n) noexcept;
85 
86 
87  // Constructors
88 
89  //- Low-level move construct
90  inline explicit UPtrList(Detail::PtrListDetail<T>&& ptrs);
91 
92 
93 public:
94 
95  // STL type definitions
96 
97  //- Type of values the list contains
98  typedef T value_type;
99 
100  //- A non-const reference to the value_type
101  typedef T& reference;
102 
103  //- A const reference to the value_type
104  typedef const T& const_reference;
105 
106  //- Random-access iterator with non-const access
107  class iterator;
108 
109  //- Random-access iterator with const access
110  class const_iterator;
111 
112 
113  // Constructors
114 
115  //- Default construct
116  inline constexpr UPtrList() noexcept;
117 
118  //- Construct with specified size, each element initialized to nullptr
119  inline explicit UPtrList(const label len);
120 
121  //- Copy construct (shallow copies addresses)
122  inline UPtrList(const UPtrList<T>& list);
123 
124  //- Move construct
125  inline UPtrList(UPtrList<T>&& list);
126 
127  //- Construct as shallow copy or re-use as specified
128  inline UPtrList(UPtrList<T>& list, bool reuse);
129 
130  //- Shallow copy from PtrList.
131  // The argument is non-const to reflect that the UPtrList can change
132  // the values (not the addresses) within the original list.
133  explicit UPtrList(PtrList<T>& list);
134 
135  //- Construct from UList of pointers
136  inline explicit UPtrList(const UList<T*>& list);
137 
138  //- Construct from UList, taking the address of each list element
139  // The argument is non-const to reflect that the UPtrList can change
140  // the values of the original list.
141  inline explicit UPtrList(UList<T>& list);
142 
143 
144  // Member Functions
145 
146  // Access
147 
148  //- The number of elements in the list
149  inline label size() const noexcept;
150 
151  //- True if the list is empty (ie, size() is zero)
152  inline bool empty() const noexcept;
153 
154  //- Return reference to the first element of the list
155  inline T& first();
156 
157  //- Return reference to first element of the list
158  inline const T& first() const;
159 
160  //- Return reference to the last element of the list
161  inline T& last();
162 
163  //- Return reference to the last element of the list
164  inline const T& last() const;
165 
166  //- Return pointer to element (can be nullptr),
167  //- without bounds checking.
168  inline T* get(const label i);
169 
170  //- Return const pointer to element (can be nullptr),
171  //- without bounds checking.
172  inline const T* get(const label i) const;
173 
174  //- Return const pointer to element (can be nullptr),
175  //- without bounds checking - same as get().
176  // The return value can also be tested as a bool.
177  const T* set(const label i) const { return this->get(i); }
178 
179 
180  // Edit
181 
182  //- Set list size to zero.
183  inline void clear();
184 
185  //- Change the size of the list.
186  // New entries are initialized to nullptr.
187  inline void resize(const label newLen);
188 
189  //- Alias for resize()
190  void setSize(const label n) { this->resize(n); }
191 
192  //- Squeeze out intermediate nullptr entries in the list of pointers
193  // \return the number of non-null entries
194  label squeezeNull();
195 
196  //- Append an element to the end of the list
197  inline void append(T* ptr);
198 
199  //- Swap content
200  inline void swap(UPtrList<T>& list);
201 
202  //- Transfer contents into this list and annul the argument
203  inline void transfer(UPtrList<T>& list);
204 
205  //- Set element to specified pointer and return the old list element,
206  //- which can be a nullptr.
207  // No-op if the new pointer value is identical to the current content.
208  inline T* set(const label i, T* ptr);
209 
210  //- Reorder elements.
211  //- Reordering must be unique (ie, shuffle).
212  // Optionally verify that all pointers have been set.
213  void reorder(const labelUList& oldToNew, const bool testNull = true);
214 
215  //- Reorder elements according to new order mapping (newToOld).
216  //- Reordering must be unique (ie, shuffle).
217  // Optionally verify that all pointers have been set.
218  void sortOrder(const labelUList& order, const bool testNull = true);
219 
220 
221  // Member Operators
222 
223  //- Return const reference to the element
224  inline const T& operator[](const label i) const;
225 
226  //- Return reference to the element
227  inline T& operator[](const label i);
228 
229  //- Return const pointer to the element - same as get().
230  inline const T* operator()(const label i) const;
231 
232  //- Copy assignment (shallow copies addresses)
233  inline void operator=(const UPtrList<T>& list);
234 
235  //- Move assignment
236  inline void operator=(UPtrList<T>&& list);
237 
238 
239  // Iterators
240 
241  //- Random-access iterator with non-const access
242  class iterator
243  {
244  // Pointer to parent
245  T** ptr_;
246 
247  public:
248 
249  using iterator_category = std::random_access_iterator_tag;
250  using value_type = T;
251  using difference_type = label;
252  using pointer = T*;
253  using reference = T&;
254  friend class const_iterator;
255 
256  //- Construct for a given entry
257  inline iterator(T** ptr) noexcept;
258 
259  // Member Functions
260 
261  //- Return pointer, can be nullptr.
262  inline pointer get() const;
263 
264  // Member Operators
265 
266  inline pointer operator->() const;
267  inline reference operator*() const;
268 
269  reference operator()() const { return this->operator*(); }
270  inline reference operator[](difference_type n) const;
271 
272  // Forward iteration
273  inline iterator& operator++() noexcept;
274  inline iterator operator++(int) noexcept;
275 
276  inline iterator& operator--() noexcept;
277  inline iterator operator--(int) noexcept;
278 
279  // Random-access
280  inline iterator& operator+=(difference_type n) noexcept;
281  inline iterator& operator-=(difference_type n) noexcept;
282  inline iterator operator+(difference_type n) const noexcept;
283  inline iterator operator-(difference_type n) const noexcept;
284 
285  inline difference_type operator-(const iterator& iter)
286  const noexcept;
287 
288  inline bool operator==(const iterator& iter) const noexcept;
289  inline bool operator!=(const iterator& iter) const noexcept;
290 
291  inline bool operator<(const iterator& iter) const noexcept;
292  inline bool operator>(const iterator& iter) const noexcept;
293 
294  inline bool operator<=(const iterator& iter) const noexcept;
295  inline bool operator>=(const iterator& iter) const noexcept;
296  };
297 
298 
299  //- Random-access iterator with const access
300  class const_iterator
301  {
302  // Pointer to parent
303  const T* const* ptr_;
304 
305  public:
306 
307  using iterator_category = std::random_access_iterator_tag;
308  using value_type = const T;
309  using difference_type = label;
310  using pointer = const T*;
311  using reference = const T&;
312 
313  //- Construct for a given entry
314  inline const_iterator(const T* const* ptr) noexcept;
315 
316  //- Copy construct from non-const iterator
317  inline const_iterator(const iterator& iter) noexcept;
318 
319  // Member Functions
320 
321  //- Return pointer, can be nullptr.
322  inline pointer get() const;
323 
324  // Member Operators
325 
326  inline pointer operator->() const;
327  inline reference operator*() const;
328 
329  reference operator()() const { return this->operator*(); }
330  inline reference operator[](difference_type n) const;
331 
332  // Forward iteration
333  inline const_iterator& operator++() noexcept;
334  inline const_iterator operator++(int) noexcept;
335 
336  inline const_iterator& operator--() noexcept;
337  inline const_iterator operator--(int) noexcept;
338 
339  // Random-access
340  inline const_iterator& operator+=(difference_type n) noexcept;
341  inline const_iterator& operator-=(difference_type n) noexcept;
342  inline const_iterator operator+(difference_type n) const noexcept;
343  inline const_iterator operator-(difference_type n) const noexcept;
344 
345  inline difference_type operator-(const const_iterator& iter)
346  const noexcept;
347 
348  inline bool operator==(const const_iterator& iter) const noexcept;
349  inline bool operator!=(const const_iterator& iter) const noexcept;
350 
351  inline bool operator<(const const_iterator& iter) const noexcept;
352  inline bool operator>(const const_iterator& iter) const noexcept;
353 
354  inline bool operator<=(const const_iterator& iter) const noexcept;
355  inline bool operator>=(const const_iterator& iter) const noexcept;
356  };
357 
358 
359  //- Return an iterator to begin traversing the UPtrList
360  inline iterator begin() noexcept;
361 
362  //- Return an iterator to end traversing the UPtrList
363  inline iterator end() noexcept;
364 
365  //- Return an const_iterator to begin traversing the UPtrList
366  inline const_iterator cbegin() const noexcept;
367 
368  //- Return an const_iterator to end traversing the UPtrList
369  inline const_iterator cend() const noexcept;
370 
371  //- Return an const_iterator to begin traversing the UPtrList
372  inline const_iterator begin() const noexcept;
373 
374  //- Return an const_iterator to end traversing the UPtrList
375  inline const_iterator end() const noexcept;
376 
377 
378  // IOstream operator
379 
380  //- Write UPtrList to Ostream
381  friend Ostream& operator<< <T>
382  (
383  Ostream& os,
384  const UPtrList<T>& list
385  );
386 };
387 
388 
389 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
390 
391 } // End namespace Foam
392 
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394 
395 #include "UPtrListI.H"
396 
397 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
398 
399 #ifdef NoRepository
400  #include "UPtrList.C"
401 #endif
402 
403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404 
405 #endif
406 
407 // ************************************************************************* //
Foam::UPtrList::iterator::value_type
T value_type
Definition: UPtrList.H:249
Foam::UPtrList::size
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:106
Foam::UPtrList::cbegin
const_iterator cbegin() const noexcept
Return an const_iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:616
Foam::UPtrList::first
T & first()
Return reference to the first element of the list.
Definition: UPtrListI.H:155
Foam::UPtrList::iterator::difference_type
label difference_type
Definition: UPtrList.H:250
Foam::UPtrList::clear
void clear()
Set list size to zero.
Definition: UPtrListI.H:134
Foam::UPtrList::iterator::operator()
reference operator()() const
Definition: UPtrList.H:268
Foam::UPtrList::swap
void swap(UPtrList< T > &list)
Swap content.
Definition: UPtrListI.H:141
Foam::UPtrList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: UPtrListI.H:190
Foam::UPtrList::begin
iterator begin() noexcept
Return an iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:600
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:100
Foam::UPtrList::get
T * get(const label i)
Definition: UPtrListI.H:120
Foam::UPtrList::setSize
void setSize(const label n)
Alias for resize()
Definition: UPtrList.H:189
Foam::UPtrList::const_iterator::pointer
const T * pointer
Definition: UPtrList.H:309
Foam::UPtrList::iterator::pointer
T * pointer
Definition: UPtrList.H:251
Foam::UPtrList::iterator::const_iterator
friend class const_iterator
Definition: UPtrList.H:253
Foam::UPtrList::const_iterator::value_type
const T value_type
Definition: UPtrList.H:307
Foam::UPtrList::operator=
void operator=(const UPtrList< T > &list)
Copy assignment (shallow copies addresses)
Definition: UPtrListI.H:649
Foam::UPtrList::const_iterator
Random-access iterator with const access.
Definition: UPtrList.H:299
Foam::UPtrList::iterator::get
pointer get() const
Return pointer, can be nullptr.
Definition: UPtrListI.H:265
Foam::UPtrList::transfer
void transfer(UPtrList< T > &list)
Transfer contents into this list and annul the argument.
Definition: UPtrListI.H:148
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)
Change the size of the list.
Definition: UPtrListI.H:183
Foam::UPtrList::value_type
T value_type
Type of values the list contains.
Definition: UPtrList.H:97
Foam::UPtrList::iterator::reference
T & reference
Definition: UPtrList.H:252
Foam::UPtrList::iterator
Random-access iterator with non-const access.
Definition: UPtrList.H:241
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::UPtrList::const_iterator::operator()
reference operator()() const
Definition: UPtrList.H:328
Foam::UPtrList::setAddressableSize
void setAddressableSize(const label n) noexcept
Adjust addressable size.
Definition: UPtrListI.H:32
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:59
Foam::UPtrList::end
iterator end() noexcept
Return an iterator to end traversing the UPtrList.
Definition: UPtrListI.H:608
Foam::UPtrList::operator
friend Ostream & operator(Ostream &os, const UPtrList< T > &list)
Write UPtrList to Ostream.
Foam::UPtrList::UPtrList
constexpr UPtrList() noexcept
Default construct.
Definition: UPtrListI.H:41
Foam::UPtrList::sortOrder
void sortOrder(const labelUList &order, const bool testNull=true)
Definition: UPtrList.C:129
os
OBJstream os(runTime.globalPath()/outputName)
PtrListDetail.H
Foam::UPtrList::iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: UPtrList.H:248
Foam::UPtrList::iterator::operator*
reference operator*() const
Definition: UPtrListI.H:279
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::UPtrList::const_iterator::reference
const T & reference
Definition: UPtrList.H:310
Foam::UPtrList::iterator::operator->
pointer operator->() const
Definition: UPtrListI.H:272
Foam::UPtrList::iterator::operator++
iterator & operator++() noexcept
Definition: UPtrListI.H:293
Foam::UPtrList::iterator::operator[]
reference operator[](difference_type n) const
Definition: UPtrListI.H:286
Foam::UPtrList::set
const T * set(const label i) const
Definition: UPtrList.H:176
Foam::UPtrList::iterator::iterator
iterator(T **ptr) noexcept
Construct for a given entry.
Definition: UPtrListI.H:258
Foam::UPtrList::last
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:169
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::operator[]
const T & operator[](const label i) const
Return const reference to the element.
Definition: UPtrListI.H:214
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:308
Foam::UPtrList::const_iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: UPtrList.H:306
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:248
Foam::UPtrList::const_reference
const typedef T & const_reference
A const reference to the value_type.
Definition: UPtrList.H:103
Foam::UPtrList::empty
bool empty() const noexcept
True if the list is empty (ie, size() is zero)
Definition: UPtrListI.H:113
UPtrList.C
Foam::UPtrList::cend
const_iterator cend() const noexcept
Return an const_iterator to end traversing the UPtrList.
Definition: UPtrListI.H:624