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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "UList.H"
30 
31 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
32 
33 template<class T, unsigned N>
35 {
36  return NullObjectRef<FixedList<T, N>>();
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
42 template<class T, unsigned N>
44 {
45  this->fill(val);
46 }
47 
48 
49 template<class T, unsigned N>
51 {
52  this->fill(Foam::zero{});
53 }
54 
55 
56 template<class T, unsigned N>
58 {
59  // std::execution::unsequenced_policy
60  std::copy(list.begin(), list.end(), v_);
61 }
62 
63 
64 template<class T, unsigned N>
66 {
67  // std::execution::unsequenced_policy
68  std::move(list.begin(), list.end(), v_);
69 }
70 
71 
72 template<class T, unsigned N>
73 inline Foam::FixedList<T, N>::FixedList(std::initializer_list<T> list)
74 {
75  checkSize(list.size());
76  std::copy_n(list.begin(), N, v_);
77 }
78 
79 
80 template<class T, unsigned N>
82 {
83  checkSize(list.size());
84  std::copy_n(list.begin(), N, v_);
85 }
86 
87 
88 template<class T, unsigned N>
89 template<unsigned AnyNum>
91 (
92  const FixedList<T, AnyNum>& list,
93  const FixedList<label, N>& indices
94 )
95 {
96  for (unsigned i = 0; i < N; ++i)
97  {
98  v_[i] = list[indices[i]];
99  }
100 }
101 
102 
103 template<class T, unsigned N>
105 (
106  const UList<T>& list,
107  const FixedList<label, N>& indices
108 )
109 {
110  for (unsigned i = 0; i < N; ++i)
111  {
112  v_[i] = list[indices[i]];
113  }
114 }
115 
116 
117 template<class T, unsigned N>
120 {
121  return autoPtr<FixedList<T, N>>::New(*this);
122 }
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
127 template<class T, unsigned N>
128 inline const T*
130 {
131  return v_;
132 }
133 
134 
135 template<class T, unsigned N>
136 inline T*
138 {
139  return v_;
140 }
141 
142 
143 template<class T, unsigned N>
144 inline const char*
146 {
147  return reinterpret_cast<const char*>(v_);
148 }
149 
150 
151 template<class T, unsigned N>
152 inline char*
154 {
155  return reinterpret_cast<char*>(v_);
156 }
157 
158 
159 template<class T, unsigned N>
160 inline std::streamsize Foam::FixedList<T, N>::size_bytes() noexcept
161 {
162  return N*sizeof(T);
163 }
164 
165 
166 template<class T, unsigned N>
167 template<unsigned Index>
169 {
170  static_assert(Index < N, "Address outside FixedList range");
171  return v_[Index];
172 }
173 
174 
175 template<class T, unsigned N>
176 template<unsigned Index>
177 inline const T& Foam::FixedList<T, N>::get() const noexcept
178 {
179  static_assert(Index < N, "Address outside FixedList range");
180  return v_[Index];
181 }
182 
183 
184 template<class T, unsigned N>
186 {
187  return v_[0];
188 }
189 
190 
191 template<class T, unsigned N>
193 {
194  return v_[0];
195 }
196 
197 
198 template<class T, unsigned N>
200 {
201  return v_[N-1];
202 }
203 
204 
205 template<class T, unsigned N>
207 {
208  return v_[N-1];
209 }
210 
211 
212 template<class T, unsigned N>
213 inline Foam::label Foam::FixedList<T, N>::fcIndex(const label i) const noexcept
214 {
215  return (i == N-1 ? 0 : i+1);
216 }
217 
218 
219 template<class T, unsigned N>
220 inline const T& Foam::FixedList<T, N>::fcValue(const label i) const
221 {
222  return this->operator[](this->fcIndex(i));
223 }
224 
225 
226 template<class T, unsigned N>
227 inline T& Foam::FixedList<T, N>::fcValue(const label i)
228 {
229  return this->operator[](this->fcIndex(i));
230 }
231 
232 
233 template<class T, unsigned N>
234 inline Foam::label Foam::FixedList<T, N>::rcIndex(const label i) const noexcept
235 {
236  return (i ? i-1 : N-1);
237 }
238 
239 
240 template<class T, unsigned N>
241 inline const T& Foam::FixedList<T, N>::rcValue(const label i) const
242 {
243  return this->operator[](this->rcIndex(i));
244 }
245 
246 
247 template<class T, unsigned N>
248 inline T& Foam::FixedList<T, N>::rcValue(const label i)
249 {
250  return this->operator[](this->rcIndex(i));
251 }
252 
253 
254 template<class T, unsigned N>
255 inline void Foam::FixedList<T, N>::checkStart(const label start) const
256 {
257  if (start < 0 || (start && unsigned(start) >= N))
258  {
259  // Note: always accept start=0, even for zero-sized lists
261  << "start " << start << " out of range [0," << N << ")"
262  << abort(FatalError);
263  }
264 }
265 
266 
267 template<class T, unsigned N>
268 inline void Foam::FixedList<T, N>::checkSize(const label size) const
269 {
270  if (unsigned(size) != N)
271  {
273  << "size " << size << " != " << N
274  << abort(FatalError);
275  }
276 }
277 
278 
279 template<class T, unsigned N>
280 inline void Foam::FixedList<T, N>::checkIndex(const label i) const
281 {
282  if (i < 0 || unsigned(i) >= N)
283  {
285  << "index " << i << " out of range [0," << N << ")"
286  << abort(FatalError);
287  }
288 }
289 
290 
291 template<class T, unsigned N>
292 inline bool Foam::FixedList<T, N>::uniform() const
293 {
294  if (empty()) return false; // <- Compile-time disabled anyhow
295 
296  // std::all_of()
297  for (unsigned i=1; i<N; ++i)
298  {
299  if (v_[0] != v_[i])
300  {
301  return false;
302  }
303  }
304 
305  return true;
306 }
307 
308 
309 template<class T, unsigned N>
310 inline bool Foam::FixedList<T, N>::contains(const T& val) const
311 {
312  const auto iter = std::find(this->cbegin(), this->cend(), val);
313  return (iter != this->cend());
314 }
315 
316 
317 template<class T, unsigned N>
319 (
320  const T& val,
321  label pos,
322  label len
323 ) const
324 {
325  return (this->find(val, pos, len) >= 0);
326 }
327 
328 
329 template<class T, unsigned N>
330 inline void Foam::FixedList<T, N>::resize(const label n)
331 {
332  #ifdef FULLDEBUG
333  checkSize(n);
334  #endif
335 }
336 
337 
338 template<class T, unsigned N>
339 inline void Foam::FixedList<T, N>::resize_fill(const label n, const T& val)
340 {
341  #ifdef FULLDEBUG
342  checkSize(n);
343  #endif
344  this->fill(val);
345 }
346 
347 
348 template<class T, unsigned N>
349 inline void Foam::FixedList<T, N>::resize_nocopy(const label n)
350 {
351  #ifdef FULLDEBUG
352  checkSize(n);
353  #endif
354 }
355 
356 
357 template<class T, unsigned N>
358 inline void Foam::FixedList<T, N>::fill(const T& val)
359 {
360  // Usually small enough that parallel execution is pointless...
361  std::fill_n(v_, N, val);
362 }
363 
364 
365 template<class T, unsigned N>
366 inline void Foam::FixedList<T, N>::fill(const Foam::zero)
367 {
368  // Usually small enough that parallel execution is pointless...
369  // Cannot use std::fill (ambiguous conversions for bool, char, etc)
370 
371  for (unsigned i = 0; i < N; ++i)
372  {
373  v_[i] = Zero;
374  }
375 }
376 
377 
378 template<class T, unsigned N>
379 inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& other)
380 {
381  if (this == &other)
382  {
383  return; // Self-swap is a no-op
384  }
385 
386  // Essentially std::swap_ranges
387  for (unsigned i=0; i<N; ++i)
388  {
389  Foam::Swap(v_[i], other.v_[i]);
390  }
391 }
392 
393 
394 template<class T, unsigned N>
395 inline void Foam::FixedList<T, N>::transfer(FixedList<T, N>& list)
396 {
397  if (this == &list)
398  {
399  return; // Self-assignment is a no-op
400  }
401 
402  // std::execution::unsequenced_policy
403  std::move(list.begin(), list.end(), v_);
404 }
405 
406 
407 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
408 
409 template<class T, unsigned N>
410 inline T& Foam::FixedList<T, N>::operator[](const label i)
411 {
412  #ifdef FULLDEBUG
413  checkIndex(i);
414  #endif
415  return v_[i];
416 }
417 
418 
419 template<class T, unsigned N>
420 inline const T& Foam::FixedList<T, N>::operator[](const label i) const
421 {
422  #ifdef FULLDEBUG
423  checkIndex(i);
424  #endif
425  return v_[i];
426 }
427 
428 
429 template<class T, unsigned N>
430 inline void Foam::FixedList<T, N>::operator=(const UList<T>& list)
431 {
432  checkSize(list.size());
433  std::copy_n(list.begin(), N, v_);
434 }
435 
436 
437 template<class T, unsigned N>
438 inline void Foam::FixedList<T, N>::operator=(std::initializer_list<T> list)
439 {
440  checkSize(list.size());
441  std::copy_n(list.begin(), N, v_);
442 }
443 
444 
445 template<class T, unsigned N>
446 inline void Foam::FixedList<T, N>::operator=(const T& val)
447 {
448  this->fill(val);
449 }
450 
451 
452 template<class T, unsigned N>
454 {
455  this->fill(Foam::zero{});
456 }
457 
458 
459 template<class T, unsigned N>
460 inline void Foam::FixedList<T, N>::operator=(const FixedList<T, N>& list)
461 {
462  if (this == &list)
463  {
464  return; // Self-assignment is a no-op
465  }
467  // std::execution::unsequenced_policy
468  std::copy(list.begin(), list.end(), v_);
469 }
470 
471 
472 template<class T, unsigned N>
474 {
475  if (this == &list)
476  {
477  return; // Self-assignment is a no-op
478  }
479 
480  // std::execution::unsequenced_policy
481  std::move(list.begin(), list.end(), v_);
482 }
483 
484 
485 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
486 
487 template<class T, unsigned N>
488 inline typename Foam::FixedList<T, N>::iterator
490 {
491  return v_;
492 }
493 
494 
495 template<class T, unsigned N>
498 {
499  return v_;
500 }
501 
502 
503 template<class T, unsigned N>
506 {
507  return v_;
508 }
509 
510 
511 template<class T, unsigned N>
512 inline typename Foam::FixedList<T, N>::iterator
514 {
515  return (v_ + N);
516 }
517 
518 
519 template<class T, unsigned N>
522 {
523  return (v_ + N);
524 }
525 
526 
527 template<class T, unsigned N>
530 {
531  return (v_ + N);
532 }
533 
534 
535 template<class T, unsigned N>
538 {
539  return reverse_iterator(end());
540 }
541 
542 
543 template<class T, unsigned N>
546 {
547  return const_reverse_iterator(end());
548 }
549 
550 
551 template<class T, unsigned N>
554 {
555  return const_reverse_iterator(end());
556 }
557 
558 
559 template<class T, unsigned N>
562 {
563  return reverse_iterator(begin());
564 }
565 
566 
567 template<class T, unsigned N>
570 {
571  return const_reverse_iterator(begin());
572 }
573 
574 
575 template<class T, unsigned N>
578 {
579  return const_reverse_iterator(begin());
580 }
581 
582 
583 // ************************************************************************* //
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:138
void resize(const label n)
Dummy function, to make FixedList consistent with List Any resizing is ignored (Fatal with bad sizing...
Definition: FixedListI.H:323
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:795
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access)
Definition: FixedList.H:153
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:482
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
void resize_fill(const label n, const T &val)
Set val for all elements. Any resizing is ignored (Fatal with bad sizing in full debug).
Definition: FixedListI.H:332
label rcIndex(const label i) const noexcept
Return the reverse circular index, i.e. previous index which returns to the last at the beginning of ...
Definition: FixedListI.H:227
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: FixedListI.H:285
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
label fcIndex(const label i) const noexcept
Return the forward circular index, i.e. next index which returns to the first at the end of the list...
Definition: FixedListI.H:206
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:506
T & back() noexcept
Access last element of the list, position [N-1].
Definition: FixedListI.H:192
void checkIndex(const label i) const
Check index is within valid range [0,N)
Definition: FixedListI.H:273
autoPtr< FixedList< T, N > > clone() const
Clone.
Definition: FixedListI.H:112
bool contains(const T &val) const
True if the value is contained in the list.
Definition: FixedListI.H:303
T & operator[](const label i)
Return element of FixedList.
Definition: FixedListI.H:403
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access)
Definition: FixedList.H:158
dimensionedScalar pos(const dimensionedScalar &ds)
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:554
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:122
const T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:138
static std::streamsize size_bytes() noexcept
Number of contiguous bytes for the list data,.
Definition: FixedListI.H:153
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: FixedListI.H:248
void operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition: FixedListI.H:423
void fill(const T &val)
Assign all entries to the given value.
Definition: FixedListI.H:351
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:223
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:146
errorManip< error > abort(error &err)
Definition: errorManip.H:139
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:391
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:105
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:133
const direction noexcept
Definition: Scalar.H:258
T & get() noexcept
Element access using compile-time indexing.
Definition: FixedListI.H:161
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition: FixedListI.H:261
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:201
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:190
const volScalarField & T
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:498
const Vector< label > N(dict.get< Vector< label >>("N"))
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:570
void resize_nocopy(const label n)
Dummy function, to make FixedList consistent with List Any resizing is ignored (Fatal with bad sizing...
Definition: FixedListI.H:342
T & front() noexcept
Access first element of the list, position [0].
Definition: FixedListI.H:178
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: FixedListI.H:234
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: FixedListI.H:213
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
Definition: DynamicList.H:692
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:530
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
label n
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:522
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:130
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:546
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:435
void transfer(FixedList< T, N > &list)
Transfer by swapping using a move assignment for the content of the individual list elements...
Definition: FixedListI.H:388
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:168
static const FixedList< T, N > & null()
Return a null FixedList.
Definition: FixedListI.H:27
FixedList()=default
Default construct.
void swap(FixedList< T, N > &other)
Swap lists by swapping the content of the individual list elements.
Definition: FixedListI.H:372
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127