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 | Copyright (C) 2011-2015 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 "error.H"
27 
28 #include "UList.H"
29 #include "ListLoopM.H"
30 #include "contiguous.H"
31 
32 #include <algorithm>
33 
34 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
35 
36 template<class T>
38 {
39  if (a.size_ != this->size_)
40  {
42  << "ULists have different sizes: "
43  << this->size_ << " " << a.size_
44  << abort(FatalError);
45  }
46 
47  if (this->size_)
48  {
49  #ifdef USEMEMCPY
50  if (contiguous<T>())
51  {
52  memcpy(this->v_, a.v_, this->byteSize());
53  }
54  else
55  #endif
56  {
57  List_ACCESS(T, (*this), vp);
58  List_CONST_ACCESS(T, a, ap);
59  List_FOR_ALL((*this), i)
60  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
62  }
63  }
64 }
65 
66 
67 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
68 
69 template<class T>
71 {
72  List_ACCESS(T, (*this), vp);
73  List_FOR_ALL((*this), i)
74  List_ELEM((*this), vp, i) = t;
76 }
77 
78 
79 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
80 
81 template<class T>
83 {
84  Swap(size_, a.size_);
85  Swap(v_, a.v_);
86 }
87 
88 
89 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90 
91 template<class T>
92 std::streamsize Foam::UList<T>::byteSize() const
93 {
94  if (!contiguous<T>())
95  {
97  << "Cannot return the binary size of a list of "
98  "non-primitive elements"
99  << abort(FatalError);
100  }
101 
102  return this->size_*sizeof(T);
103 }
104 
105 
106 template<class T>
108 {
109  std::sort(a.begin(), a.end());
110 }
111 
112 
113 template<class T, class Cmp>
114 void Foam::sort(UList<T>& a, const Cmp& cmp)
115 {
116  std::sort(a.begin(), a.end(), cmp);
117 }
118 
119 
120 template<class T>
122 {
123  std::stable_sort(a.begin(), a.end());
124 }
125 
126 
127 template<class T, class Cmp>
128 void Foam::stableSort(UList<T>& a, const Cmp& cmp)
129 {
130  std::stable_sort(a.begin(), a.end(), cmp);
131 }
132 
133 
134 template<class T>
136 {
137  std::random_shuffle(a.begin(), a.end());
138 }
139 
140 
141 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
142 
143 template<class T>
145 {
146  if (this->size_ != a.size_)
147  {
148  return false;
149  }
150 
151  bool equal = true;
152 
153  List_CONST_ACCESS(T, (*this), vp);
154  List_CONST_ACCESS(T, (a), ap);
155 
156  List_FOR_ALL((*this), i)
157  equal = equal && (List_ELEM((*this), vp, i) == List_ELEM((a), ap, i));
159 
160  return equal;
161 }
162 
163 
164 template<class T>
166 {
167  return !operator==(a);
168 }
169 
170 
171 template<class T>
173 {
174  for
175  (
176  const_iterator vi = begin(), ai = a.begin();
177  vi < end() && ai < a.end();
178  vi++, ai++
179  )
180  {
181  if (*vi < *ai)
182  {
183  return true;
184  }
185  else if (*vi > *ai)
186  {
187  return false;
188  }
189  }
190 
191  if (this->size_ < a.size_)
192  {
193  return true;
194  }
195  else
196  {
197  return false;
198  }
199 }
200 
201 
202 template<class T>
204 {
205  return a.operator<(*this);
206 }
207 
208 
209 template<class T>
211 {
212  return !operator>(a);
213 }
214 
215 
216 template<class T>
218 {
219  return !operator<(a);
220 }
221 
222 
223 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
224 
225 #include "UListIO.C"
226 
227 // ************************************************************************* //
Foam::UList::swap
void swap(UList< T > &)
Swap two ULists of the same type in constant time.
Definition: UList.C:82
Foam::UList::operator<=
bool operator<=(const UList< T > &) const
Return true if !(a > b). Takes linear time.
Definition: UList.C:210
List_FOR_ALL
#define List_FOR_ALL(f, i)
Definition: ListLoopM.H:62
Foam::UList::operator>=
bool operator>=(const UList< T > &) const
Return true if !(a < b). Takes linear time.
Definition: UList.C:217
Foam::UList::v_
T *__restrict__ v_
Vector of values of type T.
Definition: UList.H:78
Foam::operator==
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
Foam::shuffle
void shuffle(UList< T > &)
Definition: UList.C:135
Foam::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
List_END_FOR_ALL
#define List_END_FOR_ALL
Definition: ListLoopM.H:67
UListIO.C
Foam::stableSort
void stableSort(UList< T > &)
Definition: UList.C:121
Foam::UList::operator!=
bool operator!=(const UList< T > &) const
The opposite of the equality operation. Takes linear time.
Definition: UList.C:165
error.H
Foam::UList::assign
void assign(const UList< T > &)
Assign elements to those from UList.
Definition: UList.C:37
List_ELEM
#define List_ELEM(f, fp, i)
Definition: ListLoopM.H:73
Foam::FatalError
error FatalError
Foam::UList::operator==
bool operator==(const UList< T > &) const
Equality operation on ULists of the same type.
Definition: UList.C:144
operator>
Raster operator>(const Raster &rast, const double value)
Definition: Raster.C:624
Foam::UList::operator<
bool operator<(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:172
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::UList::operator>
bool operator>(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:203
Foam::UList::size_
label size_
Number of elements in UList.
Definition: UList.H:75
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
List_CONST_ACCESS
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:78
T
const volScalarField & T
Definition: createFields.H:25
operator<
Raster operator<(const Raster &rast, const double value)
Definition: Raster.C:610
UList.H
contiguous.H
Template function to specify if the data of a type are contiguous.
Foam::UList< T >
Foam::UList::byteSize
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:92
Foam::sort
void sort(UList< T > &)
Definition: UList.C:107
Foam::UList::end
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:237
Foam::Swap
void Swap(T &a, T &b)
Definition: Swap.H:43
List_ACCESS
#define List_ACCESS(type, f, fp)
Definition: ListLoopM.H:75
Foam::equal
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:78
Foam::UList::operator
friend Ostream & operator(Ostream &, const UList< T > &)
Foam::UList::operator=
void operator=(const T &)
Assignment of all entries to the given value.
Definition: UList.C:70
ListLoopM.H
List<T> is a 1D vector of objects of type T, where the size of the vector is known and used for subsc...