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 -------------------------------------------------------------------------------
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 #include "SLList.H"
31 #include <utility>
32 
33 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
34 
35 template<class T, unsigned N>
37 {
38  return NullObjectRef<FixedList<T, N>>();
39 }
40 
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
44 template<class T, unsigned N>
46 {
47  this->fill(val);
48 }
49 
50 
51 template<class T, unsigned N>
53 {
54  this->fill(Zero);
55 }
56 
57 
58 template<class T, unsigned N>
60 {
61  for (unsigned i=0; i<N; ++i)
62  {
63  v_[i] = list[i];
64  }
65 }
66 
67 
68 template<class T, unsigned N>
70 {
71  for (unsigned i=0; i<N; ++i)
72  {
73  v_[i] = list.v_[i];
74  }
75 }
76 
77 
78 template<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 
88 template<class T, unsigned N>
89 inline 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 
102 template<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 
114 template<class T, unsigned N>
115 template<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 
129 template<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 
143 template<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 }
155 
156 
157 template<class T, unsigned N>
160 {
161  return autoPtr<FixedList<T, N>>::New(*this);
162 }
163 
164 
165 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166 
167 template<class T, unsigned N>
168 inline const T*
170 {
171  return v_;
172 }
173 
174 
175 template<class T, unsigned N>
176 inline T*
178 {
179  return v_;
180 }
181 
182 
183 template<class T, unsigned N>
184 inline const char*
186 {
187  return reinterpret_cast<const char*>(v_);
188 }
189 
190 
191 template<class T, unsigned N>
192 inline char*
194 {
195  return reinterpret_cast<char*>(v_);
196 }
197 
198 
199 template<class T, unsigned N>
200 inline std::streamsize Foam::FixedList<T, N>::size_bytes() noexcept
201 {
202  return N*sizeof(T);
203 }
204 
205 
206 template<class T, unsigned N>
207 inline T& Foam::FixedList<T, N>::first() noexcept
208 {
209  return v_[0];
210 }
211 
212 
213 template<class T, unsigned N>
214 inline const T& Foam::FixedList<T, N>::first() const noexcept
215 {
216  return v_[0];
217 }
218 
219 
220 template<class T, unsigned N>
221 inline T& Foam::FixedList<T, N>::last() noexcept
222 {
223  return v_[N-1];
224 }
225 
226 
227 template<class T, unsigned N>
228 inline const T& Foam::FixedList<T, N>::last() const noexcept
229 {
230  return v_[N-1];
231 }
232 
233 
234 template<class T, unsigned N>
235 inline Foam::label Foam::FixedList<T, N>::fcIndex(const label i) const
236 {
237  return (i == N-1 ? 0 : i+1);
238 }
239 
240 
241 template<class T, unsigned N>
242 inline const T& Foam::FixedList<T, N>::fcValue(const label i) const
243 {
244  return this->operator[](this->fcIndex(i));
245 }
246 
247 
248 template<class T, unsigned N>
249 inline T& Foam::FixedList<T, N>::fcValue(const label i)
250 {
251  return this->operator[](this->fcIndex(i));
252 }
253 
254 
255 template<class T, unsigned N>
256 inline Foam::label Foam::FixedList<T, N>::rcIndex(const label i) const
257 {
258  return (i ? i-1 : N-1);
259 }
260 
261 
262 template<class T, unsigned N>
263 inline const T& Foam::FixedList<T, N>::rcValue(const label i) const
264 {
265  return this->operator[](this->rcIndex(i));
266 }
267 
268 
269 template<class T, unsigned N>
270 inline T& Foam::FixedList<T, N>::rcValue(const label i)
271 {
272  return this->operator[](this->rcIndex(i));
273 }
274 
275 
276 template<class T, unsigned N>
277 inline 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 
289 template<class T, unsigned N>
290 inline 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  }
298 }
299 
300 
301 template<class T, unsigned N>
302 inline 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  }
310 }
311 
312 
313 template<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 
330 template<class T, unsigned N>
332 (
333  const T& val,
334  label pos
335 ) const
336 {
337  return (this->find(val, pos) >= 0);
338 }
339 
340 
341 template<class T, unsigned N>
342 inline void Foam::FixedList<T, N>::resize(const label n)
343 {
344  #ifdef FULLDEBUG
345  checkSize(n);
346  #endif
347 }
348 
349 
350 template<class T, unsigned N>
351 inline void Foam::FixedList<T, N>::resize_nocopy(const label n)
352 {
353  #ifdef FULLDEBUG
354  checkSize(n);
355  #endif
356 }
357 
358 
359 template<class T, unsigned N>
360 inline void Foam::FixedList<T, N>::fill(const T& val)
361 {
362  for (unsigned i=0; i<N; ++i)
363  {
364  v_[i] = val;
365  }
366 }
367 
368 
369 template<class T, unsigned N>
371 {
372  for (unsigned i=0; i<N; ++i)
373  {
374  v_[i] = Zero;
375  }
376 }
377 
378 
379 template<class T, unsigned N>
381 {
382  if (this == &other)
383  {
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 }
392 
393 
394 template<class T, unsigned N>
396 {
397  if (this == &list)
398  {
399  return; // Self-assignment is a no-op
400  }
401 
402  for (unsigned i=0; i<N; ++i)
403  {
404  v_[i] = std::move(list[i]);
405  }
406 }
407 
408 
409 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
410 
411 template<class T, unsigned N>
412 inline T& Foam::FixedList<T, N>::operator[](const label i)
413 {
414  #ifdef FULLDEBUG
415  checkIndex(i);
416  #endif
417  return v_[i];
418 }
419 
420 
421 template<class T, unsigned N>
422 inline 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 
431 template<class T, unsigned N>
432 inline 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 
440 template<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 
451 template<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 
464 template<class T, unsigned N>
465 inline 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 
477 template<class T, unsigned N>
478 inline void Foam::FixedList<T, N>::operator=(const T& val)
479 {
480  this->fill(val);
481 }
482 
483 
484 template<class T, unsigned N>
486 {
487  this->fill(Zero);
488 }
489 
490 
491 template<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 
505 template<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 
522 template<class T, unsigned N>
523 inline typename Foam::FixedList<T, N>::iterator
525 {
526  return v_;
527 }
528 
529 
530 template<class T, unsigned N>
533 {
534  return v_;
535 }
536 
537 
538 template<class T, unsigned N>
541 {
542  return v_;
543 }
544 
545 
546 template<class T, unsigned N>
547 inline typename Foam::FixedList<T, N>::iterator
549 {
550  return (v_ + N);
551 }
552 
553 
554 template<class T, unsigned N>
557 {
558  return (v_ + N);
559 }
560 
561 
562 template<class T, unsigned N>
565 {
566  return (v_ + N);
567 }
568 
569 
570 template<class T, unsigned N>
573 {
574  return reverse_iterator(end());
575 }
576 
577 
578 template<class T, unsigned N>
581 {
582  return const_reverse_iterator(end());
583 }
584 
585 
586 template<class T, unsigned N>
589 {
590  return const_reverse_iterator(end());
591 }
592 
593 
594 template<class T, unsigned N>
597 {
598  return reverse_iterator(begin());
599 }
600 
601 
602 template<class T, unsigned N>
605 {
606  return const_reverse_iterator(begin());
607 }
608 
609 
610 template<class T, unsigned N>
613 {
614  return const_reverse_iterator(begin());
615 }
616 
617 
618 // ************************************************************************* //
Foam::FixedList::resize_nocopy
void resize_nocopy(const label n)
Dummy function, to make FixedList consistent with List.
Definition: FixedListI.H:351
Foam::FixedList::fcValue
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: FixedListI.H:242
Foam::FixedList::clone
autoPtr< FixedList< T, N > > clone() const
Clone.
Definition: FixedListI.H:159
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
Foam::FixedList::null
static const FixedList< T, N > & null()
Return a null FixedList.
Definition: FixedListI.H:36
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::FixedList::end
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:548
Foam::FixedList::cend
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:564
Foam::FixedList::data
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:177
Foam::FixedList::crbegin
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:588
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::FixedList::checkIndex
void checkIndex(const label i) const
Check index is within valid range [0,N)
Definition: FixedListI.H:302
Foam::FixedList::swap
void swap(FixedList< T, N > &other)
Swap lists by swapping the content of the individual list elements.
Definition: FixedListI.H:380
Foam::FixedList::transfer
void transfer(FixedList< T, N > &list)
Definition: FixedListI.H:395
Foam::FixedList::cdata_bytes
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:185
Foam::FixedList::last
T & last() noexcept
The last element of the list, position [N-1].
Definition: FixedListI.H:221
Foam::FixedList::cdata
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:169
Foam::FixedList::checkStart
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: FixedListI.H:277
Foam::FixedList::size_bytes
static std::streamsize size_bytes() noexcept
Number of contiguous bytes for the list data,.
Definition: FixedListI.H:200
Foam::FixedList::operator=
void operator=(const T list[N])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:432
Foam::FixedList::resize
void resize(const label n)
Dummy function, to make FixedList consistent with List.
Definition: FixedListI.H:342
Foam::FixedList::fill
void fill(const T &val)
Assign all entries to the given value.
Definition: FixedListI.H:360
Foam::FixedList::fcIndex
label fcIndex(const label i) const
Definition: FixedListI.H:235
Foam::FixedList::first
T & first() noexcept
The first element of the list, position [0].
Definition: FixedListI.H:207
Foam::LList
Template class for non-intrusive linked lists.
Definition: LList.H:54
Foam::FixedList::begin
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:524
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::FixedList::iterator
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:122
Foam::LList::begin
iterator begin()
Iterator to first item in list with non-const access.
Definition: LList.H:497
Foam::FixedList::rcValue
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: FixedListI.H:263
Foam::FixedList::crend
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:612
Foam::FatalError
error FatalError
Foam::FixedList::rend
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:596
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::FixedList::operator[]
T & operator[](const label i)
Return element of FixedList.
Definition: FixedListI.H:412
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::FixedList::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator (const access)
Definition: FixedList.H:137
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::FixedList::data_bytes
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:193
UList.H
SLList.H
Non-intrusive singly-linked list.
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::UList
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:103
Foam::FixedList::FixedList
FixedList()=default
Default construct.
Foam::FixedList::cbegin
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:540
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
N
const Vector< label > N(dict.get< Vector< label >>("N"))
Foam::FixedList::found
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: FixedListI.H:332
Foam::FixedList::checkSize
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition: FixedListI.H:290
Foam::FixedList::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access)
Definition: FixedList.H:134
Foam::FixedList::rcIndex
label rcIndex(const label i) const
Definition: FixedListI.H:256
Foam::FixedList::rbegin
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:572
Foam::FixedList::uniform
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: FixedListI.H:314
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177