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>::push_back(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>::push_back(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>::push_back(const UList<T>& list)
196 {
197  if (this == &list)
198  {
200  << "Attempted push_back 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>
217 inline void Foam::List<T>::push_back(const IndirectListBase<T, Addr>& list)
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>::push_uniq(const T& val)
233 {
234  if (this->found(val))
235  {
236  return 0;
237  }
238  else
239  {
240  this->push_back(val);
241  return 1; // Increased list length by one
242  }
243 }
244 
245 
246 template<class T>
247 inline void Foam::List<T>::pop_back(label n)
248 {
249  if (n >= this->size())
250  {
251  this->clear();
252  }
253  else if (n > 0)
254  {
255  resize(this->size() - n);
256  }
257 }
258 
259 
260 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
261 
262 template<class T>
263 inline void Foam::List<T>::operator=(const T& val)
264 {
265  UList<T>::operator=(val);
266 }
267 
268 
269 template<class T>
270 inline void Foam::List<T>::operator=(const Foam::zero)
271 {
273 }
274 
275 
276 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:118
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: ListI.H:240
patchWriters resize(patchIds.size())
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:132
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:139
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:472
static const List< T > & null()
Return a null List.
Definition: ListI.H:102
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
void push_back(const T &val)
Append an element at the end of the list.
Definition: ListI.H:168
label push_uniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:225
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:109
patchWriters clear()
errorManip< error > abort(error &err)
Definition: errorManip.H:139
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:99
const direction noexcept
Definition: Scalar.H:258
const volScalarField & T
T & newElmt(const label i)
Return subscript-checked element of UList and resizing the list if required.
Definition: ListI.H:146
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:93
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:58
label n
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
constexpr List() noexcept
Default construct.
Definition: ListI.H:88
bool found
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:157