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-2019 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  checkSize(list.size());
143 
144  auto iter = list.begin();
145  for (unsigned i=0; i<N; ++i)
146  {
147  v_[i] = *iter;
148  ++iter;
149  }
150 }
151 
152 
153 template<class T, unsigned N>
156 {
157  return autoPtr<FixedList<T, N>>::New(*this);
158 }
159 
160 
161 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
162 
163 template<class T, unsigned N>
164 inline const T*
166 {
167  return v_;
168 }
169 
170 
171 template<class T, unsigned N>
172 inline T*
174 {
175  return v_;
176 }
177 
178 
179 template<class T, unsigned N>
180 inline T& Foam::FixedList<T, N>::first() noexcept
181 {
182  return v_[0];
183 }
184 
185 
186 template<class T, unsigned N>
187 inline const T& Foam::FixedList<T, N>::first() const noexcept
188 {
189  return v_[0];
190 }
191 
192 
193 template<class T, unsigned N>
194 inline T& Foam::FixedList<T, N>::last() noexcept
195 {
196  return v_[N-1];
197 }
198 
199 
200 template<class T, unsigned N>
201 inline const T& Foam::FixedList<T, N>::last() const noexcept
202 {
203  return v_[N-1];
204 }
205 
206 
207 template<class T, unsigned N>
209 {
210  return (i == N-1 ? 0 : i+1);
211 }
212 
213 
214 template<class T, unsigned N>
215 inline const T& Foam::FixedList<T, N>::fcValue(const label i) const
216 {
217  return this->operator[](this->fcIndex(i));
218 }
219 
220 
221 template<class T, unsigned N>
223 {
224  return this->operator[](this->fcIndex(i));
225 }
226 
227 
228 template<class T, unsigned N>
230 {
231  return (i ? i-1 : N-1);
232 }
233 
234 
235 template<class T, unsigned N>
236 inline const T& Foam::FixedList<T, N>::rcValue(const label i) const
237 {
238  return this->operator[](this->rcIndex(i));
239 }
240 
241 
242 template<class T, unsigned N>
244 {
245  return this->operator[](this->rcIndex(i));
246 }
247 
248 
249 template<class T, unsigned N>
251 {
252  if (start < 0 || (start && unsigned(start) >= N))
253  {
254  // Note: always accept start=0, even for zero-sized lists
256  << "start " << start << " out of range [0," << N << ")"
257  << abort(FatalError);
258  }
259 }
260 
261 
262 template<class T, unsigned N>
263 inline void Foam::FixedList<T, N>::checkSize(const label size) const
264 {
265  if (unsigned(size) != N)
266  {
268  << "size " << size << " != " << N
269  << abort(FatalError);
270  }
271 }
272 
273 
274 template<class T, unsigned N>
275 inline void Foam::FixedList<T, N>::checkIndex(const label i) const
276 {
277  if (i < 0 || unsigned(i) >= N)
278  {
280  << "index " << i << " out of range [0," << N << ")"
281  << abort(FatalError);
282  }
283 }
284 
285 
286 template<class T, unsigned N>
288 {
289  if (empty()) return false; // <- Compile-time disabled anyhow
290 
291  for (unsigned i=1; i<N; ++i)
292  {
293  if (v_[0] != v_[i])
294  {
295  return false;
296  }
297  }
298 
299  return true;
300 }
301 
302 
303 template<class T, unsigned N>
305 (
306  const T& val,
307  const label start
308 ) const
309 {
310  return (this->find(val, start) >= 0);
311 }
312 
313 
314 template<class T, unsigned N>
316 {
317  #ifdef FULLDEBUG
318  checkSize(n);
319  #endif
320 }
321 
322 template<class T, unsigned N>
324 {
325  #ifdef FULLDEBUG
326  checkSize(n);
327  #endif
328 }
329 
330 
331 template<class T, unsigned N>
333 {
334  if (this == &list)
335  {
336  return; // Self-swap is a no-op
337  }
338 
339  for (unsigned i=0; i<N; ++i)
340  {
341  Foam::Swap(v_[i], list.v_[i]);
342  }
343 }
344 
345 
346 template<class T, unsigned N>
348 {
349  if (this == &list)
350  {
351  return; // Self-assignment is a no-op
352  }
353 
354  for (unsigned i=0; i<N; ++i)
355  {
356  v_[i] = std::move(list[i]);
357  }
358 }
359 
360 
361 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
362 
363 template<class T, unsigned N>
365 {
366  #ifdef FULLDEBUG
367  checkIndex(i);
368  #endif
369  return v_[i];
370 }
371 
372 
373 template<class T, unsigned N>
374 inline const T& Foam::FixedList<T, N>::operator[](const label i) const
375 {
376  #ifdef FULLDEBUG
377  checkIndex(i);
378  #endif
379  return v_[i];
380 }
381 
382 
383 template<class T, unsigned N>
384 inline void Foam::FixedList<T, N>::operator=(const T list[N])
385 {
386  for (unsigned i=0; i<N; ++i)
387  {
388  v_[i] = list[i];
389  }
390 }
391 
392 template<class T, unsigned N>
394 {
395  checkSize(list.size());
396 
397  for (unsigned i=0; i<N; ++i)
398  {
399  v_[i] = list[i];
400  }
401 }
402 
403 template<class T, unsigned N>
405 {
406  checkSize(list.size());
407 
408  auto iter = list.begin();
409  for (unsigned i=0; i<N; ++i)
410  {
411  v_[i] = *iter;
412  ++iter;
413  }
414 }
415 
416 template<class T, unsigned N>
417 inline void Foam::FixedList<T, N>::operator=(std::initializer_list<T> list)
418 {
419  checkSize(list.size());
420 
421  auto iter = list.begin();
422  for (unsigned i=0; i<N; ++i)
423  {
424  v_[i] = *iter;
425  ++iter;
426  }
427 }
428 
429 template<class T, unsigned N>
431 {
432  for (unsigned i=0; i<N; ++i)
433  {
434  v_[i] = val;
435  }
436 }
437 
438 template<class T, unsigned N>
440 {
441  if (this == &list)
442  {
443  return; // Self-assignment is a no-op
444  }
445 
446  for (unsigned i=0; i<N; ++i)
447  {
448  v_[i] = list.v_[i];
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  // No significant speedup observed for copy assignment on simple types,
461  // use move assignment for generality with more complex types
462  for (unsigned i=0; i<N; ++i)
463  {
464  v_[i] = std::move(list.v_[i]);
465  }
466 }
467 
468 
469 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
470 
471 template<class T, unsigned N>
472 inline typename Foam::FixedList<T, N>::iterator
474 {
475  return v_;
476 }
477 
478 
479 template<class T, unsigned N>
482 {
483  return v_;
484 }
485 
486 
487 template<class T, unsigned N>
490 {
491  return v_;
492 }
493 
494 
495 template<class T, unsigned N>
496 inline typename Foam::FixedList<T, N>::iterator
498 {
499  return (v_ + N);
500 }
501 
502 
503 template<class T, unsigned N>
506 {
507  return (v_ + N);
508 }
509 
510 
511 template<class T, unsigned N>
514 {
515  return (v_ + N);
516 }
517 
518 
519 template<class T, unsigned N>
522 {
523  return reverse_iterator(end());
524 }
525 
526 
527 template<class T, unsigned N>
530 {
531  return const_reverse_iterator(end());
532 }
533 
534 
535 template<class T, unsigned N>
538 {
539  return const_reverse_iterator(end());
540 }
541 
542 
543 template<class T, unsigned N>
546 {
547  return reverse_iterator(begin());
548 }
549 
550 
551 template<class T, unsigned N>
554 {
555  return const_reverse_iterator(begin());
556 }
557 
558 
559 template<class T, unsigned N>
562 {
563  return const_reverse_iterator(begin());
564 }
565 
566 
567 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
568 
569 template<class T, unsigned N>
571 {
572  lhs.swap(rhs);
573 }
574 
575 
576 // ************************************************************************* //
Foam::FixedList::fcValue
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: FixedListI.H:215
Foam::FixedList::clone
autoPtr< FixedList< T, N > > clone() const
Clone.
Definition: FixedListI.H:155
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
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:91
Foam::FixedList::begin
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:473
Foam::FixedList::data
T * data() noexcept
Return a pointer to the first data element.
Definition: FixedListI.H:173
Foam::FixedList::crbegin
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:537
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::FixedList::checkIndex
void checkIndex(const label i) const
Check index is within valid range [0,N)
Definition: FixedListI.H:275
Foam::FixedList::transfer
void transfer(FixedList< T, N > &list)
Definition: FixedListI.H:347
Foam::FixedList::last
T & last() noexcept
The last element of the list, position [N-1].
Definition: FixedListI.H:194
Foam::FixedList::cdata
const T * cdata() const noexcept
Return a const pointer to the first data element.
Definition: FixedListI.H:165
Foam::FixedList::checkStart
void checkStart(const label start) const
Check start is within valid range [0,size)
Definition: FixedListI.H:250
Foam::FixedList::operator=
void operator=(const T list[N])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:384
Foam::FixedList::resize
void resize(const label n)
Dummy resize function, to make FixedList consistent with List.
Definition: FixedListI.H:315
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:208
Foam::FixedList::first
T & first() noexcept
The first element of the list, position [0].
Definition: FixedListI.H:180
Foam::LList
Template class for non-intrusive linked lists.
Definition: LList.H:54
Foam::FixedList::found
bool found(const T &val, const label start=0) const
True if the value if found in the list. Linear search.
Definition: FixedListI.H:305
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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:236
Foam::FixedList::cend
const_iterator cend() const
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:513
Foam::FixedList::crend
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:561
Foam::FatalError
error FatalError
Foam::FixedList::rend
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:545
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:115
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:323
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:364
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:497
Foam::FixedList::cbegin
const_iterator cbegin() const
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:489
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:355
UList.H
SLList.H
Non-intrusive singly-linked list.
Foam::start
label ListType::const_reference const label start
Definition: ListOps.H:408
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
Null constructor.
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::checkSize
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition: FixedListI.H:263
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:229
Foam::FixedList::rbegin
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:521
Foam::FixedList::swap
void swap(FixedList< T, N > &list)
Swap lists by swapping the content of the individual list elements.
Definition: FixedListI.H:332
Foam::FixedList::uniform
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: FixedListI.H:287
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61