UList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "UList.H"
27 #include "ListLoopM.H"
28 #include "contiguous.H"
29 
30 #include <algorithm>
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 template<class T>
36 {
37  if (a.size_ != this->size_)
38  {
40  << "ULists have different sizes: "
41  << this->size_ << " " << a.size_
42  << abort(FatalError);
43  }
44 
45  if (this->size_)
46  {
47  #ifdef USEMEMCPY
48  if (contiguous<T>())
49  {
50  memcpy(this->v_, a.v_, this->byteSize());
51  }
52  else
53  #endif
54  {
55  List_ACCESS(T, (*this), vp);
56  List_CONST_ACCESS(T, a, ap);
57  List_FOR_ALL((*this), i)
58  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
60  }
61  }
62 }
63 
64 
65 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
66 
67 template<class T>
69 {
70  List_ACCESS(T, (*this), vp);
71  List_FOR_ALL((*this), i)
72  List_ELEM((*this), vp, i) = t;
74 }
75 
76 
77 template<class T>
79 {
80  List_ACCESS(T, (*this), vp);
81  List_FOR_ALL((*this), i)
82  List_ELEM((*this), vp, i) = Zero;
84 }
85 
86 
87 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
88 
89 template<class T>
91 {
92  Swap(size_, a.size_);
93  Swap(v_, a.v_);
94 }
95 
96 
97 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
98 
99 template<class T>
100 std::streamsize Foam::UList<T>::byteSize() const
101 {
102  if (!contiguous<T>())
103  {
105  << "Cannot return the binary size of a list of "
106  "non-primitive elements"
107  << abort(FatalError);
108  }
109 
110  return this->size_*sizeof(T);
111 }
112 
113 
114 template<class T>
116 {
117  std::sort(a.begin(), a.end());
118 }
119 
120 
121 template<class T, class Cmp>
122 void Foam::sort(UList<T>& a, const Cmp& cmp)
123 {
124  std::sort(a.begin(), a.end(), cmp);
125 }
126 
127 
128 template<class T>
130 {
131  std::stable_sort(a.begin(), a.end());
132 }
133 
134 
135 template<class T, class Cmp>
136 void Foam::stableSort(UList<T>& a, const Cmp& cmp)
137 {
138  std::stable_sort(a.begin(), a.end(), cmp);
139 }
140 
141 
142 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
143 
144 template<class T>
146 {
147  if (this->size_ != a.size_)
148  {
149  return false;
150  }
151 
152  bool equal = true;
153 
154  List_CONST_ACCESS(T, (*this), vp);
155  List_CONST_ACCESS(T, (a), ap);
156 
157  List_FOR_ALL((*this), i)
158  equal = equal && (List_ELEM((*this), vp, i) == List_ELEM((a), ap, i));
160 
161  return equal;
162 }
163 
164 
165 template<class T>
167 {
168  return !operator==(a);
169 }
170 
171 
172 template<class T>
174 {
175  for
176  (
177  const_iterator vi = begin(), ai = a.begin();
178  vi < end() && ai < a.end();
179  vi++, ai++
180  )
181  {
182  if (*vi < *ai)
183  {
184  return true;
185  }
186  else if (*vi > *ai)
187  {
188  return false;
189  }
190  }
191 
192  if (this->size_ < a.size_)
193  {
194  return true;
195  }
196  else
197  {
198  return false;
199  }
200 }
201 
202 
203 template<class T>
205 {
206  return a.operator<(*this);
207 }
208 
209 
210 template<class T>
212 {
213  return !operator>(a);
214 }
215 
216 
217 template<class T>
219 {
220  return !operator<(a);
221 }
222 
223 
224 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
225 
226 #include "UListIO.C"
227 
228 // ************************************************************************* //
List<T> is a 1D vector of objects of type T, where the size of the vector is known and used for subsc...
#define List_END_FOR_ALL
Definition: ListLoopM.H:67
#define List_ACCESS(type, f, fp)
Definition: ListLoopM.H:75
#define List_ELEM(f, fp, i)
Definition: ListLoopM.H:73
#define List_FOR_ALL(f, i)
Definition: ListLoopM.H:62
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:78
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
void deepCopy(const UList< T > &)
Copy elements of the given UList.
Definition: UList.C:35
bool operator<(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:173
const T * const_iterator
Random access iterator for traversing UList.
Definition: UList.H:284
bool operator>=(const UList< T > &) const
Return true if !(a < b). Takes linear time.
Definition: UList.C:218
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
bool operator>(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:204
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
void swap(UList< T > &)
Swap two ULists of the same type in constant time.
Definition: UList.C:90
bool operator==(const UList< T > &) const
Equality operation on ULists of the same type.
Definition: UList.C:145
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:224
bool operator!=(const UList< T > &) const
The opposite of the equality operation. Takes linear time.
Definition: UList.C:166
friend Ostream & operator(Ostream &, const UList< T > &)
bool operator<=(const UList< T > &) const
Return true if !(a > b). Takes linear time.
Definition: UList.C:211
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:50
Template function to specify if the data of a type are contiguous.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
static const zero Zero
Definition: zero.H:97
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:62
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
bool operator<(const instant &, const instant &)
Definition: instant.C:79
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void sort(UList< T > &)
Definition: UList.C:115
bool operator>(const instant &, const instant &)
Definition: instant.C:85
error FatalError
void Swap(T &a, T &b)
Definition: Swap.H:43
void stableSort(UList< T > &)
Definition: UList.C:129
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)