Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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(const label len)
33{
34 if (len > 0)
35 {
36 // With sign-check to avoid spurious -Walloc-size-larger-than
37 this->v_ = ListPolicy::allocate<T>(len);
38 this->size_ = len;
39 }
40}
41
42
43template<class T>
44template<class ListType>
45inline void Foam::List<T>::copyList(const ListType& list)
46{
47 // NB: operator[] for list read access (eg, an indirect list)
48 // cannot necessarily replace with std::copy
49
50 const label len = this->size_;
51
52 auto iter = this->v_;
53
54 for (label i = 0; i < len; (void)++i, (void)++iter)
55 {
56 *iter = list[i];
57 }
58}
59
60
61template<class T>
62template<class ListType, class ListIndices>
63inline void Foam::List<T>::copyList
64(
65 const ListType& list,
66 const ListIndices& indices
67)
68{
69 // NB: operator[] for list read access (eg, an indirect list)
70 // cannot necessarily replace with std::copy
71
72 const label len = this->size_;
73
74 auto iter = this->v_;
75
76 for (label i = 0; i < len; (void)++i, (void)++iter)
77 {
78 *iter = list[indices[i]];
79 }
80}
81
82
83template<class T>
84template<class InputIterator>
86(
87 InputIterator input,
88 InputIterator inputEnd, // (unused)
89 const label len
90)
91:
92 UList<T>()
93{
94 if (len > 0)
95 {
96 doAlloc(len);
97
98 // Like std::copy() or std::copy_n()
99 // but without any requirements on the iterator category
100
101 auto iter = this->v_;
102 const auto endIter = (iter + len);
103
104 for (; iter != endIter; (void)++iter, (void)++input)
105 {
106 *iter = *input;
107 }
109}
110
111
112// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
114template<class T>
115inline constexpr Foam::List<T>::List() noexcept
116{}
117
118
119template<class T>
121:
122 UList<T>(list.data(), list.size())
123{
124 // Stole content
125 list.size_ = 0;
126 list.v_ = nullptr;
127}
128
129
130template<class T>
132{
133 return autoPtr<List<T>>::New(*this);
134}
135
136
137// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
138
139template<class T>
140inline void Foam::List<T>::clear()
141{
142 ListPolicy::deallocate(this->v_, this->size_);
143 this->v_ = nullptr;
144 this->size_ = 0;
146
147
148namespace Foam
149{
150 // Template specialization for bool. Fills new entries with false
151 template<>
152 inline void List<bool>::resize(const label len)
154 this->resize(len, false);
155 }
156}
157
158
159template<class T>
160inline void Foam::List<T>::resize(const label len)
161{
162 if (this->size_ != len)
164 resize_copy(this->size_, len);
165 }
166}
167
168
169template<class T>
170inline void Foam::List<T>::resize_fill(const label len, const T& val)
172 resize_nocopy(len);
174}
175
176
177template<class T>
178inline void Foam::List<T>::resize_nocopy(const label len)
179{
180 // Same as resize_copy(0, len);
181 if (this->size_ != len)
182 {
184 doAlloc(len);
185 }
186}
187
188
189template<class T>
190inline T& Foam::List<T>::newElmt(const label i)
191{
192 label n = this->size();
193
194 if (i >= n)
195 {
196 if (!n) n = 1; // Avoid dead-lock when starting from zero-sized
197
198 do
199 {
200 n *= 2;
201 }
202 while (i >= n);
203
204 resize(n);
205 }
207 return UList<T>::operator[](i);
208}
209
210
211template<class T>
212template<class... Args>
213inline T& Foam::List<T>::emplace_back(Args&&... args)
214{
215 // This could/should be better with inplace construction
216 // (as per std::vector), but currently lacking the methods for that
217 // so resize and move assign
218
219 const label idx = this->size();
220 resize(idx + 1);
222 UList<T>::operator[](idx) = T(std::forward<Args>(args)...);
223 return UList<T>::operator[](idx);
224}
225
226
227template<class T>
228inline void Foam::List<T>::push_back(const T& val)
229{
230 const label idx = this->size();
231 resize(idx + 1);
232
233 UList<T>::operator[](idx) = val; // copy element
234}
235
236
237template<class T>
238inline void Foam::List<T>::push_back(T&& val)
239{
240 const label idx = this->size();
241 resize(idx + 1);
242
243 UList<T>::operator[](idx) = std::move(val); // move assign element
244}
245
246
247template<class T>
248inline void Foam::List<T>::push_back(const UList<T>& list)
249{
250 if (this == &list)
251 {
253 << "Attempted push_back to self" << abort(FatalError);
254 }
255
256 const label idx = this->size();
257 resize(idx + list.size());
259 std::copy(list.begin(), list.end(), this->begin(idx));
260}
261
262
263template<class T>
264template<class Addr>
266{
267 // Note: push_back will still work even if the indirect list
268 // actually references *this, since its source elements will not
269 // overlap the new destinations.
270
271 const label idx = this->size();
272 const label n = list.size();
273 resize(idx + n);
274
275 auto iter = this->begin(idx);
276
277 for (label i = 0; i < n; (void)++i, (void)++iter)
279 *iter = list[i]; // copy element
280 }
281}
282
283
284template<class T>
285inline Foam::label Foam::List<T>::push_uniq(const T& val)
286{
287 if (this->contains(val))
289 return 0;
290 }
291 else
292 {
293 this->push_back(val);
294 return 1; // Increased list length by one
295 }
296}
297
299template<class T>
300inline void Foam::List<T>::pop_back(label n)
301{
302 if (n >= this->size())
303 {
304 this->clear();
305 }
306 else if (n > 0)
307 {
308 resize(this->size() - n);
310}
311
312
313// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
314
315template<class T>
316inline void Foam::List<T>::operator=(const T& val)
317{
319}
321
322template<class T>
324{
326}
327
328
329// ************************************************************************* //
label n
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:72
T & newElmt(const label i)
Return subscript-checked element of UList and resizing the list if required.
Definition ListI.H:183
autoPtr< List< T > > clone() const
Clone.
Definition ListI.H:124
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition ListI.H:293
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition ListI.H:171
void operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition List.C:381
T & emplace_back(Args &&... args)
Construct an element at the end of the list, return reference to the new list element.
Definition ListI.H:206
constexpr List() noexcept
Default construct.
Definition ListI.H:108
void resize_copy(label count, const label len)
Change allocated size of list, retaining the first count elements.
Definition List.C:31
label push_uniq(const T &val)
Append an element if not already in the list.
Definition ListI.H:278
void push_back(const T &val)
Append an element at the end of the list.
Definition ListI.H:221
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
void resize_fill(const label len, const T &val)
Adjust allocated size of list and set val for all elements.
Definition ListI.H:163
void clear()
Clear the list, i.e. set size to zero.
Definition ListI.H:133
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:89
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
UList(const UList< T > &) noexcept=default
Copy construct, shallow copy.
bool contains(const T &val) const
True if the value is contained in the list.
Definition UListI.H:302
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition UListI.H:454
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
T & operator[](const label i)
Return element of UList.
Definition UListI.H:363
UList< T > & operator=(const UList< T > &)=delete
No copy assignment (default: shallow copy).
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
const volScalarField & T
patchWriters resize(patchIds.size())
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
surface1 clear()
void deallocate(T *ptr)
Deallocate from memory pool, or normal.
Definition ListPolicy.H:236
Namespace for OpenFOAM.
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.
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)