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-2018 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 template<class T>
144 {
145  std::random_shuffle(a.begin(), a.end());
146 }
147 
148 
149 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
150 
151 template<class T>
153 {
154  if (this->size_ != a.size_)
155  {
156  return false;
157  }
158 
159  bool equal = true;
160 
161  List_CONST_ACCESS(T, (*this), vp);
162  List_CONST_ACCESS(T, (a), ap);
163 
164  List_FOR_ALL((*this), i)
165  equal = equal && (List_ELEM((*this), vp, i) == List_ELEM((a), ap, i));
167 
168  return equal;
169 }
170 
171 
172 template<class T>
174 {
175  return !operator==(a);
176 }
177 
178 
179 template<class T>
181 {
182  for
183  (
184  const_iterator vi = begin(), ai = a.begin();
185  vi < end() && ai < a.end();
186  vi++, ai++
187  )
188  {
189  if (*vi < *ai)
190  {
191  return true;
192  }
193  else if (*vi > *ai)
194  {
195  return false;
196  }
197  }
198 
199  if (this->size_ < a.size_)
200  {
201  return true;
202  }
203  else
204  {
205  return false;
206  }
207 }
208 
209 
210 template<class T>
212 {
213  return a.operator<(*this);
214 }
215 
216 
217 template<class T>
219 {
220  return !operator>(a);
221 }
222 
223 
224 template<class T>
226 {
227  return !operator<(a);
228 }
229 
230 
231 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
232 
233 #include "UListIO.C"
234 
235 // ************************************************************************* //
void shuffle(UList< T > &)
Definition: UList.C:143
bool operator!=(const UList< T > &) const
The opposite of the equality operation. Takes linear time.
Definition: UList.C:173
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:78
friend Ostream & operator(Ostream &, const UList< T > &)
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
bool operator<(const instant &, const instant &)
Definition: instant.C:79
Template function to specify if the data of a type are contiguous.
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:224
bool operator<=(const UList< T > &) const
Return true if !(a > b). Takes linear time.
Definition: UList.C:218
void deepCopy(const UList< T > &)
Copy elements of the given UList.
Definition: UList.C:35
bool operator==(const UList< T > &) const
Equality operation on ULists of the same type.
Definition: UList.C:152
void Swap(T &a, T &b)
Definition: Swap.H:43
#define List_END_FOR_ALL
Definition: ListLoopM.H:67
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
bool operator>(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:211
void sort(UList< T > &)
Definition: UList.C:115
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
static const zero Zero
Definition: zero.H:97
errorManip< error > abort(error &err)
Definition: errorManip.H:131
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
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:60
void swap(UList< T > &)
Swap two ULists of the same type in constant time.
Definition: UList.C:90
bool operator>=(const UList< T > &) const
Return true if !(a < b). Takes linear time.
Definition: UList.C:225
bool operator>(const instant &, const instant &)
Definition: instant.C:85
List<T> is a 1D vector of objects of type T, where the size of the vector is known and used for subsc...
bool operator<(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:180
#define List_ELEM(f, fp, i)
Definition: ListLoopM.H:73
#define List_ACCESS(type, f, fp)
Definition: ListLoopM.H:75
const volScalarField & T
#define List_FOR_ALL(f, i)
Definition: ListLoopM.H:62
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:62
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:49
void stableSort(UList< T > &)
Definition: UList.C:129