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