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-2020 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 <type_traits>
32 #include <utility>
33 
34 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
35 
36 template<class T, unsigned N>
38 {
39  return NullObjectRef<FixedList<T, N>>();
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
45 template<class T, unsigned N>
47 {
48  for (unsigned i=0; i<N; ++i)
49  {
50  v_[i] = val;
51  }
52 }
53 
54 
55 template<class T, unsigned N>
57 {
58  for (unsigned i=0; i<N; ++i)
59  {
60  v_[i] = Zero;
61  }
62 }
63 
64 
65 template<class T, unsigned N>
67 {
68  for (unsigned i=0; i<N; ++i)
69  {
70  v_[i] = list[i];
71  }
72 }
73 
74 
75 template<class T, unsigned N>
77 {
78  for (unsigned i=0; i<N; ++i)
79  {
80  v_[i] = list.v_[i];
81  }
82 }
83 
84 
85 template<class T, unsigned N>
87 {
88  for (unsigned i=0; i<N; ++i)
89  {
90  v_[i] = std::move(list.v_[i]);
91  }
92 }
93 
94 
95 template<class T, unsigned N>
96 template<class InputIterator>
98 (
99  InputIterator begIter,
100  InputIterator endIter
101 )
102 {
103  checkSize(std::distance(begIter, endIter));
104 
105  for (unsigned i=0; i<N; ++i)
106  {
107  v_[i] = *begIter;
108  ++begIter;
109  }
110 }
111 
112 
113 template<class T, unsigned N>
114 inline Foam::FixedList<T, N>::FixedList(std::initializer_list<T> list)
115 {
116  checkSize(list.size());
117 
118  auto iter = list.begin();
119  for (unsigned i=0; i<N; ++i)
120  {
121  v_[i] = *iter;
122  ++iter;
123  }
124 }
125 
126 
127 template<class T, unsigned N>
129 {
130  checkSize(list.size());
131 
132  for (unsigned i=0; i<N; ++i)
133  {
134  v_[i] = list[i];
135  }
136 }
137 
138 
139 template<class T, unsigned N>
141 (
142  const UList<T>& list,
143  const FixedList<label, N>& indices
144 )
145 {
146  for (unsigned i=0; i<N; ++i)
147  {
148  v_[i] = list[indices[i]];
149  }
150 }
151 
152 
153 template<class T, unsigned N>
155 {
156  checkSize(list.size());
157 
158  auto iter = list.begin();
159  for (unsigned i=0; i<N; ++i)
160  {
161  v_[i] = *iter;
162  ++iter;
163  }
164 }
165 
166 
167 template<class T, unsigned N>
170 {
171  return autoPtr<FixedList<T, N>>::New(*this);
172 }
173 
174 
175 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
176 
177 template<class T, unsigned N>
178 inline const T*
180 {
181  return v_;
182 }
183 
184 
185 template<class T, unsigned N>
186 inline T*
188 {
189  return v_;
190 }
191 
192 
193 template<class T, unsigned N>
194 inline T& Foam::FixedList<T, N>::first() noexcept
195 {
196  return v_[0];
197 }
198 
199 
200 template<class T, unsigned N>
201 inline const T& Foam::FixedList<T, N>::first() const noexcept
202 {
203  return v_[0];
204 }
205 
206 
207 template<class T, unsigned N>
208 inline T& Foam::FixedList<T, N>::last() noexcept
209 {
210  return v_[N-1];
211 }
212 
213 
214 template<class T, unsigned N>
215 inline const T& Foam::FixedList<T, N>::last() const noexcept
216 {
217  return v_[N-1];
218 }
219 
220 
221 template<class T, unsigned N>
222 inline Foam::label Foam::FixedList<T, N>::fcIndex(const label i) const
223 {
224  return (i == N-1 ? 0 : i+1);
225 }
226 
227 
228 template<class T, unsigned N>
229 inline const T& Foam::FixedList<T, N>::fcValue(const label i) const
230 {
231  return this->operator[](this->fcIndex(i));
232 }
233 
234 
235 template<class T, unsigned N>
236 inline T& Foam::FixedList<T, N>::fcValue(const label i)
237 {
238  return this->operator[](this->fcIndex(i));
239 }
240 
241 
242 template<class T, unsigned N>
243 inline Foam::label Foam::FixedList<T, N>::rcIndex(const label i) const
244 {
245  return (i ? i-1 : N-1);
246 }
247 
248 
249 template<class T, unsigned N>
250 inline const T& Foam::FixedList<T, N>::rcValue(const label i) const
251 {
252  return this->operator[](this->rcIndex(i));
253 }
254 
255 
256 template<class T, unsigned N>
257 inline T& Foam::FixedList<T, N>::rcValue(const label i)
258 {
259  return this->operator[](this->rcIndex(i));
260 }
261 
262 
263 template<class T, unsigned N>
264 inline void Foam::FixedList<T, N>::checkStart(const label start) const
265 {
266  if (start < 0 || (start && unsigned(start) >= N))
267  {
268  // Note: always accept start=0, even for zero-sized lists
270  << "start " << start << " out of range [0," << N << ")"
271  << abort(FatalError);
272  }
273 }
274 
275 
276 template<class T, unsigned N>
277 inline void Foam::FixedList<T, N>::checkSize(const label size) const
278 {
279  if (unsigned(size) != N)
280  {
282  << "size " << size << " != " << N
283  << abort(FatalError);
284  }
285 }
286 
287 
288 template<class T, unsigned N>
289 inline void Foam::FixedList<T, N>::checkIndex(const label i) const
290 {
291  if (i < 0 || unsigned(i) >= N)
292  {
294  << "index " << i << " out of range [0," << N << ")"
295  << abort(FatalError);
296  }
297 }
298 
299 
300 template<class T, unsigned N>
302 {
303  if (empty()) return false; // <- Compile-time disabled anyhow
304 
305  for (unsigned i=1; i<N; ++i)
306  {
307  if (v_[0] != v_[i])
308  {
309  return false;
310  }
311  }
312 
313  return true;
314 }
315 
316 
317 template<class T, unsigned N>
319 (
320  const T& val,
321  label pos
322 ) const
323 {
324  return (this->find(val, pos) >= 0);
325 }
326 
327 
328 template<class T, unsigned N>
329 inline void Foam::FixedList<T, N>::resize(const label n)
330 {
331  #ifdef FULLDEBUG
332  checkSize(n);
333  #endif
334 }
335 
336 template<class T, unsigned N>
337 inline void Foam::FixedList<T, N>::setSize(const label n)
338 {
339  #ifdef FULLDEBUG
340  checkSize(n);
341  #endif
342 }
343 
344 
345 template<class T, unsigned N>
347 {
348  if (this == &list)
349  {
350  return; // Self-swap is a no-op
351  }
352 
353  for (unsigned i=0; i<N; ++i)
354  {
355  Foam::Swap(v_[i], list.v_[i]);
356  }
357 }
358 
359 
360 template<class T, unsigned N>
362 {
363  if (this == &list)
364  {
365  return; // Self-assignment is a no-op
366  }
367 
368  for (unsigned i=0; i<N; ++i)
369  {
370  v_[i] = std::move(list[i]);
371  }
372 }
373 
374 
375 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
376 
377 template<class T, unsigned N>
378 inline T& Foam::FixedList<T, N>::operator[](const label i)
379 {
380  #ifdef FULLDEBUG
381  checkIndex(i);
382  #endif
383  return v_[i];
384 }
385 
386 
387 template<class T, unsigned N>
388 inline const T& Foam::FixedList<T, N>::operator[](const label i) const
389 {
390  #ifdef FULLDEBUG
391  checkIndex(i);
392  #endif
393  return v_[i];
394 }
395 
396 
397 template<class T, unsigned N>
398 inline void Foam::FixedList<T, N>::operator=(const T list[N])
399 {
400  for (unsigned i=0; i<N; ++i)
401  {
402  v_[i] = list[i];
403  }
404 }
405 
406 template<class T, unsigned N>
408 {
409  checkSize(list.size());
410 
411  for (unsigned i=0; i<N; ++i)
412  {
413  v_[i] = list[i];
414  }
415 }
416 
417 template<class T, unsigned N>
419 {
420  checkSize(list.size());
421 
422  auto iter = list.begin();
423  for (unsigned i=0; i<N; ++i)
424  {
425  v_[i] = *iter;
426  ++iter;
427  }
428 }
429 
430 template<class T, unsigned N>
431 inline void Foam::FixedList<T, N>::operator=(std::initializer_list<T> list)
432 {
433  checkSize(list.size());
434 
435  auto iter = list.begin();
436  for (unsigned i=0; i<N; ++i)
437  {
438  v_[i] = *iter;
439  ++iter;
440  }
441 }
442 
443 template<class T, unsigned N>
444 inline void Foam::FixedList<T, N>::operator=(const T& val)
445 {
446  for (unsigned i=0; i<N; ++i)
447  {
448  v_[i] = val;
449  }
450 }
451 
452 template<class T, unsigned N>
454 {
455  if (this == &list)
456  {
457  return; // Self-assignment is a no-op
458  }
459 
460  for (unsigned i=0; i<N; ++i)
461  {
462  v_[i] = list.v_[i];
463  }
464 }
465 
466 template<class T, unsigned N>
468 {
469  if (this == &list)
470  {
471  return; // Self-assignment is a no-op
472  }
473 
474  // No significant speedup observed for copy assignment on simple types,
475  // use move assignment for generality with more complex types
476  for (unsigned i=0; i<N; ++i)
477  {
478  v_[i] = std::move(list.v_[i]);
479  }
480 }
481 
482 
483 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
484 
485 template<class T, unsigned N>
486 inline typename Foam::FixedList<T, N>::iterator
488 {
489  return v_;
490 }
491 
492 
493 template<class T, unsigned N>
496 {
497  return v_;
498 }
499 
500 
501 template<class T, unsigned N>
504 {
505  return v_;
506 }
507 
508 
509 template<class T, unsigned N>
510 inline typename Foam::FixedList<T, N>::iterator
512 {
513  return (v_ + N);
514 }
515 
516 
517 template<class T, unsigned N>
520 {
521  return (v_ + N);
522 }
523 
524 
525 template<class T, unsigned N>
528 {
529  return (v_ + N);
530 }
531 
532 
533 template<class T, unsigned N>
536 {
537  return reverse_iterator(end());
538 }
539 
540 
541 template<class T, unsigned N>
544 {
545  return const_reverse_iterator(end());
546 }
547 
548 
549 template<class T, unsigned N>
552 {
553  return const_reverse_iterator(end());
554 }
555 
556 
557 template<class T, unsigned N>
560 {
561  return reverse_iterator(begin());
562 }
563 
564 
565 template<class T, unsigned N>
568 {
569  return const_reverse_iterator(begin());
570 }
571 
572 
573 template<class T, unsigned N>
576 {
577  return const_reverse_iterator(begin());
578 }
579 
580 
581 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
582 
583 template<class T, unsigned N>
585 {
586  lhs.swap(rhs);
587 }
588 
589 
590 // ************************************************************************* //
Foam::FixedList::fcValue
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: FixedListI.H:229
Foam::FixedList::clone
autoPtr< FixedList< T, N > > clone() const
Clone.
Definition: FixedListI.H:169
Foam::FixedList::null
static const FixedList< T, N > & null()
Return a null FixedList.
Definition: FixedListI.H:37
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::begin
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:487
Foam::FixedList::data
T * data() noexcept
Return a pointer to the first data element.
Definition: FixedListI.H:187
Foam::FixedList::crbegin
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:551
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:289
Foam::FixedList::transfer
void transfer(FixedList< T, N > &list)
Definition: FixedListI.H:361
Foam::FixedList::last
T & last() noexcept
The last element of the list, position [N-1].
Definition: FixedListI.H:208
Foam::FixedList::cdata
const T * cdata() const noexcept
Return a const pointer to the first data element.
Definition: FixedListI.H:179
Foam::FixedList::checkStart
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: FixedListI.H:264
Foam::FixedList::operator=
void operator=(const T list[N])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:398
Foam::FixedList::resize
void resize(const label n)
Dummy resize function, to make FixedList consistent with List.
Definition: FixedListI.H:329
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:909
Foam::FixedList::fcIndex
label fcIndex(const label i) const
Definition: FixedListI.H:222
Foam::FixedList::first
T & first() noexcept
The first element of the list, position [0].
Definition: FixedListI.H:194
Foam::LList
Template class for non-intrusive linked lists.
Definition: LList.H:54
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::FixedList::iterator
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:119
Foam::LList::begin
iterator begin()
Iterator to first item in list with non-const access.
Definition: LList.H:494
Foam::FixedList::rcValue
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: FixedListI.H:250
Foam::FixedList::cend
const_iterator cend() const
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:527
Foam::FixedList::crend
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:575
Foam::FatalError
error FatalError
Foam::FixedList::rend
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:559
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::FixedList::setSize
void setSize(const label n)
Dummy setSize function, to make FixedList consistent with List.
Definition: FixedListI.H:337
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::FixedList::operator[]
T & operator[](const label i)
Return element of FixedList.
Definition: FixedListI.H:378
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:134
Foam::FixedList::end
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:511
Foam::FixedList::cbegin
const_iterator cbegin() const
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:503
Foam::distance
scalar distance(const vector &p1, const vector &p2)
Definition: curveTools.C:12
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:372
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::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
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:319
Foam::FixedList::checkSize
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition: FixedListI.H:277
Foam::FixedList::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator (non-const access)
Definition: FixedList.H:131
Foam::FixedList::rcIndex
label rcIndex(const label i) const
Definition: FixedListI.H:243
Foam::FixedList::rbegin
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:535
Foam::FixedList::swap
void swap(FixedList< T, N > &list)
Swap lists by swapping the content of the individual list elements.
Definition: FixedListI.H:346
Foam::FixedList::uniform
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: FixedListI.H:301
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