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-2021 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 specialisation 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 
222 template<class T>
223 inline typename Foam::UList<T>::iterator
225 {
226  return &v_[size_];
227 }
228 
229 
230 template<class T>
231 inline typename Foam::UList<T>::const_iterator
233 {
234  return v_;
235 }
236 
237 
238 template<class T>
239 inline typename Foam::UList<T>::const_iterator
241 {
242  return &v_[size_];
243 }
244 
245 
246 template<class T>
247 inline typename Foam::UList<T>::const_iterator
249 {
250  return v_;
251 }
252 
253 
254 template<class T>
255 inline typename Foam::UList<T>::const_iterator
257 {
258  return &v_[size_];
259 }
260 
261 
262 template<class T>
263 inline typename Foam::UList<T>::reverse_iterator
265 {
266  return reverse_iterator(end());
267 }
268 
269 
270 template<class T>
271 inline typename Foam::UList<T>::reverse_iterator
273 {
274  return reverse_iterator(begin());
275 }
276 
277 
278 template<class T>
281 {
282  return const_reverse_iterator(cend());
283 }
284 
285 
286 template<class T>
289 {
290  return const_reverse_iterator(cbegin());
291 }
292 
293 
294 template<class T>
297 {
298  return const_reverse_iterator(cend());
299 }
300 
301 
302 template<class T>
305 {
306  return const_reverse_iterator(cbegin());
307 }
308 
309 
310 template<class T>
312 {
313  return size_;
314 }
315 
316 
317 template<class T>
319 {
320  return labelMax;
321 }
322 
323 
324 template<class T>
325 inline bool Foam::UList<T>::empty() const
326 {
327  return !size_;
328 }
329 
330 
331 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
332 
333 template<class T>
334 inline void Foam::reverse(UList<T>& ul, const label n)
335 {
336  for (int i=0; i<n/2; i++)
337  {
338  Swap(ul[i], ul[n-1-i]);
339  }
340 }
341 
342 template<class T>
343 inline void Foam::reverse(UList<T>& ul)
344 {
345  reverse(ul, ul.size());
346 }
347 
348 
349 // ************************************************************************* //
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator for reverse traversal of constant UList.
Definition: UList.H:311
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
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 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:323
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
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:280
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:232
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:224
T & first()
Return the first element of the list.
Definition: UListI.H:114
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:288
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:264
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:272
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
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator for reverse traversal of UList.
Definition: UList.H:299
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:318
void reverse(UList< T > &, const label n)
Definition: UListI.H:334
static const UList< T > & null()
Return a null UList.
Definition: UListI.H:51
const volScalarField & T
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:240
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:311
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.