UList.C
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) 2017-2022 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 "UList.H"
30#include "ListLoopM.H"
31#include "contiguous.H"
32#include "labelRange.H"
33
34#include <algorithm>
35#include <random>
36
37// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
38
39template<class T>
41Foam::UList<T>::validateRange(const labelRange& requestedRange) const
42{
43 const labelRange range(requestedRange.subset0(this->size()));
44
45 #ifdef FULLDEBUG
46 this->checkStart(range.start());
47 this->checkSize(range.start() + range.size());
48 #endif
49
50 return range;
51}
52
53
54// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
55
56template<class T>
57void Foam::UList<T>::moveFirst(const label i)
58{
59 checkIndex(i);
60
61 for (label lower = 0; lower < i; ++lower)
62 {
63 Foam::Swap(this->operator[](lower), this->operator[](i));
64 }
65}
66
67
68template<class T>
69void Foam::UList<T>::moveLast(const label i)
70{
71 checkIndex(i);
72
73 for (label upper = size()-1; upper > i; --upper)
74 {
75 Foam::Swap(this->operator[](i), this->operator[](upper));
76 }
77}
78
79
80template<class T>
81void Foam::UList<T>::swapFirst(const label i)
82{
83 checkIndex(i);
84
85 if (i > 0)
86 {
87 Foam::Swap(this->operator[](0), this->operator[](i));
88 }
89}
90
91
92template<class T>
93void Foam::UList<T>::swapLast(const label i)
94{
95 checkIndex(i);
96
97 const label upper = size()-1;
98
99 if (i < upper)
100 {
101 Foam::Swap(this->operator[](i), this->operator[](upper));
102 }
103}
104
105
106template<class T>
108{
109 const label len = this->size_;
110
111 if (len != list.size_)
112 {
114 << "Lists have different sizes: "
115 << len << " != " << list.size() << nl
116 << abort(FatalError);
117 }
118 else if (len)
119 {
120 #ifdef USEMEMCPY
122 {
123 std::memcpy
124 (
125 static_cast<void*>(this->v_), list.v_, this->size_bytes()
126 );
127 }
128 else
129 #endif
130 {
131 List_ACCESS(T, (*this), lhs);
132 List_CONST_ACCESS(T, list, rhs);
133 for (label i = 0; i < len; ++i)
134 {
135 lhs[i] = rhs[i];
136 }
137 }
138 }
139}
140
141
142template<class T>
143template<class Addr>
145{
146 const label len = this->size_;
147
148 if (len != list.size())
149 {
151 << "Lists have different sizes: "
152 << len << " != " << list.size() << nl
153 << abort(FatalError);
154 }
155 else if (len)
156 {
157 List_ACCESS(T, (*this), lhs);
158 for (label i = 0; i < len; ++i)
159 {
160 lhs[i] = list[i];
161 }
162 }
163}
164
165
166// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
167
168template<class T>
170{
171 const label len = this->size();
172
173 List_ACCESS(T, (*this), vp);
174
175 for (label i=0; i < len; ++i)
176 {
177 vp[i] = val;
178 }
179}
180
181
182template<class T>
184{
185 const label len = this->size();
186
187 List_ACCESS(T, (*this), vp);
188
189 for (label i=0; i < len; ++i)
190 {
191 vp[i] = Zero;
192 }
193}
194
195
196// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
197
198template<class T>
199std::streamsize Foam::UList<T>::byteSize() const
200{
202 {
204 << "Invalid for non-contiguous data types"
205 << abort(FatalError);
206 }
207 return this->size_bytes();
208}
209
210
211template<class T>
212Foam::label Foam::UList<T>::find(const T& val, label pos) const
213{
214 const label len = this->size();
215
216 if (pos >= 0 && len)
217 {
218 List_CONST_ACCESS(T, (*this), list);
219
220 while (pos < len)
221 {
222 if (list[pos] == val)
223 {
224 return pos;
225 }
226
227 ++pos;
228 }
229 }
230
231 return -1;
232}
233
234
235template<class T>
236Foam::label Foam::UList<T>::rfind(const T& val, label pos) const
237{
238 // pos == -1 has same meaning as std::string::npos - search from end
239 if (pos < 0 || pos >= this->size())
240 {
241 pos = this->size()-1;
242 }
243
244 List_CONST_ACCESS(T, (*this), list);
245
246 while (pos >= 0)
247 {
248 if (list[pos] == val)
249 {
250 return pos;
251 }
252
253 --pos;
254 }
255
256 return -1;
257}
258
259
260// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
261
262template<class T>
264{
265 const label len = this->size_;
266 if (len != list.size_)
267 {
268 return false;
269 }
270
271 bool equal = true;
272
273 List_CONST_ACCESS(T, (*this), lhs);
274 List_CONST_ACCESS(T, (list), rhs);
275
276 for (label i = 0; i < len; ++i)
277 {
278 equal = (lhs[i] == rhs[i]);
279 if (!equal) break;
280 }
281
282 return equal;
283}
284
285
286template<class T>
288{
289 return !operator==(list);
290}
291
293template<class T>
294bool Foam::UList<T>::operator<(const UList<T>& list) const
295{
296 for
297 (
298 const_iterator lhs = begin(), rhs = list.begin();
299 lhs < end() && rhs < list.end();
300 ++lhs, ++rhs
301 )
302 {
303 if (*lhs < *rhs)
304 {
305 return true;
306 }
307 else if (*rhs < *lhs)
308 {
309 return false;
310 }
311 }
312
313 // Contents look to be identical, or lists have different sizes
314 return (this->size_ < list.size_);
315}
316
317
318template<class T>
319bool Foam::UList<T>::operator>(const UList<T>& list) const
320{
321 return list.operator<(*this);
322}
323
324
325template<class T>
327{
328 return !list.operator<(*this);
329}
330
331
332template<class T>
334{
335 return !operator<(list);
336}
338
339// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
341template<class T>
344 std::sort(list.begin(), list.end());
345}
347
348template<class T, class Compare>
349void Foam::sort(UList<T>& list, const Compare& comp)
350{
351 std::sort(list.begin(), list.end(), comp);
352}
353
354
355template<class T>
357{
358 std::stable_sort(list.begin(), list.end());
360
361
362template<class T, class Compare>
363void Foam::stableSort(UList<T>& list, const Compare& comp)
364{
365 std::stable_sort(list.begin(), list.end(), comp);
366}
367
368
369template<class T>
371{
372 std::shuffle(list.begin(), list.end(), std::default_random_engine());
373}
374
375
376// ************************************************************************* //
scalar range
Macros for accessing List elements.
#define List_ACCESS(type, f, fp)
Definition: ListLoopM.H:39
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:43
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
label size() const noexcept
The number of elements in the list.
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
void moveLast(const label i)
Move element to the last position.
Definition: UList.C:69
void swapLast(const label i)
Swap element with the last element. Fatal on an empty list.
Definition: UList.C:93
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
bool operator<(const UList< T > &list) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:294
const T * const_iterator
Random access iterator for traversing a UList.
Definition: UList.H:154
void swapFirst(const label i)
Swap element with the first element. Fatal on an empty list.
Definition: UList.C:81
void deepCopy(const UList< T > &list)
Copy elements of the given UList. Sizes must match!
Definition: UList.C:107
bool operator>(const UList< T > &a) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:319
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition: UList.C:236
bool operator>=(const UList< T > &a) const
Return true if !(a < b). Takes linear time.
Definition: UList.C:333
label find(const T &val, label pos=0) const
Find index of the first occurrence of the value.
Definition: UList.C:212
std::streamsize byteSize() const
Definition: UList.C:199
bool operator<=(const UList< T > &a) const
Return true if !(a > b). Takes linear time.
Definition: UList.C:326
void moveFirst(const label i)
Move element to the first position.
Definition: UList.C:57
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
labelRange validateRange(const labelRange &requestedRange) const
Definition: UList.C:41
UList< T > & operator=(const UList< T > &)=delete
No copy assignment (default: shallow copy)
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
labelRange subset0(const label size) const
Calculate the intersection with the given 0/size range.
Definition: labelRange.C:182
friend bool operator!=(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:106
friend bool operator==(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:97
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
dimensionedScalar pos(const dimensionedScalar &ds)
void shuffle(UList< T > &list)
Randomise the list order.
Definition: UList.C:370
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:342
errorManip< error > abort(error &err)
Definition: errorManip.H:144
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
error FatalError
void stableSort(UList< T > &list)
Stable sort the list.
Definition: UList.C:356
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:78