ListI.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 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 template<class T>
32 inline void Foam::List<T>::doAlloc()
33 {
34  if (this->size_ > 0)
35  {
36  // With sign-check to avoid spurious -Walloc-size-larger-than
37  this->v_ = new T[this->size_];
38  }
39 }
40 
41 
42 template<class T>
43 inline void Foam::List<T>::reAlloc(const label len)
44 {
45  if (this->size_ != len)
46  {
47  clear();
48  this->size_ = len;
49  doAlloc();
50  }
51 }
52 
53 
54 template<class T>
55 template<class List2>
56 inline void Foam::List<T>::copyList(const List2& list)
57 {
58  const label len = this->size_;
59 
60  for (label i=0; i<len; ++i)
61  {
62  this->operator[](i) = list[i];
63  }
64 }
65 
66 
67 template<class T>
68 template<class InputIterator>
70 (
71  InputIterator begIter,
72  InputIterator endIter,
73  const label len
74 )
75 :
76  UList<T>(nullptr, len)
77 {
78  if (this->size_)
79  {
80  doAlloc();
81 
82  InputIterator iter = begIter;
83  for (label i = 0; i < len; ++i)
84  {
85  this->operator[](i) = *iter;
86  ++iter;
87  }
88  }
89 }
90 
91 
92 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
93 
94 template<class T>
95 inline constexpr Foam::List<T>::List() noexcept
96 {}
97 
98 
99 template<class T>
101 {
102  return autoPtr<List<T>>::New(*this);
103 }
104 
105 
106 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
107 
108 template<class T>
110 {
111  return NullObjectRef<List<T>>();
112 }
113 
114 
115 template<class T>
116 inline void Foam::List<T>::clear()
117 {
118  if (this->v_)
119  {
120  delete[] this->v_;
121  this->v_ = nullptr;
122  }
123  this->size_ = 0;
124 }
125 
126 
127 namespace Foam
128 {
129  // Template specialization for bool. Fills new entries with false
130  template<>
131  inline void List<bool>::resize(const label newLen)
132  {
133  this->resize(newLen, false);
134  }
135 }
136 
137 
138 template<class T>
139 inline void Foam::List<T>::resize(const label len)
140 {
141  this->doResize(len);
142 }
143 
144 
145 template<class T>
146 inline void Foam::List<T>::resize_nocopy(const label len)
147 {
148  this->reAlloc(len);
149 }
150 
151 
152 template<class T>
153 inline T& Foam::List<T>::newElmt(const label i)
154 {
155  label n = this->size();
156 
157  if (i >= n)
158  {
159  if (!n) n = 1; // Avoid dead-lock when starting from zero-sized
160 
161  do
162  {
163  n *= 2;
164  }
165  while (i >= n);
166 
167  resize(n);
168  }
169 
170  return UList<T>::operator[](i);
171 }
172 
173 
174 template<class T>
175 inline void Foam::List<T>::append(const T& val)
176 {
177  const label idx = this->size();
178  resize(idx + 1);
179 
180  this->operator[](idx) = val; // copy element
181 }
182 
183 
184 template<class T>
185 inline void Foam::List<T>::append(T&& val)
186 {
187  const label idx = this->size();
188  resize(idx + 1);
189 
190  this->operator[](idx) = std::move(val); // move assign element
191 }
192 
193 
194 template<class T>
195 inline void Foam::List<T>::append(const UList<T>& list)
196 {
197  if (this == &list)
198  {
200  << "Attempted appending to self" << abort(FatalError);
201  }
202 
203  label idx = this->size();
204  const label n = list.size();
205 
206  resize(idx + n);
207 
208  for (label i=0; i<n; ++i)
209  {
210  this->operator[](idx++) = list[i]; // copy element
211  }
212 }
213 
214 
215 template<class T>
216 template<class Addr>
218 {
219  label idx = this->size();
220  const label n = list.size();
221 
222  resize(idx + n);
223 
224  for (label i=0; i<n; ++i)
225  {
226  this->operator[](idx++) = list[i]; // copy element
227  }
228 }
229 
230 
231 template<class T>
232 inline Foam::label Foam::List<T>::appendUniq(const T& val)
233 {
234  if (this->found(val))
235  {
236  return 0;
237  }
238  else
239  {
240  this->append(val);
241  return 1; // Increased list length by one
242  }
243 }
244 
245 
246 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
247 
248 template<class T>
249 inline void Foam::List<T>::operator=(const T& val)
250 {
251  UList<T>::operator=(val);
252 }
253 
254 
255 template<class T>
257 {
259 }
260 
261 
262 // ************************************************************************* //
Foam::List::newElmt
T & newElmt(const label i)
Definition: ListI.H:153
Foam::List::null
static const List< T > & null()
Return a null List.
Definition: ListI.H:109
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:175
Foam::List::resize_nocopy
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:146
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
n
label n
Definition: TABSMDCalcMethod2.H:31
resize
patchWriters resize(patchIds.size())
Foam::List::operator=
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:498
Foam::List::appendUniq
label appendUniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:232
Foam::FatalError
error FatalError
Foam::IndirectListBase::size
label size() const noexcept
The number of elements in the list.
Definition: IndirectListBase.H:125
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::List::List
constexpr List() noexcept
Default construct.
Definition: ListI.H:95
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
found
bool found
Definition: TABSMDCalcMethod2.H:32
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:453
clear
patchWriters clear()
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
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::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::List::clone
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:100
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62