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-2020 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 template<class T, unsigned Size>
332 {
333  return &v_[Size];
334 }
335 
336 
337 template<class T, unsigned Size>
340 {
341  return v_;
342 }
343 
344 
345 template<class T, unsigned Size>
348 {
349  return &v_[Size];
350 }
351 
352 
353 template<class T, unsigned Size>
356 {
357  return v_;
358 }
359 
360 
361 template<class T, unsigned Size>
364 {
365  return &v_[Size];
366 }
367 
368 
369 template<class T, unsigned Size>
372 {
373  return reverse_iterator(end());
374 }
375 
376 
377 template<class T, unsigned Size>
380 {
381  return reverse_iterator(begin());
382 }
383 
384 
385 template<class T, unsigned Size>
388 {
389  return const_reverse_iterator(cend());
390 }
391 
392 
393 template<class T, unsigned Size>
396 {
397  return const_reverse_iterator(cbegin());
398 }
399 
400 
401 template<class T, unsigned Size>
404 {
405  return const_reverse_iterator(cend());
406 }
407 
408 
409 template<class T, unsigned Size>
412 {
413  return const_reverse_iterator(cbegin());
414 }
415 
416 
417 template<class T, unsigned Size>
419 {
420  return Size;
421 }
422 
423 
424 template<class T, unsigned Size>
426 {
427  return Size;
428 }
429 
430 
431 template<class T, unsigned Size>
433 {
434  return false;
435 }
436 
437 
438 template<class T, unsigned Size>
439 template<class HashT>
441 (
442  const FixedList<T, Size>& lst,
443  unsigned seed
444 ) const
445 {
446  if (contiguous<T>())
447  {
448  // Hash directly
449  return Hasher(lst.v_, sizeof(lst.v_), seed);
450  }
451  else
452  {
453  // Hash incrementally
454  unsigned val = seed;
455 
456  for (unsigned i=0; i<Size; i++)
457  {
458  val = HashT()(lst[i], val);
459  }
460 
461  return val;
462  }
463 }
464 
465 
466 // ************************************************************************* //
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator for reverse traversal of FixedList.
Definition: FixedList.H:288
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:347
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:323
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.
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:371
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
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator for reverse traversal of constant FixedList.
Definition: FixedList.H:300
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:395
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
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:387
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:331
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:418
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:339
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:425
FixedList()
Null constructor.
Definition: FixedListI.H:33
bool empty() const
Return true if the FixedList is empty (ie, size() is zero)
Definition: FixedListI.H:432
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:311
Non-intrusive singly-linked list.
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:379
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:202