List.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 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>(NULL, s)
42 {
43  if (this->size_ < 0)
44  {
46  << "bad size " << this->size_
47  << abort(FatalError);
48  }
49 
50  if (this->size_)
51  {
52  this->v_ = new T[this->size_];
53  }
54 }
55 
56 
57 template<class T>
58 Foam::List<T>::List(const label s, const T& a)
59 :
60  UList<T>(NULL, s)
61 {
62  if (this->size_ < 0)
63  {
65  << "bad size " << this->size_
66  << abort(FatalError);
67  }
68 
69  if (this->size_)
70  {
71  this->v_ = new T[this->size_];
72 
73  List_ACCESS(T, (*this), vp);
74  List_FOR_ALL((*this), i)
75  List_ELEM((*this), vp, i) = a;
77  }
78 }
79 
80 
81 template<class T>
83 :
84  UList<T>(NULL, s)
85 {
86  if (this->size_ < 0)
87  {
89  << "bad size " << this->size_
90  << abort(FatalError);
91  }
92 
93  if (this->size_)
94  {
95  this->v_ = new T[this->size_];
96 
97  List_ACCESS(T, (*this), vp);
98  List_FOR_ALL((*this), i)
99  List_ELEM((*this), vp, i) = Zero;
101  }
102 }
103 
104 
105 template<class T>
107 :
108  UList<T>(NULL, a.size_)
109 {
110  if (this->size_)
111  {
112  this->v_ = new T[this->size_];
113 
114  #ifdef USEMEMCPY
115  if (contiguous<T>())
116  {
117  memcpy(this->v_, a.v_, this->byteSize());
118  }
119  else
120  #endif
121  {
122  List_ACCESS(T, (*this), vp);
123  List_CONST_ACCESS(T, a, ap);
124  List_FOR_ALL((*this), i)
125  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
127  }
128  }
129 }
130 
131 
132 template<class T>
134 {
135  transfer(lst());
136 }
137 
138 
139 template<class T>
141 :
142  UList<T>(NULL, a.size_)
143 {
144  if (reuse)
145  {
146  this->v_ = a.v_;
147  a.v_ = 0;
148  a.size_ = 0;
149  }
150  else if (this->size_)
151  {
152  this->v_ = new T[this->size_];
153 
154  #ifdef USEMEMCPY
155  if (contiguous<T>())
156  {
157  memcpy(this->v_, a.v_, this->byteSize());
158  }
159  else
160  #endif
161  {
162  List_ACCESS(T, (*this), vp);
163  List_CONST_ACCESS(T, a, ap);
164  List_FOR_ALL((*this), i)
165  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
167  }
168  }
169 }
170 
171 
172 template<class T>
174 :
175  UList<T>(NULL, map.size())
176 {
177  if (this->size_)
178  {
179  // Note:cannot use List_ELEM since third argument has to be index.
180 
181  this->v_ = new T[this->size_];
182 
183  forAll(*this, i)
184  {
185  this->v_[i] = a[map[i]];
186  }
187  }
188 }
189 
190 
191 template<class T>
192 template<unsigned Size>
194 :
195  UList<T>(NULL, Size)
196 {
197  if (this->size_)
198  {
199  this->v_ = new T[this->size_];
200 
201  forAll(*this, i)
202  {
203  this->operator[](i) = lst[i];
204  }
205  }
206 }
207 
208 
209 template<class T>
211 :
212  UList<T>(NULL, lst.size())
213 {
214  if (this->size_)
215  {
216  this->v_ = new T[this->size_];
217 
218  forAll(*this, i)
219  {
220  this->operator[](i) = lst[i];
221  }
222  }
223 }
224 
225 
226 template<class T>
228 :
229  UList<T>(NULL, lst.size())
230 {
231  if (this->size_)
232  {
233  this->v_ = new T[this->size_];
234 
235  label i = 0;
236  for
237  (
238  typename SLList<T>::const_iterator iter = lst.begin();
239  iter != lst.end();
240  ++iter
241  )
242  {
243  this->operator[](i++) = iter();
244  }
245  }
246 }
247 
248 
249 template<class T>
251 :
252  UList<T>(NULL, lst.size())
253 {
254  if (this->size_)
255  {
256  this->v_ = new T[this->size_];
257 
258  forAll(*this, i)
259  {
260  this->operator[](i) = lst[i];
261  }
262  }
263 }
264 
265 
266 template<class T>
268 :
269  UList<T>(NULL, lst.size())
270 {
271  if (this->size_)
272  {
273  this->v_ = new T[this->size_];
274 
275  forAll(*this, i)
276  {
277  this->operator[](i) = lst[i];
278  }
279  }
280 }
281 
282 
283 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
284 
285 template<class T>
287 {
288  if (this->v_) delete[] this->v_;
289 }
290 
291 
292 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
293 
294 template<class T>
295 void Foam::List<T>::setSize(const label newSize)
296 {
297  if (newSize < 0)
298  {
300  << "bad set size " << newSize
301  << abort(FatalError);
302  }
303 
304  if (newSize != this->size_)
305  {
306  if (newSize > 0)
307  {
308  T* nv = new T[label(newSize)];
309 
310  if (this->size_)
311  {
312  label i = min(this->size_, newSize);
313 
314  #ifdef USEMEMCPY
315  if (contiguous<T>())
316  {
317  memcpy(nv, this->v_, i*sizeof(T));
318  }
319  else
320  #endif
321  {
322  T* vv = &this->v_[i];
323  T* av = &nv[i];
324  while (i--) *--av = *--vv;
325  }
326  }
327  if (this->v_) delete[] this->v_;
328 
329  this->size_ = newSize;
330  this->v_ = nv;
331  }
332  else
333  {
334  clear();
335  }
336  }
337 }
338 
339 
340 template<class T>
341 void Foam::List<T>::setSize(const label newSize, const T& a)
342 {
343  label oldSize = label(this->size_);
344  this->setSize(newSize);
345 
346  if (newSize > oldSize)
347  {
348  label i = newSize - oldSize;
349  T* vv = &this->v_[newSize];
350  while (i--) *--vv = a;
351  }
352 }
353 
354 
355 template<class T>
357 {
358  if (this->v_) delete[] this->v_;
359  this->size_ = 0;
360  this->v_ = 0;
361 }
362 
363 
364 template<class T>
366 {
367  if (this->v_) delete[] this->v_;
368  this->size_ = a.size_;
369  this->v_ = a.v_;
370 
371  a.size_ = 0;
372  a.v_ = 0;
373 }
374 
375 
376 template<class T>
377 template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
379 {
380  // Shrink the allocated space to the number of elements used
381  a.shrink();
382  transfer(static_cast<List<T>&>(a));
383  a.clearStorage();
384 }
385 
386 
387 template<class T>
389 {
390  // Shrink away the sort indices
391  a.shrink();
392  transfer(static_cast<List<T>&>(a));
393 }
394 
395 
396 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
397 
398 template<class T>
400 {
401  if (a.size_ != this->size_)
402  {
403  if (this->v_) delete[] this->v_;
404  this->v_ = 0;
405  this->size_ = a.size_;
406  if (this->size_) this->v_ = new T[this->size_];
407  }
408 
409  if (this->size_)
410  {
411  #ifdef USEMEMCPY
412  if (contiguous<T>())
413  {
414  memcpy(this->v_, a.v_, this->byteSize());
415  }
416  else
417  #endif
418  {
419  List_ACCESS(T, (*this), vp);
420  List_CONST_ACCESS(T, a, ap);
421  List_FOR_ALL((*this), i)
422  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
424  }
425  }
426 }
427 
428 
429 template<class T>
431 {
432  if (this == &a)
433  {
435  << "attempted assignment to self"
436  << abort(FatalError);
437  }
438 
439  operator=(static_cast<const UList<T>&>(a));
440 }
441 
442 
443 template<class T>
445 {
446  if (lst.size() != this->size_)
447  {
448  if (this->v_) delete[] this->v_;
449  this->v_ = 0;
450  this->size_ = lst.size();
451  if (this->size_) this->v_ = new T[this->size_];
452  }
453 
454  if (this->size_)
455  {
456  label i = 0;
457  for
458  (
459  typename SLList<T>::const_iterator iter = lst.begin();
460  iter != lst.end();
461  ++iter
462  )
463  {
464  this->operator[](i++) = iter();
465  }
466  }
467 }
468 
469 
470 template<class T>
472 {
473  if (lst.size() != this->size_)
474  {
475  if (this->v_) delete[] this->v_;
476  this->v_ = 0;
477  this->size_ = lst.size();
478  if (this->size_) this->v_ = new T[this->size_];
479  }
480 
481  forAll(*this, i)
482  {
483  this->operator[](i) = lst[i];
484  }
485 }
486 
487 
488 template<class T>
490 {
491  if (lst.size() != this->size_)
492  {
493  if (this->v_) delete[] this->v_;
494  this->v_ = 0;
495  this->size_ = lst.size();
496  if (this->size_) this->v_ = new T[this->size_];
497  }
498 
499  forAll(*this, i)
500  {
501  this->operator[](i) = lst[i];
502  }
503 }
504 
505 
506 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
507 
508 #include "ListIO.C"
509 
510 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:78
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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:53
error FatalError
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
const iterator & end()
Definition: LList.H:273
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:87
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:68
~List()
Destructor.
Definition: List.C:286
Template function to specify if the data of a type are contiguous.
An STL-conforming const_iterator.
Definition: SLListBase.H:210
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
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
label size() const
Return number of elements in list.
Definition: SLListBaseI.H:67
void clear()
Clear the list, i.e. set size to zero.
Definition: List.C:356
#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:91
Non-intrusive singly-linked list.
Definition: SLList.H:47
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:240
#define List_ELEM(f, fp, i)
Definition: ListLoopM.H:73
#define List_ACCESS(type, f, fp)
Definition: ListLoopM.H:75
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
label size() const
Return the number of elements in the UList.
Definition: ListI.H:83
const volScalarField & T
#define List_FOR_ALL(f, i)
Definition: ListLoopM.H:62
List()
Null constructor.
Definition: ListI.H:29
Indexes into negList (negative index) or posList (zero or positive index).
void operator=(const UList< T > &)
Assignment from UList operator. Takes linear time.
Definition: List.C:399
void setSize(const label)
Reset size of List.
Definition: List.C:295
label size() const
Return the number of elements in the list.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:62
A List with indirect addressing.
Definition: fvMatrix.H:106
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:231
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:49
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:365