UListI.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) 2015-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 #include "error.H"
30 #include "pTraits.H"
31 #include "Swap.H"
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
35 template<class T>
36 inline constexpr Foam::UList<T>::UList() noexcept
37 :
38  size_(0),
39  v_(nullptr)
40 {}
41 
42 
43 template<class T>
44 inline Foam::UList<T>::UList(T* __restrict__ v, label size) noexcept
45 :
46  size_(size),
47  v_(v)
48 {}
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
53 template<class T>
55 {
56  return NullObjectRef<UList<T>>();
57 }
58 
59 
60 template<class T>
61 inline Foam::label Foam::UList<T>::fcIndex(const label i) const
62 {
63  return (i == size()-1 ? 0 : i+1);
64 }
65 
66 
67 template<class T>
68 inline const T& Foam::UList<T>::fcValue(const label i) const
69 {
70  return this->operator[](this->fcIndex(i));
71 }
72 
73 
74 template<class T>
75 inline T& Foam::UList<T>::fcValue(const label i)
76 {
77  return this->operator[](this->fcIndex(i));
78 }
79 
80 
81 template<class T>
82 inline Foam::label Foam::UList<T>::rcIndex(const label i) const
83 {
84  return (i ? i-1 : size()-1);
85 }
86 
87 
88 template<class T>
89 inline const T& Foam::UList<T>::rcValue(const label i) const
90 {
91  return this->operator[](this->rcIndex(i));
92 }
93 
94 
95 template<class T>
96 inline T& Foam::UList<T>::rcValue(const label i)
97 {
98  return this->operator[](this->rcIndex(i));
99 }
100 
101 
102 template<class T>
103 inline void Foam::UList<T>::checkStart(const label start) const
104 {
105  if (start < 0 || (start && start >= size_))
106  {
107  // Note: accept start=0 for zero-sized lists
109  << "start " << start << " out of range [0," << size_ << "]"
110  << abort(FatalError);
111  }
112 }
113 
114 
115 template<class T>
116 inline void Foam::UList<T>::checkSize(const label size) const
117 {
118  if (size < 0 || size > size_)
119  {
121  << "size " << size << " out of range [0," << size_ << "]"
122  << abort(FatalError);
123  }
124 }
125 
126 
127 template<class T>
128 inline void Foam::UList<T>::checkIndex(const label i) const
129 {
130  if (!size_)
131  {
133  << "attempt to access element " << i << " from zero sized list"
134  << abort(FatalError);
135  }
136  else if (i < 0 || i >= size_)
137  {
139  << "index " << i << " out of range [0," << size_ << "]"
140  << abort(FatalError);
141  }
142 }
143 
144 
145 template<class T>
146 inline bool Foam::UList<T>::uniform() const
147 {
148  const label len = size();
149 
150  if (len == 0)
151  {
152  return false;
153  }
154 
155  const T& val = first();
156 
157  for (label i=1; i<len; ++i)
158  {
159  if (val != (*this)[i])
160  {
161  return false;
162  }
163  }
164 
165  return true;
166 }
167 
168 
169 template<class T>
171 {
172  return this->operator[](0);
173 }
174 
175 
176 template<class T>
177 inline const T& Foam::UList<T>::first() const
178 {
179  return this->operator[](0);
180 }
181 
182 
183 template<class T>
185 {
186  return this->operator[](this->size()-1);
187 }
188 
189 
190 template<class T>
191 inline const T& Foam::UList<T>::last() const
192 {
193  return this->operator[](this->size()-1);
194 }
195 
196 
197 template<class T>
198 inline const T* Foam::UList<T>::cdata() const
199 {
200  return v_;
201 }
202 
203 
204 template<class T>
206 {
207  return v_;
208 }
209 
210 
211 template<class T>
212 inline bool Foam::UList<T>::found(const T& val, label pos) const
213 {
214  return (this->find(val, pos) >= 0);
215 }
216 
217 
218 template<class T>
219 inline void Foam::UList<T>::shallowCopy(const UList<T>& list)
220 {
221  size_ = list.size_;
222  v_ = list.v_;
223 }
224 
225 
226 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
227 
228 namespace Foam
229 {
230  // Template specialization for bool
231  template<>
232  inline const bool& Foam::UList<bool>::operator[](const label i) const
233  {
234  // Lazy evaluation - return false for out-of-range
235  if (i >= 0 && i < size_)
236  {
237  return v_[i];
238  }
239 
241  }
242 }
243 
244 
245 template<class T>
246 inline T& Foam::UList<T>::operator[](const label i)
247 {
248  #ifdef FULLDEBUG
249  checkIndex(i);
250  #endif
251  return v_[i];
252 }
253 
254 
255 template<class T>
256 inline const T& Foam::UList<T>::operator[](const label i) const
257 {
258  #ifdef FULLDEBUG
259  checkIndex(i);
260  #endif
261  return v_[i];
262 }
263 
264 
265 template<class T>
267 {
268  return *reinterpret_cast<const List<T>*>(this);
269 }
270 
271 
272 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
273 
274 template<class T>
275 inline typename Foam::UList<T>::iterator
277 {
278  return v_;
279 }
280 
281 template<class T>
282 inline typename Foam::UList<T>::const_iterator
284 {
285  return v_;
286 }
287 
288 template<class T>
289 inline typename Foam::UList<T>::const_iterator
291 {
292  return v_;
293 }
294 
295 template<class T>
296 inline typename Foam::UList<T>::iterator
298 {
299  return (v_ + size_);
300 }
301 
302 template<class T>
303 inline typename Foam::UList<T>::const_iterator
305 {
306  return (v_ + size_);
307 }
308 
309 template<class T>
310 inline typename Foam::UList<T>::const_iterator
312 {
313  return (v_ + size_);
314 }
315 
316 template<class T>
317 inline typename Foam::UList<T>::reverse_iterator
319 {
320  return reverse_iterator(end());
321 }
322 
323 template<class T>
326 {
327  return const_reverse_iterator(end());
328 }
329 
330 template<class T>
333 {
334  return const_reverse_iterator(end());
335 }
336 
337 template<class T>
338 inline typename Foam::UList<T>::reverse_iterator
340 {
341  return reverse_iterator(begin());
342 }
343 
344 template<class T>
347 {
348  return const_reverse_iterator(begin());
349 }
350 
351 template<class T>
354 {
355  return const_reverse_iterator(begin());
356 }
357 
358 
359 template<class T>
360 inline void Foam::UList<T>::size(const label n) noexcept
361 {
362  size_ = n;
363 }
364 
365 
366 template<class T>
367 inline Foam::label Foam::UList<T>::size() const noexcept
368 {
369  return size_;
370 }
371 
372 
373 template<class T>
374 inline bool Foam::UList<T>::empty() const noexcept
375 {
376  return !size_;
377 }
378 
379 
380 template<class T>
381 inline void Foam::UList<T>::swap(UList<T>& list)
382 {
383  if (&list == this)
384  {
385  return; // Self-swap is a no-op
386  }
387 
388  Foam::Swap(size_, list.size_);
389  Foam::Swap(v_, list.v_);
390 }
391 
392 
393 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
394 
395 template<class T>
396 inline void Foam::reverse(UList<T>& list, const label n)
397 {
398  for (label i=0; i<n/2; ++i)
399  {
400  Foam::Swap(list[i], list[n-1-i]);
401  }
402 }
403 
404 
405 template<class T>
406 inline void Foam::reverse(UList<T>& list)
407 {
408  reverse(list, list.size());
409 }
410 
411 
412 template<class T>
413 inline void Foam::Swap(UList<T>& a, UList<T>& b)
414 {
415  a.swap(b);
416 }
417 
418 
419 // ************************************************************************* //
Foam::UList::cbegin
const_iterator cbegin() const
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:290
Foam::reverse
void reverse(UList< T > &list, const label n)
Definition: UListI.H:396
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::UList::cend
const_iterator cend() const
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:311
Foam::UList::first
T & first()
Return the first element of the list.
Definition: UListI.H:170
Foam::UList::UList
constexpr UList() noexcept
Default construct, zero-sized and nullptr.
Definition: UListI.H:36
Foam::UList::data
T * data()
Return a pointer to the first data element.
Definition: UListI.H:205
Foam::UList::checkSize
void checkSize(const label size) const
Check size is within valid range [0,size].
Definition: UListI.H:116
Foam::UList::uniform
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: UListI.H:146
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:913
Foam::UList::rcIndex
label rcIndex(const label i) const
Definition: UListI.H:82
Foam::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:276
Swap.H
Swap arguments as per std::swap, but in Foam namespace.
Foam::UList::checkStart
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: UListI.H:103
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
error.H
Foam::UList::iterator
T * iterator
Random access iterator for traversing a UList.
Definition: UList.H:143
Foam::UList::last
T & last()
Return the last element of the list.
Definition: UListI.H:184
Foam::UList::crend
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:353
Foam::UList::empty
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:374
Foam::FatalError
error FatalError
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
pTraits.H
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::UList::rend
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:339
Foam::UList::fcValue
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: UListI.H:68
Foam::UList::shallowCopy
void shallowCopy(const UList< T > &list)
Copy the pointer held by the given UList.
Definition: UListI.H:219
Foam::UList::swap
void swap(UList< T > &list)
Swap content with another UList of the same type in constant time.
Definition: UListI.H:381
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::UList::fcIndex
label fcIndex(const label i) const
Definition: UListI.H:61
Foam::UList::rbegin
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:318
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:54
Foam::UList::operator[]
T & operator[](const label i)
Return element of UList.
Definition: UListI.H:246
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::UList::size
label size() const noexcept
The number of elements in the UList.
Definition: UListI.H:367
Foam::UList::cdata
const T * cdata() const
Return a const pointer to the first data element.
Definition: UListI.H:198
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::UList::found
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: UListI.H:212
Foam::UList::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access)
Definition: UList.H:158
Foam::UList::null
static const UList< T > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:54
Foam::UList::end
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:297
Foam::UList::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access)
Definition: UList.H:155
Foam::UList::checkIndex
void checkIndex(const label i) const
Check index is within valid range [0,size)
Definition: UListI.H:128
Foam::UList::rcValue
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: UListI.H:89
Foam::UList::crbegin
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:332
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177