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 // <algorithm> already included by stdFoam.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 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
51 
52 template<class T>
53 inline void Foam::UList<T>::fill_uniform(const T& val)
54 {
55  // Can dispatch with
56  // - std::execution::parallel_unsequenced_policy
57  // - std::execution::unsequenced_policy
58  std::fill_n
59  (
60  this->v_, this->size_, val
61  );
62 }
63 
64 
65 template<class T>
67 {
68  // Note: ambiguous conversions for char can still cause compilation
69  // issues.
70  // May also have special triggers when assigning non-contiguous from zero...
71 
73  {
74  // Can dispatch with
75  // - std::execution::parallel_unsequenced_policy
76  // - std::execution::unsequenced_policy
77  std::fill_n
78  (
79  this->data_bytes(), this->size_bytes(), char(0)
80  );
81  }
82  else
83  {
84  const auto last = (this->v_ + this->size_);
85 
86  for (auto first = this->v_; (first != last); (void)++first)
87  {
88  *first = Foam::zero{};
89  }
90  }
91 }
92 
93 
94 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
95 
96 template<class T>
97 inline Foam::label Foam::UList<T>::fcIndex(const label i) const noexcept
98 {
99  return (i == size()-1 ? 0 : i+1);
100 }
101 
102 
103 template<class T>
104 inline Foam::label Foam::UList<T>::rcIndex(const label i) const noexcept
105 {
106  return (i ? i-1 : size()-1);
107 }
108 
109 
110 template<class T>
111 inline const T& Foam::UList<T>::fcValue(const label i) const
112 {
113  return this->operator[](this->fcIndex(i));
114 }
115 
116 
117 template<class T>
118 inline T& Foam::UList<T>::fcValue(const label i)
119 {
120  return this->operator[](this->fcIndex(i));
121 }
122 
123 
124 template<class T>
125 inline const T& Foam::UList<T>::rcValue(const label i) const
126 {
127  return this->operator[](this->rcIndex(i));
128 }
129 
130 
131 template<class T>
132 inline T& Foam::UList<T>::rcValue(const label i)
133 {
134  return this->operator[](this->rcIndex(i));
135 }
136 
137 
138 template<class T>
139 inline void Foam::UList<T>::checkStart(const label start) const
140 {
141  if (start < 0 || (start && start >= size_))
142  {
143  // Note: accept start=0 for zero-sized lists
145  << "start " << start << " out of range [0,"
146  << size_ << "]\n"
147  << abort(FatalError);
148  }
149 }
150 
151 
152 template<class T>
153 inline void Foam::UList<T>::checkSize(const label size) const
154 {
155  if (size < 0 || size > size_)
156  {
158  << "size " << size << " out of range [0,"
159  << size_ << "]\n"
161  }
162 }
163 
164 
165 template<class T>
166 inline void Foam::UList<T>::checkRange
167 (
168  const label start,
169  const label len
170 ) const
171 {
172  // Artificially allow the start of a zero-sized subList to be
173  // one past the end of the original list.
174  if (len)
175  {
176  if (len < 0)
177  {
179  << "size " << len << " is negative, out of range [0,"
180  << size_ << "]\n"
181  << abort(FatalError);
182  }
183  this->checkStart(start);
184  this->checkSize(start + len);
185  }
186  else
187  {
188  // Start index needs to fall between 0 and size. One position
189  // behind the last element is allowed
190  this->checkSize(start);
191  }
192 }
193 
194 
195 template<class T>
196 inline void Foam::UList<T>::checkIndex(const label i) const
197 {
198  if (!size_)
199  {
201  << "attempt to access element " << i << " from zero sized list"
202  << abort(FatalError);
203  }
204  else if (i < 0 || i >= size_)
205  {
207  << "index " << i << " out of range [0,"
208  << size_ << "]\n"
209  << abort(FatalError);
210  }
211 }
212 
213 
214 template<class T>
215 inline bool Foam::UList<T>::uniform() const
216 {
217  if (!size_)
218  {
219  return false;
220  }
221 
222  // std::all_of()
223 
224  for (label i = 1; i < size_; ++i)
225  {
226  if (this->v_[0] != this->v_[i])
227  {
228  return false;
229  }
230  }
231 
232  return true;
233 }
234 
235 
236 template<class T>
238 {
239  return this->operator[](0);
240 }
241 
242 
243 template<class T>
244 inline const T& Foam::UList<T>::front() const
245 {
246  return this->operator[](0);
247 }
248 
249 
250 template<class T>
252 {
253  return this->operator[](this->size()-1);
254 }
255 
256 
257 template<class T>
258 inline const T& Foam::UList<T>::back() const
259 {
260  return this->operator[](this->size()-1);
261 }
262 
263 
264 template<class T>
265 inline const T* Foam::UList<T>::cdata() const noexcept
266 {
267  return v_;
268 }
269 
270 
271 template<class T>
273 {
274  return v_;
275 }
276 
277 
278 template<class T>
279 inline const char* Foam::UList<T>::cdata_bytes() const noexcept
280 {
281  return reinterpret_cast<const char*>(v_);
282 }
283 
284 
285 template<class T>
287 {
288  return reinterpret_cast<char*>(v_);
289 }
290 
291 
292 template<class T>
293 inline std::streamsize Foam::UList<T>::size_bytes() const noexcept
294 {
295  return std::streamsize(size_)*sizeof(T);
296 }
297 
298 
299 template<class T>
300 inline bool Foam::UList<T>::contains(const T& val) const
301 {
302  const auto iter = std::find(this->begin(), this->end(), val);
303  return (iter != this->end());
304 }
305 
306 
307 template<class T>
308 inline bool Foam::UList<T>::contains(const T& val, label pos, label len) const
309 {
310  return (this->find(val, pos, len) >= 0);
311 }
312 
313 
314 template<class T>
315 inline void Foam::UList<T>::shallowCopy
316 (
317  T* __restrict__ ptr,
318  const label len
319 ) noexcept
320 {
321  size_ = len;
322  v_ = ptr;
323 }
324 
325 
326 template<class T>
327 inline void Foam::UList<T>::shallowCopy(std::nullptr_t) noexcept
328 {
329  size_ = 0;
330  v_ = nullptr;
331 }
332 
333 
334 template<class T>
335 inline void Foam::UList<T>::shallowCopy(const UList<T>& list) noexcept
336 {
337  size_ = list.size_;
338  v_ = list.v_;
339 }
340 
341 
342 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
343 
344 template<class T>
345 inline void Foam::UList<T>::operator=(const T& val)
346 {
347  this->fill_uniform(val);
348 }
349 
350 
351 namespace Foam
352 {
353  // Template specialization for bool
354  template<>
355  inline const bool& Foam::UList<bool>::operator[](const label i) const
356  {
357  // Lazy evaluation - return false for out-of-range
358  if (i >= 0 && i < size_)
359  {
360  return v_[i];
361  }
364  }
365 } // End namespace Foam
366 
367 
368 template<class T>
369 inline T& Foam::UList<T>::operator[](const label i)
370 {
371  #ifdef FULLDEBUG
372  checkIndex(i);
373  #endif
374  return v_[i];
375 }
376 
377 
378 template<class T>
379 inline const T& Foam::UList<T>::operator[](const label i) const
380 {
381  #ifdef FULLDEBUG
382  checkIndex(i);
383  #endif
384  return v_[i];
385 }
386 
387 
388 template<class T>
389 inline Foam::UList<T>::operator const Foam::List<T>&() const
390 {
391  return *reinterpret_cast<const List<T>*>(this);
392 }
393 
394 
395 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
396 
397 template<class T>
398 inline typename Foam::UList<T>::iterator
400 {
401  return v_;
402 }
403 
404 template<class T>
405 inline typename Foam::UList<T>::const_iterator
407 {
408  return v_;
409 }
410 
411 template<class T>
412 inline typename Foam::UList<T>::const_iterator
414 {
415  return v_;
416 }
417 
418 
419 template<class T>
420 inline typename Foam::UList<T>::iterator
422 {
423  return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
424 }
425 
426 template<class T>
427 inline typename Foam::UList<T>::const_iterator
428 Foam::UList<T>::begin(const label i) const noexcept
429 {
430  return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
431 }
432 
433 template<class T>
434 inline typename Foam::UList<T>::const_iterator
435 Foam::UList<T>::cbegin(const label i) const noexcept
436 {
437  return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
438 }
439 
440 
441 template<class T>
442 inline typename Foam::UList<T>::iterator
444 {
445  return (v_ + size_);
446 }
447 
448 template<class T>
449 inline typename Foam::UList<T>::const_iterator
451 {
452  return (v_ + size_);
453 }
454 
455 template<class T>
456 inline typename Foam::UList<T>::const_iterator
458 {
459  return (v_ + size_);
460 }
461 
462 template<class T>
463 inline typename Foam::UList<T>::reverse_iterator
465 {
466  return reverse_iterator(end());
467 }
468 
469 template<class T>
472 {
473  return const_reverse_iterator(end());
474 }
475 
476 template<class T>
479 {
480  return const_reverse_iterator(end());
481 }
482 
483 template<class T>
484 inline typename Foam::UList<T>::reverse_iterator
486 {
487  return reverse_iterator(begin());
488 }
489 
490 template<class T>
493 {
494  return const_reverse_iterator(begin());
495 }
496 
497 template<class T>
500 {
501  return const_reverse_iterator(begin());
502 }
503 
504 
505 template<class T>
507 {
508  size_ = n;
509 }
510 
511 
512 template<class T>
513 inline void Foam::UList<T>::swap(UList<T>& list) noexcept
514 {
515  if (&list == this)
516  {
517  return; // Self-swap is a no-op
518  }
519 
520  std::swap(size_, list.size_);
521  std::swap(v_, list.v_);
522 }
523 
524 
525 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
526 
527 template<class T>
528 inline void Foam::reverse(UList<T>& list, const label n)
529 {
530  const label nBy2 = n/2;
531 
532  for (label i = 0; i < nBy2; ++i)
533  {
534  Foam::Swap(list[i], list[n-1-i]);
535  }
536 }
537 
538 
539 template<class T>
540 inline void Foam::reverse(UList<T>& list)
541 {
542  Foam::reverse(list, list.size());
543 }
544 
545 
546 // ************************************************************************* //
void swap(UList< T > &list) noexcept
Swap content with another UList of the same type in constant time.
Definition: UListI.H:506
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:827
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access)
Definition: UList.H:211
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:450
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:608
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:471
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:265
T & front()
Access first element of the list, position [0].
Definition: UListI.H:230
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
T * iterator
Random access iterator for traversing a UList.
Definition: UList.H:186
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: UListI.H:208
void setAddressableSize(const label n) noexcept
Set addressed size to be inconsistent with allocated storage.
Definition: UListI.H:499
bool contains(const T &val) const
True if the value is contained in the list.
Definition: UListI.H:293
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:279
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:492
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:272
T & operator[](const label i)
Return element of UList.
Definition: UListI.H:362
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:90
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:457
UList< T > & operator=(const UList< T > &)=delete
No copy assignment (default: shallow copy)
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:478
dimensionedScalar pos(const dimensionedScalar &ds)
void fill_uniform(const T &val)
Assign all entries to the given value.
Definition: UListI.H:46
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: UListI.H:104
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:521
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access)
Definition: UList.H:206
errorManip< error > abort(error &err)
Definition: errorManip.H:139
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:392
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:105
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:201
const volScalarField & T
void checkRange(const label start, const label len) const
Check that start and length define a valid range.
Definition: UListI.H:160
constexpr UList() noexcept
Default construct, zero-sized and nullptr.
Definition: UListI.H:28
const T * const_iterator
Random access iterator for traversing a UList.
Definition: UList.H:191
A template class to specify that a data type can be considered as being contiguous in memory...
Definition: contiguous.H:70
void shallowCopy(T *__restrict__ ptr, const label len) noexcept
Copy the pointer and size.
Definition: UListI.H:309
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
Definition: DynamicList.H:692
T & back()
Access last element of the list, position [size()-1].
Definition: UListI.H:244
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:406
void checkSize(const label size) const
Check size is within valid range [0,size].
Definition: UListI.H:146
label n
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:258
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:436
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:97
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:168
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: UListI.H:132
void checkIndex(const label i) const
Check index is within valid range [0,size)
Definition: UListI.H:189
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: UListI.H:118
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:286
Namespace for OpenFOAM.