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>
98 {
99  return NullObjectRef<UList<T>>();
100 }
101 
102 
103 template<class T>
104 inline Foam::label Foam::UList<T>::fcIndex(const label i) const noexcept
105 {
106  return (i == size()-1 ? 0 : i+1);
107 }
108 
109 
110 template<class T>
111 inline Foam::label Foam::UList<T>::rcIndex(const label i) const noexcept
112 {
113  return (i ? i-1 : size()-1);
114 }
115 
116 
117 template<class T>
118 inline const T& Foam::UList<T>::fcValue(const label i) const
119 {
120  return this->operator[](this->fcIndex(i));
121 }
122 
123 
124 template<class T>
125 inline T& Foam::UList<T>::fcValue(const label i)
126 {
127  return this->operator[](this->fcIndex(i));
128 }
129 
130 
131 template<class T>
132 inline const T& Foam::UList<T>::rcValue(const label i) const
133 {
134  return this->operator[](this->rcIndex(i));
135 }
136 
137 
138 template<class T>
139 inline T& Foam::UList<T>::rcValue(const label i)
140 {
141  return this->operator[](this->rcIndex(i));
142 }
143 
144 
145 template<class T>
146 inline void Foam::UList<T>::checkStart(const label start) const
147 {
148  if (start < 0 || (start && start >= size_))
149  {
150  // Note: accept start=0 for zero-sized lists
152  << "start " << start << " out of range [0,"
153  << size_ << "]\n"
154  << abort(FatalError);
155  }
156 }
157 
158 
159 template<class T>
160 inline void Foam::UList<T>::checkSize(const label size) const
161 {
162  if (size < 0 || size > size_)
163  {
165  << "size " << size << " out of range [0,"
166  << size_ << "]\n"
168  }
169 }
170 
171 
172 template<class T>
173 inline void Foam::UList<T>::checkRange
174 (
175  const label start,
176  const label len
177 ) const
178 {
179  // Artificially allow the start of a zero-sized subList to be
180  // one past the end of the original list.
181  if (len)
182  {
183  if (len < 0)
184  {
186  << "size " << len << " is negative, out of range [0,"
187  << size_ << "]\n"
188  << abort(FatalError);
189  }
190  this->checkStart(start);
191  this->checkSize(start + len);
192  }
193  else
194  {
195  // Start index needs to fall between 0 and size. One position
196  // behind the last element is allowed
197  this->checkSize(start);
198  }
199 }
200 
201 
202 template<class T>
203 inline void Foam::UList<T>::checkIndex(const label i) const
204 {
205  if (!size_)
206  {
208  << "attempt to access element " << i << " from zero sized list"
209  << abort(FatalError);
210  }
211  else if (i < 0 || i >= size_)
212  {
214  << "index " << i << " out of range [0,"
215  << size_ << "]\n"
216  << abort(FatalError);
217  }
218 }
219 
220 
221 template<class T>
222 inline bool Foam::UList<T>::uniform() const
223 {
224  if (!size_)
225  {
226  return false;
227  }
228 
229  // std::all_of()
230 
231  for (label i = 1; i < size_; ++i)
232  {
233  if (this->v_[0] != this->v_[i])
234  {
235  return false;
236  }
237  }
238 
239  return true;
240 }
241 
242 
243 template<class T>
245 {
246  return this->operator[](0);
247 }
248 
249 
250 template<class T>
251 inline const T& Foam::UList<T>::front() const
252 {
253  return this->operator[](0);
254 }
255 
256 
257 template<class T>
259 {
260  return this->operator[](this->size()-1);
261 }
262 
263 
264 template<class T>
265 inline const T& Foam::UList<T>::back() const
266 {
267  return this->operator[](this->size()-1);
268 }
269 
270 
271 template<class T>
272 inline const T* Foam::UList<T>::cdata() const noexcept
273 {
274  return v_;
275 }
276 
277 
278 template<class T>
280 {
281  return v_;
282 }
283 
284 
285 template<class T>
286 inline const char* Foam::UList<T>::cdata_bytes() const noexcept
287 {
288  return reinterpret_cast<const char*>(v_);
289 }
290 
291 
292 template<class T>
294 {
295  return reinterpret_cast<char*>(v_);
296 }
297 
298 
299 template<class T>
300 inline std::streamsize Foam::UList<T>::size_bytes() const noexcept
301 {
302  return std::streamsize(size_)*sizeof(T);
303 }
304 
305 
306 template<class T>
307 inline bool Foam::UList<T>::contains(const T& val) const
308 {
309  const auto iter = std::find(this->begin(), this->end(), val);
310  return (iter != this->end());
311 }
312 
313 
314 template<class T>
315 inline bool Foam::UList<T>::contains(const T& val, label pos, label len) const
316 {
317  return (this->find(val, pos, len) >= 0);
318 }
319 
320 
321 template<class T>
322 inline void Foam::UList<T>::shallowCopy
323 (
324  T* __restrict__ ptr,
325  const label len
326 ) noexcept
327 {
328  size_ = len;
329  v_ = ptr;
330 }
331 
332 
333 template<class T>
334 inline void Foam::UList<T>::shallowCopy(const UList<T>& list) noexcept
335 {
336  size_ = list.size_;
337  v_ = list.v_;
338 }
339 
340 
341 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
342 
343 template<class T>
344 inline void Foam::UList<T>::operator=(const T& val)
345 {
346  this->fill_uniform(val);
347 }
348 
349 
350 namespace Foam
351 {
352  // Template specialization for bool
353  template<>
354  inline const bool& Foam::UList<bool>::operator[](const label i) const
355  {
356  // Lazy evaluation - return false for out-of-range
357  if (i >= 0 && i < size_)
358  {
359  return v_[i];
360  }
363  }
364 } // End namespace Foam
365 
366 
367 template<class T>
368 inline T& Foam::UList<T>::operator[](const label i)
369 {
370  #ifdef FULLDEBUG
371  checkIndex(i);
372  #endif
373  return v_[i];
374 }
375 
376 
377 template<class T>
378 inline const T& Foam::UList<T>::operator[](const label i) const
379 {
380  #ifdef FULLDEBUG
381  checkIndex(i);
382  #endif
383  return v_[i];
384 }
385 
386 
387 template<class T>
388 inline Foam::UList<T>::operator const Foam::List<T>&() const
389 {
390  return *reinterpret_cast<const List<T>*>(this);
391 }
392 
393 
394 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
395 
396 template<class T>
397 inline typename Foam::UList<T>::iterator
399 {
400  return v_;
401 }
402 
403 template<class T>
404 inline typename Foam::UList<T>::const_iterator
406 {
407  return v_;
408 }
409 
410 template<class T>
411 inline typename Foam::UList<T>::const_iterator
413 {
414  return v_;
415 }
416 
417 
418 template<class T>
419 inline typename Foam::UList<T>::iterator
421 {
422  return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
423 }
424 
425 template<class T>
426 inline typename Foam::UList<T>::const_iterator
427 Foam::UList<T>::begin(const label i) const noexcept
428 {
429  return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
430 }
431 
432 template<class T>
433 inline typename Foam::UList<T>::const_iterator
434 Foam::UList<T>::cbegin(const label i) const noexcept
435 {
436  return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
437 }
438 
439 
440 template<class T>
441 inline typename Foam::UList<T>::iterator
443 {
444  return (v_ + size_);
445 }
446 
447 template<class T>
448 inline typename Foam::UList<T>::const_iterator
450 {
451  return (v_ + size_);
452 }
453 
454 template<class T>
455 inline typename Foam::UList<T>::const_iterator
457 {
458  return (v_ + size_);
459 }
460 
461 template<class T>
462 inline typename Foam::UList<T>::reverse_iterator
464 {
465  return reverse_iterator(end());
466 }
467 
468 template<class T>
471 {
472  return const_reverse_iterator(end());
473 }
474 
475 template<class T>
478 {
479  return const_reverse_iterator(end());
480 }
481 
482 template<class T>
483 inline typename Foam::UList<T>::reverse_iterator
485 {
486  return reverse_iterator(begin());
487 }
488 
489 template<class T>
492 {
493  return const_reverse_iterator(begin());
494 }
495 
496 template<class T>
499 {
500  return const_reverse_iterator(begin());
501 }
502 
503 
504 template<class T>
506 {
507  size_ = n;
508 }
509 
510 
511 template<class T>
512 inline void Foam::UList<T>::swap(UList<T>& list) noexcept
513 {
514  if (&list == this)
515  {
516  return; // Self-swap is a no-op
517  }
518 
519  std::swap(size_, list.size_);
520  std::swap(v_, list.v_);
521 }
522 
523 
524 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
525 
526 template<class T>
527 inline void Foam::reverse(UList<T>& list, const label n)
528 {
529  const label nBy2 = n/2;
530 
531  for (label i = 0; i < nBy2; ++i)
532  {
533  Foam::Swap(list[i], list[n-1-i]);
534  }
535 }
536 
537 
538 template<class T>
539 inline void Foam::reverse(UList<T>& list)
540 {
541  Foam::reverse(list, list.size());
542 }
543 
544 
545 // ************************************************************************* //
void swap(UList< T > &list) noexcept
Swap content with another UList of the same type in constant time.
Definition: UListI.H:505
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:795
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:449
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:598
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:470
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:272
T & front()
Access first element of the list, position [0].
Definition: UListI.H:237
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:215
void setAddressableSize(const label n) noexcept
Set addressed size to be inconsistent with allocated storage.
Definition: UListI.H:498
bool contains(const T &val) const
True if the value is contained in the list.
Definition: UListI.H:300
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:286
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:491
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:279
T & operator[](const label i)
Return element of UList.
Definition: UListI.H:361
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:97
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:456
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:477
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:111
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:520
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:391
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
static const UList< T > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:90
const volScalarField & T
void checkRange(const label start, const label len) const
Check that start and length define a valid range.
Definition: UListI.H:167
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:316
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:251
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:405
void checkSize(const label size) const
Check size is within valid range [0,size].
Definition: UListI.H:153
label n
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:265
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:435
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:104
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:139
void checkIndex(const label i) const
Check index is within valid range [0,size)
Definition: UListI.H:196
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: UListI.H:125
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:293
Namespace for OpenFOAM.