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-2021 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#include "SLList.H"
31#include <utility>
32
33// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
34
35template<class T, unsigned N>
37{
38 return NullObjectRef<FixedList<T, N>>();
39}
40
41
42// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43
44template<class T, unsigned N>
46{
47 this->fill(val);
48}
49
50
51template<class T, unsigned N>
53{
54 this->fill(Zero);
55}
56
57
58template<class T, unsigned N>
60{
61 for (unsigned i=0; i<N; ++i)
62 {
63 v_[i] = list[i];
64 }
65}
66
67
68template<class T, unsigned N>
70{
71 for (unsigned i=0; i<N; ++i)
72 {
73 v_[i] = list.v_[i];
74 }
75}
76
77
78template<class T, unsigned N>
80{
81 for (unsigned i=0; i<N; ++i)
82 {
83 v_[i] = std::move(list.v_[i]);
84 }
85}
86
87
88template<class T, unsigned N>
89inline Foam::FixedList<T, N>::FixedList(std::initializer_list<T> list)
90{
91 checkSize(list.size());
92
93 auto iter = list.begin();
94 for (unsigned i=0; i<N; ++i)
95 {
96 v_[i] = *iter;
97 ++iter;
98 }
99}
100
101
102template<class T, unsigned N>
104{
105 checkSize(list.size());
106
107 for (unsigned i=0; i<N; ++i)
108 {
109 v_[i] = list[i];
110 }
111}
112
113
114template<class T, unsigned N>
115template<unsigned AnyNum>
117(
118 const FixedList<T, AnyNum>& list,
119 const FixedList<label, N>& indices
120)
121{
122 for (unsigned i=0; i<N; ++i)
123 {
124 v_[i] = list[indices[i]];
125 }
126}
127
128
129template<class T, unsigned N>
131(
132 const UList<T>& list,
133 const FixedList<label, N>& indices
134)
135{
136 for (unsigned i=0; i<N; ++i)
137 {
138 v_[i] = list[indices[i]];
139 }
140}
141
142
143template<class T, unsigned N>
145{
146 checkSize(list.size());
147
148 auto iter = list.begin();
149 for (unsigned i=0; i<N; ++i)
150 {
151 v_[i] = *iter;
152 ++iter;
153 }
154}
156
157template<class T, unsigned N>
160{
161 return autoPtr<FixedList<T, N>>::New(*this);
162}
163
164
165// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166
167template<class T, unsigned N>
168inline const T*
170{
171 return v_;
172}
173
174
175template<class T, unsigned N>
176inline T*
178{
179 return v_;
180}
181
183template<class T, unsigned N>
184inline const char*
186{
187 return reinterpret_cast<const char*>(v_);
188}
190
191template<class T, unsigned N>
192inline char*
194{
195 return reinterpret_cast<char*>(v_);
196}
197
198
199template<class T, unsigned N>
200inline std::streamsize Foam::FixedList<T, N>::size_bytes() noexcept
201{
202 return N*sizeof(T);
204
205
206template<class T, unsigned N>
208{
209 return v_[0];
210}
212
213template<class T, unsigned N>
214inline const T& Foam::FixedList<T, N>::first() const noexcept
215{
216 return v_[0];
217}
218
219
220template<class T, unsigned N>
222{
223 return v_[N-1];
224}
225
227template<class T, unsigned N>
228inline const T& Foam::FixedList<T, N>::last() const noexcept
230 return v_[N-1];
231}
232
234template<class T, unsigned N>
235inline Foam::label Foam::FixedList<T, N>::fcIndex(const label i) const
236{
237 return (i == N-1 ? 0 : i+1);
238}
239
240
241template<class T, unsigned N>
242inline const T& Foam::FixedList<T, N>::fcValue(const label i) const
243{
244 return this->operator[](this->fcIndex(i));
245}
246
248template<class T, unsigned N>
249inline T& Foam::FixedList<T, N>::fcValue(const label i)
250{
251 return this->operator[](this->fcIndex(i));
252}
253
255template<class T, unsigned N>
256inline Foam::label Foam::FixedList<T, N>::rcIndex(const label i) const
258 return (i ? i-1 : N-1);
259}
260
261
262template<class T, unsigned N>
263inline const T& Foam::FixedList<T, N>::rcValue(const label i) const
264{
265 return this->operator[](this->rcIndex(i));
267
268
269template<class T, unsigned N>
270inline T& Foam::FixedList<T, N>::rcValue(const label i)
271{
272 return this->operator[](this->rcIndex(i));
273}
274
275
276template<class T, unsigned N>
277inline void Foam::FixedList<T, N>::checkStart(const label start) const
278{
279 if (start < 0 || (start && unsigned(start) >= N))
280 {
281 // Note: always accept start=0, even for zero-sized lists
283 << "start " << start << " out of range [0," << N << ")"
284 << abort(FatalError);
285 }
286}
287
288
289template<class T, unsigned N>
290inline void Foam::FixedList<T, N>::checkSize(const label size) const
291{
292 if (unsigned(size) != N)
293 {
295 << "size " << size << " != " << N
296 << abort(FatalError);
297 }
299
300
301template<class T, unsigned N>
302inline void Foam::FixedList<T, N>::checkIndex(const label i) const
303{
304 if (i < 0 || unsigned(i) >= N)
305 {
307 << "index " << i << " out of range [0," << N << ")"
308 << abort(FatalError);
309 }
311
312
313template<class T, unsigned N>
315{
316 if (empty()) return false; // <- Compile-time disabled anyhow
317
318 for (unsigned i=1; i<N; ++i)
319 {
320 if (v_[0] != v_[i])
321 {
322 return false;
323 }
324 }
325
326 return true;
327}
328
329
330template<class T, unsigned N>
333 const T& val,
334 label pos
335) const
336{
337 return (this->find(val, pos) >= 0);
339
340
341template<class T, unsigned N>
342inline void Foam::FixedList<T, N>::resize(const label n)
343{
344 #ifdef FULLDEBUG
345 checkSize(n);
346 #endif
348
349
350template<class T, unsigned N>
352{
353 #ifdef FULLDEBUG
354 checkSize(n);
355 #endif
357
358
359template<class T, unsigned N>
360inline void Foam::FixedList<T, N>::fill(const T& val)
361{
362 for (unsigned i=0; i<N; ++i)
363 {
364 v_[i] = val;
366}
367
369template<class T, unsigned N>
371{
372 for (unsigned i=0; i<N; ++i)
373 {
374 v_[i] = Zero;
375 }
376}
378
379template<class T, unsigned N>
381{
382 if (this == &other)
384 return; // Self-swap is a no-op
385 }
386
387 for (unsigned i=0; i<N; ++i)
388 {
389 Foam::Swap(v_[i], other.v_[i]);
390 }
391}
393
394template<class T, unsigned N>
396{
397 if (this == &list)
399 return; // Self-assignment is a no-op
400 }
402 for (unsigned i=0; i<N; ++i)
403 {
404 v_[i] = std::move(list[i]);
405 }
406}
408
409// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
410
411template<class T, unsigned N>
413{
414 #ifdef FULLDEBUG
415 checkIndex(i);
416 #endif
417 return v_[i];
418}
419
420
421template<class T, unsigned N>
422inline const T& Foam::FixedList<T, N>::operator[](const label i) const
423{
424 #ifdef FULLDEBUG
425 checkIndex(i);
426 #endif
427 return v_[i];
428}
429
430
431template<class T, unsigned N>
432inline void Foam::FixedList<T, N>::operator=(const T list[N])
433{
434 for (unsigned i=0; i<N; ++i)
435 {
436 v_[i] = list[i];
437 }
438}
439
440template<class T, unsigned N>
442{
443 checkSize(list.size());
444
445 for (unsigned i=0; i<N; ++i)
446 {
447 v_[i] = list[i];
448 }
449}
450
451template<class T, unsigned N>
453{
454 checkSize(list.size());
455
456 auto iter = list.begin();
457 for (unsigned i=0; i<N; ++i)
458 {
459 v_[i] = *iter;
460 ++iter;
461 }
462}
463
464template<class T, unsigned N>
465inline void Foam::FixedList<T, N>::operator=(std::initializer_list<T> list)
466{
467 checkSize(list.size());
468
469 auto iter = list.begin();
470 for (unsigned i=0; i<N; ++i)
471 {
472 v_[i] = *iter;
473 ++iter;
474 }
475}
476
477template<class T, unsigned N>
478inline void Foam::FixedList<T, N>::operator=(const T& val)
479{
480 this->fill(val);
481}
482
483
484template<class T, unsigned N>
486{
487 this->fill(Zero);
488}
489
490
491template<class T, unsigned N>
493{
494 if (this == &list)
495 {
496 return; // Self-assignment is a no-op
497 }
498
499 for (unsigned i=0; i<N; ++i)
500 {
501 v_[i] = list.v_[i];
502 }
503}
504
505template<class T, unsigned N>
507{
508 if (this == &list)
509 {
510 return; // Self-assignment is a no-op
511 }
512
513 for (unsigned i=0; i<N; ++i)
514 {
515 v_[i] = std::move(list.v_[i]);
516 }
517}
518
519
520// * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
521
522template<class T, unsigned N>
525{
526 return v_;
527}
528
529
530template<class T, unsigned N>
533{
534 return v_;
535}
536
537
538template<class T, unsigned N>
541{
542 return v_;
543}
544
545
546template<class T, unsigned N>
549{
550 return (v_ + N);
551}
552
553
554template<class T, unsigned N>
557{
558 return (v_ + N);
559}
560
561
562template<class T, unsigned N>
565{
566 return (v_ + N);
567}
568
569
570template<class T, unsigned N>
573{
574 return reverse_iterator(end());
575}
576
577
578template<class T, unsigned N>
581{
582 return const_reverse_iterator(end());
583}
584
585
586template<class T, unsigned N>
589{
590 return const_reverse_iterator(end());
591}
592
593
594template<class T, unsigned N>
597{
598 return reverse_iterator(begin());
599}
600
601
602template<class T, unsigned N>
605{
606 return const_reverse_iterator(begin());
607}
608
609
610template<class T, unsigned N>
613{
614 return const_reverse_iterator(begin());
615}
616
617
618// ************************************************************************* //
Non-intrusive singly-linked list.
bool found
label n
virtual bool resize()
Resize the ODE solver.
Definition: Euler.C:53
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:564
void swap(FixedList< T, N > &other)
Swap lists by swapping the content of the individual list elements.
Definition: FixedListI.H:380
void resize_nocopy(const label n)
Dummy function, to make FixedList consistent with List.
Definition: FixedListI.H:351
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:588
static std::streamsize size_bytes() noexcept
Number of contiguous bytes for the list data,.
Definition: FixedListI.H:200
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:193
label rcIndex(const label i) const
Definition: FixedListI.H:256
const T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:125
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:540
void checkIndex(const label i) const
Check index is within valid range [0,N)
Definition: FixedListI.H:302
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:122
T & first() noexcept
The first element of the list, position [0].
Definition: FixedListI.H:207
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access)
Definition: FixedList.H:137
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: FixedListI.H:263
T & last() noexcept
The last element of the list, position [N-1].
Definition: FixedListI.H:221
label fcIndex(const label i) const
Definition: FixedListI.H:235
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:612
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:169
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:548
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: FixedListI.H:242
void operator=(const T list[N])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:432
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access)
Definition: FixedList.H:134
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:177
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: FixedListI.H:314
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:185
static const FixedList< T, N > & null()
Return a null FixedList.
Definition: FixedListI.H:36
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:572
T & operator[](const label i)
Return element of FixedList.
Definition: FixedListI.H:412
FixedList()=default
Default construct.
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:596
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:524
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: FixedListI.H:277
autoPtr< FixedList< T, N > > clone() const
Clone.
Definition: FixedListI.H:159
Template class for non-intrusive linked lists.
Definition: LList.H:79
iterator begin()
Iterator to first item in list with non-const access.
Definition: LList.H:503
void checkSize() const
Check that dimensions are positive, non-zero.
Definition: MatrixI.H:167
virtual char fill() const =0
Get padding character.
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:94
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
dimensionedScalar pos(const dimensionedScalar &ds)
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
const Vector< label > N(dict.get< Vector< label > >("N"))