Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
29#include "error.H"
30// <algorithm> already included by stdFoam.H
31
32// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33
34template<class T>
35inline constexpr Foam::UList<T>::UList() noexcept
37 size_(0),
38 v_(nullptr)
39{}
40
41
42template<class T>
43inline Foam::UList<T>::UList(T* __restrict__ ptr, const label len) noexcept
44:
45 size_(len),
46 v_(ptr)
47{}
48
49
50// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
51
52template<class T>
53inline void Foam::UList<T>::fill_uniform(const T& val)
54{
55 // Can dispatch with
56 // - std::execution::par_unseq
57 // - std::execution::unseq
58 std::fill_n
59 (
60 this->v_, this->size_, val
61 );
62}
63
64
65template<class T>
67{
68 if constexpr (std::is_arithmetic_v<T>)
69 {
70 // Can dispatch with
71 // - std::execution::par_unseq
72 // - std::execution::unseq
73 std::fill_n
74 (
75 this->v_, this->size_, T(0)
76 );
77 }
78 else if constexpr (is_contiguous_v<T>)
79 {
80 // Can dispatch with
81 // - std::execution::par_unseq
82 // - std::execution::unseq
83 std::fill_n
84 (
85 this->data_bytes(), this->size_bytes(), char(0)
86 );
87 }
88 else
89 {
90 // May also have special triggers when assigning non-contiguous
91 // from zero...
92
93 const auto last = (this->v_ + this->size_);
94
95 for (auto first = this->v_; (first != last); (void)++first)
96 {
97 *first = Foam::zero{};
98 }
99 }
100}
101
102
103// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
104
105template<class T>
106inline Foam::label Foam::UList<T>::fcIndex(const label i) const noexcept
107{
108 return (i == size()-1 ? 0 : i+1);
109}
110
111
112template<class T>
113inline Foam::label Foam::UList<T>::rcIndex(const label i) const noexcept
114{
115 return (i ? i-1 : size()-1);
116}
117
118
119template<class T>
120inline const T& Foam::UList<T>::fcValue(const label i) const
121{
122 return this->operator[](this->fcIndex(i));
123}
124
125
126template<class T>
127inline T& Foam::UList<T>::fcValue(const label i)
128{
129 return this->operator[](this->fcIndex(i));
130}
131
132
133template<class T>
134inline const T& Foam::UList<T>::rcValue(const label i) const
135{
136 return this->operator[](this->rcIndex(i));
137}
138
139
140template<class T>
141inline T& Foam::UList<T>::rcValue(const label i)
142{
143 return this->operator[](this->rcIndex(i));
144}
145
146
147template<class T>
148inline void Foam::UList<T>::checkStart(const label start) const
149{
150 if (start < 0 || (start && start >= size_))
151 {
152 // Note: accept start=0 for zero-sized lists
154 << "start " << start << " out of range [0,"
155 << size_ << "]\n"
156 << abort(FatalError);
157 }
158}
159
160
161template<class T>
162inline void Foam::UList<T>::checkSize(const label size) const
163{
164 if (size < 0 || size > size_)
165 {
167 << "size " << size << " out of range [0,"
168 << size_ << "]\n"
169 << abort(FatalError);
170 }
171}
172
173
174template<class T>
176(
177 const label start,
178 const label len
179) const
180{
181 // Artificially allow the start of a zero-sized subList to be
182 // one past the end of the original list.
183 if (len)
184 {
185 if (len < 0)
186 {
188 << "size " << len << " is negative, out of range [0,"
189 << size_ << "]\n"
190 << abort(FatalError);
191 }
192 this->checkStart(start);
193 this->checkSize(start + len);
194 }
195 else
196 {
197 // Start index needs to fall between 0 and size. One position
198 // behind the last element is allowed
199 this->checkSize(start);
200 }
201}
202
203
204template<class T>
205inline void Foam::UList<T>::checkIndex(const label i) const
206{
207 if (!size_)
208 {
210 << "attempt to access element " << i << " from zero sized list"
211 << abort(FatalError);
212 }
213 else if (i < 0 || i >= size_)
214 {
216 << "index " << i << " out of range [0,"
217 << size_ << "]\n"
218 << abort(FatalError);
219 }
220}
221
222
223template<class T>
224inline bool Foam::UList<T>::uniform() const
225{
226 if (!size_)
227 {
228 return false;
229 }
230
231 // std::all_of()
232
233 for (label i = 1; i < size_; ++i)
234 {
235 if (this->v_[0] != this->v_[i])
236 {
237 return false;
238 }
240
241 return true;
242}
243
244
245template<class T>
247{
248 return this->operator[](0);
249}
250
251
252template<class T>
253inline const T& Foam::UList<T>::front() const
254{
255 return this->operator[](0);
256}
257
258
259template<class T>
261{
262 return this->operator[](this->size()-1);
263}
264
265
266template<class T>
267inline const T& Foam::UList<T>::back() const
268{
269 return this->operator[](this->size()-1);
270}
271
272
273template<class T>
274inline const T* Foam::UList<T>::cdata() const noexcept
275{
276 return v_;
277}
278
279
280template<class T>
282{
283 return v_;
284}
285
286
287template<class T>
288inline const char* Foam::UList<T>::cdata_bytes() const noexcept
289{
290 return reinterpret_cast<const char*>(v_);
291}
292
293
294template<class T>
296{
297 return reinterpret_cast<char*>(v_);
298}
299
300
301template<class T>
302inline std::streamsize Foam::UList<T>::size_bytes() const noexcept
303{
304 return std::streamsize(size_)*sizeof(T);
305}
306
307
308template<class T>
309inline bool Foam::UList<T>::contains(const T& val) const
311 const auto iter = std::find(this->begin(), this->end(), val);
312 return (iter != this->end());
313}
314
315
316template<class T>
317inline bool Foam::UList<T>::contains(const T& val, label pos, label len) const
318{
319 return (this->find(val, pos, len) >= 0);
320}
321
322
323template<class T>
325(
326 T* __restrict__ ptr,
327 const label len
328) noexcept
330 size_ = len;
331 v_ = ptr;
333
334
335template<class T>
336inline void Foam::UList<T>::shallowCopy(std::nullptr_t) noexcept
338 size_ = 0;
339 v_ = nullptr;
340}
341
342
343template<class T>
344inline void Foam::UList<T>::shallowCopy(const UList<T>& list) noexcept
345{
346 size_ = list.size_;
347 v_ = list.v_;
348}
349
350
351// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
352
353template<class T>
354inline void Foam::UList<T>::operator=(const T& val)
355{
356 this->fill_uniform(val);
358
359
360// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
361
362template<class T>
364{
365 this->fill_uniform(Foam::zero{});
366}
367
368
369template<class T>
370inline T& Foam::UList<T>::operator[](const label i)
371{
372 if constexpr (std::is_same_v<bool, std::remove_cv_t<T>>)
373 {
374 // Lazy evaluation - return false for out-of-range
375 // Note: strictly speaking should not be modifiable but we cannot
376 // alway control which signature (const or non-const) is called
377 if (i < 0 || i >= size_)
379 return const_cast<bool&>(Foam::pTraits<bool>::null());
380 }
381 }
382 else
384 #ifdef FULLDEBUG
385 checkIndex(i);
386 #endif
387 }
388 return v_[i];
389}
390
391
392template<class T>
393inline const T& Foam::UList<T>::operator[](const label i) const
394{
395 if constexpr (std::is_same_v<bool, std::remove_cv_t<T>>)
396 {
397 // Lazy evaluation - return false for out-of-range
398 if (i < 0 || i >= size_)
399 {
401 }
402 }
403 else
404 {
405 #ifdef FULLDEBUG
406 checkIndex(i);
407 #endif
408 }
409 return v_[i];
411
412
413// * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
414
415template<class T>
416inline typename Foam::UList<T>::iterator
418{
419 return v_;
420}
421
422template<class T>
423inline typename Foam::UList<T>::const_iterator
425{
426 return v_;
427}
428
429template<class T>
430inline typename Foam::UList<T>::const_iterator
433 return v_;
435
436
437template<class T>
438inline typename Foam::UList<T>::iterator
439Foam::UList<T>::begin(const label i) noexcept
440{
441 return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
442}
443
444template<class T>
445inline typename Foam::UList<T>::const_iterator
446Foam::UList<T>::begin(const label i) const noexcept
448 return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
449}
450
451template<class T>
452inline typename Foam::UList<T>::const_iterator
453Foam::UList<T>::cbegin(const label i) const noexcept
455 return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
456}
457
458
459template<class T>
460inline typename Foam::UList<T>::iterator
462{
463 return (v_ + size_);
464}
465
466template<class T>
467inline typename Foam::UList<T>::const_iterator
469{
470 return (v_ + size_);
471}
472
473template<class T>
474inline typename Foam::UList<T>::const_iterator
476{
477 return (v_ + size_);
478}
479
480template<class T>
483{
484 return reverse_iterator(end());
485}
486
487template<class T>
490{
491 return const_reverse_iterator(end());
492}
493
494template<class T>
497{
498 return const_reverse_iterator(end());
499}
500
501template<class T>
504{
505 return reverse_iterator(begin());
506}
507
508template<class T>
511{
513}
514
515template<class T>
518{
520}
521
522
523template<class T>
524inline void Foam::UList<T>::setAddressableSize(const label n) noexcept
525{
526 size_ = n;
527}
528
529
530template<class T>
531inline void Foam::UList<T>::swap(UList<T>& list) noexcept
532{
533 if (&list == this)
534 {
535 return; // Self-swap is a no-op
536 }
537
538 std::swap(size_, list.size_);
539 std::swap(v_, list.v_);
540}
541
542
543// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
544
545template<class T>
546inline void Foam::reverse(UList<T>& list, const label n)
547{
548 const label nBy2 = n/2;
549
550 for (label i = 0; i < nBy2; ++i)
552 Foam::Swap(list[i], list[n-1-i]);
553 }
554}
555
556
557template<class T>
558inline void Foam::reverse(UList<T>& list)
559{
560 Foam::reverse(list, list.size());
561}
562
563
564// ************************************************************************* //
label n
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
void shallowCopy(T *__restrict__ ptr, const label len) noexcept
Copy the pointer and size.
Definition UListI.H:318
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition UListI.H:489
void fill_uniform(const T &val)
Assign all entries to the given value.
Definition UListI.H:46
T & first()
Access first element of the list, position [0].
Definition UList.H:957
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition UListI.H:288
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition UListI.H:510
const T * const_iterator
Random access iterator for traversing a UList.
Definition UList.H:183
void checkIndex(const label i) const
Check index is within valid range [0,size).
Definition UListI.H:198
T * iterator
Random access iterator for traversing a UList.
Definition UList.H:178
UList(const UList< CloudFunctionObject< CloudType > * > &) noexcept=default
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access).
Definition UList.H:203
bool contains(const T &val) const
True if the value is contained in the list.
Definition UListI.H:302
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list).
Definition UListI.H:127
T & back()
Access last element of the list, position [size()-1].
Definition UListI.H:253
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition UListI.H:454
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant UList.
Definition UListI.H:468
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition UListI.H:267
constexpr UList() noexcept
Default construct, zero-sized and nullptr.
Definition UListI.H:28
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition UListI.H:475
void swap(UList< T > &list) noexcept
Swap content with another UList of the same type in constant time.
Definition UListI.H:524
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition UListI.H:424
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list).
Definition UListI.H:113
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:106
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access).
Definition UList.H:198
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition UListI.H:274
T & front()
Access first element of the list, position [0].
Definition UListI.H:239
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition UListI.H:217
void checkSize(const label size) const
Check size is within valid range [0,size].
Definition UListI.H:155
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition UListI.H:281
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition UListI.H:496
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition UListI.H:295
void checkRange(const label start, const label len) const
Check that start and length define a valid range.
Definition UListI.H:169
void setAddressableSize(const label n) noexcept
Set addressed size to be inconsistent with allocated storage.
Definition UListI.H:517
T & operator[](const label i)
Return element of UList.
Definition UListI.H:363
T & last()
Access last element of the list, position [size()-1].
Definition UList.H:971
UList< T > & operator=(const UList< T > &)=delete
No copy assignment (default: shallow copy).
label find(const T &val) const
Find index of the first occurrence of the value.
Definition UList.C:160
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:99
void checkStart(const label start) const
Check start is within valid range [0,size).
Definition UListI.H:141
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
dimensionedScalar pos(const dimensionedScalar &ds)
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition UListI.H:539
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
constexpr bool is_contiguous_v
The is_contiguous value of Type (after stripping of qualifiers).
Definition contiguous.H:77
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)