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-2021 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 <algorithm>
35 #include <random>
36 
37 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
38 
39 template<class T>
41 Foam::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 
56 template<class T>
57 void 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 
68 template<class T>
69 void 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 
80 template<class T>
81 void 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 
92 template<class T>
93 void 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 
106 template<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 
142 template<class T>
143 template<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 
168 template<class T>
169 void Foam::UList<T>::operator=(const T& val)
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 
182 template<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 
198 template<class T>
199 std::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 
211 template<class T>
212 Foam::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 
235 template<class T>
236 Foam::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 template<class T>
262 {
263  std::sort(a.begin(), a.end());
264 }
265 
266 
267 template<class T, class Compare>
268 void Foam::sort(UList<T>& a, const Compare& comp)
269 {
270  std::sort(a.begin(), a.end(), comp);
271 }
272 
273 
274 template<class T>
276 {
277  std::stable_sort(a.begin(), a.end());
278 }
279 
280 
281 template<class T, class Compare>
282 void Foam::stableSort(UList<T>& a, const Compare& comp)
283 {
284  std::stable_sort(a.begin(), a.end(), comp);
285 }
286 
287 
288 template<class T>
290 {
291  std::shuffle(a.begin(), a.end(), std::default_random_engine());
292 }
293 
294 
295 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
296 
297 template<class T>
298 bool Foam::UList<T>::operator==(const UList<T>& list) const
299 {
300  const label len = this->size_;
301  if (len != list.size_)
302  {
303  return false;
304  }
305 
306  bool equal = true;
307 
308  List_CONST_ACCESS(T, (*this), lhs);
309  List_CONST_ACCESS(T, (list), rhs);
310 
311  for (label i = 0; i < len; ++i)
312  {
313  equal = (lhs[i] == rhs[i]);
314  if (!equal) break;
315  }
316 
317  return equal;
318 }
319 
320 
321 template<class T>
322 bool Foam::UList<T>::operator!=(const UList<T>& list) const
323 {
324  return !operator==(list);
325 }
326 
327 
328 template<class T>
329 bool Foam::UList<T>::operator<(const UList<T>& list) const
330 {
331  for
332  (
333  const_iterator lhs = begin(), rhs = list.begin();
334  lhs < end() && rhs < list.end();
335  ++lhs, ++rhs
336  )
337  {
338  if (*lhs < *rhs)
339  {
340  return true;
341  }
342  else if (*rhs < *lhs)
343  {
344  return false;
345  }
346  }
347 
348  // Contents look to be identical, or lists have different sizes
349  return (this->size_ < list.size_);
350 }
351 
352 
353 template<class T>
354 bool Foam::UList<T>::operator>(const UList<T>& list) const
355 {
356  return list.operator<(*this);
357 }
358 
359 
360 template<class T>
361 bool Foam::UList<T>::operator<=(const UList<T>& list) const
362 {
363  return !list.operator<(*this);
364 }
365 
366 
367 template<class T>
368 bool Foam::UList<T>::operator>=(const UList<T>& list) const
369 {
370  return !operator<(list);
371 }
372 
373 
374 // ************************************************************************* //
Foam::UList::operator<
bool operator<(const UList< T > &list) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:329
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::UList::validateRange
labelRange validateRange(const labelRange &requestedRange) const
Definition: UList.C:41
Foam::UList::end
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
Foam::UList::operator>=
bool operator>=(const UList< T > &a) const
Return true if !(a < b). Takes linear time.
Definition: UList.C:368
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::UList::operator==
bool operator==(const UList< T > &a) const
Equality operation on ULists of the same type.
Definition: UList.C:298
Foam::UList::operator<=
bool operator<=(const UList< T > &a) const
Return true if !(a > b). Takes linear time.
Definition: UList.C:361
Foam::UList::deepCopy
void deepCopy(const UList< T > &list)
Copy elements of the given UList. Sizes must match!
Definition: UList.C:107
Foam::MatrixTools::equal
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:34
Foam::UList::swapFirst
void swapFirst(const label i)
Swap element with the first element. Fatal on an empty list.
Definition: UList.C:81
Foam::stableSort
void stableSort(UList< T > &a)
Definition: UList.C:275
Foam::UList::rfind
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition: UList.C:236
Foam::UList::begin
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
Foam::stringOps::lower
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1184
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::UList::swapLast
void swapLast(const label i)
Swap element with the last element. Fatal on an empty list.
Definition: UList.C:93
Foam::UList::moveFirst
void moveFirst(const label i)
Move element to the first position.
Definition: UList.C:57
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
Foam::UList::operator!=
bool operator!=(const UList< T > &a) const
The opposite of the equality operation. Takes linear time.
Definition: UList.C:322
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:55
Foam::FatalError
error FatalError
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::IndirectListBase::size
label size() const noexcept
The number of elements in the list.
Definition: IndirectListBase.H:125
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::UList::operator>
bool operator>(const UList< T > &a) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:354
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
List_CONST_ACCESS
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:43
UList.H
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::nl
constexpr char nl
Definition: Ostream.H:404
labelRange.H
Foam::shuffle
void shuffle(UList< T > &a)
Definition: UList.C:289
contiguous.H
Foam::UList::operator=
UList< T > & operator=(const UList< T > &)=delete
No copy assignment (default: shallow copy)
Foam::UList
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:103
Foam::labelRange::subset0
labelRange subset0(const label size) const
Calculate the intersection with the given 0/size range.
Definition: labelRange.C:182
Foam::UList::byteSize
std::streamsize byteSize() const
Definition: UList.C:199
Foam::operator<
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
Definition: IOstreamOption.H:398
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::stringOps::upper
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1200
Foam::UList::moveLast
void moveLast(const label i)
Move element to the last position.
Definition: UList.C:69
List_ACCESS
#define List_ACCESS(type, f, fp)
Definition: ListLoopM.H:39
Foam::UList::find
label find(const T &val, label pos=0) const
Find index of the first occurrence of the value.
Definition: UList.C:212
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75
ListLoopM.H
Macros for accessing List elements.
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177