Loading...
Searching...
No Matches
UPtrListI.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-2024 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
27\*---------------------------------------------------------------------------*/
28
29// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
30
31template<class T>
32inline void Foam::UPtrList<T>::setAddressableSize(const label n) noexcept
33{
34 ptrs_.setAddressableSize(n);
35}
36
37
38template<class T>
39inline Foam::label Foam::UPtrList<T>::find_next(label pos) const
40{
41 return ptrs_.find_next(pos);
42}
43
44
45// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
46
47template<class T>
48inline Foam::UPtrList<T>::UPtrList(const label len)
49:
50 ptrs_(len)
51{}
52
53
54template<class T>
56:
57 ptrs_(std::move(ptrs))
58{}
59
60
61template<class T>
63:
64 ptrs_(list.ptrs_)
65{}
66
67
68template<class T>
70:
71 ptrs_(std::move(list.ptrs_))
72{}
73
74
75template<class T>
77:
78 ptrs_(list.ptrs_, reuse)
79{}
80
81
82template<class T>
84:
85 ptrs_(list)
86{}
87
88
89template<class T>
91:
92 ptrs_(list.size())
93{
94 const label len = ptrs_.size();
95
96 for (label i = 0; i < len; ++i)
97 {
98 ptrs_[i] = &(list[i]);
99 }
100}
101
102
103// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
104
105template<class T>
107{
108 return ptrs_.empty();
109}
110
111
112template<class T>
113inline Foam::label Foam::UPtrList<T>::size() const noexcept
114{
115 return ptrs_.size();
116}
118
119template<class T>
120inline Foam::label Foam::UPtrList<T>::capacity() const noexcept
121{
122 return ptrs_.capacity();
123}
124
125
126template<class T>
128{
129 return ptrs_.count_nonnull();
131
132
133template<class T>
134inline const T* Foam::UPtrList<T>::test(const label i) const
135{
136 return ptrs_.get(i);
137}
138
139
140template<class T>
141inline const T* Foam::UPtrList<T>::get(const label i) const
142{
143 return ptrs_.get(i);
144}
145
146
147template<class T>
148inline T* Foam::UPtrList<T>::get(const label i)
149{
150 return ptrs_.get(i);
151}
152
153
154template<class T>
155inline const T& Foam::UPtrList<T>::at(const label i) const
156{
157 const T* ptr = ptrs_.get(i);
158
159 if (!ptr)
160 {
162 << "Cannot dereference nullptr at index " << i
163 << " in range [0," << size() << ")\n"
164 << abort(FatalError);
166
167 return *ptr;
168}
169
170
171template<class T>
172inline T& Foam::UPtrList<T>::at(const label i)
173{
174 T* ptr = ptrs_.get(i);
175
176 if (!ptr)
177 {
179 << "Cannot dereference nullptr at index " << i
180 << " in range [0," << size() << ")\n"
181 << abort(FatalError);
183
184 return *ptr;
185}
186
187
188template<class T>
189inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
190{
191 T* old = ptrs_[i];
192 if (old == ptr)
193 {
194 return nullptr; // Content did not change
196 ptrs_[i] = ptr;
197 return old;
198}
199
200
201template<class T>
203{
204 ptrs_.clear();
205}
206
207
208template<class T>
210{
211 ptrs_ = nullptr;
212}
213
214
215template<class T>
216inline void Foam::UPtrList<T>::swap(UPtrList<T>& list) noexcept
217{
218 ptrs_.swap(list.ptrs_);
219}
220
221
222template<class T>
224{
225 ptrs_.transfer(list.ptrs_);
226}
227
228
229template<class T>
231{
232 return this->at(0);
233}
234
235
236template<class T>
237inline const T& Foam::UPtrList<T>::front() const
238{
239 return this->at(0);
240}
241
242
243template<class T>
245{
246 return this->at(this->size()-1);
247}
248
249
250template<class T>
251inline const T& Foam::UPtrList<T>::back() const
252{
253 return this->at(this->size()-1);
254}
255
256
257template<class T>
258inline void Foam::UPtrList<T>::resize(const label newLen)
259{
260 ptrs_.resize(newLen);
261}
262
263
264template<class T>
265inline void Foam::UPtrList<T>::resize_null(const label newLen)
266{
267 ptrs_.resize_null(newLen);
268}
269
270
271template<class T>
273{
274 ptrs_.push_back(ptr);
275}
276
277
278template<class T>
279inline void Foam::UPtrList<T>::push_back(UPtrList<T>&& other)
281 ptrs_.push_back(other.ptrs_);
282 other.ptrs_.clear();
283}
284
285
286template<class T>
287inline void Foam::UPtrList<T>::checkNonNull() const
288{
289 ptrs_.checkNonNull();
290}
291
292
293// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
294
295template<class T>
296inline const T& Foam::UPtrList<T>::operator[](const label i) const
297{
298 return this->at(i);
300
301
302template<class T>
303inline T& Foam::UPtrList<T>::operator[](const label i)
305 return this->at(i);
306}
308
309// * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * //
310
311template<class T>
312template<bool Const>
313inline constexpr
315:
316 list_(nullptr),
317 pos_(-1)
318{}
320
321template<class T>
322template<bool Const>
324(
325 list_type* list
326)
327:
328 list_(list),
329 pos_(-1)
330{
331 if (list_)
332 {
333 pos_ = list_->find_next(-1);
334 }
335}
336
337
338template<class T>
339template<bool Const>
341{
342 //TDB: extra safety? if (iter.good())
343 {
344 pos_ = list_->find_next(pos_);
346}
347
348
349// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
351template<class T>
353{
354 return this->good() ? this->list_->get(this->pos_) : nullptr;
355}
356
357
358template<class T>
361 return this->list_->at(this->pos_);
362}
363
364
365template<class T>
366inline typename Foam::UPtrList<T>::iterator&
368{
369 this->increment();
370 return *this;
371}
372
373
374template<class T>
375inline typename Foam::UPtrList<T>::iterator
377{
378 iterator iter(*this);
379 this->increment();
380 return iter;
381}
382
383
384// * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * //
386template<class T>
388{
389 return this->good() ? this->list_->get(this->pos_) : nullptr;
390}
392
393template<class T>
394inline const T& Foam::UPtrList<T>::const_iterator::val() const
396 return this->list_->at(this->pos_);
397}
398
399
400template<class T>
403{
404 this->increment();
405 return *this;
406}
407
408
409template<class T>
412{
413 const_iterator iter(*this);
414 this->increment();
415 return iter;
417
418
419// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
420
421template<class T>
422inline typename Foam::UPtrList<T>::iterator
425 return iterator(Iterator<false>(this));
426}
427
428
429template<class T>
433 return const_iterator(Iterator<true>(this));
434}
435
436
437template<class T>
441 return const_iterator(Iterator<true>(this));
442}
443
444
445template<class T>
446inline typename Foam::UPtrList<T>::iterator
449 return iterator();
450}
452
453template<class T>
458}
459
460
461template<class T>
464{
466}
467
468
469// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
470
471template<class T>
473{
474 ptrs_ = list.ptrs_; // shallow copy
475}
476
477
478template<class T>
480{
481 ptrs_.transfer(list.ptrs_);
482}
483
484
485// ************************************************************************* //
label n
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
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:89
Internally used base for iterator and const_iterator.
Definition UPtrList.H:548
label pos_
The position within the list.
Definition UPtrList.H:574
bool good() const noexcept
Definition UPtrList.H:617
std::conditional_t< Const, const UPtrList< T >, UPtrList< T > > list_type
The list container type.
Definition UPtrList.H:556
constexpr Iterator() noexcept
Default construct. Also the same as the end iterator.
Definition UPtrListI.H:307
list_type * list_
The parent being iterated.
Definition UPtrList.H:569
void increment()
Increment to the next non-null position.
Definition UPtrListI.H:333
Forward iterator with const access.
Definition UPtrList.H:714
const_iterator()=default
Default construct (end iterator).
const_iterator & operator++()
Move to the next non-nullptr entry.
Definition UPtrListI.H:395
reference val() const
Reference to the object.
Definition UPtrListI.H:387
pointer get() const
Pointer to the referenced object (failsafe).
Definition UPtrListI.H:380
Forward iterator with non-const access.
Definition UPtrList.H:651
reference val() const
Reference to the object.
Definition UPtrListI.H:352
iterator & operator++()
Move to the next non-nullptr entry.
Definition UPtrListI.H:360
constexpr iterator() noexcept=default
Default construct - an end iterator.
pointer get() const
Pointer to the referenced object (failsafe).
Definition UPtrListI.H:345
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
const T & operator[](const label i) const
Return const reference to the element at given position. FatalError for bounds problem or nullptr....
Definition UPtrListI.H:289
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie,...
Definition UPtrList.H:366
iterator begin()
Return iterator to begin traversal of non-nullptr entries.
Definition UPtrListI.H:416
const T & at(const label i) const
Return const reference to the element at given position. FatalError for bounds problem or nullptr.
Definition UPtrListI.H:148
void push_back(T *ptr)
Append an element to the end of the list.
Definition UPtrListI.H:265
bool empty() const noexcept
True if the list is empty (ie, size() is zero).
Definition UPtrListI.H:99
void operator=(const UPtrList< T > &list)
Copy assignment (shallow copies addresses).
Definition UPtrListI.H:465
T & back()
Reference to the last element of the list.
Definition UPtrListI.H:237
const T * test(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie,...
Definition UPtrListI.H:127
UPtrList(Detail::PtrListDetail< T > &&ptrs) noexcept
Low-level move construct.
Definition UPtrListI.H:48
const_iterator cend() const noexcept
Return const_iterator beyond end of UPtrList traversal.
Definition UPtrListI.H:456
label capacity() const noexcept
Size of the underlying storage.
Definition UPtrListI.H:113
const T * get(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie,...
Definition UPtrListI.H:134
label size() const noexcept
The number of entries in the list.
Definition UPtrListI.H:106
const_iterator cbegin() const
Return const_iterator to begin traversal of non-nullptr entries.
Definition UPtrListI.H:432
constexpr UPtrList() noexcept=default
Default construct.
T & front()
Reference to the first element of the list.
Definition UPtrListI.H:223
void free()
Nullify all entries. Does not change the list size.
Definition UPtrListI.H:202
label count_nonnull() const noexcept
The number of non-nullptr entries in the list.
Definition UPtrListI.H:120
void resize_null(const label newLen)
Set the list to the given size and set all entries to nullptr.
Definition UPtrListI.H:258
void transfer(UPtrList< T > &list)
Transfer contents into this list and annul the argument.
Definition UPtrListI.H:216
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition UPtrList.H:109
void clear()
Set list size to zero.
Definition UPtrListI.H:195
void checkNonNull() const
Check and raise FatalError if any nullptr exists in the list.
Definition UPtrListI.H:280
iterator end() noexcept
Return iterator beyond end of UPtrList traversal.
Definition UPtrListI.H:440
label find_next(label pos) const
The next non-null entry after the specified position.
Definition UPtrListI.H:32
void setAddressableSize(const label n) noexcept
Adjust addressable size.
Definition UPtrListI.H:25
void resize(const label newLen)
Change the size of the list. Any new entries are nullptr.
Definition UPtrListI.H:251
void swap(UPtrList< T > &list) noexcept
Swap content.
Definition UPtrListI.H:209
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
dimensionedScalar pos(const dimensionedScalar &ds)
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)