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