FreeFOAM The Cross-Platform CFD Toolkit
UListI.H
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 #include <OpenFOAM/pTraits.H>
28 #include <OpenFOAM/Swap.H>
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class T>
34 :
35  size_(0),
36  v_(0)
37 {}
38 
39 
40 template<class T>
41 inline Foam::UList<T>::UList(T* __restrict__ v, label size)
42 :
43  size_(size),
44  v_(v)
45 {}
46 
47 
48 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49 
50 template<class T>
52 {
53  return *reinterpret_cast< UList<T>* >(0);
54 }
55 
56 
57 template<class T>
58 inline Foam::label Foam::UList<T>::fcIndex(const label i) const
59 {
60  return (i == size()-1 ? 0 : i+1);
61 }
62 
63 
64 template<class T>
65 inline Foam::label Foam::UList<T>::rcIndex(const label i) const
66 {
67  return (i ? i-1 : size()-1);
68 }
69 
70 
71 // Check start is within valid range (0 ... size-1).
72 template<class T>
73 inline void Foam::UList<T>::checkStart(const label start) const
74 {
75  if (start<0 || (start && start>=size_))
76  {
77  FatalErrorIn("UList<T>::checkStart(const label)")
78  << "start " << start << " out of range 0 ... " << max(size_-1, 0)
79  << abort(FatalError);
80  }
81 }
82 
83 
84 // Check size is within valid range (0 ... size).
85 template<class T>
86 inline void Foam::UList<T>::checkSize(const label size) const
87 {
88  if (size<0 || size>size_)
89  {
90  FatalErrorIn("UList<T>::checkSize(const label)")
91  << "size " << size << " out of range 0 ... " << size_
92  << abort(FatalError);
93  }
94 }
95 
96 
97 // Check index i is within valid range (0 ... size-1).
98 template<class T>
99 inline void Foam::UList<T>::checkIndex(const label i) const
100 {
101  if (!size_)
102  {
103  FatalErrorIn("UList<T>::checkIndex(const label)")
104  << "attempt to access element from zero sized list"
105  << abort(FatalError);
106  }
107  else if (i<0 || i>=size_)
108  {
109  FatalErrorIn("UList<T>::checkIndex(const label)")
110  << "index " << i << " out of range 0 ... " << size_-1
111  << abort(FatalError);
112  }
113 }
114 
115 
116 template<class T>
117 inline const T* Foam::UList<T>::cdata() const
118 {
119  return v_;
120 }
121 
122 
123 template<class T>
125 {
126  return v_;
127 }
128 
129 
130 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
131 
132 
133 // element access
134 template<class T>
135 inline T& Foam::UList<T>::operator[](const label i)
136 {
137 # ifdef FULLDEBUG
138  checkIndex(i);
139 # endif
140  return v_[i];
141 }
142 
143 
144 namespace Foam
145 {
146 
147  // Template specialization for bool
148  template<>
149  inline const bool& Foam::UList<bool>::operator[](const label i) const
150  {
151  // lazy evaluation - return false for out-of-range
152  if (i < size_)
153  {
154  return v_[i];
155  }
156  else
157  {
159  }
160  }
161 
162 } // end of namespace Foam
163 
164 
165 // const element access
166 template<class T>
167 inline const T& Foam::UList<T>::operator[](const label i) const
168 {
169 # ifdef FULLDEBUG
170  checkIndex(i);
171 # endif
172  return v_[i];
173 }
174 
175 
176 // Allow cast to a const List<T>&
177 template<class T>
179 {
180  return *reinterpret_cast<const List<T>*>(this);
181 }
182 
183 
184 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
185 
186 template<class T>
187 inline typename Foam::UList<T>::iterator
189 {
190  return v_;
191 }
192 
193 template<class T>
194 inline typename Foam::UList<T>::const_iterator
196 {
197  return v_;
198 }
199 
200 template<class T>
201 inline typename Foam::UList<T>::const_iterator
203 {
204  return v_;
205 }
206 
207 template<class T>
208 inline typename Foam::UList<T>::iterator
210 {
211  return &v_[size_];
212 }
213 
214 template<class T>
215 inline typename Foam::UList<T>::const_iterator
217 {
218  return &v_[size_];
219 }
220 
221 template<class T>
222 inline typename Foam::UList<T>::const_iterator
224 {
225  return &v_[size_];
226 }
227 
228 template<class T>
229 inline typename Foam::UList<T>::iterator
231 {
232  return &v_[size_-1];
233 }
234 
235 template<class T>
236 inline typename Foam::UList<T>::const_iterator
238 {
239  return &v_[size_-1];
240 }
241 
242 template<class T>
243 inline typename Foam::UList<T>::const_iterator
245 {
246  return &v_[size_-1];
247 }
248 
249 template<class T>
250 inline typename Foam::UList<T>::iterator
252 {
253  return &v_[-1];
254 }
255 
256 template<class T>
257 inline typename Foam::UList<T>::const_iterator
259 {
260  return &v_[-1];
261 }
262 
263 template<class T>
264 inline typename Foam::UList<T>::const_iterator
266 {
267  return &v_[-1];
268 }
269 
270 template<class T>
271 inline Foam::label Foam::UList<T>::size() const
272 {
273  return size_;
274 }
275 
276 
277 template<class T>
278 inline Foam::label Foam::UList<T>::max_size() const
279 {
280  return labelMax;
281 }
282 
283 
284 template<class T>
285 inline bool Foam::UList<T>::empty() const
286 {
287  return !size_;
288 }
289 
290 
291 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
292 
293 template<class T>
294 inline void Foam::reverse(UList<T>& ul, const label n)
295 {
296  for (int i=0; i<n/2; i++)
297  {
298  Swap(ul[i], ul[n-1-i]);
299  }
300 }
301 
302 template<class T>
303 inline void Foam::reverse(UList<T>& ul)
304 {
305  reverse(ul, ul.size());
306 }
307 
308 
309 // ************************ vim: set sw=4 sts=4 et: ************************ //