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-2026 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
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 template<class T, unsigned Size>
245 template<unsigned I>
247 {
248  static_assert(I < Size, "Index out of bounds");
249 
250  return v_[I];
251 }
252 
253 
254 template<class T, unsigned Size>
255 template<unsigned I>
256 inline const T& Foam::FixedList<T, Size>::elmt() const
257 {
258  static_assert(I < Size, "Index out of bounds");
259 
260  return v_[I];
261 }
262 
263 
264 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
265 
266 template<class T, unsigned Size>
268 {
269  #ifdef FULLDEBUG
270  checkIndex(i);
271  #endif
272  return v_[i];
273 }
274 
275 
276 template<class T, unsigned Size>
277 inline const T& Foam::FixedList<T, Size>::operator[](const label i) const
278 {
279  #ifdef FULLDEBUG
280  checkIndex(i);
281  #endif
282  return v_[i];
283 }
284 
285 
286 template<class T, unsigned Size>
287 inline void Foam::FixedList<T, Size>::operator=(const T lst[Size])
288 {
289  for (unsigned i=0; i<Size; i++)
290  {
291  v_[i] = lst[i];
292  }
293 }
294 
295 template<class T, unsigned Size>
297 {
298  checkSize(lst.size());
299 
300  for (unsigned i=0; i<Size; i++)
301  {
302  v_[i] = lst[i];
303  }
304 }
305 
306 template<class T, unsigned Size>
308 {
309  checkSize(lst.size());
310 
311  typename SLList<T>::const_iterator iter = lst.begin();
312  for (unsigned i=0; i<Size; i++)
313  {
314  v_[i] = *iter++;
315  }
316 }
317 
318 template<class T, unsigned Size>
319 inline void Foam::FixedList<T, Size>::operator=(std::initializer_list<T> lst)
320 {
321  checkSize(lst.size());
322 
323  typename std::initializer_list<T>::iterator iter = lst.begin();
324  for (unsigned i=0; i<Size; i++)
325  {
326  v_[i] = *iter++;
327  }
328 }
329 
330 template<class T, unsigned Size>
332 {
333  for (unsigned i=0; i<Size; i++)
334  {
335  v_[i] = t;
336  }
337 }
338 
339 
340 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
341 
342 template<class T, unsigned Size>
345 {
346  return v_;
347 }
348 
349 template<class T, unsigned Size>
352 {
353  return &v_[Size];
354 }
355 
356 
357 template<class T, unsigned Size>
360 {
361  return v_;
362 }
363 
364 
365 template<class T, unsigned Size>
368 {
369  return &v_[Size];
370 }
371 
372 
373 template<class T, unsigned Size>
376 {
377  return v_;
378 }
379 
380 
381 template<class T, unsigned Size>
384 {
385  return &v_[Size];
386 }
387 
388 
389 template<class T, unsigned Size>
392 {
393  return reverse_iterator(end());
394 }
395 
396 
397 template<class T, unsigned Size>
400 {
401  return reverse_iterator(begin());
402 }
403 
404 
405 template<class T, unsigned Size>
408 {
409  return const_reverse_iterator(cend());
410 }
411 
412 
413 template<class T, unsigned Size>
416 {
417  return const_reverse_iterator(cbegin());
418 }
419 
420 
421 template<class T, unsigned Size>
424 {
425  return const_reverse_iterator(cend());
426 }
427 
428 
429 template<class T, unsigned Size>
432 {
433  return const_reverse_iterator(cbegin());
434 }
435 
436 
437 template<class T, unsigned Size>
439 {
440  return Size;
441 }
442 
443 
444 template<class T, unsigned Size>
446 {
447  return Size;
448 }
449 
450 
451 template<class T, unsigned Size>
453 {
454  return false;
455 }
456 
457 
458 template<class T, unsigned Size>
459 template<class HashT>
461 (
462  const FixedList<T, Size>& lst,
463  unsigned seed
464 ) const
465 {
466  if (contiguous<T>())
467  {
468  // Hash directly
469  return Hasher(lst.v_, sizeof(lst.v_), seed);
470  }
471  else
472  {
473  // Hash incrementally
474  unsigned val = seed;
475 
476  for (unsigned i=0; i<Size; i++)
477  {
478  val = HashT()(lst[i], val);
479  }
480 
481  return val;
482  }
483 }
484 
485 
486 // ************************************************************************* //
Non-intrusive singly-linked list.
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:78
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:407
static const FixedList< T, Size > & null()
Return a null FixedList.
Definition: FixedListI.H:118
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:391
T & first()
Return the first element of the list.
Definition: FixedListI.H:217
T & elmt()
Return element at a given index.
Definition: FixedListI.H:246
label max_size() const
Return size of the largest possible FixedList.
Definition: FixedListI.H:445
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: FixedListI.H:132
const T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:277
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1)
Definition: FixedListI.H:163
const_iterator cend() const
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:367
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:265
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator for reverse traversal of constant FixedList.
Definition: FixedList.H:307
friend Ostream & operator(Ostream &, const FixedList< T, Size > &)
Write FixedList to Ostream.
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:438
void resize(const label)
Dummy resize function.
Definition: FixedListI.H:175
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:351
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: FixedListI.H:125
bool empty() const
Return true if the FixedList is empty (ie, size() is zero)
Definition: FixedListI.H:452
const_iterator cbegin() const
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:359
T & operator[](const label)
Return element of FixedList.
Definition: FixedListI.H:267
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator for reverse traversal of FixedList.
Definition: FixedList.H:295
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:202
void checkSize(const label size) const
Check size is within valid range (0 ... size)
Definition: FixedListI.H:151
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:415
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:344
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:191
void operator=(const T v[Size])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:287
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:210
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:399
void setSize(const label)
Dummy setSize function.
Definition: FixedListI.H:183
T & last()
Return the last element of the list.
Definition: FixedListI.H:231
FixedList()
Null constructor.
Definition: FixedListI.H:33
autoPtr< FixedList< T, Size > > clone() const
Clone.
Definition: FixedListI.H:109
void checkStart(const label start) const
Check start is within valid range (0 ... size-1)
Definition: FixedListI.H:139
An STL-conforming const_iterator.
Definition: LList.H:300
Template class for non-intrusive linked lists.
Definition: LList.H:76
iterator begin()
Definition: LList.H:281
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Template function to specify if the data of a type are contiguous.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
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
errorManip< error > abort(error &err)
Definition: errorManip.H:131
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins's 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:476
static const Identity< scalar > I
Definition: Identity.H:93
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
error FatalError
void T(GeometricField< Type, GeoMesh, PrimitiveField1 > &gf, const GeometricField< Type, GeoMesh, PrimitiveField2 > &gf1)