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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class T, unsigned N>
35 {
36  this->fill(val);
37 }
38 
39 
40 template<class T, unsigned N>
42 {
43  this->fill(Foam::zero{});
44 }
45 
46 
47 template<class T, unsigned N>
49 {
50  // std::execution::unsequenced_policy
51  std::copy(list.begin(), list.end(), v_);
52 }
53 
54 
55 template<class T, unsigned N>
57 {
58  // std::execution::unsequenced_policy
59  std::move(list.begin(), list.end(), v_);
60 }
61 
62 
63 template<class T, unsigned N>
64 inline Foam::FixedList<T, N>::FixedList(std::initializer_list<T> list)
65 {
66  checkSize(list.size());
67  std::copy_n(list.begin(), N, v_);
68 }
69 
70 
71 template<class T, unsigned N>
73 {
74  checkSize(list.size());
75  std::copy_n(list.begin(), N, v_);
76 }
77 
78 
79 template<class T, unsigned N>
80 template<unsigned AnyNum>
82 (
83  const FixedList<T, AnyNum>& list,
84  const FixedList<label, N>& indices
85 )
86 {
87  for (unsigned i = 0; i < N; ++i)
88  {
89  v_[i] = list[indices[i]];
90  }
91 }
92 
93 
94 template<class T, unsigned N>
96 (
97  const UList<T>& list,
98  const FixedList<label, N>& indices
99 )
100 {
101  for (unsigned i = 0; i < N; ++i)
102  {
103  v_[i] = list[indices[i]];
104  }
105 }
106 
107 
108 template<class T, unsigned N>
111 {
112  return autoPtr<FixedList<T, N>>::New(*this);
113 }
114 
115 
116 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
117 
118 template<class T, unsigned N>
119 inline const T*
121 {
122  return v_;
123 }
124 
125 
126 template<class T, unsigned N>
127 inline T*
129 {
130  return v_;
131 }
132 
133 
134 template<class T, unsigned N>
135 inline const char*
137 {
138  return reinterpret_cast<const char*>(v_);
139 }
140 
141 
142 template<class T, unsigned N>
143 inline char*
145 {
146  return reinterpret_cast<char*>(v_);
147 }
148 
149 
150 template<class T, unsigned N>
151 inline std::streamsize Foam::FixedList<T, N>::size_bytes() noexcept
152 {
153  return N*sizeof(T);
154 }
155 
156 
157 template<class T, unsigned N>
158 template<unsigned Index>
160 {
161  static_assert(Index < N, "Address outside FixedList range");
162  return v_[Index];
163 }
164 
165 
166 template<class T, unsigned N>
167 template<unsigned Index>
168 inline const T& Foam::FixedList<T, N>::get() const noexcept
169 {
170  static_assert(Index < N, "Address outside FixedList range");
171  return v_[Index];
172 }
173 
174 
175 template<class T, unsigned N>
177 {
178  return v_[0];
179 }
180 
181 
182 template<class T, unsigned N>
184 {
185  return v_[0];
186 }
187 
188 
189 template<class T, unsigned N>
191 {
192  return v_[N-1];
193 }
194 
195 
196 template<class T, unsigned N>
198 {
199  return v_[N-1];
200 }
201 
202 
203 template<class T, unsigned N>
204 inline Foam::label Foam::FixedList<T, N>::fcIndex(const label i) const noexcept
205 {
206  return (i == N-1 ? 0 : i+1);
207 }
208 
209 
210 template<class T, unsigned N>
211 inline const T& Foam::FixedList<T, N>::fcValue(const label i) const
212 {
213  return this->operator[](this->fcIndex(i));
214 }
215 
216 
217 template<class T, unsigned N>
218 inline T& Foam::FixedList<T, N>::fcValue(const label i)
219 {
220  return this->operator[](this->fcIndex(i));
221 }
222 
223 
224 template<class T, unsigned N>
225 inline Foam::label Foam::FixedList<T, N>::rcIndex(const label i) const noexcept
226 {
227  return (i ? i-1 : N-1);
228 }
229 
230 
231 template<class T, unsigned N>
232 inline const T& Foam::FixedList<T, N>::rcValue(const label i) const
233 {
234  return this->operator[](this->rcIndex(i));
235 }
236 
237 
238 template<class T, unsigned N>
239 inline T& Foam::FixedList<T, N>::rcValue(const label i)
240 {
241  return this->operator[](this->rcIndex(i));
242 }
243 
244 
245 template<class T, unsigned N>
246 inline void Foam::FixedList<T, N>::checkStart(const label start) const
247 {
248  if (start < 0 || (start && unsigned(start) >= N))
249  {
250  // Note: always accept start=0, even for zero-sized lists
252  << "start " << start << " out of range [0," << N << ")"
253  << abort(FatalError);
254  }
255 }
256 
257 
258 template<class T, unsigned N>
259 inline void Foam::FixedList<T, N>::checkSize(const label size) const
260 {
261  if (unsigned(size) != N)
262  {
264  << "size " << size << " != " << N
265  << abort(FatalError);
266  }
267 }
268 
269 
270 template<class T, unsigned N>
271 inline void Foam::FixedList<T, N>::checkIndex(const label i) const
272 {
273  if (i < 0 || unsigned(i) >= N)
274  {
276  << "index " << i << " out of range [0," << N << ")"
277  << abort(FatalError);
278  }
279 }
280 
281 
282 template<class T, unsigned N>
283 inline bool Foam::FixedList<T, N>::uniform() const
284 {
285  if (empty()) return false; // <- Compile-time disabled anyhow
286 
287  // std::all_of()
288  for (unsigned i=1; i<N; ++i)
289  {
290  if (v_[0] != v_[i])
291  {
292  return false;
293  }
294  }
295 
296  return true;
297 }
298 
299 
300 template<class T, unsigned N>
301 inline bool Foam::FixedList<T, N>::contains(const T& val) const
302 {
303  const auto iter = std::find(this->cbegin(), this->cend(), val);
304  return (iter != this->cend());
305 }
306 
307 
308 template<class T, unsigned N>
310 (
311  const T& val,
312  label pos,
313  label len
314 ) const
315 {
316  return (this->find(val, pos, len) >= 0);
317 }
318 
319 
320 template<class T, unsigned N>
321 inline void Foam::FixedList<T, N>::resize(const label n)
322 {
323  #ifdef FULLDEBUG
324  checkSize(n);
325  #endif
326 }
327 
328 
329 template<class T, unsigned N>
330 inline void Foam::FixedList<T, N>::resize_fill(const label n, const T& val)
331 {
332  #ifdef FULLDEBUG
333  checkSize(n);
334  #endif
335  this->fill(val);
336 }
337 
338 
339 template<class T, unsigned N>
340 inline void Foam::FixedList<T, N>::resize_nocopy(const label n)
341 {
342  #ifdef FULLDEBUG
343  checkSize(n);
344  #endif
345 }
346 
347 
348 template<class T, unsigned N>
349 inline void Foam::FixedList<T, N>::fill(const T& val)
350 {
351  // Usually small enough that parallel execution is pointless...
352  std::fill_n(v_, N, val);
353 }
354 
355 
356 template<class T, unsigned N>
357 inline void Foam::FixedList<T, N>::fill(const Foam::zero)
358 {
359  // Usually small enough that parallel execution is pointless...
360  // Cannot use std::fill (ambiguous conversions for bool, char, etc)
361 
362  for (unsigned i = 0; i < N; ++i)
363  {
364  v_[i] = Zero;
365  }
366 }
367 
368 
369 template<class T, unsigned N>
370 inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& other)
371 {
372  if (this == &other)
373  {
374  return; // Self-swap is a no-op
375  }
376 
377  // Essentially std::swap_ranges
378  for (unsigned i=0; i<N; ++i)
379  {
380  Foam::Swap(v_[i], other.v_[i]);
381  }
382 }
383 
384 
385 template<class T, unsigned N>
386 inline void Foam::FixedList<T, N>::transfer(FixedList<T, N>& list)
387 {
388  if (this == &list)
389  {
390  return; // Self-assignment is a no-op
391  }
392 
393  // std::execution::unsequenced_policy
394  std::move(list.begin(), list.end(), v_);
395 }
396 
397 
398 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
399 
400 template<class T, unsigned N>
401 inline T& Foam::FixedList<T, N>::operator[](const label i)
402 {
403  #ifdef FULLDEBUG
404  checkIndex(i);
405  #endif
406  return v_[i];
407 }
408 
409 
410 template<class T, unsigned N>
411 inline const T& Foam::FixedList<T, N>::operator[](const label i) const
412 {
413  #ifdef FULLDEBUG
414  checkIndex(i);
415  #endif
416  return v_[i];
417 }
418 
419 
420 template<class T, unsigned N>
421 inline void Foam::FixedList<T, N>::operator=(const UList<T>& list)
422 {
423  checkSize(list.size());
424  std::copy_n(list.begin(), N, v_);
425 }
426 
427 
428 template<class T, unsigned N>
429 inline void Foam::FixedList<T, N>::operator=(std::initializer_list<T> list)
430 {
431  checkSize(list.size());
432  std::copy_n(list.begin(), N, v_);
433 }
434 
435 
436 template<class T, unsigned N>
437 inline void Foam::FixedList<T, N>::operator=(const T& val)
438 {
439  this->fill(val);
440 }
441 
442 
443 template<class T, unsigned N>
445 {
446  this->fill(Foam::zero{});
447 }
448 
449 
450 template<class T, unsigned N>
451 inline void Foam::FixedList<T, N>::operator=(const FixedList<T, N>& list)
452 {
453  if (this == &list)
454  {
455  return; // Self-assignment is a no-op
456  }
458  // std::execution::unsequenced_policy
459  std::copy(list.begin(), list.end(), v_);
460 }
461 
462 
463 template<class T, unsigned N>
465 {
466  if (this == &list)
467  {
468  return; // Self-assignment is a no-op
469  }
470 
471  // std::execution::unsequenced_policy
472  std::move(list.begin(), list.end(), v_);
473 }
474 
475 
476 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
477 
478 template<class T, unsigned N>
479 inline typename Foam::FixedList<T, N>::iterator
481 {
482  return v_;
483 }
484 
485 
486 template<class T, unsigned N>
489 {
490  return v_;
491 }
492 
493 
494 template<class T, unsigned N>
497 {
498  return v_;
499 }
500 
501 
502 template<class T, unsigned N>
503 inline typename Foam::FixedList<T, N>::iterator
505 {
506  return (v_ + N);
507 }
508 
509 
510 template<class T, unsigned N>
513 {
514  return (v_ + N);
515 }
516 
517 
518 template<class T, unsigned N>
521 {
522  return (v_ + N);
523 }
524 
525 
526 template<class T, unsigned N>
529 {
530  return reverse_iterator(end());
531 }
532 
533 
534 template<class T, unsigned N>
537 {
538  return const_reverse_iterator(end());
539 }
540 
541 
542 template<class T, unsigned N>
545 {
546  return const_reverse_iterator(end());
547 }
548 
549 
550 template<class T, unsigned N>
553 {
554  return reverse_iterator(begin());
555 }
556 
557 
558 template<class T, unsigned N>
561 {
562  return const_reverse_iterator(begin());
563 }
564 
565 
566 template<class T, unsigned N>
569 {
570  return const_reverse_iterator(begin());
571 }
572 
573 
574 // ************************************************************************* //
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:129
void resize(const label n)
Dummy function, to make FixedList consistent with List Any resizing is ignored (Fatal with bad sizing...
Definition: FixedListI.H:314
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:827
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:154
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:473
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:608
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:323
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:218
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: FixedListI.H:276
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:197
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:497
T & back() noexcept
Access last element of the list, position [N-1].
Definition: FixedListI.H:183
void checkIndex(const label i) const
Check index is within valid range [0,N)
Definition: FixedListI.H:264
autoPtr< FixedList< T, N > > clone() const
Clone.
Definition: FixedListI.H:103
bool contains(const T &val) const
True if the value is contained in the list.
Definition: FixedListI.H:294
T & operator[](const label i)
Return element of FixedList.
Definition: FixedListI.H:394
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access)
Definition: FixedList.H:159
dimensionedScalar pos(const dimensionedScalar &ds)
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:545
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:113
const T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:139
static std::streamsize size_bytes() noexcept
Number of contiguous bytes for the list data,.
Definition: FixedListI.H:144
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: FixedListI.H:239
void operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition: FixedListI.H:414
void fill(const T &val)
Assign all entries to the given value.
Definition: FixedListI.H:342
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:137
errorManip< error > abort(error &err)
Definition: errorManip.H:139
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:392
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:134
const direction noexcept
Definition: Scalar.H:258
T & get() noexcept
Element access using compile-time indexing.
Definition: FixedListI.H:152
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition: FixedListI.H:252
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:489
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:561
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:333
T & front() noexcept
Access first element of the list, position [0].
Definition: FixedListI.H:169
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: FixedListI.H:225
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: FixedListI.H:204
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:521
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:513
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:121
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:537
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:436
void transfer(FixedList< T, N > &list)
Transfer by swapping using a move assignment for the content of the individual list elements...
Definition: FixedListI.H:379
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:168
FixedList()=default
Default construct.
void swap(FixedList< T, N > &other)
Swap lists by swapping the content of the individual list elements.
Definition: FixedListI.H:363
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127