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-2019 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>
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>
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>
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>
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>
162 {
163  List_ACCESS(T, (*this), vp);
164  List_FOR_ALL((*this), i)
165  {
166  vp[i] = val;
167  }
168 }
169 
170 
171 template<class T>
173 {
174  List_ACCESS(T, (*this), vp);
175  List_FOR_ALL((*this), i)
176  {
177  vp[i] = Zero;
178  }
179 }
180 
181 
182 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
183 
184 template<class T>
185 std::streamsize Foam::UList<T>::byteSize() const
186 {
188  {
190  << "Cannot return binary size of a list with non-primitive elements"
191  << abort(FatalError);
192  }
193 
194  return this->size_*sizeof(T);
195 }
196 
197 
198 template<class T>
200 {
201  const label len = this->size();
202 
203  if (start >= 0 && len)
204  {
205  List_CONST_ACCESS(T, (*this), vp);
206 
207  for (label i = start; i < len; ++i)
208  {
209  if (vp[i] == val)
210  {
211  return i;
212  }
213  }
214  }
215 
216  return -1;
217 }
218 
219 
220 template<class T>
222 {
223  List_CONST_ACCESS(T, (*this), vp);
224 
225  const label len1 = (this->size()-1);
226 
227  // pos == -1 has same meaning as std::string::npos - search from end
228  for (label i = ((pos >= 0 && pos < len1) ? pos : len1); i >= 0; --i)
229  {
230  if (vp[i] == val)
231  {
232  return i;
233  }
234  }
235 
236  return -1;
237 }
238 
239 
240 template<class T>
242 {
243  std::sort(a.begin(), a.end());
244 }
245 
246 
247 template<class T, class Compare>
248 void Foam::sort(UList<T>& a, const Compare& comp)
249 {
250  std::sort(a.begin(), a.end(), comp);
251 }
252 
253 
254 template<class T>
256 {
257  std::stable_sort(a.begin(), a.end());
258 }
259 
260 
261 template<class T, class Compare>
262 void Foam::stableSort(UList<T>& a, const Compare& comp)
263 {
264  std::stable_sort(a.begin(), a.end(), comp);
265 }
266 
267 
268 template<class T>
270 {
271  std::random_shuffle(a.begin(), a.end());
272 }
273 
274 
275 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
276 
277 template<class T>
278 bool Foam::UList<T>::operator==(const UList<T>& list) const
279 {
280  const label len = this->size_;
281  if (len != list.size_)
282  {
283  return false;
284  }
285 
286  bool equal = true;
287 
288  List_CONST_ACCESS(T, (*this), lhs);
289  List_CONST_ACCESS(T, (list), rhs);
290 
291  for (label i = 0; i < len; ++i)
292  {
293  equal = (lhs[i] == rhs[i]);
294  if (!equal) break;
295  }
296 
297  return equal;
298 }
299 
300 
301 template<class T>
302 bool Foam::UList<T>::operator!=(const UList<T>& list) const
303 {
304  return !operator==(list);
305 }
306 
307 
308 template<class T>
309 bool Foam::UList<T>::operator<(const UList<T>& list) const
310 {
311  for
312  (
313  const_iterator lhs = begin(), rhs = list.begin();
314  lhs < end() && rhs < list.end();
315  ++lhs, ++rhs
316  )
317  {
318  if (*lhs < *rhs)
319  {
320  return true;
321  }
322  else if (*rhs < *lhs)
323  {
324  return false;
325  }
326  }
327 
328  // Contents look to be identical, or lists have different sizes
329  return (this->size_ < list.size_);
330 }
331 
332 
333 template<class T>
334 bool Foam::UList<T>::operator>(const UList<T>& list) const
335 {
336  return list.operator<(*this);
337 }
338 
339 
340 template<class T>
341 bool Foam::UList<T>::operator<=(const UList<T>& list) const
342 {
343  return !list.operator<(*this);
344 }
345 
346 
347 template<class T>
348 bool Foam::UList<T>::operator>=(const UList<T>& list) const
349 {
350  return !operator<(list);
351 }
352 
353 
354 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
355 
356 #include "UListIO.C"
357 
358 // ************************************************************************* //
Foam::UList::operator<
bool operator<(const UList< T > &list) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:309
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:91
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:348
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::UList::operator==
bool operator==(const UList< T > &a) const
Equality operation on ULists of the same type.
Definition: UList.C:278
List_FOR_ALL
#define List_FOR_ALL(f, i)
Definition: ListLoopM.H:46
Foam::UList::operator<=
bool operator<=(const UList< T > &a) const
Return true if !(a > b). Takes linear time.
Definition: UList.C:341
Foam::stringOps::lower
string lower(const std::string &str)
Return string transformed with std::tolower on each character.
Definition: stringOps.C:1199
Foam::labelRange::size
label size() const noexcept
The effective size of the range.
Definition: labelRangeI.H:226
Foam::UList::find
label find(const T &val, const label start=0) const
Find index of the first occurrence of the value.
Definition: UList.C:199
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:909
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:255
UListIO.C
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::UList::rfind
label rfind(const T &val, const label pos=-1) const
Find index of the last occurrence of the value.
Definition: UList.C:221
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:241
Foam::UList::operator!=
bool operator!=(const UList< T > &a) const
The opposite of the equality operation. Takes linear time.
Definition: UList.C:302
Foam::stringOps::upper
string upper(const std::string &str)
Return string transformed with std::toupper on each character.
Definition: stringOps.C:1219
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
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:115
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
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:334
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
List_CONST_ACCESS
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:43
UList.H
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::operator<
bool operator<(const Instant< T1 > &a, const Instant< T2 > &b)
Compare instant values for less-than.
Definition: Instant.C:90
labelRange.H
Foam::shuffle
void shuffle(UList< T > &a)
Definition: UList.C:269
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::start
label ListType::const_reference const label start
Definition: ListOps.H:408
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::start
label start() const noexcept
The (inclusive) lower value of the range.
Definition: labelRangeI.H:232
Foam::UList::byteSize
std::streamsize byteSize() const
Definition: UList.C:185
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::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), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177