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