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