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