Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
33template<class T, unsigned N>
35{
36 this->fill(val);
37}
38
39
40template<class T, unsigned N>
46
47template<class T, unsigned N>
50 // std::execution::unsequenced_policy
51 std::copy(list.begin(), list.end(), v_);
52}
53
54
55template<class T, unsigned N>
58 // std::execution::unsequenced_policy
59 std::move(list.begin(), list.end(), v_);
60}
61
62
63template<class T, unsigned N>
64inline Foam::FixedList<T, N>::FixedList(std::initializer_list<T> list)
66 checkSize(list.size());
67 std::copy_n(list.begin(), N, v_);
68}
69
70
71template<class T, unsigned N>
73{
74 checkSize(list.size());
75 std::copy_n(list.begin(), N, v_);
76}
77
78
79template<class T, unsigned N>
80template<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
94template<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
108template<class T, unsigned N>
111{
112 return autoPtr<FixedList<T, N>>::New(*this);
114
115
116// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
117
118template<class T, unsigned N>
119inline const T*
122 return v_;
123}
124
125
126template<class T, unsigned N>
127inline T*
130 return v_;
131}
132
133
134template<class T, unsigned N>
135inline const char*
138 return reinterpret_cast<const char*>(v_);
139}
140
141
142template<class T, unsigned N>
143inline char*
145{
146 return reinterpret_cast<char*>(v_);
147}
148
149
150template<class T, unsigned N>
151inline std::streamsize Foam::FixedList<T, N>::size_bytes() noexcept
153 return N*sizeof(T);
154}
155
156
157template<class T, unsigned N>
158template<unsigned Index>
160{
161 static_assert(Index < N, "Address outside FixedList range");
162 return v_[Index];
163}
164
165
166template<class T, unsigned N>
167template<unsigned Index>
168inline const T& Foam::FixedList<T, N>::get() const noexcept
170 static_assert(Index < N, "Address outside FixedList range");
171 return v_[Index];
172}
173
174
175template<class T, unsigned N>
177{
178 return v_[0];
179}
180
181
182template<class T, unsigned N>
184{
185 return v_[0];
186}
187
188
189template<class T, unsigned N>
191{
192 return v_[N-1];
193}
194
195
196template<class T, unsigned N>
198{
199 return v_[N-1];
200}
201
202
203template<class T, unsigned N>
204inline Foam::label Foam::FixedList<T, N>::fcIndex(const label i) const noexcept
205{
206 return (i == N-1 ? 0 : i+1);
207}
208
209
210template<class T, unsigned N>
211inline const T& Foam::FixedList<T, N>::fcValue(const label i) const
212{
213 return this->operator[](this->fcIndex(i));
214}
215
216
217template<class T, unsigned N>
218inline T& Foam::FixedList<T, N>::fcValue(const label i)
219{
220 return this->operator[](this->fcIndex(i));
221}
222
223
224template<class T, unsigned N>
225inline Foam::label Foam::FixedList<T, N>::rcIndex(const label i) const noexcept
226{
227 return (i ? i-1 : N-1);
228}
229
230
231template<class T, unsigned N>
232inline const T& Foam::FixedList<T, N>::rcValue(const label i) const
233{
234 return this->operator[](this->rcIndex(i));
236
237
238template<class T, unsigned N>
239inline T& Foam::FixedList<T, N>::rcValue(const label i)
240{
241 return this->operator[](this->rcIndex(i));
242}
243
244
245template<class T, unsigned N>
246inline 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
258template<class T, unsigned N>
259inline void Foam::FixedList<T, N>::checkSize(const label size) const
260{
261 if (unsigned(size) != N)
262 {
264 << "size " << size << " != " << N
265 << abort(FatalError);
267}
268
269
270template<class T, unsigned N>
271inline 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
282template<class T, unsigned N>
283inline 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 }
295
296 return true;
297}
298
299
300template<class T, unsigned N>
301inline bool Foam::FixedList<T, N>::contains(const T& val) const
303 const auto iter = std::find(this->cbegin(), this->cend(), val);
304 return (iter != this->cend());
305}
307
308template<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);
318
319
320template<class T, unsigned N>
321inline void Foam::FixedList<T, N>::resize(const label n)
323 #ifdef FULLDEBUG
324 checkSize(n);
325 #endif
326}
327
328
329template<class T, unsigned N>
330inline void Foam::FixedList<T, N>::resize_fill(const label n, const T& val)
331{
332 #ifdef FULLDEBUG
334 #endif
335 this->fill(val);
336}
337
339template<class T, unsigned N>
340inline void Foam::FixedList<T, N>::resize_nocopy(const label n)
341{
342 #ifdef FULLDEBUG
343 checkSize(n);
344 #endif
345}
346
347
348template<class T, unsigned N>
349inline void Foam::FixedList<T, N>::fill(const T& val)
351 // Usually small enough that parallel execution is pointless...
352 std::fill_n(v_, N, val);
353}
354
355
356template<class T, unsigned N>
358{
359 if constexpr (std::is_arithmetic_v<T>)
360 {
361 // Usually small enough that parallel execution is pointless...
362 std::fill_n(v_, N, T(0));
363 }
364 else
365 {
366 for (unsigned i = 0; i < N; ++i)
367 {
368 v_[i] = Foam::zero{};
369 }
370 }
371}
372
373
374template<class T, unsigned N>
376{
377 if (this == &other)
378 {
379 return; // Self-swap is a no-op
380 }
381
382 // Essentially std::swap_ranges
383 for (unsigned i=0; i<N; ++i)
385 Foam::Swap(v_[i], other.v_[i]);
386 }
387}
388
389
390template<class T, unsigned N>
391inline void Foam::FixedList<T, N>::transfer(FixedList<T, N>& list)
392{
393 if (this == &list)
394 {
395 return; // Self-assignment is a no-op
396 }
397
398 // std::execution::unsequenced_policy
399 std::move(list.begin(), list.end(), v_);
400}
401
402
403// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
404
405template<class T, unsigned N>
406inline T& Foam::FixedList<T, N>::operator[](const label i)
407{
408 #ifdef FULLDEBUG
410 #endif
411 return v_[i];
412}
413
414
415template<class T, unsigned N>
416inline const T& Foam::FixedList<T, N>::operator[](const label i) const
417{
418 #ifdef FULLDEBUG
420 #endif
421 return v_[i];
422}
423
424
425template<class T, unsigned N>
428 checkSize(list.size());
429 std::copy_n(list.begin(), N, v_);
430}
431
433template<class T, unsigned N>
434inline void Foam::FixedList<T, N>::operator=(std::initializer_list<T> list)
436 checkSize(list.size());
437 std::copy_n(list.begin(), N, v_);
438}
439
440
441template<class T, unsigned N>
442inline void Foam::FixedList<T, N>::operator=(const T& val)
443{
444 this->fill(val);
445}
446
447
448template<class T, unsigned N>
450{
451 this->fill(Foam::zero{});
452}
453
454
455template<class T, unsigned N>
457{
458 if (this == &list)
459 {
460 return; // Self-assignment is a no-op
461 }
463 // std::execution::unsequenced_policy
464 std::copy(list.begin(), list.end(), v_);
465}
466
467
468template<class T, unsigned N>
470{
471 if (this == &list)
472 {
473 return; // Self-assignment is a no-op
474 }
475
476 // std::execution::unsequenced_policy
477 std::move(list.begin(), list.end(), v_);
479
480
481// * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
482
483template<class T, unsigned N>
487 return v_;
488}
489
490
491template<class T, unsigned N>
495 return v_;
496}
497
498
499template<class T, unsigned N>
503 return v_;
504}
505
506
507template<class T, unsigned N>
509Foam::FixedList<T, N>::begin(const int i) noexcept
511 return (v_ + (i < 0 ? 0 : int(N) < i ? int(N) : i));
512}
513
514
515template<class T, unsigned N>
517Foam::FixedList<T, N>::begin(const int i) const noexcept
519 return (v_ + (i < 0 ? 0 : int(N) < i ? int(N) : i));
520}
521
522
523template<class T, unsigned N>
525Foam::FixedList<T, N>::cbegin(const int i) const noexcept
527 return (v_ + (i < 0 ? 0 : int(N) < i ? int(N) : i));
528}
530
531template<class T, unsigned N>
535 return (v_ + N);
536}
537
538
539template<class T, unsigned N>
543 return (v_ + N);
544}
545
546
547template<class T, unsigned N>
551 return (v_ + N);
552}
553
554
555template<class T, unsigned N>
559 return reverse_iterator(end());
560}
561
562
563template<class T, unsigned N>
567 return const_reverse_iterator(end());
568}
569
570
571template<class T, unsigned N>
575 return const_reverse_iterator(end());
576}
577
579template<class T, unsigned N>
584}
585
586
587template<class T, unsigned N>
592}
593
594
595template<class T, unsigned N>
598{
600}
601
602
603// ************************************************************************* //
label n
A 1D vector of objects of type <T> with a fixed length <N>.
Definition FixedList.H:73
const_iterator cend() const noexcept
Definition FixedListI.H:542
void swap(FixedList< T, N > &other)
Swap lists by swapping the content of the individual list elements.
Definition FixedListI.H:368
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
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition FixedListI.H:566
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
static std::streamsize size_bytes() noexcept
Number of contiguous bytes for the list data,.
Definition FixedListI.H:144
void operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition FixedListI.H:419
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition FixedListI.H:137
static constexpr label size() noexcept
Return the number of elements in the FixedList.
Definition FixedList.H:619
const T * const_iterator
Random access iterator for traversing FixedList.
Definition FixedList.H:135
const_iterator cbegin() const noexcept
Definition FixedListI.H:494
void checkIndex(const label i) const
Check index is within valid range [0,N).
Definition FixedListI.H:264
void fill(const T &val)
Assign all entries to the given value.
Definition FixedListI.H:342
T * iterator
Random access iterator for traversing FixedList.
Definition FixedList.H:130
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access).
Definition FixedList.H:155
bool contains(const T &val) const
True if the value is contained in the list.
Definition FixedListI.H:294
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list).
Definition FixedListI.H:225
T & get() noexcept
Element access using compile-time indexing.
Definition FixedListI.H:152
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition FixedListI.H:590
void transfer(FixedList< T, N > &list)
Transfer by swapping using a move assignment for the content of the individual list elements.
Definition FixedListI.H:384
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition FixedListI.H:113
T & back() noexcept
Access last element of the list, position [N-1].
Definition FixedListI.H:183
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
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition FixedListI.H:526
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list).
Definition FixedListI.H:204
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
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access).
Definition FixedList.H:150
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition FixedListI.H:121
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition FixedListI.H:276
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition FixedListI.H:252
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition FixedListI.H:129
T & front() noexcept
Access first element of the list, position [0].
Definition FixedListI.H:169
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition FixedListI.H:550
T & operator[](const label i)
Return element of FixedList.
Definition FixedListI.H:399
FixedList()=default
Default construct.
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition FixedListI.H:574
label find(const T &val) const
Definition FixedList.C:42
static constexpr bool empty() noexcept
Always false since zero-sized FixedList is compile-time disabled.
Definition FixedList.H:614
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition FixedListI.H:478
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
void checkStart(const label start) const
Check start is within valid range [0,size).
Definition FixedListI.H:239
autoPtr< FixedList< T, N > > clone() const
Clone.
Definition FixedListI.H:103
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:89
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
dimensionedScalar pos(const dimensionedScalar &ds)
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.
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const Vector< label > N(dict.get< Vector< label > >("N"))