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-------------------------------------------------------------------------------
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\*---------------------------------------------------------------------------*/
28
29#include "error.H"
30#include "pTraits.H"
31
32// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33
34template<class T>
35inline constexpr Foam::UList<T>::UList() noexcept
36:
37 size_(0),
38 v_(nullptr)
39{}
40
41
42template<class T>
43inline 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
52template<class T>
54{
55 return NullObjectRef<UList<T>>();
56}
57
58
59template<class T>
60inline Foam::label Foam::UList<T>::fcIndex(const label i) const noexcept
61{
62 return (i == size()-1 ? 0 : i+1);
63}
64
65
66template<class T>
67inline Foam::label Foam::UList<T>::rcIndex(const label i) const noexcept
68{
69 return (i ? i-1 : size()-1);
70}
71
72
73template<class T>
74inline const T& Foam::UList<T>::fcValue(const label i) const
75{
76 return this->operator[](this->fcIndex(i));
77}
78
79
80template<class T>
81inline T& Foam::UList<T>::fcValue(const label i)
82{
83 return this->operator[](this->fcIndex(i));
84}
85
86
87template<class T>
88inline const T& Foam::UList<T>::rcValue(const label i) const
89{
90 return this->operator[](this->rcIndex(i));
91}
92
93
94template<class T>
95inline T& Foam::UList<T>::rcValue(const label i)
96{
97 return this->operator[](this->rcIndex(i));
98}
99
100
101template<class T>
102inline 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
115template<class T>
116inline 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
128template<class T>
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
158template<class T>
159inline 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
177template<class T>
178inline bool Foam::UList<T>::uniform() const
179{
180 const label len = size();
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
201template<class T>
203{
204 return this->operator[](0);
205}
206
207
208template<class T>
209inline const T& Foam::UList<T>::first() const
210{
211 return this->operator[](0);
212}
213
214
215template<class T>
217{
218 return this->operator[](this->size()-1);
219}
220
221
222template<class T>
223inline const T& Foam::UList<T>::last() const
224{
225 return this->operator[](this->size()-1);
226}
227
229template<class T>
230inline const T* Foam::UList<T>::cdata() const noexcept
232 return v_;
233}
234
235
236template<class T>
237inline T* Foam::UList<T>::data() noexcept
238{
239 return v_;
241
242
243template<class T>
244inline const char* Foam::UList<T>::cdata_bytes() const noexcept
245{
246 return reinterpret_cast<const char*>(v_);
248
249
250template<class T>
251inline char* Foam::UList<T>::data_bytes() noexcept
252{
253 return reinterpret_cast<char*>(v_);
254}
255
257template<class T>
258inline std::streamsize Foam::UList<T>::size_bytes() const noexcept
260 return std::streamsize(size_)*sizeof(T);
261}
263
264template<class T>
265inline bool Foam::UList<T>::found(const T& val, label pos) const
266{
267 return (this->find(val, pos) >= 0);
268}
269
270
271template<class T>
272inline void Foam::UList<T>::shallowCopy(const UList<T>& list)
273{
274 size_ = list.size_;
275 v_ = list.v_;
276}
277
279// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
280
281namespace 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
298template<class T>
299inline T& Foam::UList<T>::operator[](const label i)
300{
301 #ifdef FULLDEBUG
302 checkIndex(i);
303 #endif
304 return v_[i];
305}
306
308template<class T>
309inline const T& Foam::UList<T>::operator[](const label i) const
311 #ifdef FULLDEBUG
312 checkIndex(i);
313 #endif
314 return v_[i];
315}
316
317
318template<class T>
320{
321 return *reinterpret_cast<const List<T>*>(this);
322}
323
324
325// * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
326
327template<class T>
328inline typename Foam::UList<T>::iterator
330{
331 return v_;
332}
333
334template<class T>
335inline typename Foam::UList<T>::const_iterator
336Foam::UList<T>::begin() const noexcept
337{
338 return v_;
339}
340
341template<class T>
342inline typename Foam::UList<T>::const_iterator
344{
345 return v_;
346}
347
348template<class T>
349inline typename Foam::UList<T>::iterator
351{
352 return (v_ + size_);
353}
354
355template<class T>
356inline typename Foam::UList<T>::const_iterator
357Foam::UList<T>::end() const noexcept
358{
359 return (v_ + size_);
360}
361
362template<class T>
363inline typename Foam::UList<T>::const_iterator
364Foam::UList<T>::cend() const noexcept
365{
366 return (v_ + size_);
367}
368
369template<class T>
372{
373 return reverse_iterator(end());
374}
375
376template<class T>
379{
380 return const_reverse_iterator(end());
381}
382
383template<class T>
386{
387 return const_reverse_iterator(end());
388}
390template<class T>
393{
394 return reverse_iterator(begin());
395}
396
397template<class T>
400{
401 return const_reverse_iterator(begin());
402}
403
404template<class T>
408 return const_reverse_iterator(begin());
409}
410
411
412template<class T>
413inline void Foam::UList<T>::setAddressableSize(const label n) noexcept
414{
415 size_ = n;
417
418
419template<class T>
420inline Foam::label Foam::UList<T>::size() const noexcept
421{
422 return size_;
423}
424
425
426template<class T>
427inline bool Foam::UList<T>::empty() const noexcept
429 return !size_;
430}
432
433template<class T>
435{
436 if (&list == this)
438 return; // Self-swap is a no-op
439 }
441 std::swap(size_, list.size_);
442 std::swap(v_, list.v_);
444
445
446// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
447
448template<class T>
449inline void Foam::reverse(UList<T>& list, const label n)
450{
451 const label nBy2 = n/2;
453 for (label i = 0; i < nBy2; ++i)
454 {
455 Foam::Swap(list[i], list[n-1-i]);
456 }
457}
458
459
460template<class T>
461inline void Foam::reverse(UList<T>& list)
462{
463 Foam::reverse(list, list.size());
464}
465
466
467// ************************************************************************* //
bool found
label n
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
void checkSize() const
Check that dimensions are positive, non-zero.
Definition: MatrixI.H:167
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:94
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:385
T & first()
Return the first element of the list.
Definition: UListI.H:202
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
void swap(UList< T > &list)
Swap content with another UList of the same type in constant time.
Definition: UListI.H:434
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:251
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:406
static const UList< T > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:53
const T * const_iterator
Random access iterator for traversing a UList.
Definition: UList.H:154
void checkIndex(const label i) const
Check index is within valid range [0,size)
Definition: UListI.H:159
T * iterator
Random access iterator for traversing a UList.
Definition: UList.H:151
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access)
Definition: UList.H:166
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: UListI.H:88
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:364
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:230
constexpr UList() noexcept
Default construct, zero-sized and nullptr.
Definition: UListI.H:35
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:371
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:343
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: UListI.H:74
label rcIndex(const label i) const noexcept
Definition: UListI.H:67
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access)
Definition: UList.H:163
label size() const noexcept
The number of elements in the UList.
Definition: UListI.H:420
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:237
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: UListI.H:178
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:244
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:392
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:258
void checkRange(const label start, const label len) const
Check that start and length define a valid range.
Definition: UListI.H:130
void setAddressableSize(const label n) noexcept
Set addressed size to be inconsistent with allocated storage.
Definition: UListI.H:413
void shallowCopy(const UList< T > &list)
Copy the pointer and size held by the given UList.
Definition: UListI.H:272
T & operator[](const label i)
Return element of UList.
Definition: UListI.H:299
T & last()
Return the last element of the list.
Definition: UListI.H:216
label fcIndex(const label i) const noexcept
Definition: UListI.H:60
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: UListI.H:102
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Namespace for OpenFOAM.
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:449
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408
errorManip< error > abort(error &err)
Definition: errorManip.H:144
const direction noexcept
Definition: Scalar.H:223
error FatalError
A non-counting (dummy) refCount.
Definition: refCount.H:59