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-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 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
30 
31 template<class T>
32 inline void Foam::UPtrList<T>::setAddressableSize(const label n) noexcept
33 {
34  ptrs_.setAddressableSize(n);
35 }
36 
37 
38 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
39 
40 template<class T>
41 inline constexpr Foam::UPtrList<T>::UPtrList() noexcept
42 :
43  ptrs_()
44 {}
45 
46 
47 template<class T>
48 inline Foam::UPtrList<T>::UPtrList(const label len)
49 :
50  ptrs_(len)
51 {}
52 
53 
54 template<class T>
56 :
57  ptrs_(std::move(ptrs))
58 {}
59 
60 
61 template<class T>
63 :
64  ptrs_(list.ptrs_)
65 {}
66 
67 
68 template<class T>
70 :
71  ptrs_(std::move(list.ptrs_))
72 {}
73 
74 
75 template<class T>
76 inline Foam::UPtrList<T>::UPtrList(UPtrList<T>& list, bool reuse)
77 :
78  ptrs_(list.ptrs_, reuse)
79 {}
80 
81 
82 template<class T>
84 :
85  ptrs_(list)
86 {}
87 
88 
89 template<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 
105 template<class T>
106 inline Foam::label Foam::UPtrList<T>::size() const noexcept
107 {
108  return ptrs_.size();
109 }
110 
111 
112 template<class T>
113 inline bool Foam::UPtrList<T>::empty() const noexcept
114 {
115  return ptrs_.empty();
116 }
117 
118 
119 template<class T>
120 inline T* Foam::UPtrList<T>::get(const label i)
121 {
122  return ptrs_[i];
123 }
124 
125 
126 template<class T>
127 inline const T* Foam::UPtrList<T>::get(const label i) const
128 {
129  return ptrs_[i];
130 }
131 
132 
133 template<class T>
135 {
136  ptrs_.clear();
137 }
138 
139 
140 template<class T>
142 {
143  ptrs_.swap(list.ptrs_);
144 }
145 
146 
147 template<class T>
149 {
150  ptrs_.transfer(list.ptrs_);
151 }
152 
153 
154 template<class T>
156 {
157  return this->operator[](0);
158 }
159 
160 
161 template<class T>
162 inline const T& Foam::UPtrList<T>::first() const
163 {
164  return this->operator[](0);
165 }
166 
167 
168 template<class T>
170 {
171  return this->operator[](this->size()-1);
172 }
173 
174 
175 template<class T>
176 inline const T& Foam::UPtrList<T>::last() const
177 {
178  return this->operator[](this->size()-1);
179 }
180 
181 
182 template<class T>
183 inline void Foam::UPtrList<T>::resize(const label newLen)
184 {
185  ptrs_.resize(newLen);
186 }
187 
188 
189 template<class T>
190 inline void Foam::UPtrList<T>::append(T* ptr)
191 {
192  const label idx = this->size();
193  ptrs_.resize(idx + 1);
194  ptrs_[idx] = ptr;
195 }
196 
197 
198 template<class T>
199 inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
200 {
201  T* old = ptrs_[i];
202  if (old == ptr)
203  {
204  return nullptr; // Content did not change
205  }
206  ptrs_[i] = ptr;
207  return old;
208 }
209 
210 
211 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
212 
213 template<class T>
214 inline const T& Foam::UPtrList<T>::operator[](const label i) const
215 {
216  const T* ptr = ptrs_[i];
217 
218  if (!ptr)
219  {
221  << "Cannot dereference nullptr at index " << i
222  << " in range [0," << size() << ")\n"
223  << abort(FatalError);
224  }
225 
226  return *ptr;
227 }
228 
229 
230 template<class T>
231 inline T& Foam::UPtrList<T>::operator[](const label i)
232 {
233  T* ptr = ptrs_[i];
234 
235  if (!ptr)
236  {
238  << "Cannot dereference nullptr at index " << i
239  << " in range [0," << size() << ")\n"
240  << abort(FatalError);
241  }
242 
243  return *ptr;
244 }
245 
246 
247 template<class T>
248 inline const T* Foam::UPtrList<T>::operator()(const label i) const
249 {
250  return ptrs_[i];
251 }
252 
253 
254 // * * * * * * * * * * * * * * * * iterator * * * * * * * * * * * * * * * * //
255 
256 template<class T>
258 iterator(T** ptr) noexcept
259 :
260  ptr_(ptr)
261 {}
262 
263 
264 template<class T>
266 {
267  return *ptr_;
268 }
269 
270 
271 template<class T>
273 {
274  return *ptr_;
275 }
276 
277 
278 template<class T>
280 {
281  return **ptr_;
282 }
283 
284 
285 template<class T>
286 inline T& Foam::UPtrList<T>::iterator::operator[](label n) const
287 {
288  return **(ptr_ + n);
289 }
290 
291 template<class T>
292 inline typename Foam::UPtrList<T>::iterator&
294 {
295  ++ptr_;
296  return *this;
297 }
298 
299 
300 template<class T>
301 inline typename Foam::UPtrList<T>::iterator
303 {
304  iterator iter(*this);
305  ++ptr_;
306  return iter;
307 }
308 
309 
310 template<class T>
311 inline typename Foam::UPtrList<T>::iterator&
313 {
314  --ptr_;
315  return *this;
316 }
317 
318 
319 template<class T>
320 inline typename Foam::UPtrList<T>::iterator
322 {
323  iterator iter(*this);
324  --ptr_;
325  return iter;
326 }
327 
328 
329 template<class T>
330 inline typename Foam::UPtrList<T>::iterator&
332 {
333  ptr_ += n;
334  return *this;
335 }
336 
337 
338 template<class T>
339 inline typename Foam::UPtrList<T>::iterator&
341 {
342  ptr_ -= n;
343  return *this;
344 }
345 
346 
347 template<class T>
348 inline typename Foam::UPtrList<T>::iterator
349 Foam::UPtrList<T>::iterator::operator+(label n) const noexcept
350 {
351  return iterator(ptr_ + n);
352 }
353 
354 
355 template<class T>
356 inline typename Foam::UPtrList<T>::iterator
357 Foam::UPtrList<T>::iterator::operator-(label n) const noexcept
358 {
359  return iterator(ptr_ - n);
360 }
361 
362 
363 template<class T>
364 inline Foam::label
366 operator-(const iterator& iter) const noexcept
367 {
368  return (ptr_ - iter.ptr_);
369 }
370 
371 
372 template<class T>
374 operator==(const iterator& iter) const noexcept
375 {
376  return ptr_ == iter.ptr_;
377 }
378 
379 
380 template<class T>
382 operator!=(const iterator& iter) const noexcept
383 {
384  return ptr_ != iter.ptr_;
385 }
386 
387 
388 template<class T>
390 operator<(const iterator& iter) const noexcept
391 {
392  return ptr_ < iter.ptr_;
393 }
394 
395 
396 template<class T>
398 operator>(const iterator& iter) const noexcept
399 {
400  return ptr_ > iter.ptr_;
401 }
402 
403 
404 template<class T>
406 operator<=(const iterator& iter) const noexcept
407 {
408  return ptr_ <= iter.ptr_;
409 }
410 
411 
412 template<class T>
414 operator>=(const iterator& iter) const noexcept
415 {
416  return ptr_ >= iter.ptr_;
417 }
418 
419 
420 // * * * * * * * * * * * * * * * const_iterator * * * * * * * * * * * * * * //
421 
422 template<class T>
424 const_iterator(const T* const* ptr) noexcept
425 :
426  ptr_(ptr)
427 {}
428 
429 
430 template<class T>
432 const_iterator(const iterator& iter) noexcept
433 :
434  ptr_(iter.ptr_)
435 {}
436 
437 
438 template<class T>
440 {
441  return *ptr_;
442 }
443 
444 
445 template<class T>
447 {
448  return *ptr_;
449 }
450 
451 
452 template<class T>
454 {
455  return **ptr_;
456 }
457 
458 
459 template<class T>
460 inline const T&
462 {
463  return **(ptr_ + n);
464 }
465 
466 
467 template<class T>
468 inline typename Foam::UPtrList<T>::const_iterator&
470 {
471  ++ptr_;
472  return *this;
473 }
474 
475 
476 template<class T>
477 inline typename Foam::UPtrList<T>::const_iterator
479 {
480  const_iterator iter(*this);
481  ++ptr_;
482  return iter;
483 }
484 
485 
486 template<class T>
487 inline typename Foam::UPtrList<T>::const_iterator&
489 {
490  --ptr_;
491  return *this;
492 }
493 
494 
495 template<class T>
496 inline typename Foam::UPtrList<T>::const_iterator
498 {
499  const_iterator iter(*this);
500  --ptr_;
501  return iter;
502 }
503 
504 
505 template<class T>
506 inline typename Foam::UPtrList<T>::const_iterator&
508 {
509  ptr_ += n;
510  return *this;
511 }
512 
513 
514 template<class T>
515 inline typename Foam::UPtrList<T>::const_iterator&
517 {
518  ptr_ -= n;
519  return *this;
520 }
521 
522 
523 template<class T>
524 inline typename Foam::UPtrList<T>::const_iterator
526 {
527  return const_iterator(ptr_ + n);
528 }
529 
530 
531 template<class T>
532 inline typename Foam::UPtrList<T>::const_iterator
534 {
535  return const_iterator(ptr_ - n);
536 }
537 
538 
539 template<class T>
540 inline Foam::label
542 operator-(const const_iterator& iter) const noexcept
543 {
544  return (ptr_ - iter.ptr_);
545 }
546 
547 
548 template<class T>
550 operator==(const const_iterator& iter) const noexcept
551 {
552  return ptr_ == iter.ptr_;
553 }
554 
555 
556 template<class T>
558 operator!=(const const_iterator& iter) const noexcept
559 {
560  return ptr_ != iter.ptr_;
561 }
562 
563 
564 template<class T>
566 operator<(const const_iterator& iter) const noexcept
567 {
568  return ptr_ < iter.ptr_;
569 }
570 
571 
572 template<class T>
574 operator>(const const_iterator& iter) const noexcept
575 {
576  return ptr_ > iter.ptr_;
577 }
578 
579 
580 template<class T>
582 operator<=(const const_iterator& iter) const noexcept
583 {
584  return ptr_ <= iter.ptr_;
585 }
586 
587 
588 template<class T>
590 operator>=(const const_iterator& iter) const noexcept
591 {
592  return ptr_ >= iter.ptr_;
593 }
594 
595 
596 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
597 
598 template<class T>
599 inline typename Foam::UPtrList<T>::iterator
600 Foam::UPtrList<T>::begin() noexcept
601 {
602  return ptrs_.begin();
603 }
604 
605 
606 template<class T>
607 inline typename Foam::UPtrList<T>::iterator
608 Foam::UPtrList<T>::end() noexcept
609 {
610  return ptrs_.end();
611 }
612 
613 
614 template<class T>
615 inline typename Foam::UPtrList<T>::const_iterator
616 Foam::UPtrList<T>::cbegin() const noexcept
617 {
618  return ptrs_.cbegin();
619 }
620 
621 
622 template<class T>
623 inline typename Foam::UPtrList<T>::const_iterator
624 Foam::UPtrList<T>::cend() const noexcept
625 {
626  return ptrs_.cend();
627 }
628 
629 
630 template<class T>
631 inline typename Foam::UPtrList<T>::const_iterator
632 Foam::UPtrList<T>::begin() const noexcept
633 {
634  return ptrs_.begin();
635 }
636 
637 
638 template<class T>
639 inline typename Foam::UPtrList<T>::const_iterator
640 Foam::UPtrList<T>::end() const noexcept
641 {
642  return ptrs_.end();
643 }
644 
645 
646 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
647 
648 template<class T>
650 {
651  ptrs_ = list.ptrs_; // shallow copy
652 }
653 
654 
655 template<class T>
656 inline void Foam::UPtrList<T>::operator=(UPtrList<T>&& list)
657 {
658  ptrs_.transfer(list.ptrs_);
659 }
660 
661 
662 // ************************************************************************* //
Foam::UPtrList::iterator::operator+=
iterator & operator+=(difference_type n) noexcept
Definition: UPtrListI.H:324
Foam::UPtrList::end
iterator end() noexcept
Foam::UPtrList::size
label size() const noexcept
Definition: UPtrListI.H:99
Foam::UPtrList::cbegin
const_iterator cbegin() const noexcept
Definition: UPtrListI.H:609
Foam::UPtrList::iterator::operator<
bool operator<(const iterator &iter) const noexcept
Definition: UPtrListI.H:383
Foam::UPtrList::first
T & first()
Definition: UPtrListI.H:148
Foam::UPtrList::const_iterator::operator<=
bool operator<=(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:575
Foam::UPtrList::clear
void clear()
Definition: UPtrListI.H:127
Foam::UPtrList::swap
void swap(UPtrList< T > &list)
Definition: UPtrListI.H:134
Foam::UPtrList::append
void append(T *ptr)
Definition: UPtrListI.H:183
Foam::UPtrList::const_iterator::operator>=
bool operator>=(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:583
Foam::UPtrList::ptrs_
Detail::PtrListDetail< T > ptrs_
Definition: UPtrList.H:73
Foam::UPtrList::get
T * get(const label i)
Definition: UPtrListI.H:113
Foam::UPtrList::const_iterator::operator+
const_iterator operator+(difference_type n) const noexcept
Definition: UPtrListI.H:518
Foam::UPtrList::iterator::operator+
iterator operator+(difference_type n) const noexcept
Definition: UPtrListI.H:342
Foam::UPtrList::iterator::operator++
iterator & operator++() noexcept
Foam::UPtrList::const_iterator::operator-
const_iterator operator-(difference_type n) const noexcept
Foam::UPtrList::const_iterator::operator==
bool operator==(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:543
Foam::UPtrList::operator=
void operator=(const UPtrList< T > &list)
Definition: UPtrListI.H:642
Foam::UPtrList::const_iterator::operator*
reference operator*() const
Definition: UPtrListI.H:446
Foam::UPtrList::const_iterator
Definition: UPtrList.H:295
Foam::UPtrList::iterator::operator>=
bool operator>=(const iterator &iter) const noexcept
Definition: UPtrListI.H:407
Foam::UPtrList::iterator::get
pointer get() const
Definition: UPtrListI.H:258
Foam::UPtrList::const_iterator::operator-=
const_iterator & operator-=(difference_type n) noexcept
Definition: UPtrListI.H:509
Foam::UPtrList::transfer
void transfer(UPtrList< T > &list)
Definition: UPtrListI.H:141
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::UPtrList::resize
void resize(const label newLen)
Definition: UPtrListI.H:176
Foam::UPtrList::const_iterator::operator<
bool operator<(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:559
Foam::UPtrList::const_iterator::const_iterator
const_iterator(const T *const *ptr) noexcept
Foam::UPtrList::iterator
Definition: UPtrList.H:237
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:51
Foam::UPtrList::setAddressableSize
void setAddressableSize(const label n) noexcept
Definition: UPtrListI.H:25
Foam::UPtrList::begin
iterator begin() noexcept
Foam::UPtrList< T >
Foam::UPtrList::const_iterator::get
pointer get() const
Definition: UPtrListI.H:432
Foam::UPtrList::UPtrList
constexpr UPtrList() noexcept
Foam::UPtrList::iterator::operator-
iterator operator-(difference_type n) const noexcept
Foam::FatalError
error FatalError
Foam::UPtrList::const_iterator::operator--
const_iterator & operator--() noexcept
Foam::UPtrList::iterator::operator--
iterator & operator--() noexcept
Foam::UPtrList::const_iterator::operator>
bool operator>(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:567
Foam::UPtrList::iterator::operator*
reference operator*() const
Definition: UPtrListI.H:272
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:139
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::UPtrList::iterator::operator!=
bool operator!=(const iterator &iter) const noexcept
Definition: UPtrListI.H:375
Foam::UPtrList::iterator::operator->
pointer operator->() const
Definition: UPtrListI.H:265
Foam::UPtrList::iterator::operator==
bool operator==(const iterator &iter) const noexcept
Definition: UPtrListI.H:367
Foam::UPtrList::const_iterator::operator+=
const_iterator & operator+=(difference_type n) noexcept
Definition: UPtrListI.H:500
Foam::UPtrList::const_iterator::operator!=
bool operator!=(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:551
Foam::UPtrList::iterator::operator-=
iterator & operator-=(difference_type n) noexcept
Definition: UPtrListI.H:333
Foam::UPtrList::const_iterator::operator++
const_iterator & operator++() noexcept
Foam::UPtrList::iterator::operator[]
reference operator[](difference_type n) const
Definition: UPtrListI.H:279
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::UPtrList::set
const T * set(const label i) const
Definition: UPtrList.H:172
Foam::UPtrList::iterator::iterator
iterator(T **ptr) noexcept
Definition: UPtrListI.H:251
Foam::UPtrList::iterator::operator>
bool operator>(const iterator &iter) const noexcept
Definition: UPtrListI.H:391
Foam::UPtrList::const_iterator::operator[]
reference operator[](difference_type n) const
Definition: UPtrListI.H:454
Foam::UPtrList::last
T & last()
Definition: UPtrListI.H:162
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:99
Foam::UPtrList::operator[]
const T & operator[](const label i) const
Definition: UPtrListI.H:207
Foam::Detail::PtrListDetail
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:57
Foam::UPtrList::const_iterator::operator->
pointer operator->() const
Definition: UPtrListI.H:439
Foam::UPtrList::iterator::operator<=
bool operator<=(const iterator &iter) const noexcept
Definition: UPtrListI.H:399
Foam::UPtrList::operator()
const T * operator()(const label i) const
Definition: UPtrListI.H:241
Foam::UPtrList::empty
bool empty() const noexcept
Definition: UPtrListI.H:106
Foam::UPtrList::cend
const_iterator cend() const noexcept
Definition: UPtrListI.H:617