List.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "List.H"
27 #include "ListLoopM.H"
28 #include "FixedList.H"
29 #include "PtrList.H"
30 #include "SLList.H"
31 #include "IndirectList.H"
32 #include "UIndirectList.H"
33 #include "BiIndirectList.H"
34 #include "contiguous.H"
35 
36 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
37 
38 template<class T>
40 :
41  UList<T>(nullptr, s)
42 {
43  if (this->size_ < 0)
44  {
46  << "bad size " << this->size_
47  << abort(FatalError);
48  }
49 
50  alloc();
51 }
52 
53 
54 template<class T>
55 Foam::List<T>::List(const label s, const T& a)
56 :
57  UList<T>(nullptr, s)
58 {
59  if (this->size_ < 0)
60  {
62  << "bad size " << this->size_
63  << abort(FatalError);
64  }
65 
66  alloc();
67 
68  if (this->size_)
69  {
70  List_ACCESS(T, (*this), vp);
71  List_FOR_ALL((*this), i)
72  List_ELEM((*this), vp, i) = a;
74  }
75 }
76 
77 
78 template<class T>
80 :
81  UList<T>(nullptr, s)
82 {
83  if (this->size_ < 0)
84  {
86  << "bad size " << this->size_
87  << abort(FatalError);
88  }
89 
90  alloc();
91 
92  if (this->size_)
93  {
94  List_ACCESS(T, (*this), vp);
95  List_FOR_ALL((*this), i)
96  List_ELEM((*this), vp, i) = Zero;
98  }
99 }
100 
101 
102 template<class T>
104 :
105  UList<T>(nullptr, a.size_)
106 {
107  if (this->size_)
108  {
109  alloc();
110 
111  #ifdef USEMEMCPY
112  if (contiguous<T>())
113  {
114  memcpy(this->v_, a.v_, this->byteSize());
115  }
116  else
117  #endif
118  {
119  List_ACCESS(T, (*this), vp);
120  List_CONST_ACCESS(T, a, ap);
121  List_FOR_ALL((*this), i)
122  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
124  }
125  }
126 }
127 
128 
129 template<class T>
130 template<class T2>
132 :
133  UList<T>(nullptr, a.size())
134 {
135  if (this->size_)
136  {
137  alloc();
138 
139  List_ACCESS(T, (*this), vp);
140  List_CONST_ACCESS(T2, a, ap);
141  List_FOR_ALL((*this), i)
142  List_ELEM((*this), vp, i) = T(List_ELEM(a, ap, i));
144  }
145 }
146 
147 
148 template<class T>
150 {
151  transfer(lst);
152 }
153 
154 
155 template<class T>
157 :
158  UList<T>(nullptr, a.size_)
159 {
160  if (reuse)
161  {
162  this->v_ = a.v_;
163  a.v_ = 0;
164  a.size_ = 0;
165  }
166  else if (this->size_)
167  {
168  alloc();
169 
170  #ifdef USEMEMCPY
171  if (contiguous<T>())
172  {
173  memcpy(this->v_, a.v_, this->byteSize());
174  }
175  else
176  #endif
177  {
178  List_ACCESS(T, (*this), vp);
179  List_CONST_ACCESS(T, a, ap);
180  List_FOR_ALL((*this), i)
181  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
183  }
184  }
185 }
186 
187 
188 template<class T>
190 :
191  UList<T>(nullptr, map.size())
192 {
193  if (this->size_)
194  {
195  // Note:cannot use List_ELEM since third argument has to be index.
196 
197  alloc();
198 
199  forAll(*this, i)
200  {
201  this->operator[](i) = a[map[i]];
202  }
203  }
204 }
205 
206 
207 template<class T>
208 template<class InputIterator>
209 Foam::List<T>::List(InputIterator first, InputIterator last)
210 :
211  List<T>(first, last, std::distance(first, last))
212 {}
213 
214 
215 template<class T>
216 template<unsigned Size>
218 :
219  UList<T>(nullptr, Size)
220 {
221  allocCopyList(lst);
222 }
223 
224 
225 template<class T>
227 :
228  UList<T>(nullptr, lst.size())
229 {
230  allocCopyList(lst);
231 }
232 
233 
234 template<class T>
236 :
237  List<T>(lst.begin(), lst.end(), lst.size())
238 {}
239 
240 
241 template<class T>
243 :
244  UList<T>(nullptr, lst.size())
245 {
246  allocCopyList(lst);
247 }
248 
249 
250 template<class T>
252 :
253  UList<T>(nullptr, lst.size())
254 {
255  allocCopyList(lst);
256 }
257 
258 
259 template<class T>
260 Foam::List<T>::List(std::initializer_list<T> lst)
261 :
262  List<T>(lst.begin(), lst.end())
263 {}
264 
265 
266 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
267 
268 template<class T>
270 {
271  if (this->v_)
272  {
273  delete[] this->v_;
274  }
275 }
276 
277 
278 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
279 
280 template<class T>
281 void Foam::List<T>::setSize(const label newSize)
282 {
283  if (newSize < 0)
284  {
286  << "bad size " << newSize
287  << abort(FatalError);
288  }
289 
290  if (newSize != this->size_)
291  {
292  if (newSize > 0)
293  {
294  T* nv = new T[label(newSize)];
295 
296  if (this->size_)
297  {
298  label i = min(this->size_, newSize);
299 
300  #ifdef USEMEMCPY
301  if (contiguous<T>())
302  {
303  memcpy(nv, this->v_, i*sizeof(T));
304  }
305  else
306  #endif
307  {
308  T* vv = &this->v_[i];
309  T* av = &nv[i];
310  while (i--) *--av = *--vv;
311  }
312  }
313 
314  clear();
315  this->size_ = newSize;
316  this->v_ = nv;
317  }
318  else
319  {
320  clear();
321  }
322  }
323 }
324 
325 
326 template<class T>
327 void Foam::List<T>::setSize(const label newSize, const T& a)
328 {
329  label oldSize = label(this->size_);
330  this->setSize(newSize);
331 
332  if (newSize > oldSize)
333  {
334  label i = newSize - oldSize;
335  T* vv = &this->v_[newSize];
336  while (i--) *--vv = a;
337  }
338 }
339 
340 
341 template<class T>
343 {
344  clear();
345  this->size_ = a.size_;
346  this->v_ = a.v_;
347 
348  a.size_ = 0;
349  a.v_ = 0;
350 }
351 
352 
353 template<class T>
354 template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
356 {
357  // Shrink the allocated space to the number of elements used
358  a.shrink();
359  transfer(static_cast<List<T>&>(a));
360  a.clearStorage();
361 }
362 
363 
364 template<class T>
366 {
367  // Shrink away the sort indices
368  a.shrink();
369  transfer(static_cast<List<T>&>(a));
370 }
371 
372 
373 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
374 
375 template<class T>
377 {
378  reAlloc(a.size_);
379 
380  if (this->size_)
381  {
382  #ifdef USEMEMCPY
383  if (contiguous<T>())
384  {
385  memcpy(this->v_, a.v_, this->byteSize());
386  }
387  else
388  #endif
389  {
390  List_ACCESS(T, (*this), vp);
391  List_CONST_ACCESS(T, a, ap);
392  List_FOR_ALL((*this), i)
393  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
395  }
396  }
397 }
398 
399 
400 template<class T>
402 {
403  if (this == &a)
404  {
406  << "attempted assignment to self"
407  << abort(FatalError);
408  }
409 
410  operator=(static_cast<const UList<T>&>(a));
411 }
412 
413 
414 template<class T>
416 {
417  if (this == &a)
418  {
420  << "attempted assignment to self"
421  << abort(FatalError);
422  }
423 
424  transfer(a);
425 }
426 
427 
428 template<class T>
430 {
431  reAlloc(lst.size());
432 
433  if (this->size_)
434  {
435  label i = 0;
436  for
437  (
438  typename SLList<T>::const_iterator iter = lst.begin();
439  iter != lst.end();
440  ++iter
441  )
442  {
443  this->operator[](i++) = iter();
444  }
445  }
446 }
447 
448 
449 template<class T>
451 {
452  reAlloc(lst.size());
453  copyList(lst);
454 }
455 
456 
457 template<class T>
459 {
460  reAlloc(lst.size());
461  copyList(lst);
462 }
463 
464 
465 template<class T>
466 void Foam::List<T>::operator=(std::initializer_list<T> lst)
467 {
468  reAlloc(lst.size());
469 
470  typename std::initializer_list<T>::iterator iter = lst.begin();
471  forAll(*this, i)
472  {
473  this->operator[](i) = *iter++;
474  }
475 }
476 
477 
478 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
479 
480 #include "ListIO.C"
481 
482 // ************************************************************************* //
label size() const
Return the number of elements in the list.
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:78
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
tUEqn clear()
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:54
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
const iterator & end()
Definition: LList.H:286
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:104
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: List.H:80
Template class for non-intrusive linked lists.
Definition: LList.H:51
An STL-conforming const_iterator.
Definition: LList.H:297
~List()
Destructor.
Definition: List.C:269
Template function to specify if the data of a type are contiguous.
scalar distance(const vector &p1, const vector &p2)
Definition: curveTools.C:12
points setSize(newPointi)
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
#define List_END_FOR_ALL
Definition: ListLoopM.H:67
label size() const
Return the number of elements in the list.
static const zero Zero
Definition: zero.H:97
errorManip< error > abort(error &err)
Definition: errorManip.H:131
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:60
List<T> is a 1D vector of objects of type T, where the size of the vector is known and used for subsc...
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:252
#define List_ELEM(f, fp, i)
Definition: ListLoopM.H:73
#define List_ACCESS(type, f, fp)
Definition: ListLoopM.H:75
iterator begin()
Definition: LList.H:281
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
const volScalarField & T
#define List_FOR_ALL(f, i)
Definition: ListLoopM.H:62
List()
Null constructor.
Definition: ListI.H:104
Indexes into negList (negative index) or posList (zero or positive index).
void operator=(const UList< T > &)
Assignment to UList operator. Takes linear time.
Definition: List.C:376
void setSize(const label)
Reset size of List.
Definition: List.C:281
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
A List with indirect addressing.
Definition: fvMatrix.H:106
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:243
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:49
Non-intrusive singly-linked list.
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342