All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UListI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 template<class T>
72 inline void Foam::UList<T>::checkStart(const label start) const
73 {
74  if (start<0 || (start && start>=size_))
75  {
77  << "start " << start << " out of range 0 ... " << max(size_-1, 0)
78  << abort(FatalError);
79  }
80 }
81 
82 
83 template<class T>
84 inline void Foam::UList<T>::checkSize(const label size) const
85 {
86  if (size<0 || size>size_)
87  {
89  << "size " << size << " out of range 0 ... " << size_
90  << abort(FatalError);
91  }
92 }
93 
94 
95 template<class T>
96 inline void Foam::UList<T>::checkIndex(const label i) const
97 {
98  if (!size_)
99  {
101  << "attempt to access element from zero sized list"
102  << abort(FatalError);
103  }
104  else if (i<0 || i>=size_)
105  {
107  << "index " << i << " out of range 0 ... " << size_-1
108  << abort(FatalError);
109  }
110 }
111 
112 
113 template<class T>
115 {
116  return this->operator[](0);
117 }
118 
119 
120 template<class T>
121 inline const T& Foam::UList<T>::first() const
122 {
123  return this->operator[](0);
124 }
125 
126 
127 template<class T>
129 {
130  return this->operator[](this->size()-1);
131 }
132 
133 
134 template<class T>
135 inline const T& Foam::UList<T>::last() const
136 {
137  return this->operator[](this->size()-1);
138 }
139 
140 
141 template<class T>
142 inline const T* Foam::UList<T>::cdata() const
143 {
144  return v_;
145 }
146 
147 
148 template<class T>
150 {
151  return v_;
152 }
153 
154 
155 template<class T>
157 {
158  size_ = a.size_;
159  v_ = a.v_;
160 }
161 
162 
163 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
164 
165 
166 template<class T>
168 {
169  #ifdef FULLDEBUG
170  checkIndex(i);
171  #endif
172  return v_[i];
173 }
174 
175 
176 namespace Foam
177 {
178  // Template specialization for bool
179  template<>
180  inline const bool& Foam::UList<bool>::operator[](const label i) const
181  {
182  // lazy evaluation - return false for out-of-range
183  if (i < size_)
184  {
185  return v_[i];
186  }
187  else
188  {
190  }
191  }
192 }
193 
194 
195 template<class T>
196 inline const T& Foam::UList<T>::operator[](const label i) const
197 {
198  #ifdef FULLDEBUG
199  checkIndex(i);
200  #endif
201  return v_[i];
202 }
203 
204 
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 // ************************************************************************* //
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:313
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
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:279
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:258
error FatalError
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: UListI.H:65
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
const T * cdata() const
Return a const pointer to the first data element,.
Definition: UListI.H:142
UList()
Null constructor.
Definition: UListI.H:33
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: UListI.H:58
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
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:269
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:237
T & first()
Return the first element of the list.
Definition: UListI.H:114
void Swap(T &a, T &b)
Definition: Swap.H:43
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
static const label labelMax
Definition: label.H:62
errorManip< error > abort(error &err)
Definition: errorManip.H:131
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
label max_size() const
Return size of the largest possible UList.
Definition: UListI.H:306
void reverse(UList< T > &, const label n)
Definition: UListI.H:322
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:272
static const UList< T > & null()
Return a null UList.
Definition: UListI.H:51
const volScalarField & T
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:293
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:149
void shallowCopy(const UList< T > &)
Copy the pointer held by the given UList.
Definition: UListI.H:156
const T * const_iterator
Random access iterator for traversing UList.
Definition: UList.H:281
Swap its arguments.
void checkSize(const label size) const
Check size is within valid range (0 ... size)
Definition: UListI.H:84
label n
const_iterator cend() const
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:251
T & last()
Return the last element of the list.
Definition: UListI.H:128
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
void checkStart(const label start) const
Check start is within valid range (0 ... size-1)
Definition: UListI.H:72
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1)
Definition: UListI.H:96
Namespace for OpenFOAM.