FixedListI.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-2019 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>
110 {
112 }
113 
114 
115 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
116 
117 template<class T, unsigned Size>
119 {
120  return NullObjectRef<FixedList<T, Size>>();
121 }
122 
123 
124 template<class T, unsigned Size>
126 {
127  return (i == Size-1 ? 0 : i+1);
128 }
129 
130 
131 template<class T, unsigned Size>
133 {
134  return (i ? i-1 : Size-1);
135 }
136 
137 
138 template<class T, unsigned Size>
139 inline void Foam::FixedList<T, Size>::checkStart(const label start) const
140 {
141  if (start < 0 || (start && unsigned(start) >= Size))
142  {
144  << "start " << start << " out of range 0 ... " << (Size-1)
145  << abort(FatalError);
146  }
147 }
148 
149 
150 template<class T, unsigned Size>
151 inline void Foam::FixedList<T, Size>::checkSize(const label size) const
152 {
153  if (unsigned(size) != Size)
154  {
156  << "size " << size << " != " << Size
157  << abort(FatalError);
158  }
159 }
160 
161 
162 template<class T, unsigned Size>
163 inline void Foam::FixedList<T, Size>::checkIndex(const label i) const
164 {
165  if (i < 0 || unsigned(i) >= Size)
166  {
168  << "index " << i << " out of range 0 ... " << (Size-1)
169  << abort(FatalError);
170  }
171 }
172 
173 
174 template<class T, unsigned Size>
176 {
177  #ifdef FULLDEBUG
178  checkSize(s);
179  #endif
180 }
181 
182 template<class T, unsigned Size>
184 {
185  #ifdef FULLDEBUG
186  checkSize(s);
187  #endif
188 }
189 
190 template<class T, unsigned Size>
192 {
193  for (unsigned i=0; i<Size; i++)
194  {
195  v_[i] = lst[i];
196  }
197 }
198 
199 
200 template<class T, unsigned Size>
201 inline const T*
203 {
204  return v_;
205 }
206 
207 
208 template<class T, unsigned Size>
209 inline T*
211 {
212  return v_;
213 }
214 
215 
216 template<class T, unsigned Size>
218 {
219  return v_[0];
220 }
221 
222 
223 template<class T, unsigned Size>
224 inline const T& Foam::FixedList<T, Size>::first() const
225 {
226  return v_[0];
227 }
228 
229 
230 template<class T, unsigned Size>
232 {
233  return v_[Size-1];
234 }
235 
236 
237 template<class T, unsigned Size>
238 inline const T& Foam::FixedList<T, Size>::last() const
239 {
240  return v_[Size-1];
241 }
242 
243 
244 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
245 
246 template<class T, unsigned Size>
248 {
249  #ifdef FULLDEBUG
250  checkIndex(i);
251  #endif
252  return v_[i];
253 }
254 
255 
256 template<class T, unsigned Size>
257 inline const T& Foam::FixedList<T, Size>::operator[](const label i) const
258 {
259  #ifdef FULLDEBUG
260  checkIndex(i);
261  #endif
262  return v_[i];
263 }
264 
265 
266 template<class T, unsigned Size>
267 inline void Foam::FixedList<T, Size>::operator=(const T lst[Size])
268 {
269  for (unsigned i=0; i<Size; i++)
270  {
271  v_[i] = lst[i];
272  }
273 }
274 
275 template<class T, unsigned Size>
277 {
278  checkSize(lst.size());
279 
280  for (unsigned i=0; i<Size; i++)
281  {
282  v_[i] = lst[i];
283  }
284 }
285 
286 template<class T, unsigned Size>
288 {
289  checkSize(lst.size());
290 
291  typename SLList<T>::const_iterator iter = lst.begin();
292  for (unsigned i=0; i<Size; i++)
293  {
294  v_[i] = *iter++;
295  }
296 }
297 
298 template<class T, unsigned Size>
299 inline void Foam::FixedList<T, Size>::operator=(std::initializer_list<T> lst)
300 {
301  checkSize(lst.size());
302 
303  typename std::initializer_list<T>::iterator iter = lst.begin();
304  for (unsigned i=0; i<Size; i++)
305  {
306  v_[i] = *iter++;
307  }
308 }
309 
310 template<class T, unsigned Size>
312 {
313  for (unsigned i=0; i<Size; i++)
314  {
315  v_[i] = t;
316  }
317 }
318 
319 
320 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
321 
322 template<class T, unsigned Size>
325 {
326  return v_;
327 }
328 
329 
330 template<class T, unsigned Size>
333 {
334  return v_;
335 }
336 
337 
338 template<class T, unsigned Size>
341 {
342  return v_;
343 }
344 
345 
346 template<class T, unsigned Size>
349 {
350  return &v_[Size];
351 }
352 
353 
354 template<class T, unsigned Size>
357 {
358  return &v_[Size];
359 }
360 
361 
362 template<class T, unsigned Size>
365 {
366  return &v_[Size];
367 }
368 
369 
370 template<class T, unsigned Size>
373 {
374  return &v_[Size-1];
375 }
376 
377 
378 template<class T, unsigned Size>
381 {
382  return &v_[Size-1];
383 }
384 
385 
386 template<class T, unsigned Size>
389 {
390  return &v_[Size-1];
391 }
392 
393 
394 template<class T, unsigned Size>
397 {
398  return &v_[-1];
399 }
400 
401 
402 template<class T, unsigned Size>
405 {
406  return &v_[-1];
407 }
408 
409 
410 template<class T, unsigned Size>
413 {
414  return &v_[-1];
415 }
416 
417 
418 template<class T, unsigned Size>
420 {
421  return Size;
422 }
423 
424 
425 template<class T, unsigned Size>
427 {
428  return Size;
429 }
430 
431 
432 template<class T, unsigned Size>
434 {
435  return false;
436 }
437 
438 
439 template<class T, unsigned Size>
440 template<class HashT>
442 (
443  const FixedList<T, Size>& lst,
444  unsigned seed
445 ) const
446 {
447  if (contiguous<T>())
448  {
449  // Hash directly
450  return Hasher(lst.v_, sizeof(lst.v_), seed);
451  }
452  else
453  {
454  // Hash incrementally
455  unsigned val = seed;
456 
457  for (unsigned i=0; i<Size; i++)
458  {
459  val = HashT()(lst[i], val);
460  }
461 
462  return val;
463  }
464 }
465 
466 
467 // ************************************************************************* //
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:372
static const FixedList< T, Size > & null()
Return a null FixedList.
Definition: FixedListI.H:118
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:364
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:388
Template class for non-intrusive linked lists.
Definition: LList.H:51
An STL-conforming const_iterator.
Definition: LList.H:297
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:163
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:396
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:412
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:267
const T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:270
T & operator[](const label)
Return element of FixedList.
Definition: FixedListI.H:247
void checkStart(const label start) const
Check start is within valid range (0 ... size-1)
Definition: FixedListI.H:139
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:191
T & first()
Return the first element of the list.
Definition: FixedListI.H:217
T & last()
Return the last element of the list.
Definition: FixedListI.H:231
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 fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: FixedListI.H:125
void resize(const label)
Dummy resize function.
Definition: FixedListI.H:175
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:258
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:348
void checkSize(const label size) const
Check size is within valid range (0 ... size)
Definition: FixedListI.H:151
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:419
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:324
const_iterator cbegin() const
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:340
iterator begin()
Definition: LList.H:281
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: FixedListI.H:132
const volScalarField & T
label max_size() const
Return size of the largest possible FixedList.
Definition: FixedListI.H:426
FixedList()
Null constructor.
Definition: FixedListI.H:33
bool empty() const
Return true if the FixedList is empty (ie, size() is zero)
Definition: FixedListI.H:433
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:109
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:210
void setSize(const label)
Dummy setSize function.
Definition: FixedListI.H:183
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:202