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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::UPtrList
29
30Description
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
35Note
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
40See Also
41 Foam::PtrList
42 Foam::PtrDynList
43
44SourceFiles
45 UPtrListI.H
46 UPtrList.C
47
48\*---------------------------------------------------------------------------*/
49
50#ifndef Foam_UPtrList_H
51#define Foam_UPtrList_H
52
53#include "PtrListDetail.H"
54#include <iterator>
55
56// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57
58namespace Foam
59{
60
61// Forward Declarations
62template<class T> class PtrList;
63template<class T> class UPtrList;
64template<class T> Ostream& operator<<(Ostream& os, const UPtrList<T>& list);
65
66/*---------------------------------------------------------------------------*\
67 Class UPtrList Declaration
68\*---------------------------------------------------------------------------*/
69
70template<class T>
71class UPtrList
72{
73protected:
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
93public:
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 // Public Classes
114
115 //- A wrapper for a binary comparison of values that interjects
116 //- pointer dereferencing with null pointer guards.
117 // It will also sort any null pointers to the end
118 // (eg, rubbish that can be truncated)
119 template<class Compare>
120 struct value_compare
122 const Compare& comp;
124 value_compare(const Compare& cmp)
125 :
126 comp(cmp)
127 {}
128
129 //- Compare dereferenced pointers
130 bool operator()(const T* const a, const T* const b) const
131 {
132 return (a && b) ? comp(*a, *b) : bool(a);
133 }
134 };
135
136 //- A UPtrList compare binary predicate for normal sort order.
137 //- Null entries (if any) sort to the end.
138 struct less
140 const UPtrList<T>& values;
142 less(const UPtrList<T>& list)
143 :
144 values(list)
145 {}
146
147 //- Compare dereferenced pointer locations for normal sort.
148 bool operator()(const label ai, const label bi) const
149 {
150 const T* const a = values.get(ai);
151 const T* const b = values.get(bi);
152
153 return (a && b) ? (*a < *b) : bool(a);
154 }
155 };
156
157 //- A UPtrList compare binary predicate for reverse sort order.
158 // Null entries (if any) sort to the end.
159 struct greater
161 const UPtrList<T>& values;
163 greater(const UPtrList<T>& list)
164 :
165 values(list)
166 {}
167
168 //- Compare dereferenced pointer locations for reverse sort
169 bool operator()(const label ai, const label bi) const
170 {
171 const T* const a = values.get(ai);
172 const T* const b = values.get(bi);
173
174 return (a && b) ? (*b < *a) : bool(b);
175 }
176 };
177
178
179 // Constructors
180
181 //- Default construct
182 inline constexpr UPtrList() noexcept;
183
184 //- Construct with specified size, each element initialized to nullptr
185 inline explicit UPtrList(const label len);
186
187 //- Copy construct (shallow copies addresses)
188 inline UPtrList(const UPtrList<T>& list);
189
190 //- Move construct
191 inline UPtrList(UPtrList<T>&& list);
192
193 //- Construct as shallow copy or re-use as specified
194 inline UPtrList(UPtrList<T>& list, bool reuse);
195
196 //- Shallow copy from PtrList.
197 // The argument is non-const to reflect that the UPtrList can change
198 // the values (not the addresses) within the original list.
199 explicit UPtrList(PtrList<T>& list);
200
201 //- Construct from UList of pointers (shallow copy)
202 inline explicit UPtrList(const UList<T*>& list);
203
204 //- Construct from UList, taking the address of each list element
205 // The argument is non-const to reflect that the UPtrList can change
206 // the values of the original list.
207 inline explicit UPtrList(UList<T>& list);
208
209
210 // Member Functions
211
212 // Access
213
214 //- The number of elements in the list
215 inline label size() const noexcept;
216
217 //- True if the list is empty (ie, size() is zero)
218 inline bool empty() const noexcept;
219
220 //- Return reference to the first element of the list
221 inline T& first();
222
223 //- Return reference to first element of the list
224 inline const T& first() const;
225
226 //- Return reference to the last element of the list
227 inline T& last();
228
229 //- Return reference to the last element of the list
230 inline const T& last() const;
231
232 //- Return const pointer to element (can be nullptr),
233 //- or nullptr for out-of-range access (ie, \em with bounds checking).
234 // The return value can be tested as a bool.
235 inline const T* test(const label i) const;
236
237 //- Return pointer to element (can be nullptr),
238 //- \em without bounds checking.
239 // The return value can be tested as a bool.
240 inline T* get(const label i);
241
242 //- Return const pointer to element (can be nullptr),
243 //- \em without bounds checking.
244 inline const T* get(const label i) const;
245
246 //- Return const pointer to element (can be nullptr),
247 //- \em without bounds checking - same as get().
248 // The return value can be tested as a bool.
249 const T* set(const label i) const { return this->get(i); }
250
251
252 // Edit
253
254 //- Set list size to zero.
255 inline void clear();
256
257 //- Change the size of the list.
258 // New entries are initialized to nullptr.
259 inline void resize(const label newLen);
260
261 //- Alias for resize()
262 void setSize(const label n) { this->resize(n); }
263
264 //- Squeeze out intermediate nullptr entries in the list of pointers
265 // \return the number of non-null entries
266 label squeezeNull();
267
268 //- Append an element to the end of the list
269 inline void append(T* ptr);
270
271 //- Move append another list to the end of this list.
272 inline void append(UPtrList<T>&& other);
273
274 //- Swap content
275 inline void swap(UPtrList<T>& list);
276
277 //- Transfer contents into this list and annul the argument
278 inline void transfer(UPtrList<T>& list);
279
280 //- Set element to specified pointer and return the old list element,
281 //- which can be a nullptr.
282 // No-op if the new pointer value is identical to the current content.
283 inline T* set(const label i, T* ptr);
284
285 //- Reorder elements.
286 //- Reordering must be unique (ie, shuffle).
287 // Optionally check that all pointers have been set.
288 void reorder(const labelUList& oldToNew, const bool check = false);
289
290 //- Reorder elements according to new order mapping (newToOld).
291 //- Reordering must be unique (ie, shuffle).
292 // Optionally check that all pointers have been set.
293 void sortOrder(const labelUList& order, const bool check = false);
294
295
296 // Checks
297
298 //- Check and raise FatalError if any nullptr exists in the list
299 inline void checkNonNull() const;
300
301
302 // Member Operators
303
304 //- Return const reference to the element
305 inline const T& operator[](const label i) const;
306
307 //- Return reference to the element
308 inline T& operator[](const label i);
309
310 //- Return const pointer to the element - same as get().
311 inline const T* operator()(const label i) const;
312
313 //- Copy assignment (shallow copies addresses)
314 inline void operator=(const UPtrList<T>& list);
315
316 //- Move assignment
317 inline void operator=(UPtrList<T>&& list);
318
319
320 // Iterators
321
322 //- Random-access iterator with non-const access
323 class iterator
324 {
325 // Pointer to parent
326 T** ptr_;
327
328 public:
330 using iterator_category = std::random_access_iterator_tag;
331 using value_type = T;
332 using difference_type = label;
333 using pointer = T*;
334 using reference = T&;
335 friend class const_iterator;
336
337 //- Construct for a given entry
338 inline iterator(T** ptr) noexcept;
339
340 // Member Functions
341
342 //- Return pointer, can be nullptr.
343 inline pointer get() const;
344
345 // Member Operators
346
347 inline pointer operator->() const;
348 inline reference operator*() const;
350 reference operator()() const { return this->operator*(); }
351 inline reference operator[](difference_type n) const;
352
353 // Forward iteration
354 inline iterator& operator++() noexcept;
355 inline iterator operator++(int) noexcept;
356
357 inline iterator& operator--() noexcept;
358 inline iterator operator--(int) noexcept;
359
360 // Random-access
365
366 inline difference_type operator-(const iterator& iter)
367 const noexcept;
368
369 inline bool operator==(const iterator& iter) const noexcept;
370 inline bool operator!=(const iterator& iter) const noexcept;
371
372 inline bool operator<(const iterator& iter) const noexcept;
373 inline bool operator>(const iterator& iter) const noexcept;
374
375 inline bool operator<=(const iterator& iter) const noexcept;
376 inline bool operator>=(const iterator& iter) const noexcept;
377 };
378
379
380 //- Random-access iterator with const access
381 class const_iterator
382 {
383 // Pointer to parent
384 const T* const* ptr_;
385
386 public:
388 using iterator_category = std::random_access_iterator_tag;
389 using value_type = const T;
390 using difference_type = label;
391 using pointer = const T*;
392 using reference = const T&;
393
394 //- Construct for a given entry
395 inline const_iterator(const T* const* ptr) noexcept;
396
397 //- Copy construct from non-const iterator
398 inline const_iterator(const iterator& iter) noexcept;
399
400 // Member Functions
401
402 //- Return pointer, can be nullptr.
403 inline pointer get() const;
404
405 // Member Operators
406
407 inline pointer operator->() const;
408 inline reference operator*() const;
410 reference operator()() const { return this->operator*(); }
411 inline reference operator[](difference_type n) const;
412
413 // Forward iteration
415 inline const_iterator operator++(int) noexcept;
416
417 inline const_iterator& operator--() noexcept;
418 inline const_iterator operator--(int) noexcept;
419
420 // Random-access
425
426 inline difference_type operator-(const const_iterator& iter)
427 const noexcept;
428
429 inline bool operator==(const const_iterator& iter) const noexcept;
430 inline bool operator!=(const const_iterator& iter) const noexcept;
431
432 inline bool operator<(const const_iterator& iter) const noexcept;
433 inline bool operator>(const const_iterator& iter) const noexcept;
434
435 inline bool operator<=(const const_iterator& iter) const noexcept;
436 inline bool operator>=(const const_iterator& iter) const noexcept;
437 };
438
439
440 //- Iterator to begin of raw pointers traversal (use with caution)
441 T** begin_ptr() noexcept { return ptrs_.begin(); }
442
443 //- Iterator beyond end of raw pointers traversal (use with caution)
444 T** end_ptr() noexcept { return ptrs_.end(); }
445
446
447 //- Return an iterator to begin of UPtrList traversal
448 inline iterator begin() noexcept;
449
450 //- Return iterator beyond end of UPtrList traversal
451 inline iterator end() noexcept;
452
453 //- Return const_iterator to begin of UPtrList traversal
454 inline const_iterator cbegin() const noexcept;
455
456 //- Return const_iterator beyond end of UPtrList traversal
457 inline const_iterator cend() const noexcept;
458
459 //- Return const_iterator to begin of UPtrList traversal
460 inline const_iterator begin() const noexcept;
461
462 //- Return const_iterator beyond end of UPtrList traversal
463 inline const_iterator end() const noexcept;
464
465
466 // IOstream operator
467
468 //- Write UPtrList to Ostream
469 friend Ostream& operator<< <T>
470 (
471 Ostream& os,
472 const UPtrList<T>& list
473 );
474};
475
476
477// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
478
479//- Inplace (stable) sorting of pointer list.
480template<class T>
481void sort(UPtrList<T>& list);
482
483//- Inplace (stable) sorting of pointer list using given comparator,
484//- which compares objects, not pointers.
485// This sort function includes null pointer guards and will also sort
486// any null pointers to the end (eg, rubbish that can be truncated)
487template<class T, class Compare>
488void sort(UPtrList<T>& list, const Compare& comp);
489
490
491// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
492
493} // End namespace Foam
494
495// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
496
497#include "UPtrListI.H"
498
499// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
500
501#ifdef NoRepository
502 #include "UPtrList.C"
503#endif
504
505// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
506
507#endif
508
509// ************************************************************************* //
label n
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
Random-access iterator with const access.
Definition: UPtrList.H:381
reference operator()() const
Definition: UPtrList.H:409
std::random_access_iterator_tag iterator_category
Definition: UPtrList.H:387
Random-access iterator with non-const access.
Definition: UPtrList.H:323
reference operator*() const
Definition: UPtrListI.H:299
reference operator()() const
Definition: UPtrList.H:349
pointer operator->() const
Definition: UPtrListI.H:292
std::random_access_iterator_tag iterator_category
Definition: UPtrList.H:329
friend class const_iterator
Definition: UPtrList.H:334
reference operator[](difference_type n) const
Definition: UPtrListI.H:306
pointer get() const
Return pointer, can be nullptr.
Definition: UPtrListI.H:285
iterator & operator++() noexcept
Definition: UPtrListI.H:313
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
const T & operator[](const label i) const
Return const reference to the element.
Definition: UPtrListI.H:234
const T * set(const label i) const
Definition: UPtrList.H:248
void swap(UPtrList< T > &list)
Swap content.
Definition: UPtrListI.H:148
void append(T *ptr)
Append an element to the end of the list.
Definition: UPtrListI.H:197
void setSize(const label n)
Alias for resize()
Definition: UPtrList.H:261
T & first()
Return reference to the first element of the list.
Definition: UPtrListI.H:162
T ** begin_ptr() noexcept
Iterator to begin of raw pointers traversal (use with caution)
Definition: UPtrList.H:440
T value_type
Type of values the list contains.
Definition: UPtrList.H:97
void reorder(const labelUList &oldToNew, const bool check=false)
Definition: UPtrList.C:69
bool empty() const noexcept
True if the list is empty (ie, size() is zero)
Definition: UPtrListI.H:113
void operator=(const UPtrList< T > &list)
Copy assignment (shallow copies addresses)
Definition: UPtrListI.H:669
const_iterator cbegin() const noexcept
Return const_iterator to begin of UPtrList traversal.
Definition: UPtrListI.H:636
constexpr UPtrList() noexcept
Default construct.
Definition: UPtrListI.H:41
const T * test(const label i) const
Definition: UPtrListI.H:120
void sortOrder(const labelUList &order, const bool check=false)
Definition: UPtrList.C:118
T ** end_ptr() noexcept
Iterator beyond end of raw pointers traversal (use with caution)
Definition: UPtrList.H:443
const_iterator cend() const noexcept
Return const_iterator beyond end of UPtrList traversal.
Definition: UPtrListI.H:644
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:106
T * get(const label i)
Definition: UPtrListI.H:127
T & reference
A non-const reference to the value_type.
Definition: UPtrList.H:100
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers.
Definition: UPtrList.C:45
void transfer(UPtrList< T > &list)
Transfer contents into this list and annul the argument.
Definition: UPtrListI.H:155
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:77
void clear()
Set list size to zero.
Definition: UPtrListI.H:141
void checkNonNull() const
Check and raise FatalError if any nullptr exists in the list.
Definition: UPtrListI.H:225
iterator begin() noexcept
Return an iterator to begin of UPtrList traversal.
Definition: UPtrListI.H:620
iterator end() noexcept
Return iterator beyond end of UPtrList traversal.
Definition: UPtrListI.H:628
void setAddressableSize(const label n) noexcept
Adjust addressable size.
Definition: UPtrListI.H:32
const T * operator()(const label i) const
Return const pointer to the element - same as get().
Definition: UPtrListI.H:268
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:176
friend Ostream & operator(Ostream &os, const UPtrList< T > &list)
Write UPtrList to Ostream.
void resize(const label newLen)
Change the size of the list.
Definition: UPtrListI.H:190
const T & const_reference
A const reference to the value_type.
Definition: UPtrList.H:103
const volScalarField & T
bool
Definition: EEqn.H:20
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
static void check(const int retVal, const char *what)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:342
const direction noexcept
Definition: Scalar.H:223
volScalarField & b
Definition: createFields.H:27
A UPtrList compare binary predicate for reverse sort order.
Definition: UPtrList.H:159
bool operator()(const label ai, const label bi) const
Compare dereferenced pointer locations for reverse sort.
Definition: UPtrList.H:168
greater(const UPtrList< T > &list)
Definition: UPtrList.H:162
const UPtrList< T > & values
Definition: UPtrList.H:160
less(const UPtrList< T > &list)
Definition: UPtrList.H:141
bool operator()(const label ai, const label bi) const
Compare dereferenced pointer locations for normal sort.
Definition: UPtrList.H:147
const UPtrList< T > & values
Definition: UPtrList.H:139
value_compare(const Compare &cmp)
Definition: UPtrList.H:123
bool operator()(const T *const a, const T *const b) const
Compare dereferenced pointers.
Definition: UPtrList.H:129