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-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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  {
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  {
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  {
104  << "attempt to access element " << i << " from zero sized list"
105  << abort(FatalError);
106  }
107  else if (i<0 || i>=size_)
108  {
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>
163 inline T& Foam::UList<T>::operator[](const label i)
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 // ************************************************************************* //
Foam::UList::crbegin
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:272
Foam::UList::cbegin
const_iterator cbegin() const
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:230
Foam::UList::operator[]
T & operator[](const label)
Return element of UList.
Foam::UList::cend
const_iterator cend() const
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:251
Foam::UList::first
T & first()
Return the first element of the list.
Definition: UListI.H:117
Foam::UList::max_size
label max_size() const
Return size of the largest possible UList.
Definition: UListI.H:306
Foam::labelMax
static const label labelMax
Definition: label.H:62
Foam::UList::data
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:152
Foam::UList::checkSize
void checkSize(const label size) const
Check size is within valid range (0 ... size).
Definition: UListI.H:86
Foam::UList::rcIndex
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: UListI.H:65
Foam::UList::rbegin
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:258
Foam::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
Foam::UList::crend
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:293
Swap.H
Swap its arguments.
Foam::UList::checkStart
void checkStart(const label start) const
Check start is within valid range (0 ... size-1).
Definition: UListI.H:73
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::label
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
error.H
Foam::UList::iterator
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:250
Foam::UList::last
T & last()
Return the last element of the list.
Definition: UListI.H:131
Foam::UList::rend
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:279
Foam::FatalError
error FatalError
pTraits.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::UList::UList
UList()
Null constructor.
Definition: UListI.H:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
T
const volScalarField & T
Definition: createFields.H:25
Foam::UList::fcIndex
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: UListI.H:58
Foam::List< T >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:50
Foam::UList< T >
Foam::UList::cdata
const T * cdata() const
Return a const pointer to the first data element,.
Definition: UListI.H:145
Foam::UList::empty
bool empty() const
Return true if the UList is empty (ie, size() is zero).
Definition: UListI.H:313
Foam::UList::null
static const UList< T > & null()
Return a null UList.
Definition: UListI.H:51
Foam::UList::end
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:237
Foam::UList::size
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
Foam::Swap
void Swap(T &a, T &b)
Definition: Swap.H:43
Foam::UList::checkIndex
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1).
Definition: UListI.H:99
Foam::reverse
void reverse(UList< T > &, const label n)
Definition: UListI.H:322