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) 2011-2014 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 #include "pTraits.H"
28 #include "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 NullObjectRef<UList<T> >();
54 }
55 
56 
57 template<class T>
59 {
60  return (i == size()-1 ? 0 : i+1);
61 }
62 
63 
64 template<class T>
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>
118 {
119  return this->operator[](0);
120 }
121 
122 
123 template<class T>
124 inline const T& Foam::UList<T>::first() const
125 {
126  return this->operator[](0);
127 }
128 
129 
130 template<class T>
132 {
133  return this->operator[](this->size()-1);
134 }
135 
136 
137 template<class T>
138 inline const T& Foam::UList<T>::last() const
139 {
140  return this->operator[](this->size()-1);
141 }
142 
143 
144 template<class T>
145 inline const T* Foam::UList<T>::cdata() const
146 {
147  return v_;
148 }
149 
150 
151 template<class T>
153 {
154  return v_;
155 }
156 
157 
158 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
159 
160 
161 // element access
162 template<class T>
164 {
165 # ifdef FULLDEBUG
166  checkIndex(i);
167 # endif
168  return v_[i];
169 }
170 
171 
172 namespace Foam
173 {
174 
175  // Template specialization for bool
176  template<>
177  inline const bool& Foam::UList<bool>::operator[](const label i) const
178  {
179  // lazy evaluation - return false for out-of-range
180  if (i < size_)
181  {
182  return v_[i];
183  }
184  else
185  {
187  }
188  }
189 
190 } // end of namespace Foam
191 
192 
193 // const element access
194 template<class T>
195 inline const T& Foam::UList<T>::operator[](const label i) const
196 {
197 # ifdef FULLDEBUG
198  checkIndex(i);
199 # endif
200  return v_[i];
201 }
202 
203 
204 // Allow cast to a const List<T>&
205 template<class T>
207 {
208  return *reinterpret_cast<const List<T>*>(this);
209 }
210 
211 
212 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
213 
214 template<class T>
215 inline typename Foam::UList<T>::iterator
217 {
218  return v_;
219 }
220 
221 template<class T>
222 inline typename Foam::UList<T>::const_iterator
224 {
225  return v_;
226 }
227 
228 template<class T>
229 inline typename Foam::UList<T>::const_iterator
231 {
232  return v_;
233 }
234 
235 template<class T>
236 inline typename Foam::UList<T>::iterator
238 {
239  return &v_[size_];
240 }
241 
242 template<class T>
243 inline typename Foam::UList<T>::const_iterator
245 {
246  return &v_[size_];
247 }
248 
249 template<class T>
250 inline typename Foam::UList<T>::const_iterator
252 {
253  return &v_[size_];
254 }
255 
256 template<class T>
257 inline typename Foam::UList<T>::iterator
259 {
260  return &v_[size_-1];
261 }
262 
263 template<class T>
264 inline typename Foam::UList<T>::const_iterator
266 {
267  return &v_[size_-1];
268 }
269 
270 template<class T>
271 inline typename Foam::UList<T>::const_iterator
273 {
274  return &v_[size_-1];
275 }
276 
277 template<class T>
278 inline typename Foam::UList<T>::iterator
280 {
281  return &v_[-1];
282 }
283 
284 template<class T>
285 inline typename Foam::UList<T>::const_iterator
287 {
288  return &v_[-1];
289 }
290 
291 template<class T>
292 inline typename Foam::UList<T>::const_iterator
294 {
295  return &v_[-1];
296 }
297 
298 template<class T>
300 {
301  return size_;
302 }
303 
304 
305 template<class T>
307 {
308  return labelMax;
309 }
310 
311 
312 template<class T>
313 inline bool Foam::UList<T>::empty() const
314 {
315  return !size_;
316 }
317 
318 
319 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
320 
321 template<class T>
322 inline void Foam::reverse(UList<T>& ul, const label n)
323 {
324  for (int i=0; i<n/2; i++)
325  {
326  Swap(ul[i], ul[n-1-i]);
327  }
328 }
329 
330 template<class T>
331 inline void Foam::reverse(UList<T>& ul)
332 {
333  reverse(ul, ul.size());
334 }
335 
336 
337 // ************************************************************************* //
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:237
static const UList< T > & null()
Return a null UList.
Definition: UListI.H:51
UList()
Null constructor.
Definition: UListI.H:33
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const_iterator cend() const
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:251
bool empty() const
Return true if the UList is empty (ie, size() is zero).
Definition: UListI.H:313
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:272
T & last()
Return the last element of the list.
Definition: UListI.H:131
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:250
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: UListI.H:58
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1).
Definition: UListI.H:99
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:279
T & first()
Return the first element of the list.
Definition: UListI.H:117
Namespace for OpenFOAM.
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:152
void checkSize(const label size) const
Check size is within valid range (0 ... size).
Definition: UListI.H:86
label n
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
T & operator[](const label)
Return element of UList.
Definition: UListI.H:163
void checkStart(const label start) const
Check start is within valid range (0 ... size-1).
Definition: UListI.H:73
label max_size() const
Return size of the largest possible UList.
Definition: UListI.H:306
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Swap its arguments.
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:258
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void Swap(T &a, T &b)
Definition: Swap.H:43
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
const_iterator cbegin() const
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:230
Traits class for primitives.
Definition: pTraits.H:50
error FatalError
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
const T * cdata() const
Return a const pointer to the first data element,.
Definition: UListI.H:145
void reverse(UList< T > &, const label n)
Definition: UListI.H:322
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: UListI.H:65
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:293
const T * const_iterator
Random access iterator for traversing UList.
Definition: UList.H:262
static const label labelMax
Definition: label.H:62