FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/error.H>
27 
28 #include "UList.H"
29 #include <OpenFOAM/ListLoopM.H>
30 #include <OpenFOAM/contiguous.H>
31 
32 #include <algorithm>
33 
34 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
35 
36 template<class T>
38 {
39  if (a.size_ != this->size_)
40  {
41  FatalErrorIn("UList<T>::assign(const UList<T>&)")
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  if (a.size_ != this->size_)
85  {
86  FatalErrorIn("UList<T>::swap(const UList<T>&)")
87  << "ULists have different sizes: "
88  << this->size_ << " " << a.size_
89  << abort(FatalError);
90  }
91 
92  List_ACCESS(T, (*this), vp);
93  List_ACCESS(T, a, ap);
94  T tmp;
95  List_FOR_ALL((*this), i)
96  tmp = List_ELEM((*this), vp, i);
97  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
98  List_ELEM(a, ap, i) = tmp;
100 }
101 
102 
103 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
104 
105 template<class T>
106 Foam::label Foam::UList<T>::byteSize() const
107 {
108  if (!contiguous<T>())
109  {
110  FatalErrorIn("UList<T>::byteSize()")
111  << "Cannot return the binary size of a list of "
112  "non-primitive elements"
113  << abort(FatalError);
114  }
115 
116  return this->size_*sizeof(T);
117 }
118 
119 
120 template<class T>
122 {
123  std::sort(a.begin(), a.end());
124 }
125 
126 
127 template<class T, class Cmp>
128 void Foam::sort(UList<T>& a, const Cmp& cmp)
129 {
130  std::sort(a.begin(), a.end(), cmp);
131 }
132 
133 
134 template<class T>
136 {
137  std::stable_sort(a.begin(), a.end());
138 }
139 
140 
141 template<class T, class Cmp>
142 void Foam::stableSort(UList<T>& a, const Cmp& cmp)
143 {
144  std::stable_sort(a.begin(), a.end(), cmp);
145 }
146 
147 
148 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
149 
150 template<class T>
152 {
153  if (this->size_ != a.size_)
154  {
155  return false;
156  }
157 
158  bool equal = true;
159 
160  List_CONST_ACCESS(T, (*this), vp);
161  List_CONST_ACCESS(T, (a), ap);
162 
163  List_FOR_ALL((*this), i)
164  equal = equal && (List_ELEM((*this), vp, i) == List_ELEM((a), ap, i));
166 
167  return equal;
168 }
169 
170 
171 template<class T>
173 {
174  return !operator==(a);
175 }
176 
177 
178 template<class T>
180 {
181  for
182  (
183  const_iterator vi = begin(), ai = a.begin();
184  vi < end() && ai < a.end();
185  vi++, ai++
186  )
187  {
188  if (*vi < *ai)
189  {
190  return true;
191  }
192  else if (*vi > *ai)
193  {
194  return false;
195  }
196  }
197 
198  if (this->size_ < a.size_)
199  {
200  return true;
201  }
202  else
203  {
204  return false;
205  }
206 }
207 
208 
209 template<class T>
211 {
212  return a.operator<(*this);
213 }
214 
215 
216 template<class T>
218 {
219  return !operator>(a);
220 }
221 
222 
223 template<class T>
225 {
226  return !operator<(a);
227 }
228 
229 
230 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
231 
232 #include "UListIO.C"
233 
234 // ************************ vim: set sw=4 sts=4 et: ************************ //