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 
32 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33 
34 template<class T, unsigned N>
36 {
37  return NullObjectRef<FixedList<T, N>>();
38 }
39 
40 
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 
43 template<class T, unsigned N>
45 {
46  this->fill(val);
47 }
48 
49 
50 template<class T, unsigned N>
52 {
53  this->fill(Zero);
54 }
55 
56 
57 template<class T, unsigned N>
58 inline Foam::FixedList<T, N>::FixedList(const T list[N])
59 {
60  for (unsigned i=0; i<N; ++i)
61  {
62  v_[i] = list[i];
63  }
64 }
65 
66 
67 template<class T, unsigned N>
68 inline Foam::FixedList<T, N>::FixedList(const FixedList<T, N>& list)
69 {
70  for (unsigned i=0; i<N; ++i)
71  {
72  v_[i] = list.v_[i];
73  }
74 }
75 
76 
77 template<class T, unsigned N>
78 inline Foam::FixedList<T, N>::FixedList(FixedList<T, N>&& list)
79 {
80  for (unsigned i=0; i<N; ++i)
81  {
82  v_[i] = std::move(list.v_[i]);
83  }
84 }
85 
86 
87 template<class T, unsigned N>
88 inline Foam::FixedList<T, N>::FixedList(std::initializer_list<T> list)
89 {
90  checkSize(list.size());
91 
92  auto iter = list.begin();
93  for (unsigned i=0; i<N; ++i)
94  {
95  v_[i] = *iter;
96  ++iter;
97  }
98 }
99 
100 
101 template<class T, unsigned N>
102 inline Foam::FixedList<T, N>::FixedList(const UList<T>& list)
103 {
104  checkSize(list.size());
105 
106  for (unsigned i=0; i<N; ++i)
107  {
108  v_[i] = list[i];
109  }
110 }
111 
112 
113 template<class T, unsigned N>
114 template<unsigned AnyNum>
116 (
117  const FixedList<T, AnyNum>& list,
118  const FixedList<label, N>& indices
119 )
120 {
121  for (unsigned i=0; i<N; ++i)
122  {
123  v_[i] = list[indices[i]];
124  }
125 }
126 
127 
128 template<class T, unsigned N>
130 (
131  const UList<T>& list,
132  const FixedList<label, N>& indices
133 )
134 {
135  for (unsigned i=0; i<N; ++i)
136  {
137  v_[i] = list[indices[i]];
138  }
139 }
140 
141 
142 template<class T, unsigned N>
143 inline Foam::FixedList<T, N>::FixedList(const SLList<T>& list)
144 {
145  checkSize(list.size());
146 
147  auto iter = list.begin();
148  for (unsigned i=0; i<N; ++i)
149  {
150  v_[i] = *iter;
151  ++iter;
152  }
153 }
154 
155 
156 template<class T, unsigned N>
159 {
160  return autoPtr<FixedList<T, N>>::New(*this);
161 }
162 
163 
164 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
165 
166 template<class T, unsigned N>
167 inline const T*
169 {
170  return v_;
171 }
172 
173 
174 template<class T, unsigned N>
175 inline T*
177 {
178  return v_;
179 }
180 
181 
182 template<class T, unsigned N>
183 inline const char*
185 {
186  return reinterpret_cast<const char*>(v_);
187 }
188 
189 
190 template<class T, unsigned N>
191 inline char*
193 {
194  return reinterpret_cast<char*>(v_);
195 }
196 
197 
198 template<class T, unsigned N>
199 inline std::streamsize Foam::FixedList<T, N>::size_bytes() noexcept
200 {
201  return N*sizeof(T);
202 }
203 
204 
205 template<class T, unsigned N>
206 template<unsigned Index>
208 {
209  static_assert(Index < N, "Address outside FixedList range");
210  return v_[Index];
211 }
212 
213 
214 template<class T, unsigned N>
215 template<unsigned Index>
216 inline const T& Foam::FixedList<T, N>::get() const noexcept
217 {
218  static_assert(Index < N, "Address outside FixedList range");
219  return v_[Index];
220 }
221 
222 
223 template<class T, unsigned N>
225 {
226  return v_[0];
227 }
228 
229 
230 template<class T, unsigned N>
232 {
233  return v_[0];
234 }
235 
236 
237 template<class T, unsigned N>
239 {
240  return v_[N-1];
241 }
242 
243 
244 template<class T, unsigned N>
246 {
247  return v_[N-1];
248 }
249 
250 
251 template<class T, unsigned N>
252 inline Foam::label Foam::FixedList<T, N>::fcIndex(const label i) const
253 {
254  return (i == N-1 ? 0 : i+1);
255 }
256 
257 
258 template<class T, unsigned N>
259 inline const T& Foam::FixedList<T, N>::fcValue(const label i) const
260 {
261  return this->operator[](this->fcIndex(i));
262 }
263 
264 
265 template<class T, unsigned N>
266 inline T& Foam::FixedList<T, N>::fcValue(const label i)
267 {
268  return this->operator[](this->fcIndex(i));
269 }
270 
271 
272 template<class T, unsigned N>
273 inline Foam::label Foam::FixedList<T, N>::rcIndex(const label i) const
274 {
275  return (i ? i-1 : N-1);
276 }
277 
278 
279 template<class T, unsigned N>
280 inline const T& Foam::FixedList<T, N>::rcValue(const label i) const
281 {
282  return this->operator[](this->rcIndex(i));
283 }
284 
285 
286 template<class T, unsigned N>
287 inline T& Foam::FixedList<T, N>::rcValue(const label i)
288 {
289  return this->operator[](this->rcIndex(i));
290 }
291 
292 
293 template<class T, unsigned N>
294 inline void Foam::FixedList<T, N>::checkStart(const label start) const
295 {
296  if (start < 0 || (start && unsigned(start) >= N))
297  {
298  // Note: always accept start=0, even for zero-sized lists
300  << "start " << start << " out of range [0," << N << ")"
301  << abort(FatalError);
302  }
303 }
304 
305 
306 template<class T, unsigned N>
307 inline void Foam::FixedList<T, N>::checkSize(const label size) const
308 {
309  if (unsigned(size) != N)
310  {
312  << "size " << size << " != " << N
313  << abort(FatalError);
314  }
315 }
316 
317 
318 template<class T, unsigned N>
319 inline void Foam::FixedList<T, N>::checkIndex(const label i) const
320 {
321  if (i < 0 || unsigned(i) >= N)
322  {
324  << "index " << i << " out of range [0," << N << ")"
325  << abort(FatalError);
326  }
327 }
328 
329 
330 template<class T, unsigned N>
331 inline bool Foam::FixedList<T, N>::uniform() const
332 {
333  if (empty()) return false; // <- Compile-time disabled anyhow
334 
335  for (unsigned i=1; i<N; ++i)
336  {
337  if (v_[0] != v_[i])
338  {
339  return false;
340  }
341  }
343  return true;
344 }
345 
346 
347 template<class T, unsigned N>
349 (
350  const T& val,
351  label pos
352 ) const
353 {
354  return (this->find(val, pos) >= 0);
355 }
356 
357 
358 template<class T, unsigned N>
359 inline void Foam::FixedList<T, N>::resize(const label n)
360 {
361  #ifdef FULLDEBUG
362  checkSize(n);
363  #endif
364 }
365 
366 
367 template<class T, unsigned N>
368 inline void Foam::FixedList<T, N>::resize_nocopy(const label n)
369 {
370  #ifdef FULLDEBUG
371  checkSize(n);
372  #endif
373 }
374 
375 
376 template<class T, unsigned N>
377 inline void Foam::FixedList<T, N>::fill(const T& val)
378 {
379  for (unsigned i=0; i<N; ++i)
380  {
381  v_[i] = val;
382  }
383 }
384 
385 
386 template<class T, unsigned N>
387 inline void Foam::FixedList<T, N>::fill(const Foam::zero)
388 {
389  for (unsigned i=0; i<N; ++i)
390  {
391  v_[i] = Zero;
392  }
393 }
394 
395 
396 template<class T, unsigned N>
397 inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& other)
398 {
399  if (this == &other)
400  {
401  return; // Self-swap is a no-op
402  }
403 
404  for (unsigned i=0; i<N; ++i)
405  {
406  Foam::Swap(v_[i], other.v_[i]);
407  }
408 }
409 
410 
411 template<class T, unsigned N>
412 inline void Foam::FixedList<T, N>::transfer(FixedList<T, N>& list)
413 {
414  if (this == &list)
415  {
416  return; // Self-assignment is a no-op
417  }
418 
419  for (unsigned i=0; i<N; ++i)
420  {
421  v_[i] = std::move(list[i]);
422  }
423 }
424 
425 
426 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
427 
428 template<class T, unsigned N>
429 inline T& Foam::FixedList<T, N>::operator[](const label i)
430 {
431  #ifdef FULLDEBUG
432  checkIndex(i);
433  #endif
434  return v_[i];
435 }
436 
437 
438 template<class T, unsigned N>
439 inline const T& Foam::FixedList<T, N>::operator[](const label i) const
440 {
441  #ifdef FULLDEBUG
442  checkIndex(i);
443  #endif
444  return v_[i];
445 }
446 
447 
448 template<class T, unsigned N>
449 inline void Foam::FixedList<T, N>::operator=(const T list[N])
450 {
451  for (unsigned i=0; i<N; ++i)
452  {
453  v_[i] = list[i];
454  }
455 }
456 
457 template<class T, unsigned N>
458 inline void Foam::FixedList<T, N>::operator=(const UList<T>& list)
459 {
460  checkSize(list.size());
461 
462  for (unsigned i=0; i<N; ++i)
463  {
464  v_[i] = list[i];
465  }
466 }
467 
468 template<class T, unsigned N>
469 inline void Foam::FixedList<T, N>::operator=(const SLList<T>& list)
470 {
471  checkSize(list.size());
472 
473  auto iter = list.begin();
474  for (unsigned i=0; i<N; ++i)
475  {
476  v_[i] = *iter;
477  ++iter;
478  }
479 }
480 
481 template<class T, unsigned N>
482 inline void Foam::FixedList<T, N>::operator=(std::initializer_list<T> list)
483 {
484  checkSize(list.size());
485 
486  auto iter = list.begin();
487  for (unsigned i=0; i<N; ++i)
488  {
489  v_[i] = *iter;
490  ++iter;
491  }
492 }
493 
494 template<class T, unsigned N>
495 inline void Foam::FixedList<T, N>::operator=(const T& val)
496 {
497  this->fill(val);
498 }
499 
500 
501 template<class T, unsigned N>
503 {
504  this->fill(Zero);
505 }
506 
507 
508 template<class T, unsigned N>
509 inline void Foam::FixedList<T, N>::operator=(const FixedList<T, N>& list)
510 {
511  if (this == &list)
512  {
513  return; // Self-assignment is a no-op
514  }
515 
516  for (unsigned i=0; i<N; ++i)
517  {
518  v_[i] = list.v_[i];
519  }
520 }
521 
522 template<class T, unsigned N>
523 inline void Foam::FixedList<T, N>::operator=(FixedList<T, N>&& list)
524 {
525  if (this == &list)
526  {
527  return; // Self-assignment is a no-op
528  }
529 
530  for (unsigned i=0; i<N; ++i)
531  {
532  v_[i] = std::move(list.v_[i]);
533  }
534 }
535 
536 
537 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
538 
539 template<class T, unsigned N>
540 inline typename Foam::FixedList<T, N>::iterator
542 {
543  return v_;
544 }
545 
546 
547 template<class T, unsigned N>
550 {
551  return v_;
552 }
553 
554 
555 template<class T, unsigned N>
558 {
559  return v_;
560 }
561 
562 
563 template<class T, unsigned N>
564 inline typename Foam::FixedList<T, N>::iterator
566 {
567  return (v_ + N);
568 }
569 
570 
571 template<class T, unsigned N>
574 {
575  return (v_ + N);
576 }
577 
578 
579 template<class T, unsigned N>
582 {
583  return (v_ + N);
584 }
585 
586 
587 template<class T, unsigned N>
590 {
591  return reverse_iterator(end());
592 }
593 
594 
595 template<class T, unsigned N>
598 {
599  return const_reverse_iterator(end());
600 }
601 
602 
603 template<class T, unsigned N>
606 {
607  return const_reverse_iterator(end());
608 }
609 
610 
611 template<class T, unsigned N>
614 {
615  return reverse_iterator(begin());
616 }
617 
618 
619 template<class T, unsigned N>
622 {
623  return const_reverse_iterator(begin());
624 }
625 
626 
627 template<class T, unsigned N>
630 {
631  return const_reverse_iterator(begin());
632 }
633 
634 
635 // ************************************************************************* //
void operator=(const T list[N])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:442
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:177
void resize(const label n)
Dummy function, to make FixedList consistent with List.
Definition: FixedListI.H:352
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:790
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: FixedListI.H:342
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:101
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:534
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:578
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: FixedListI.H:324
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:558
T & back() noexcept
Access last element of the list, position [N-1].
Definition: FixedListI.H:231
void checkIndex(const label i) const
Check index is within valid range [0,N)
Definition: FixedListI.H:312
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
autoPtr< FixedList< T, N > > clone() const
Clone.
Definition: FixedListI.H:151
T & operator[](const label i)
Return element of FixedList.
Definition: FixedListI.H:422
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:606
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:161
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:192
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: FixedListI.H:287
void fill(const T &val)
Assign all entries to the given value.
Definition: FixedListI.H:370
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:185
errorManip< error > abort(error &err)
Definition: errorManip.H:139
label fcIndex(const label i) const
Return the forward circular index, i.e. next index which returns to the first at the end of the list...
Definition: FixedListI.H:245
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:200
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition: FixedListI.H:300
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:193
label rcIndex(const label i) const
Return the reverse circular index, i.e. previous index which returns to the last at the beginning of ...
Definition: FixedListI.H:266
const volScalarField & T
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:550
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:622
void resize_nocopy(const label n)
Dummy function, to make FixedList consistent with List.
Definition: FixedListI.H:361
T & front() noexcept
Access first element of the list, position [0].
Definition: FixedListI.H:217
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: FixedListI.H:273
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: FixedListI.H:252
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:647
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:582
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:58
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:574
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:169
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:598
Non-intrusive singly-linked list.
void transfer(FixedList< T, N > &list)
Transfer by swapping using a move assignment for the content of the individual list elements...
Definition: FixedListI.H:405
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:160
static const FixedList< T, N > & null()
Return a null FixedList.
Definition: FixedListI.H:28
FixedList()=default
Default construct.
void swap(FixedList< T, N > &other)
Swap lists by swapping the content of the individual list elements.
Definition: FixedListI.H:390
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:157