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-2016 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] = t;
43  }
44 }
45 
46 
47 template<class T, unsigned Size>
49 {
50  for (unsigned i=0; i<Size; i++)
51  {
52  v_[i] = v[i];
53  }
54 }
55 
56 
57 template<class T, unsigned Size>
58 template<class InputIterator>
60 (
61  InputIterator first,
62  InputIterator last
63 )
64 {
65  checkSize(std::distance(first, last));
66 
67  InputIterator iter = first;
68  for (unsigned i=0; i<Size; i++)
69  {
70  v_[i] = *iter++;
71  }
72 }
73 
74 
75 template<class T, unsigned Size>
76 inline Foam::FixedList<T, Size>::FixedList(std::initializer_list<T> lst)
77 :
78  FixedList<T, Size>(lst.begin(), lst.end())
79 {}
80 
81 
82 template<class T, unsigned Size>
84 {
85  checkSize(lst.size());
86 
87  for (unsigned i=0; i<Size; i++)
88  {
89  v_[i] = lst[i];
90  }
91 }
92 
93 
94 template<class T, unsigned Size>
96 {
97  checkSize(lst.size());
98 
99  typename SLList<T>::const_iterator iter = lst.begin();
100  for (unsigned i=0; i<Size; i++)
101  {
102  v_[i] = *iter++;
103  }
104 }
105 
106 
107 template<class T, unsigned Size>
109 {
110  for (unsigned i=0; i<Size; i++)
111  {
112  v_[i] = lst[i];
113  }
114 }
115 
116 
117 template<class T, unsigned Size>
120 {
122 }
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
127 template<class T, unsigned Size>
129 {
130  return NullObjectRef<FixedList<T, Size>>();
131 }
132 
133 
134 template<class T, unsigned Size>
136 {
137  return (i == Size-1 ? 0 : i+1);
138 }
139 
140 
141 template<class T, unsigned Size>
143 {
144  return (i ? i-1 : Size-1);
145 }
146 
147 
148 template<class T, unsigned Size>
149 inline void Foam::FixedList<T, Size>::checkStart(const label start) const
150 {
151  if (start < 0 || (start && unsigned(start) >= Size))
152  {
154  << "start " << start << " out of range 0 ... " << (Size-1)
155  << abort(FatalError);
156  }
157 }
158 
159 
160 template<class T, unsigned Size>
161 inline void Foam::FixedList<T, Size>::checkSize(const label size) const
162 {
163  if (unsigned(size) != Size)
164  {
166  << "size " << size << " != " << Size
167  << abort(FatalError);
168  }
169 }
170 
171 
172 template<class T, unsigned Size>
173 inline void Foam::FixedList<T, Size>::checkIndex(const label i) const
174 {
175  if (i < 0 || unsigned(i) >= Size)
176  {
178  << "index " << i << " out of range 0 ... " << (Size-1)
179  << abort(FatalError);
180  }
181 }
182 
183 
184 template<class T, unsigned Size>
186 {
187  #ifdef FULLDEBUG
188  checkSize(s);
189  #endif
190 }
191 
192 template<class T, unsigned Size>
194 {
195  #ifdef FULLDEBUG
196  checkSize(s);
197  #endif
198 }
199 
200 template<class T, unsigned Size>
202 {
203  for (unsigned i=0; i<Size; i++)
204  {
205  v_[i] = lst[i];
206  }
207 }
208 
209 
210 template<class T, unsigned Size>
211 inline const T*
213 {
214  return v_;
215 }
216 
217 
218 template<class T, unsigned Size>
219 inline T*
221 {
222  return v_;
223 }
224 
225 
226 template<class T, unsigned Size>
228 {
229  return v_[0];
230 }
231 
232 
233 template<class T, unsigned Size>
234 inline const T& Foam::FixedList<T, Size>::first() const
235 {
236  return v_[0];
237 }
238 
239 
240 template<class T, unsigned Size>
242 {
243  return v_[Size-1];
244 }
245 
246 
247 template<class T, unsigned Size>
248 inline const T& Foam::FixedList<T, Size>::last() const
249 {
250  return v_[Size-1];
251 }
252 
253 
254 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
255 
256 template<class T, unsigned Size>
258 {
259  #ifdef FULLDEBUG
260  checkIndex(i);
261  #endif
262  return v_[i];
263 }
264 
265 
266 template<class T, unsigned Size>
267 inline const T& Foam::FixedList<T, Size>::operator[](const label i) const
268 {
269  #ifdef FULLDEBUG
270  checkIndex(i);
271  #endif
272  return v_[i];
273 }
274 
275 
276 template<class T, unsigned Size>
277 inline void Foam::FixedList<T, Size>::operator=(const T lst[Size])
278 {
279  for (unsigned i=0; i<Size; i++)
280  {
281  v_[i] = lst[i];
282  }
283 }
284 
285 template<class T, unsigned Size>
287 {
288  checkSize(lst.size());
289 
290  for (unsigned i=0; i<Size; i++)
291  {
292  v_[i] = lst[i];
293  }
294 }
295 
296 template<class T, unsigned Size>
298 {
299  checkSize(lst.size());
300 
301  typename SLList<T>::const_iterator iter = lst.begin();
302  for (unsigned i=0; i<Size; i++)
303  {
304  v_[i] = *iter++;
305  }
306 }
307 
308 template<class T, unsigned Size>
309 inline void Foam::FixedList<T, Size>::operator=(std::initializer_list<T> lst)
310 {
311  checkSize(lst.size());
312 
313  typename std::initializer_list<T>::iterator iter = lst.begin();
314  for (unsigned i=0; i<Size; i++)
315  {
316  v_[i] = *iter++;
317  }
318 }
319 
320 template<class T, unsigned Size>
322 {
323  for (unsigned i=0; i<Size; i++)
324  {
325  v_[i] = t;
326  }
327 }
328 
329 
330 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
331 
332 template<class T, unsigned Size>
335 {
336  return v_;
337 }
338 
339 
340 template<class T, unsigned Size>
343 {
344  return v_;
345 }
346 
347 
348 template<class T, unsigned Size>
351 {
352  return v_;
353 }
354 
355 
356 template<class T, unsigned Size>
359 {
360  return &v_[Size];
361 }
362 
363 
364 template<class T, unsigned Size>
367 {
368  return &v_[Size];
369 }
370 
371 
372 template<class T, unsigned Size>
375 {
376  return &v_[Size];
377 }
378 
379 
380 template<class T, unsigned Size>
383 {
384  return &v_[Size-1];
385 }
386 
387 
388 template<class T, unsigned Size>
391 {
392  return &v_[Size-1];
393 }
394 
395 
396 template<class T, unsigned Size>
399 {
400  return &v_[Size-1];
401 }
402 
403 
404 template<class T, unsigned Size>
407 {
408  return &v_[-1];
409 }
410 
411 
412 template<class T, unsigned Size>
415 {
416  return &v_[-1];
417 }
418 
419 
420 template<class T, unsigned Size>
423 {
424  return &v_[-1];
425 }
426 
427 
428 template<class T, unsigned Size>
430 {
431  return Size;
432 }
433 
434 
435 template<class T, unsigned Size>
437 {
438  return Size;
439 }
440 
441 
442 template<class T, unsigned Size>
444 {
445  return false;
446 }
447 
448 
449 template<class T, unsigned Size>
450 template<class HashT>
452 (
453  const FixedList<T, Size>& lst,
454  unsigned seed
455 ) const
456 {
457  if (contiguous<T>())
458  {
459  // Hash directly
460  return Hasher(lst.v_, sizeof(lst.v_), seed);
461  }
462  else
463  {
464  // Hash incrementally
465  unsigned val = seed;
466 
467  for (unsigned i=0; i<Size; i++)
468  {
469  val = HashT()(lst[i], val);
470  }
471 
472  return val;
473  }
474 }
475 
476 
477 // ************************************************************************* //
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:382
static const FixedList< T, Size > & null()
Return a null FixedList.
Definition: FixedListI.H:128
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
const_iterator cend() const
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:374
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:54
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:398
Template class for non-intrusive linked lists.
Definition: LList.H:51
An STL-conforming const_iterator.
Definition: LList.H:291
Template function to specify if the data of a type are contiguous.
friend Ostream & operator(Ostream &, const FixedList< T, Size > &)
Write FixedList to Ostream.
scalar distance(const vector &p1, const vector &p2)
Definition: curveTools.C:12
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1)
Definition: FixedListI.H:173
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:406
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:422
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))
void operator=(const T v[Size])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:277
const T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:276
T & operator[](const label)
Return element of FixedList.
Definition: FixedListI.H:257
void checkStart(const label start) const
Check start is within valid range (0 ... size-1)
Definition: FixedListI.H:149
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:201
T & first()
Return the first element of the list.
Definition: FixedListI.H:227
T & last()
Return the last element of the list.
Definition: FixedListI.H:241
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:61
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: FixedListI.H:135
void resize(const label)
Dummy resize function.
Definition: FixedListI.H:185
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:264
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:358
void checkSize(const label size) const
Check size is within valid range (0 ... size)
Definition: FixedListI.H:161
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:429
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:334
const_iterator cbegin() const
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:350
iterator begin()
Definition: LList.H:275
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: FixedListI.H:142
const volScalarField & T
label max_size() const
Return size of the largest possible FixedList.
Definition: FixedListI.H:436
FixedList()
Null constructor.
Definition: FixedListI.H:33
bool empty() const
Return true if the FixedList is empty (ie, size() is zero)
Definition: FixedListI.H:443
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins&#39;s 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:476
autoPtr< FixedList< T, Size > > clone() const
Clone.
Definition: FixedListI.H:119
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:220
void setSize(const label)
Dummy setSize function.
Definition: FixedListI.H:193
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
Non-intrusive singly-linked list.
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:212