FixedListI.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 |
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 "UList.H"
27 #include "SLList.H"
28 #include "contiguous.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class T, unsigned Size>
34 {}
35 
36 
37 template<class T, unsigned Size>
39 {
40  for (unsigned i=0; i<Size; i++)
41  {
42  v_[i] = v[i];
43  }
44 }
45 
46 
47 template<class T, unsigned Size>
49 {
50  for (unsigned i=0; i<Size; i++)
51  {
52  v_[i] = t;
53  }
54 }
55 
56 
57 template<class T, unsigned Size>
58 inline Foam::FixedList<T, Size>::FixedList(const UList<T>& lst)
59 {
60  checkSize(lst.size());
61 
62  for (unsigned i=0; i<Size; i++)
63  {
64  v_[i] = lst[i];
65  }
66 }
67 
68 
69 template<class T, unsigned Size>
70 inline Foam::FixedList<T, Size>::FixedList(const SLList<T>& lst)
71 {
72  checkSize(lst.size());
73 
74  label i = 0;
75  for
76  (
77  typename SLList<T>::const_iterator iter = lst.begin();
78  iter != lst.end();
79  ++iter
80  )
81  {
82  operator[](i++) = iter();
83  }
84 }
85 
86 
87 template<class T, unsigned Size>
88 inline Foam::FixedList<T, Size>::FixedList(const FixedList<T, Size>& lst)
89 {
90  for (unsigned i=0; i<Size; i++)
91  {
92  v_[i] = lst[i];
93  }
94 }
95 
96 
97 template<class T, unsigned Size>
100 {
101  return autoPtr< FixedList<T, Size> >(new FixedList<T, Size>(*this));
102 }
103 
104 
105 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106 
107 template<class T, unsigned Size>
109 {
110  return NullObjectRef<FixedList<T, Size> >();
111 }
112 
113 
114 template<class T, unsigned Size>
116 {
117  return (i == Size-1 ? 0 : i+1);
118 }
119 
120 
121 template<class T, unsigned Size>
123 {
124  return (i ? i-1 : Size-1);
125 }
126 
127 
128 // Check start is within valid range (0 ... size-1).
129 template<class T, unsigned Size>
130 inline void Foam::FixedList<T, Size>::checkStart(const label start) const
131 {
132  if (start < 0 || (start && unsigned(start) >= Size))
133  {
135  << "start " << start << " out of range 0 ... " << (Size-1)
136  << abort(FatalError);
137  }
138 }
139 
140 
141 // Check size is within valid range (0 ... size).
142 template<class T, unsigned Size>
143 inline void Foam::FixedList<T, Size>::checkSize(const label size) const
144 {
145  if (size < 0 || unsigned(size) > Size)
146  {
148  << "size " << size << " out of range 0 ... " << (Size)
149  << abort(FatalError);
150  }
151 }
152 
153 
154 // Check index i is within valid range (0 ... size-1)
155 // The check for zero-sized list is already done in static assert
156 template<class T, unsigned Size>
157 inline void Foam::FixedList<T, Size>::checkIndex(const label i) const
158 {
159  if (i < 0 || unsigned(i) >= Size)
160  {
162  << "index " << i << " out of range 0 ... " << (Size-1)
163  << abort(FatalError);
164  }
165 }
166 
167 
168 template<class T, unsigned Size>
170 {
171 # ifdef FULLDEBUG
172  checkSize(s);
173 # endif
174 }
175 
176 template<class T, unsigned Size>
178 {
179 # ifdef FULLDEBUG
180  checkSize(s);
181 # endif
182 }
183 
184 template<class T, unsigned Size>
186 {
187  for (unsigned i=0; i<Size; i++)
188  {
189  v_[i] = lst[i];
190  }
191 }
192 
193 
194 template<class T, unsigned Size>
195 inline const T*
197 {
198  return v_;
199 }
200 
201 
202 template<class T, unsigned Size>
203 inline T*
205 {
206  return v_;
207 }
208 
209 
210 template<class T, unsigned Size>
212 {
213  return v_[0];
214 }
215 
216 
217 template<class T, unsigned Size>
218 inline const T& Foam::FixedList<T, Size>::first() const
219 {
220  return v_[0];
221 }
222 
223 
224 template<class T, unsigned Size>
226 {
227  return v_[Size-1];
228 }
229 
230 
231 template<class T, unsigned Size>
232 inline const T& Foam::FixedList<T, Size>::last() const
233 {
234  return v_[Size-1];
235 }
236 
237 
238 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
239 
240 // element access
241 template<class T, unsigned Size>
243 {
244 # ifdef FULLDEBUG
245  checkIndex(i);
246 # endif
247  return v_[i];
248 }
249 
250 
251 // const element access
252 template<class T, unsigned Size>
253 inline const T& Foam::FixedList<T, Size>::operator[](const label i) const
254 {
255 # ifdef FULLDEBUG
256  checkIndex(i);
257 # endif
258  return v_[i];
259 }
260 
261 
262 template<class T, unsigned Size>
263 inline void Foam::FixedList<T, Size>::operator=(const T lst[Size])
264 {
265  for (unsigned i=0; i<Size; i++)
266  {
267  v_[i] = lst[i];
268  }
269 }
270 
271 template<class T, unsigned Size>
272 inline void Foam::FixedList<T, Size>::operator=(const UList<T>& lst)
273 {
274  checkSize(lst.size());
275 
276  for (unsigned i=0; i<Size; i++)
277  {
278  v_[i] = lst[i];
279  }
280 }
281 
282 template<class T, unsigned Size>
283 inline void Foam::FixedList<T, Size>::operator=(const SLList<T>& lst)
284 {
285  checkSize(lst.size());
286 
287  label i = 0;
288  for
289  (
290  typename SLList<T>::const_iterator iter = lst.begin();
291  iter != lst.end();
292  ++iter
293  )
294  {
295  operator[](i++) = iter();
296  }
297 }
298 
299 template<class T, unsigned Size>
300 inline void Foam::FixedList<T, Size>::operator=(const T& t)
301 {
302  for (unsigned i=0; i<Size; i++)
303  {
304  v_[i] = t;
305  }
306 }
307 
308 
309 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
310 
311 template<class T, unsigned Size>
314 {
315  return v_;
316 }
317 
318 
319 template<class T, unsigned Size>
322 {
323  return v_;
324 }
325 
326 
327 template<class T, unsigned Size>
330 {
331  return v_;
332 }
333 
334 
335 template<class T, unsigned Size>
338 {
339  return &v_[Size];
340 }
341 
342 
343 template<class T, unsigned Size>
346 {
347  return &v_[Size];
348 }
349 
350 
351 template<class T, unsigned Size>
354 {
355  return &v_[Size];
356 }
357 
358 
359 template<class T, unsigned Size>
362 {
363  return &v_[Size-1];
364 }
365 
366 
367 template<class T, unsigned Size>
370 {
371  return &v_[Size-1];
372 }
373 
374 
375 template<class T, unsigned Size>
378 {
379  return &v_[Size-1];
380 }
381 
382 
383 template<class T, unsigned Size>
386 {
387  return &v_[-1];
388 }
389 
390 
391 template<class T, unsigned Size>
394 {
395  return &v_[-1];
396 }
397 
398 
399 template<class T, unsigned Size>
402 {
403  return &v_[-1];
404 }
405 
406 
407 template<class T, unsigned Size>
409 {
410  return Size;
411 }
412 
413 
414 template<class T, unsigned Size>
416 {
417  return Size;
418 }
419 
420 
421 template<class T, unsigned Size>
423 {
424  return false;
425 }
426 
427 
428 template<class T, unsigned Size>
429 template<class HashT>
431 (
432  const FixedList<T, Size>& lst,
433  unsigned seed
434 ) const
435 {
436  if (contiguous<T>())
437  {
438  // hash directly
439  return Hasher(lst.v_, sizeof(lst.v_), seed);
440  }
441  else
442  {
443  // hash incrementally
444  unsigned val = seed;
445 
446  for (unsigned i=0; i<Size; i++)
447  {
448  val = HashT()(lst[i], val);
449  }
450 
451  return val;
452  }
453 }
454 
455 
456 // ************************************************************************* //
Foam::FixedList::clone
autoPtr< FixedList< T, Size > > clone() const
Clone.
Definition: FixedListI.H:99
Foam::FixedList::rend
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:385
Foam::FixedList::v_
T v_[Size]
Vector of values of type T of size Size.
Definition: FixedList.H:78
Foam::FixedList::checkIndex
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1).
Definition: FixedListI.H:157
Foam::Hasher
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins's 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:476
Foam::FixedList::operator[]
T & operator[](const label)
Return element of FixedList.
Foam::FixedList::checkStart
void checkStart(const label start) const
Check start is within valid range (0 ... size-1).
Definition: FixedListI.H:130
Foam::FixedList::cend
const_iterator cend() const
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:353
Foam::FixedList::cdata
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:196
Foam::FixedList::first
T & first()
Return the first element of the list.
Definition: FixedListI.H:211
Foam::FixedList::crbegin
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:377
Foam::FixedList::null
static const FixedList< T, Size > & null()
Return a null FixedList.
Definition: FixedListI.H:108
Foam::FixedList::fcIndex
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: FixedListI.H:115
Foam::FixedList::last
T & last()
Return the last element of the list.
Definition: FixedListI.H:225
Foam::FixedList::operator
friend Ostream & operator(Ostream &, const FixedList< T, Size > &)
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
Foam::FixedList::resize
void resize(const label)
Dummy resize function.
Definition: FixedListI.H:169
Foam::FixedList::max_size
label max_size() const
Return size of the largest possible FixedList.
Definition: FixedListI.H:415
Foam::FixedList::iterator
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:246
Foam::FixedList::end
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:337
Foam::FixedList::cbegin
const_iterator cbegin() const
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:329
Foam::FixedList::data
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:204
Foam::FatalError
error FatalError
Foam::FixedList::setSize
void setSize(const label)
Dummy setSize function.
Definition: FixedListI.H:177
Foam::FixedList::crend
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:401
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
s
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Foam::FixedList::transfer
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:185
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
T
const volScalarField & T
Definition: createFields.H:25
UList.H
Foam::FixedList::operator=
void operator=(const T v[Size])
Assignment from array operator. Takes linear time.
SLList.H
contiguous.H
Template function to specify if the data of a type are contiguous.
Foam::FixedList::size
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:408
Foam::FixedList
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:53
Foam::FixedList::empty
bool empty() const
Return true if the FixedList is empty (ie, size() is zero).
Definition: FixedListI.H:422
Foam::FixedList::begin
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:313
Foam::FixedList::rbegin
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:361
Foam::FixedList::checkSize
void checkSize(const label size) const
Check size is within valid range (0 ... size).
Definition: FixedListI.H:143
Foam::FixedList::rcIndex
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: FixedListI.H:122
Foam::FixedList::FixedList
FixedList()
Null constructor.
Definition: FixedListI.H:33