Loading...
Searching...
No Matches
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-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\*---------------------------------------------------------------------------*/
28
29#include "UList.H"
30#include <random>
31
32// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
33
34template<class T>
36Foam::UList<T>::validateRange(const labelRange& requestedRange) const
37{
38 const labelRange range(requestedRange.subset0(this->size()));
39
40 #ifdef FULLDEBUG
41 this->checkStart(range.start());
42 this->checkSize(range.start() + range.size());
43 #endif
44
45 return range;
46}
47
48
49// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50
51template<class T>
52void Foam::UList<T>::moveFirst(const label i)
53{
54 checkIndex(i);
55
56 for (label lower = 0; lower < i; ++lower)
57 {
58 Foam::Swap(this->operator[](lower), this->operator[](i));
59 }
60}
61
62
63template<class T>
64void Foam::UList<T>::moveLast(const label i)
65{
66 checkIndex(i);
67
68 for (label upper = size()-1; upper > i; --upper)
69 {
70 Foam::Swap(this->operator[](i), this->operator[](upper));
71 }
72}
73
74
75template<class T>
76void Foam::UList<T>::swapFirst(const label i)
77{
78 checkIndex(i);
79
80 if (i > 0)
81 {
82 Foam::Swap(this->operator[](0), this->operator[](i));
83 }
84}
85
86
87template<class T>
88void Foam::UList<T>::swapLast(const label i)
89{
90 checkIndex(i);
91
92 const label upper = size()-1;
93
94 if (i < upper)
95 {
96 Foam::Swap(this->operator[](i), this->operator[](upper));
97 }
98}
99
100
101template<class T>
102void Foam::UList<T>::deepCopy(const UList<T>& list)
103{
104 if (this->size_ != list.size_)
105 {
107 << "Lists have different sizes: "
108 << this->size_ << " != " << list.size() << nl
109 << abort(FatalError);
110 }
111 else if (this->size_ > 0)
112 {
113 // Can dispatch with
114 // - std::execution::par_unseq
115 // - std::execution::unseq
116 std::copy(list.cbegin(), list.cend(), this->v_);
117 }
118}
119
120
121template<class T>
122template<class Addr>
123void Foam::UList<T>::deepCopy(const IndirectListBase<T, Addr>& list)
124{
125 if (this->size_ != list.size())
126 {
128 << "Lists have different sizes: "
129 << this->size_ << " != " << list.size() << nl
130 << abort(FatalError);
131 }
132 else if (this->size_)
133 {
134 // Copy the indirect list contents
135
136 // NB: operator[] for list read access (eg, an indirect list)
137 // cannot replace with std::copy
138
139 const label len = this->size_;
140
141 auto iter = this->v_;
142
143 for (label i = 0; i < len; (void)++i, (void)++iter)
144 {
145 *iter = list[i];
146 }
148}
149
150
151// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
152
153template<class T>
154std::streamsize Foam::UList<T>::byteSize() const
155{
156 if constexpr (!is_contiguous_v<T>)
157 {
159 << "Invalid for non-contiguous data types"
161 }
162 return this->size_bytes();
163}
164
165
166template<class T>
167Foam::label Foam::UList<T>::find(const T& val) const
169 const auto iter = std::find(this->cbegin(), this->cend(), val);
170 return (iter != this->cend() ? label(iter - this->cbegin()) : label(-1));
171}
172
173
174template<class T>
175Foam::label Foam::UList<T>::find(const T& val, label pos, label len) const
176{
177 if (pos >= 0 && pos < this->size())
178 {
179 // Change sub-length to (one-past) end position
180 // len == -1 (like std::string::npos) - search until end
181
182 if (len > 0) len += pos;
183 if (len < 0 || len > this->size())
184 {
185 len = this->size();
186 }
187
188 const auto iter = std::find
189 (
190 (this->cbegin() + pos),
191 (this->cbegin() + len),
192 val
193 );
194
195 if (iter != (this->cbegin() + len))
196 {
197 return label(iter - this->cbegin());
198 }
200
201 return -1;
202}
203
204
205template<class T>
206Foam::label Foam::UList<T>::rfind(const T& val, label pos) const
207{
208 // pos == -1 (like std::string::npos) - search from end
209
210 if (pos < 0 || pos >= this->size())
211 {
212 pos = this->size()-1;
213 }
214
215 while (pos >= 0)
216 {
217 if (this->v_[pos] == val)
218 {
219 return pos;
220 }
221
222 --pos;
223 }
224
225 return -1;
226}
227
228
229// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
230
231template<class T>
232bool Foam::UList<T>::operator==(const UList<T>& list) const
233{
234 // Can dispatch with
235 // - std::execution::par_unseq
236 // - std::execution::unseq
237 return
238 (
239 (this->size_ == list.size_)
240 && std::equal(this->cbegin(), this->cend(), list.cbegin())
241 );
242}
243
244
245template<class T>
247{
248 return !operator==(list);
249}
250
251
252template<class T>
253bool Foam::UList<T>::operator<(const UList<T>& list) const
254{
255 // Can dispatch with
256 // - std::execution::par_unseq
257 // - std::execution::unseq
258 return std::lexicographical_compare
259 (
260 this->cbegin(), this->cend(),
261 list.cbegin(), list.cend()
262 );
263}
264
265
266template<class T>
267bool Foam::UList<T>::operator>(const UList<T>& list) const
268{
269 return list.operator<(*this);
270}
271
272
273template<class T>
275{
276 return !list.operator<(*this);
277}
278
279
280template<class T>
281bool Foam::UList<T>::operator>=(const UList<T>& list) const
282{
283 return !operator<(list);
284}
285
286
287// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
288
289template<class T>
290void Foam::sort(UList<T>& list)
292 // With which std::execution:: ?
293 std::sort(list.begin(), list.end());
294}
295
296
297template<class T, class Compare>
298void Foam::sort(UList<T>& list, const Compare& comp)
300 // With which std::execution:: ?
301 std::sort(list.begin(), list.end(), comp);
302}
303
304
305template<class T>
306void Foam::stableSort(UList<T>& list)
308 // With which std::execution:: ?
309 std::stable_sort(list.begin(), list.end());
310}
311
312
313template<class T, class Compare>
314void Foam::stableSort(UList<T>& list, const Compare& comp)
316 // With which std::execution:: ?
317 std::stable_sort(list.begin(), list.end(), comp);
318}
319
320
321template<class T>
322void Foam::shuffle(UList<T>& list)
323{
324 std::shuffle(list.begin(), list.end(), std::default_random_engine());
325}
326
327
328// ************************************************************************* //
scalar range
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:89
void moveLast(const label i)
Move element to the last position.
Definition UList.C:57
void swapLast(const label i)
Swap element with the last element. Fatal on an empty list.
Definition UList.C:81
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
bool operator<(const UList< T > &list) const
Compare two ULists lexicographically. Takes linear time.
Definition UList.C:246
void checkIndex(const label i) const
Check index is within valid range [0,size).
Definition UListI.H:198
void swapFirst(const label i)
Swap element with the first element. Fatal on an empty list.
Definition UList.C:69
void deepCopy(const UList< T > &list)
Copy elements of the given UList. Sizes must match!
Definition UList.C:95
UList(const UList< T > &) noexcept=default
Copy construct, shallow copy.
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
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition UList.C:199
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition UListI.H:424
bool operator>=(const UList< T > &list) const
Return true if !(a < b). Takes linear time.
Definition UList.C:274
bool operator!=(const UList< T > &list) const
The opposite of the equality operation. Takes linear time.
Definition UList.C:239
std::streamsize byteSize() const
Number of contiguous bytes for the List data, runtime FatalError if type is not contiguous.
Definition UList.C:147
void checkSize(const label size) const
Check size is within valid range [0,size].
Definition UListI.H:155
bool operator>(const UList< T > &list) const
Compare two ULists lexicographically. Takes linear time.
Definition UList.C:260
void moveFirst(const label i)
Move element to the first position.
Definition UList.C:45
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
bool operator==(const UList< T > &list) const
Equality operation on ULists of the same type.
Definition UList.C:225
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition UListI.H:295
labelRange validateRange(const labelRange &requestedRange) const
Return a validated (start,size) subset range, which means that it always addresses a valid section of...
Definition UList.C:29
bool operator<=(const UList< T > &list) const
Return true if !(a > b). Takes linear time.
Definition UList.C:267
label find(const T &val) const
Find index of the first occurrence of the value.
Definition UList.C:160
void checkStart(const label start) const
Check start is within valid range [0,size).
Definition UListI.H:141
A range or interval of labels defined by a start and a size.
Definition labelRange.H:66
labelRange subset0(label len) const
Calculate the intersection with the given 0/size range.
Definition labelRangeI.H:87
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
dimensionedScalar pos(const dimensionedScalar &ds)
void shuffle(UList< T > &list)
Randomise the list order.
Definition UList.C:315
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
void sort(UList< T > &list)
Sort the list.
Definition UList.C:283
errorManip< error > abort(error &err)
Definition errorManip.H:139
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
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)
void stableSort(UList< T > &list)
Stable sort the list.
Definition UList.C:299
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50