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-------------------------------------------------------------------------------
11License
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
31template<class T>
32inline 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
42template<class T>
43inline 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
54template<class T>
55template<class List2>
56inline 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
67template<class T>
68template<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
94template<class T>
95inline constexpr Foam::List<T>::List() noexcept
96{}
97
98
99template<class T>
101{
102 return autoPtr<List<T>>::New(*this);
103}
104
105
106// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
107
108template<class T>
110{
111 return NullObjectRef<List<T>>();
112}
113
114
115template<class T>
118 if (this->v_)
119 {
120 delete[] this->v_;
121 this->v_ = nullptr;
122 }
123 this->size_ = 0;
124}
125
126
127namespace 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
138template<class T>
139inline void Foam::List<T>::resize(const label len)
140{
141 this->doResize(len);
142}
143
144
145template<class T>
146inline void Foam::List<T>::resize_nocopy(const label len)
147{
148 this->reAlloc(len);
149}
150
151
152template<class T>
153inline 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
174template<class T>
175inline 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
184template<class T>
185inline 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
194template<class T>
195inline void Foam::List<T>::append(const UList<T>& list)
196{
197 if (this == &list)
198 {
200 << "Attempted appending to self" << abort(FatalError);
201 }
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
215template<class T>
216template<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 }
229
230
231template<class T>
232inline 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
246// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
247
248template<class T>
249inline void Foam::List<T>::operator=(const T& val)
250{
252}
253
254
255template<class T>
257{
259}
260
261
262// ************************************************************************* //
bool found
label n
virtual bool resize()
Resize the ODE solver.
Definition: Euler.C:53
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
label size() const noexcept
The number of elements in the list.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
T & newElmt(const label i)
Definition: ListI.H:153
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:100
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:146
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:480
constexpr List() noexcept
Default construct.
Definition: ListI.H:95
label appendUniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:232
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
static const List< T > & null()
Return a null List.
Definition: ListI.H:109
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
bool append() const noexcept
True if output format uses an append mode.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
patchWriters resize(patchIds.size())
patchWriters clear()
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Namespace for OpenFOAM.
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError