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-2020 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 
36 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
37 
38 template<class T>
40 {
41  const labelRange slice = range.subset0(this->size());
42 
43  #ifdef FULLDEBUG
44  this->checkStart(slice.start());
45  this->checkSize(slice.start() + slice.size());
46  #endif
47 
48  return slice;
49 }
50 
51 
52 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
53 
54 template<class T>
55 void Foam::UList<T>::moveFirst(const label i)
56 {
57  checkIndex(i);
58 
59  for (label lower = 0; lower < i; ++lower)
60  {
61  Foam::Swap(this->operator[](lower), this->operator[](i));
62  }
63 }
64 
65 
66 template<class T>
67 void Foam::UList<T>::moveLast(const label i)
68 {
69  checkIndex(i);
70 
71  for (label upper = size()-1; upper > i; --upper)
72  {
73  Foam::Swap(this->operator[](i), this->operator[](upper));
74  }
75 }
76 
77 
78 template<class T>
79 void Foam::UList<T>::swapFirst(const label i)
80 {
81  checkIndex(i);
82 
83  if (i > 0)
84  {
85  Foam::Swap(this->operator[](0), this->operator[](i));
86  }
87 }
88 
89 
90 template<class T>
91 void Foam::UList<T>::swapLast(const label i)
92 {
93  checkIndex(i);
94 
95  const label upper = size()-1;
96 
97  if (i < upper)
98  {
99  Foam::Swap(this->operator[](i), this->operator[](upper));
100  }
101 }
102 
103 
104 template<class T>
106 {
107  const label len = this->size_;
108 
109  if (len != list.size_)
110  {
112  << "ULists have different sizes: "
113  << len << " " << list.size_
114  << abort(FatalError);
115  }
116  else if (len)
117  {
118  #ifdef USEMEMCPY
120  {
121  std::memcpy
122  (
123  static_cast<void*>(this->v_), list.v_, this->byteSize()
124  );
125  }
126  else
127  #endif
128  {
129  List_ACCESS(T, (*this), lhs);
130  List_CONST_ACCESS(T, list, rhs);
131  for (label i = 0; i < len; ++i)
132  {
133  lhs[i] = rhs[i];
134  }
135  }
136  }
137 }
138 
139 
140 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
141 
142 template<class T>
144 {
145  const labelRange slice = validateRange(range);
146 
147  return UList<T>(&(this->v_[slice.start()]), slice.size()); // SubList
148 }
149 
150 
151 template<class T>
153 {
154  const labelRange slice = validateRange(range);
155 
156  return UList<T>(&(this->v_[slice.start()]), slice.size()); // SubList
157 }
158 
159 
160 template<class T>
161 void Foam::UList<T>::operator=(const T& val)
162 {
163  const label len = this->size();
164 
165  List_ACCESS(T, (*this), vp);
166 
167  for (label i=0; i < len; ++i)
168  {
169  vp[i] = val;
170  }
171 }
172 
173 
174 template<class T>
176 {
177  const label len = this->size();
178 
179  List_ACCESS(T, (*this), vp);
180 
181  for (label i=0; i < len; ++i)
182  {
183  vp[i] = Zero;
184  }
185 }
186 
187 
188 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
189 
190 template<class T>
191 std::streamsize Foam::UList<T>::byteSize() const
192 {
194  {
196  << "Cannot return binary size of a list with non-primitive elements"
197  << abort(FatalError);
198  }
199 
200  return this->size_*sizeof(T);
201 }
202 
203 
204 template<class T>
205 Foam::label Foam::UList<T>::find(const T& val, label pos) const
206 {
207  const label len = this->size();
208 
209  if (pos >= 0 && len)
210  {
211  List_CONST_ACCESS(T, (*this), list);
212 
213  while (pos < len)
214  {
215  if (list[pos] == val)
216  {
217  return pos;
218  }
219 
220  ++pos;
221  }
222  }
223 
224  return -1;
225 }
226 
227 
228 template<class T>
229 Foam::label Foam::UList<T>::rfind(const T& val, label pos) const
230 {
231  // pos == -1 has same meaning as std::string::npos - search from end
232  if (pos < 0 || pos >= this->size())
233  {
234  pos = this->size()-1;
235  }
236 
237  List_CONST_ACCESS(T, (*this), list);
238 
239  while (pos >= 0)
240  {
241  if (list[pos] == val)
242  {
243  return pos;
244  }
245 
246  --pos;
247  }
248 
249  return -1;
250 }
251 
252 
253 template<class T>
255 {
256  std::sort(a.begin(), a.end());
257 }
258 
259 
260 template<class T, class Compare>
261 void Foam::sort(UList<T>& a, const Compare& comp)
262 {
263  std::sort(a.begin(), a.end(), comp);
264 }
265 
266 
267 template<class T>
269 {
270  std::stable_sort(a.begin(), a.end());
271 }
272 
273 
274 template<class T, class Compare>
275 void Foam::stableSort(UList<T>& a, const Compare& comp)
276 {
277  std::stable_sort(a.begin(), a.end(), comp);
278 }
279 
280 
281 template<class T>
283 {
284  std::random_shuffle(a.begin(), a.end());
285 }
286 
287 
288 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
289 
290 template<class T>
291 bool Foam::UList<T>::operator==(const UList<T>& list) const
292 {
293  const label len = this->size_;
294  if (len != list.size_)
295  {
296  return false;
297  }
298 
299  bool equal = true;
300 
301  List_CONST_ACCESS(T, (*this), lhs);
302  List_CONST_ACCESS(T, (list), rhs);
303 
304  for (label i = 0; i < len; ++i)
305  {
306  equal = (lhs[i] == rhs[i]);
307  if (!equal) break;
308  }
309 
310  return equal;
311 }
312 
313 
314 template<class T>
315 bool Foam::UList<T>::operator!=(const UList<T>& list) const
316 {
317  return !operator==(list);
318 }
319 
320 
321 template<class T>
322 bool Foam::UList<T>::operator<(const UList<T>& list) const
323 {
324  for
325  (
326  const_iterator lhs = begin(), rhs = list.begin();
327  lhs < end() && rhs < list.end();
328  ++lhs, ++rhs
329  )
330  {
331  if (*lhs < *rhs)
332  {
333  return true;
334  }
335  else if (*rhs < *lhs)
336  {
337  return false;
338  }
339  }
340 
341  // Contents look to be identical, or lists have different sizes
342  return (this->size_ < list.size_);
343 }
344 
345 
346 template<class T>
347 bool Foam::UList<T>::operator>(const UList<T>& list) const
348 {
349  return list.operator<(*this);
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 !operator<(list);
364 }
365 
366 
367 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
368 
369 #include "UListIO.C"
370 
371 // ************************************************************************* //
Foam::UList::operator<
bool operator<(const UList< T > &list) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:322
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 &range) const
Definition: UList.C:39
Foam::UList::operator>=
bool operator>=(const UList< T > &a) const
Return true if !(a < b). Takes linear time.
Definition: UList.C:361
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:291
Foam::UList::operator<=
bool operator<=(const UList< T > &a) const
Return true if !(a > b). Takes linear time.
Definition: UList.C:354
Foam::UList::deepCopy
void deepCopy(const UList< T > &list)
Copy elements of the given UList.
Definition: UList.C:105
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::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:913
Foam::UList::swapFirst
void swapFirst(const label i)
Swap element with the first element. Fatal on an empty list.
Definition: UList.C:79
Foam::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:276
Foam::stableSort
void stableSort(UList< T > &a)
Definition: UList.C:268
UListIO.C
Foam::UList::rfind
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition: UList.C:229
Foam::stringOps::lower
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1186
Foam::IntRange::size
IntType size() const noexcept
The size of the range.
Definition: IntRangeI.H:415
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:91
Foam::UList::moveFirst
void moveFirst(const label i)
Move element to the first position.
Definition: UList.C:55
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:254
Foam::UList::operator!=
bool operator!=(const UList< T > &a) const
The opposite of the equality operation. Takes linear time.
Definition: UList.C:315
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::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:347
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
List_CONST_ACCESS
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:43
UList.H
range
scalar range
Definition: LISASMDCalcMethod1.H:12
labelRange.H
Foam::shuffle
void shuffle(UList< T > &a)
Definition: UList.C:282
contiguous.H
Foam::UList::operator[]
T & operator[](const label i)
Return element of UList.
Definition: UListI.H:246
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::UList::byteSize
std::streamsize byteSize() const
Definition: UList.C:191
Foam::operator<
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
Definition: IOstreamOption.H:397
Foam::stringOps::upper
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1202
Foam::UList::end
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:297
Foam::UList::moveLast
void moveLast(const label i)
Move element to the last position.
Definition: UList.C:67
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:205
Foam::IntRange::start
IntType start() const noexcept
The (inclusive) lower value of the range.
Definition: IntRangeI.H:408
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