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