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-2023 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 ListType>
56 inline void Foam::List<T>::copyList(const ListType& list)
57 {
58  // NB: operator[] for list read access (eg, an indirect list)
59  // cannot necessarily replace with std::copy
60 
61  const label len = this->size_;
62 
63  auto iter = this->v_;
64 
65  for (label i = 0; i < len; (void)++i, (void)++iter)
66  {
67  *iter = list[i];
68  }
69 }
70 
71 
72 template<class T>
73 template<class ListType, class ListIndices>
74 inline void Foam::List<T>::copyList
75 (
76  const ListType& list,
77  const ListIndices& indices
78 )
79 {
80  // NB: operator[] for list read access (eg, an indirect list)
81  // cannot necessarily replace with std::copy
82 
83  const label len = this->size_;
84 
85  auto iter = this->v_;
86 
87  for (label i = 0; i < len; (void)++i, (void)++iter)
88  {
89  *iter = list[indices[i]];
90  }
91 }
92 
93 
94 template<class T>
95 template<class InputIterator>
97 (
98  InputIterator firstIter,
99  InputIterator lastIter, // (unused)
100  const label len
101 )
102 :
103  UList<T>(nullptr, len)
104 {
105  if (this->size_)
106  {
107  doAlloc();
108 
109  // Like std::copy() or std::copy_n()
110  // but without any requirements on the iterator category
111 
112  for (label i = 0; i < len; (void)++i, (void)++firstIter)
113  {
114  this->v_[i] = *firstIter;
115  }
116  }
117 }
118 
119 
120 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
122 template<class T>
123 inline constexpr Foam::List<T>::List() noexcept
124 {}
125 
126 
127 template<class T>
129 {
130  return autoPtr<List<T>>::New(*this);
131 }
132 
133 
134 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
135 
136 template<class T>
138 {
139  return NullObjectRef<List<T>>();
140 }
141 
142 
143 template<class T>
144 inline void Foam::List<T>::clear()
145 {
146  if (this->v_)
147  {
148  delete[] this->v_;
149  this->v_ = nullptr;
150  }
151  this->size_ = 0;
152 }
153 
154 
155 namespace Foam
156 {
157  // Template specialization for bool. Fills new entries with false
158  template<>
159  inline void List<bool>::resize(const label newLen)
160  {
161  this->resize(newLen, false);
162  }
163 }
164 
165 
166 template<class T>
167 inline void Foam::List<T>::resize(const label len)
168 {
169  this->doResize(len);
170 }
171 
172 
173 template<class T>
174 inline void Foam::List<T>::resize_fill(const label len, const T& val)
175 {
176  this->reAlloc(len);
177  UList<T>::operator=(val);
178 }
179 
180 
181 template<class T>
182 inline void Foam::List<T>::resize_nocopy(const label len)
183 {
184  this->reAlloc(len);
185 }
186 
187 
188 template<class T>
189 inline void Foam::List<T>::resize_unsafe(const label len) noexcept
190 {
192 }
193 
194 
195 template<class T>
196 inline T& Foam::List<T>::newElmt(const label i)
197 {
198  label n = this->size();
199 
200  if (i >= n)
201  {
202  if (!n) n = 1; // Avoid dead-lock when starting from zero-sized
203 
204  do
205  {
206  n *= 2;
207  }
208  while (i >= n);
209 
210  resize(n);
211  }
213  return UList<T>::operator[](i);
214 }
215 
216 
217 template<class T>
218 template<class... Args>
219 inline T& Foam::List<T>::emplace_back(Args&&... args)
220 {
221  // This could/should be better with inplace construction
222  // (as per std::vector), but currently lacking the methods for that
223  // so resize and move assign
224 
225  const label idx = this->size();
226  resize(idx + 1);
228  UList<T>::operator[](idx) = T(std::forward<Args>(args)...);
229  return UList<T>::operator[](idx);
230 }
231 
232 
233 template<class T>
234 inline void Foam::List<T>::push_back(const T& val)
235 {
236  const label idx = this->size();
237  resize(idx + 1);
238 
239  UList<T>::operator[](idx) = val; // copy element
240 }
241 
242 
243 template<class T>
244 inline void Foam::List<T>::push_back(T&& val)
245 {
246  const label idx = this->size();
247  resize(idx + 1);
248 
249  UList<T>::operator[](idx) = std::move(val); // move assign element
250 }
251 
252 
253 template<class T>
254 inline void Foam::List<T>::push_back(const UList<T>& list)
255 {
256  if (this == &list)
257  {
259  << "Attempted push_back to self" << abort(FatalError);
260  }
261 
262  const label idx = this->size();
263  resize(idx + list.size());
265  std::copy(list.begin(), list.end(), this->begin(idx));
266 }
267 
268 
269 template<class T>
270 template<class Addr>
272 {
273  // Note: push_back will still work even if the indirect list
274  // actually references *this, since its source elements will not
275  // overlap the new destinations.
276 
277  const label idx = this->size();
278  const label n = list.size();
279 
280  resize(idx + n);
281 
282  auto iter = this->begin(idx);
283 
284  for (label i = 0; i < n; (void)++i, (void)++iter)
285  {
286  *iter = list[i]; // copy element
287  }
288 }
289 
290 
291 template<class T>
292 inline Foam::label Foam::List<T>::push_uniq(const T& val)
293 {
294  if (this->contains(val))
295  {
296  return 0;
297  }
298  else
299  {
300  this->push_back(val);
301  return 1; // Increased list length by one
302  }
303 }
304 
305 
306 template<class T>
307 inline void Foam::List<T>::pop_back(label n)
308 {
309  if (n >= this->size())
310  {
311  this->clear();
312  }
313  else if (n > 0)
314  {
315  resize(this->size() - n);
316  }
317 }
318 
319 
320 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
321 
322 template<class T>
323 inline void Foam::List<T>::operator=(const T& val)
324 {
325  UList<T>::operator=(val);
326 }
327 
328 
329 template<class T>
330 inline void Foam::List<T>::operator=(const Foam::zero)
331 {
333 }
334 
335 
336 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: ListI.H:300
patchWriters resize(patchIds.size())
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
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:598
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
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:175
static const List< T > & null()
Return a null List.
Definition: ListI.H:130
void resize_fill(const label len, const T &val)
Adjust allocated size of list and set val for all elements.
Definition: ListI.H:167
void push_back(const T &val)
Append an element at the end of the list.
Definition: ListI.H:227
label push_uniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:285
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
void operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition: List.C:360
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:137
patchWriters clear()
label size() const noexcept
The number of elements in the list.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:391
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:105
const direction noexcept
Definition: Scalar.H:258
void resize_unsafe(const label len) noexcept
Change the addressed list size directly without affecting any memory management (advanced usage)...
Definition: ListI.H:182
const volScalarField & T
T & emplace_back(Args &&... args)
Construct an element at the end of the list, return reference to the new list element.
Definition: ListI.H:212
T & newElmt(const label i)
Return subscript-checked element of UList and resizing the list if required.
Definition: ListI.H:189
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:121
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
label n
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:435
Foam::argList args(argc, argv)
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:168
constexpr List() noexcept
Default construct.
Definition: ListI.H:116
Namespace for OpenFOAM.