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-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 #include "error.H"
30 #include "pTraits.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 template<class T>
35 inline constexpr Foam::UList<T>::UList() noexcept
36 :
37  size_(0),
38  v_(nullptr)
39 {}
40 
41 
42 template<class T>
43 inline Foam::UList<T>::UList(T* __restrict__ v, const label len) noexcept
44 :
45  size_(len),
46  v_(v)
47 {}
48 
49 
50 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
51 
52 template<class T>
54 {
55  return NullObjectRef<UList<T>>();
56 }
57 
58 
59 template<class T>
60 inline Foam::label Foam::UList<T>::fcIndex(const label i) const noexcept
61 {
62  return (i == size()-1 ? 0 : i+1);
63 }
64 
65 
66 template<class T>
67 inline Foam::label Foam::UList<T>::rcIndex(const label i) const noexcept
68 {
69  return (i ? i-1 : size()-1);
70 }
71 
72 
73 template<class T>
74 inline const T& Foam::UList<T>::fcValue(const label i) const
75 {
76  return this->operator[](this->fcIndex(i));
77 }
78 
79 
80 template<class T>
81 inline T& Foam::UList<T>::fcValue(const label i)
82 {
83  return this->operator[](this->fcIndex(i));
84 }
85 
86 
87 template<class T>
88 inline const T& Foam::UList<T>::rcValue(const label i) const
89 {
90  return this->operator[](this->rcIndex(i));
91 }
92 
93 
94 template<class T>
95 inline T& Foam::UList<T>::rcValue(const label i)
96 {
97  return this->operator[](this->rcIndex(i));
98 }
99 
100 
101 template<class T>
102 inline void Foam::UList<T>::checkStart(const label start) const
103 {
104  if (start < 0 || (start && start >= size_))
105  {
106  // Note: accept start=0 for zero-sized lists
108  << "start " << start << " out of range [0,"
109  << size_ << "]\n"
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,"
122  << size_ << "]\n"
123  << abort(FatalError);
124  }
125 }
126 
127 
128 template<class T>
129 inline void Foam::UList<T>::checkRange
130 (
131  const label start,
132  const label len
133 ) const
134 {
135  // Artificially allow the start of a zero-sized subList to be
136  // one past the end of the original list.
137  if (len)
138  {
139  if (len < 0)
140  {
142  << "size " << len << " is negative, out of range [0,"
143  << size_ << "]\n"
144  << abort(FatalError);
145  }
146  this->checkStart(start);
147  this->checkSize(start + len);
148  }
149  else
150  {
151  // Start index needs to fall between 0 and size. One position
152  // behind the last element is allowed
153  this->checkSize(start);
154  }
155 }
156 
157 
158 template<class T>
159 inline void Foam::UList<T>::checkIndex(const label i) const
160 {
161  if (!size_)
162  {
164  << "attempt to access element " << i << " from zero sized list"
165  << abort(FatalError);
166  }
167  else if (i < 0 || i >= size_)
168  {
170  << "index " << i << " out of range [0,"
171  << size_ << "]\n"
172  << abort(FatalError);
173  }
174 }
175 
176 
177 template<class T>
178 inline bool Foam::UList<T>::uniform() const
179 {
180  const label len = size();
181 
182  if (!len)
183  {
184  return false;
185  }
186 
187  const T& val = (*this)[0]; // first
188 
189  for (label i = 1; i < len; ++i)
190  {
191  if (val != (*this)[i])
192  {
193  return false;
194  }
195  }
196 
197  return true;
198 }
199 
200 
201 template<class T>
203 {
204  return this->operator[](0);
205 }
206 
207 
208 template<class T>
209 inline const T& Foam::UList<T>::first() const
210 {
211  return this->operator[](0);
212 }
213 
214 
215 template<class T>
217 {
218  return this->operator[](this->size()-1);
219 }
220 
221 
222 template<class T>
223 inline const T& Foam::UList<T>::last() const
224 {
225  return this->operator[](this->size()-1);
226 }
227 
228 
229 template<class T>
230 inline const T* Foam::UList<T>::cdata() const noexcept
231 {
232  return v_;
233 }
234 
235 
236 template<class T>
237 inline T* Foam::UList<T>::data() noexcept
238 {
239  return v_;
240 }
241 
242 
243 template<class T>
244 inline const char* Foam::UList<T>::cdata_bytes() const noexcept
245 {
246  return reinterpret_cast<const char*>(v_);
247 }
248 
249 
250 template<class T>
251 inline char* Foam::UList<T>::data_bytes() noexcept
252 {
253  return reinterpret_cast<char*>(v_);
254 }
255 
256 
257 template<class T>
258 inline std::streamsize Foam::UList<T>::size_bytes() const noexcept
259 {
260  return std::streamsize(size_)*sizeof(T);
261 }
262 
263 
264 template<class T>
265 inline bool Foam::UList<T>::found(const T& val, label pos) const
266 {
267  return (this->find(val, pos) >= 0);
268 }
269 
270 
271 template<class T>
272 inline void Foam::UList<T>::shallowCopy(const UList<T>& list)
273 {
274  size_ = list.size_;
275  v_ = list.v_;
276 }
277 
278 
279 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
280 
281 namespace Foam
282 {
283  // Template specialization for bool
284  template<>
285  inline const bool& Foam::UList<bool>::operator[](const label i) const
286  {
287  // Lazy evaluation - return false for out-of-range
288  if (i >= 0 && i < size_)
289  {
290  return v_[i];
291  }
292 
294  }
295 }
296 
297 
298 template<class T>
299 inline T& Foam::UList<T>::operator[](const label i)
300 {
301  #ifdef FULLDEBUG
302  checkIndex(i);
303  #endif
304  return v_[i];
305 }
306 
307 
308 template<class T>
309 inline const T& Foam::UList<T>::operator[](const label i) const
310 {
311  #ifdef FULLDEBUG
312  checkIndex(i);
313  #endif
314  return v_[i];
315 }
316 
317 
318 template<class T>
320 {
321  return *reinterpret_cast<const List<T>*>(this);
322 }
323 
324 
325 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
326 
327 template<class T>
328 inline typename Foam::UList<T>::iterator
330 {
331  return v_;
332 }
333 
334 template<class T>
335 inline typename Foam::UList<T>::const_iterator
336 Foam::UList<T>::begin() const noexcept
337 {
338  return v_;
339 }
340 
341 template<class T>
342 inline typename Foam::UList<T>::const_iterator
343 Foam::UList<T>::cbegin() const noexcept
344 {
345  return v_;
346 }
347 
348 template<class T>
349 inline typename Foam::UList<T>::iterator
351 {
352  return (v_ + size_);
353 }
354 
355 template<class T>
356 inline typename Foam::UList<T>::const_iterator
357 Foam::UList<T>::end() const noexcept
358 {
359  return (v_ + size_);
360 }
361 
362 template<class T>
363 inline typename Foam::UList<T>::const_iterator
364 Foam::UList<T>::cend() const noexcept
365 {
366  return (v_ + size_);
367 }
368 
369 template<class T>
370 inline typename Foam::UList<T>::reverse_iterator
372 {
373  return reverse_iterator(end());
374 }
375 
376 template<class T>
379 {
380  return const_reverse_iterator(end());
381 }
382 
383 template<class T>
386 {
387  return const_reverse_iterator(end());
388 }
389 
390 template<class T>
391 inline typename Foam::UList<T>::reverse_iterator
393 {
394  return reverse_iterator(begin());
395 }
396 
397 template<class T>
400 {
401  return const_reverse_iterator(begin());
402 }
403 
404 template<class T>
407 {
408  return const_reverse_iterator(begin());
409 }
410 
411 
412 template<class T>
413 inline void Foam::UList<T>::setAddressableSize(const label n) noexcept
414 {
415  size_ = n;
416 }
417 
418 
419 template<class T>
420 inline Foam::label Foam::UList<T>::size() const noexcept
421 {
422  return size_;
423 }
424 
425 
426 template<class T>
427 inline bool Foam::UList<T>::empty() const noexcept
428 {
429  return !size_;
430 }
431 
432 
433 template<class T>
434 inline void Foam::UList<T>::swap(UList<T>& list)
435 {
436  if (&list == this)
437  {
438  return; // Self-swap is a no-op
439  }
440 
441  std::swap(size_, list.size_);
442  std::swap(v_, list.v_);
443 }
444 
445 
446 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
447 
448 template<class T>
449 inline void Foam::reverse(UList<T>& list, const label n)
450 {
451  for (label i=0; i<n/2; ++i)
452  {
453  Foam::Swap(list[i], list[n-1-i]);
454  }
455 }
456 
457 
458 template<class T>
459 inline void Foam::reverse(UList<T>& list)
460 {
461  reverse(list, list.size());
462 }
463 
464 
465 // ************************************************************************* //
Foam::UList::cdata_bytes
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:244
Foam::reverse
void reverse(UList< T > &list, const label n)
Definition: UListI.H:449
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
Foam::UList::cdata
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:230
Foam::UList::rcIndex
label rcIndex(const label i) const noexcept
Definition: UListI.H:67
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::end
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
Foam::UList::first
T & first()
Return the first element of the list.
Definition: UListI.H:202
Foam::UList::UList
constexpr UList() noexcept
Default construct, zero-sized and nullptr.
Definition: UListI.H:35
Foam::UList::setAddressableSize
void setAddressableSize(const label n) noexcept
Set addressed size to be inconsistent with allocated storage.
Definition: UListI.H:413
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:178
Foam::UList::data_bytes
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:251
Foam::UList::fcIndex
label fcIndex(const label i) const noexcept
Definition: UListI.H:60
Foam::UList::checkRange
void checkRange(const label start, const label len) const
Check that start and length define a valid range.
Definition: UListI.H:130
Foam::UList::checkStart
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: UListI.H:102
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::UList::begin
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
Foam::UList::cend
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:364
error.H
Foam::UList::iterator
T * iterator
Random access iterator for traversing a UList.
Definition: UList.H:151
Foam::UList::last
T & last()
Return the last element of the list.
Definition: UListI.H:216
Foam::UList::crend
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:406
Foam::UList::empty
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
Foam::FatalError
error FatalError
Foam::UList::data
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:237
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:392
Foam::UList::fcValue
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: UListI.H:74
Foam::UList::cbegin
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:343
Foam::UList::shallowCopy
void shallowCopy(const UList< T > &list)
Copy the pointer and size held by the given UList.
Definition: UListI.H:272
Foam::UList::swap
void swap(UList< T > &list)
Swap content with another UList of the same type in constant time.
Definition: UListI.H:434
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::UList::rbegin
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:371
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
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::UList::operator[]
T & operator[](const label i)
Return element of UList.
Definition: UListI.H:299
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:420
Foam::UList::found
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: UListI.H:265
Foam::UList::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access)
Definition: UList.H:166
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::UList::null
static const UList< T > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:53
Foam::UList::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access)
Definition: UList.H:163
Foam::UList::checkIndex
void checkIndex(const label i) const
Check index is within valid range [0,size)
Definition: UListI.H:159
Foam::UList::size_bytes
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:258
Foam::UList::rcValue
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: UListI.H:88
Foam::UList::crbegin
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:385
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177