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 #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  }
342 
343  return true;
344 }
345 
346 
347 template<class T, unsigned N>
348 inline bool Foam::FixedList<T, N>::contains(const T& val, label pos) const
349 {
350  return (this->find(val, pos) >= 0);
351 }
352 
353 
354 template<class T, unsigned N>
355 inline void Foam::FixedList<T, N>::resize(const label n)
356 {
357  #ifdef FULLDEBUG
358  checkSize(n);
359  #endif
360 }
361 
362 
363 template<class T, unsigned N>
364 inline void Foam::FixedList<T, N>::resize_nocopy(const label n)
365 {
366  #ifdef FULLDEBUG
367  checkSize(n);
368  #endif
369 }
370 
371 
372 template<class T, unsigned N>
373 inline void Foam::FixedList<T, N>::fill(const T& val)
374 {
375  for (unsigned i=0; i<N; ++i)
376  {
377  v_[i] = val;
378  }
379 }
380 
381 
382 template<class T, unsigned N>
383 inline void Foam::FixedList<T, N>::fill(const Foam::zero)
384 {
385  for (unsigned i=0; i<N; ++i)
386  {
387  v_[i] = Zero;
388  }
389 }
390 
391 
392 template<class T, unsigned N>
393 inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& other)
394 {
395  if (this == &other)
396  {
397  return; // Self-swap is a no-op
398  }
399 
400  for (unsigned i=0; i<N; ++i)
401  {
402  Foam::Swap(v_[i], other.v_[i]);
403  }
404 }
405 
406 
407 template<class T, unsigned N>
408 inline void Foam::FixedList<T, N>::transfer(FixedList<T, N>& list)
409 {
410  if (this == &list)
411  {
412  return; // Self-assignment is a no-op
413  }
414 
415  for (unsigned i=0; i<N; ++i)
416  {
417  v_[i] = std::move(list[i]);
418  }
419 }
420 
421 
422 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
423 
424 template<class T, unsigned N>
425 inline T& Foam::FixedList<T, N>::operator[](const label i)
426 {
427  #ifdef FULLDEBUG
428  checkIndex(i);
429  #endif
430  return v_[i];
431 }
432 
433 
434 template<class T, unsigned N>
435 inline const T& Foam::FixedList<T, N>::operator[](const label i) const
436 {
437  #ifdef FULLDEBUG
438  checkIndex(i);
439  #endif
440  return v_[i];
441 }
442 
443 
444 template<class T, unsigned N>
445 inline void Foam::FixedList<T, N>::operator=(const T list[N])
446 {
447  for (unsigned i=0; i<N; ++i)
448  {
449  v_[i] = list[i];
450  }
451 }
452 
453 template<class T, unsigned N>
454 inline void Foam::FixedList<T, N>::operator=(const UList<T>& list)
455 {
456  checkSize(list.size());
457 
458  for (unsigned i=0; i<N; ++i)
459  {
460  v_[i] = list[i];
461  }
462 }
463 
464 template<class T, unsigned N>
465 inline void Foam::FixedList<T, N>::operator=(const SLList<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=(std::initializer_list<T> list)
479 {
480  checkSize(list.size());
481 
482  auto iter = list.begin();
483  for (unsigned i=0; i<N; ++i)
484  {
485  v_[i] = *iter;
486  ++iter;
487  }
488 }
489 
490 template<class T, unsigned N>
491 inline void Foam::FixedList<T, N>::operator=(const T& val)
492 {
493  this->fill(val);
494 }
495 
496 
497 template<class T, unsigned N>
499 {
500  this->fill(Zero);
501 }
502 
503 
504 template<class T, unsigned N>
505 inline void Foam::FixedList<T, N>::operator=(const FixedList<T, N>& list)
506 {
507  if (this == &list)
508  {
509  return; // Self-assignment is a no-op
510  }
511 
512  for (unsigned i=0; i<N; ++i)
513  {
514  v_[i] = list.v_[i];
515  }
516 }
517 
518 template<class T, unsigned N>
519 inline void Foam::FixedList<T, N>::operator=(FixedList<T, N>&& list)
520 {
521  if (this == &list)
522  {
523  return; // Self-assignment is a no-op
524  }
525 
526  for (unsigned i=0; i<N; ++i)
527  {
528  v_[i] = std::move(list.v_[i]);
529  }
530 }
531 
532 
533 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
534 
535 template<class T, unsigned N>
536 inline typename Foam::FixedList<T, N>::iterator
538 {
539  return v_;
540 }
541 
542 
543 template<class T, unsigned N>
546 {
547  return v_;
548 }
549 
550 
551 template<class T, unsigned N>
554 {
555  return v_;
556 }
557 
558 
559 template<class T, unsigned N>
560 inline typename Foam::FixedList<T, N>::iterator
562 {
563  return (v_ + N);
564 }
565 
566 
567 template<class T, unsigned N>
570 {
571  return (v_ + N);
572 }
573 
574 
575 template<class T, unsigned N>
578 {
579  return (v_ + N);
580 }
581 
582 
583 template<class T, unsigned N>
586 {
587  return reverse_iterator(end());
588 }
589 
590 
591 template<class T, unsigned N>
594 {
595  return const_reverse_iterator(end());
596 }
597 
598 
599 template<class T, unsigned N>
602 {
603  return const_reverse_iterator(end());
604 }
605 
606 
607 template<class T, unsigned N>
610 {
611  return reverse_iterator(begin());
612 }
613 
614 
615 template<class T, unsigned N>
618 {
619  return const_reverse_iterator(begin());
620 }
621 
622 
623 template<class T, unsigned N>
626 {
627  return const_reverse_iterator(begin());
628 }
629 
630 
631 // ************************************************************************* //
void operator=(const T list[N])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:438
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:348
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:790
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:530
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
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.
bool contains(const T &val, label pos=0) const
Is the value contained in the list?
Definition: FixedListI.H:341
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:554
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
autoPtr< FixedList< T, N > > clone() const
Clone.
Definition: FixedListI.H:151
T & operator[](const label i)
Return element of FixedList.
Definition: FixedListI.H:418
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:602
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:366
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:194
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:546
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:618
void resize_nocopy(const label n)
Dummy function, to make FixedList consistent with List.
Definition: FixedListI.H:357
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:659
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:578
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:570
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:594
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:401
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:161
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:386
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:133