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-2025 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_ = ListPolicy::allocate<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 (len > 0)
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>
137 inline void Foam::List<T>::clear()
138 {
139  ListPolicy::deallocate(this->v_, this->size_);
140  this->v_ = nullptr;
141  this->size_ = 0;
142 }
143 
144 
145 namespace Foam
146 {
147  // Template specialization for bool. Fills new entries with false
148  template<>
149  inline void List<bool>::resize(const label len)
150  {
151  this->resize(len, false);
152  }
153 }
154 
155 
156 template<class T>
157 inline void Foam::List<T>::resize(const label len)
158 {
159  if (this->size_ != len)
160  {
161  this->resize_copy(this->size_, len);
162  }
163 }
164 
165 
166 template<class T>
167 inline void Foam::List<T>::resize_fill(const label len, const T& val)
168 {
169  this->reAlloc(len);
170  UList<T>::operator=(val);
171 }
172 
173 
174 template<class T>
175 inline void Foam::List<T>::resize_nocopy(const label len)
176 {
177  this->reAlloc(len);
178 }
179 
180 
181 template<class T>
182 inline void Foam::List<T>::resize_unsafe(const label len) noexcept
183 {
185 }
186 
187 
188 template<class T>
189 inline T& Foam::List<T>::newElmt(const label i)
190 {
191  label n = this->size();
192 
193  if (i >= n)
194  {
195  if (!n) n = 1; // Avoid dead-lock when starting from zero-sized
196 
197  do
198  {
199  n *= 2;
200  }
201  while (i >= n);
202 
203  resize(n);
204  }
206  return UList<T>::operator[](i);
207 }
208 
209 
210 template<class T>
211 template<class... Args>
212 inline T& Foam::List<T>::emplace_back(Args&&... args)
213 {
214  // This could/should be better with inplace construction
215  // (as per std::vector), but currently lacking the methods for that
216  // so resize and move assign
217 
218  const label idx = this->size();
219  resize(idx + 1);
221  UList<T>::operator[](idx) = T(std::forward<Args>(args)...);
222  return UList<T>::operator[](idx);
223 }
224 
225 
226 template<class T>
227 inline void Foam::List<T>::push_back(const T& val)
228 {
229  const label idx = this->size();
230  resize(idx + 1);
231 
232  UList<T>::operator[](idx) = val; // copy element
233 }
234 
235 
236 template<class T>
237 inline void Foam::List<T>::push_back(T&& val)
238 {
239  const label idx = this->size();
240  resize(idx + 1);
241 
242  UList<T>::operator[](idx) = std::move(val); // move assign element
243 }
244 
245 
246 template<class T>
247 inline void Foam::List<T>::push_back(const UList<T>& list)
248 {
249  if (this == &list)
250  {
252  << "Attempted push_back to self" << abort(FatalError);
253  }
254 
255  const label idx = this->size();
256  resize(idx + list.size());
258  std::copy(list.begin(), list.end(), this->begin(idx));
259 }
260 
261 
262 template<class T>
263 template<class Addr>
265 {
266  // Note: push_back will still work even if the indirect list
267  // actually references *this, since its source elements will not
268  // overlap the new destinations.
269 
270  const label idx = this->size();
271  const label n = list.size();
272  resize(idx + n);
273 
274  auto iter = this->begin(idx);
275 
276  for (label i = 0; i < n; (void)++i, (void)++iter)
277  {
278  *iter = list[i]; // copy element
279  }
280 }
281 
282 
283 template<class T>
284 inline Foam::label Foam::List<T>::push_uniq(const T& val)
285 {
286  if (this->contains(val))
287  {
288  return 0;
289  }
290  else
291  {
292  this->push_back(val);
293  return 1; // Increased list length by one
294  }
295 }
296 
297 
298 template<class T>
299 inline void Foam::List<T>::pop_back(label n)
300 {
301  if (n >= this->size())
302  {
303  this->clear();
304  }
305  else if (n > 0)
306  {
307  resize(this->size() - n);
308  }
309 }
310 
311 
312 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
313 
314 template<class T>
315 inline void Foam::List<T>::operator=(const T& val)
316 {
317  UList<T>::operator=(val);
318 }
319 
320 
321 template<class T>
323 {
325 }
326 
327 
328 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: ListI.H:292
patchWriters resize(patchIds.size())
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:150
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:600
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:168
void resize_fill(const label len, const T &val)
Adjust allocated size of list and set val for all elements.
Definition: ListI.H:160
void push_back(const T &val)
Append an element at the end of the list.
Definition: ListI.H:220
label push_uniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:277
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:371
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:130
void deallocate(T *ptr)
Definition: ListPolicy.H:173
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:385
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: scalarImpl.H:255
void resize_unsafe(const label len) noexcept
Change the addressed list size directly without affecting any memory management (advanced usage)...
Definition: ListI.H:175
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:205
T & newElmt(const label i)
Return subscript-checked element of UList and resizing the list if required.
Definition: ListI.H:182
surface1 clear()
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:429
Foam::argList args(argc, argv)
constexpr List() noexcept
Default construct.
Definition: ListI.H:116
Namespace for OpenFOAM.