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-2020 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_)
35  {
36  this->v_ = new T[this->size_];
37  }
38 }
39 
40 
41 template<class T>
42 inline void Foam::List<T>::reAlloc(const label len)
43 {
44  if (this->size_ != len)
45  {
46  clear();
47  this->size_ = len;
48  doAlloc();
49  }
50 }
51 
52 
53 template<class T>
54 template<class List2>
55 inline void Foam::List<T>::copyList(const List2& list)
56 {
57  const label len = this->size_;
58 
59  for (label i=0; i<len; ++i)
60  {
61  this->operator[](i) = list[i];
62  }
63 }
64 
65 
66 template<class T>
67 template<class InputIterator>
69 (
70  InputIterator begIter,
71  InputIterator endIter,
72  const label len
73 )
74 :
75  UList<T>(nullptr, len)
76 {
77  if (this->size_)
78  {
79  doAlloc();
80 
81  InputIterator iter = begIter;
82  for (label i = 0; i < len; ++i)
83  {
84  this->operator[](i) = *iter;
85  ++iter;
86  }
87  }
88 }
89 
90 
91 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
92 
93 template<class T>
94 inline constexpr Foam::List<T>::List() noexcept
95 {}
96 
97 
98 template<class T>
100 {
101  return autoPtr<List<T>>::New(*this);
102 }
103 
104 
105 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106 
107 template<class T>
109 {
110  return NullObjectRef<List<T>>();
111 }
112 
113 
114 template<class T>
115 inline void Foam::List<T>::clear()
116 {
117  if (this->v_)
118  {
119  delete[] this->v_;
120  this->v_ = nullptr;
121  }
122 
123  this->size_ = 0;
124 }
125 
126 
127 namespace Foam
128 {
129  // Template specialization for bool. Fills with false
130  template<>
131  inline void List<bool>::resize(const label newSize)
132  {
133  this->resize(newSize, 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>::setSize(const label len)
147 {
148  this->resize(len);
149 }
150 
151 
152 template<class T>
153 inline void Foam::List<T>::setSize(const label len, const T& val)
154 {
155  this->resize(len, val);
156 }
157 
158 
159 template<class T>
160 inline T& Foam::List<T>::newElmt(const label i)
161 {
162  label n = this->size();
163 
164  if (i >= n)
165  {
166  if (!n) n = 1; // Avoid dead-lock when starting from zero-sized
167 
168  do
169  {
170  n *= 2;
171  }
172  while (i >= n);
173 
174  resize(n);
175  }
176 
177  return UList<T>::operator[](i);
178 }
179 
180 
181 template<class T>
182 inline void Foam::List<T>::append(const T& val)
183 {
184  resize(this->size() + 1, val); // copy element
185 }
186 
187 
188 template<class T>
189 inline void Foam::List<T>::append(T&& val)
190 {
191  const label idx = this->size();
192  resize(idx + 1);
193 
194  this->operator[](idx) = std::move(val); // move assign element
195 }
196 
197 
198 template<class T>
199 inline void Foam::List<T>::append(const UList<T>& list)
200 {
201  if (this == &list)
202  {
204  << "Attempted appending to self" << abort(FatalError);
205  }
206 
207  label idx = this->size();
208  const label n = list.size();
209 
210  resize(idx + n);
211 
212  for (label i=0; i<n; ++i)
213  {
214  this->operator[](idx++) = list[i]; // copy element
215  }
216 }
217 
218 
219 template<class T>
220 template<class Addr>
222 {
223  label idx = this->size();
224  const label n = list.size();
225 
226  resize(idx + n);
227 
228  for (label i=0; i<n; ++i)
229  {
230  this->operator[](idx++) = list[i]; // copy element
231  }
232 }
233 
234 
235 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
236 
237 template<class T>
238 inline void Foam::List<T>::operator=(const T& val)
239 {
240  UList<T>::operator=(val);
241 }
242 
243 
244 template<class T>
246 {
248 }
249 
250 
251 // ************************************************************************* //
Foam::List::newElmt
T & newElmt(const label i)
Definition: ListI.H:160
Foam::List::null
static const List< T > & null()
Return a null List.
Definition: ListI.H:108
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:182
n
label n
Definition: TABSMDCalcMethod2.H:31
resize
patchWriters resize(patchIds.size())
Foam::List::operator=
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:501
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::List::List
constexpr List() noexcept
Null constructor.
Definition: ListI.H:94
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
clear
patchWriters clear()
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::UList
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:103
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:115
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::List::clone
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:99
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::IndirectListBase::size
label size() const
The number of elements in the list.
Definition: IndirectListBase.H:125
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62