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-2015 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 
29 #include "FixedList.H"
30 #include "PtrList.H"
31 #include "SLList.H"
32 #include "IndirectList.H"
33 #include "UIndirectList.H"
34 #include "BiIndirectList.H"
35 #include "contiguous.H"
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
39 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
40 
41 // Construct with length specified
42 template<class T>
44 :
45  UList<T>(NULL, s)
46 {
47  if (this->size_ < 0)
48  {
49  FatalErrorIn("List<T>::List(const label size)")
50  << "bad size " << this->size_
51  << abort(FatalError);
52  }
53 
54  if (this->size_)
55  {
56  this->v_ = new T[this->size_];
57  }
58 }
59 
60 
61 // Construct with length and single value specified
62 template<class T>
63 Foam::List<T>::List(const label s, const T& a)
64 :
65  UList<T>(NULL, s)
66 {
67  if (this->size_ < 0)
68  {
69  FatalErrorIn("List<T>::List(const label size, const T&)")
70  << "bad size " << this->size_
71  << abort(FatalError);
72  }
73 
74  if (this->size_)
75  {
76  this->v_ = new T[this->size_];
77 
78  List_ACCESS(T, (*this), vp);
79  List_FOR_ALL((*this), i)
80  List_ELEM((*this), vp, i) = a;
82  }
83 }
84 
85 
86 // Construct as copy
87 template<class T>
89 :
90  UList<T>(NULL, a.size_)
91 {
92  if (this->size_)
93  {
94  this->v_ = new T[this->size_];
95 
96 # ifdef USEMEMCPY
97  if (contiguous<T>())
98  {
99  memcpy(this->v_, a.v_, this->byteSize());
100  }
101  else
102 # endif
103  {
104  List_ACCESS(T, (*this), vp);
105  List_CONST_ACCESS(T, a, ap);
106  List_FOR_ALL((*this), i)
107  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
109  }
110  }
111 }
112 
113 
114 // Construct by transferring the parameter contents
115 template<class T>
117 {
118  transfer(lst());
119 }
120 
121 
122 // Construct as copy or re-use as specified.
123 template<class T>
125 :
126  UList<T>(NULL, a.size_)
127 {
128  if (reUse)
129  {
130  this->v_ = a.v_;
131  a.v_ = 0;
132  a.size_ = 0;
133  }
134  else if (this->size_)
135  {
136  this->v_ = new T[this->size_];
137 
138 # ifdef USEMEMCPY
139  if (contiguous<T>())
140  {
141  memcpy(this->v_, a.v_, this->byteSize());
142  }
143  else
144 # endif
145  {
146  List_ACCESS(T, (*this), vp);
147  List_CONST_ACCESS(T, a, ap);
148  List_FOR_ALL((*this), i)
149  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
151  }
152  }
153 }
154 
155 
156 // Construct as subset
157 template<class T>
159 :
160  UList<T>(NULL, map.size())
161 {
162  if (this->size_)
163  {
164  // Note:cannot use List_ELEM since third argument has to be index.
165 
166  this->v_ = new T[this->size_];
167 
168  forAll(*this, i)
169  {
170  this->v_[i] = a[map[i]];
171  }
172  }
173 }
174 
175 
176 // Construct given start and end iterators.
177 template<class T>
178 template<class InputIterator>
179 Foam::List<T>::List(InputIterator first, InputIterator last)
180 {
181  label s = 0;
182  for
183  (
184  InputIterator iter = first;
185  iter != last;
186  ++iter
187  )
188  {
189  s++;
190  }
191 
192  setSize(s);
193 
194  s = 0;
195 
196  for
197  (
198  InputIterator iter = first;
199  iter != last;
200  ++iter
201  )
202  {
203  this->operator[](s++) = *iter;
204  }
205 }
206 
207 
208 // Construct as copy of FixedList<T, Size>
209 template<class T>
210 template<unsigned Size>
212 :
213  UList<T>(NULL, Size)
214 {
215  if (this->size_)
216  {
217  this->v_ = new T[this->size_];
218 
219  forAll(*this, i)
220  {
221  this->operator[](i) = lst[i];
222  }
223  }
224 }
225 
226 
227 // Construct as copy of PtrList<T>
228 template<class T>
230 :
231  UList<T>(NULL, lst.size())
232 {
233  if (this->size_)
234  {
235  this->v_ = new T[this->size_];
236 
237  forAll(*this, i)
238  {
239  this->operator[](i) = lst[i];
240  }
241  }
242 }
243 
244 
245 // Construct as copy of SLList<T>
246 template<class T>
248 :
249  UList<T>(NULL, lst.size())
250 {
251  if (this->size_)
252  {
253  this->v_ = new T[this->size_];
254 
255  label i = 0;
256  for
257  (
258  typename SLList<T>::const_iterator iter = lst.begin();
259  iter != lst.end();
260  ++iter
261  )
262  {
263  this->operator[](i++) = iter();
264  }
265  }
266 }
267 
268 
269 // Construct as copy of UIndirectList<T>
270 template<class T>
272 :
273  UList<T>(NULL, lst.size())
274 {
275  if (this->size_)
276  {
277  this->v_ = new T[this->size_];
278 
279  forAll(*this, i)
280  {
281  this->operator[](i) = lst[i];
282  }
283  }
284 }
285 
286 
287 // Construct as copy of BiIndirectList<T>
288 template<class T>
290 :
291  UList<T>(NULL, lst.size())
292 {
293  if (this->size_)
294  {
295  this->v_ = new T[this->size_];
296 
297  forAll(*this, i)
298  {
299  this->operator[](i) = lst[i];
300  }
301  }
302 }
303 
304 
305 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
306 
307 // Destroy list elements
308 template<class T>
310 {
311  if (this->v_) delete[] this->v_;
312 }
313 
314 
315 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
316 
317 template<class T>
318 void Foam::List<T>::setSize(const label newSize)
319 {
320  if (newSize < 0)
321  {
322  FatalErrorIn("List<T>::setSize(const label)")
323  << "bad set size " << newSize
324  << abort(FatalError);
325  }
326 
327  if (newSize != this->size_)
328  {
329  if (newSize > 0)
330  {
331  T* nv = new T[label(newSize)];
332 
333  if (this->size_)
334  {
335  label i = min(this->size_, newSize);
336 
337 # ifdef USEMEMCPY
338  if (contiguous<T>())
339  {
340  memcpy(nv, this->v_, i*sizeof(T));
341  }
342  else
343 # endif
344  {
345  T* vv = &this->v_[i];
346  T* av = &nv[i];
347  while (i--) *--av = *--vv;
348  }
349  }
350  if (this->v_) delete[] this->v_;
351 
352  this->size_ = newSize;
353  this->v_ = nv;
354  }
355  else
356  {
357  clear();
358  }
359  }
360 }
361 
362 
363 template<class T>
364 void Foam::List<T>::setSize(const label newSize, const T& a)
365 {
366  label oldSize = label(this->size_);
367  this->setSize(newSize);
368 
369  if (newSize > oldSize)
370  {
371  label i = newSize - oldSize;
372  T* vv = &this->v_[newSize];
373  while (i--) *--vv = a;
374  }
375 }
376 
377 
378 template<class T>
380 {
381  if (this->v_) delete[] this->v_;
382  this->size_ = 0;
383  this->v_ = 0;
384 }
385 
386 
387 // Transfer the contents of the argument List into this List
388 // and annul the argument list
389 template<class T>
391 {
392  if (this->v_) delete[] this->v_;
393  this->size_ = a.size_;
394  this->v_ = a.v_;
395 
396  a.size_ = 0;
397  a.v_ = 0;
398 }
399 
400 
401 // Transfer the contents of the argument DynamicList into this List
402 // and annul the argument list
403 template<class T>
404 template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
406 {
407  // shrink the allocated space to the number of elements used
408  a.shrink();
409  transfer(static_cast<List<T>&>(a));
410  a.clearStorage();
411 }
412 
413 
414 // Transfer the contents of the argument SortableList into this List
415 // and annul the argument list
416 template<class T>
418 {
419  // shrink away the sort indices
420  a.shrink();
421  transfer(static_cast<List<T>&>(a));
422 }
423 
424 
425 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
426 
427 // Assignment to UList operator. Takes linear time.
428 template<class T>
430 {
431  if (a.size_ != this->size_)
432  {
433  if (this->v_) delete[] this->v_;
434  this->v_ = 0;
435  this->size_ = a.size_;
436  if (this->size_) this->v_ = new T[this->size_];
437  }
438 
439  if (this->size_)
440  {
441 # ifdef USEMEMCPY
442  if (contiguous<T>())
443  {
444  memcpy(this->v_, a.v_, this->byteSize());
445  }
446  else
447 # endif
448  {
449  List_ACCESS(T, (*this), vp);
450  List_CONST_ACCESS(T, a, ap);
451  List_FOR_ALL((*this), i)
452  List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
454  }
455  }
456 }
457 
458 
459 // Assignment operator. Takes linear time.
460 template<class T>
462 {
463  if (this == &a)
464  {
465  FatalErrorIn("List<T>::operator=(const List<T>&)")
466  << "attempted assignment to self"
467  << abort(FatalError);
468  }
469 
470  operator=(static_cast<const UList<T>&>(a));
471 }
472 
473 
474 // Assignment operator. Takes linear time.
475 template<class T>
477 {
478  if (lst.size() != this->size_)
479  {
480  if (this->v_) delete[] this->v_;
481  this->v_ = 0;
482  this->size_ = lst.size();
483  if (this->size_) this->v_ = new T[this->size_];
484  }
485 
486  if (this->size_)
487  {
488  label i = 0;
489  for
490  (
491  typename SLList<T>::const_iterator iter = lst.begin();
492  iter != lst.end();
493  ++iter
494  )
495  {
496  this->operator[](i++) = iter();
497  }
498  }
499 }
500 
501 
502 // Assignment operator. Takes linear time.
503 template<class T>
505 {
506  if (lst.size() != this->size_)
507  {
508  if (this->v_) delete[] this->v_;
509  this->v_ = 0;
510  this->size_ = lst.size();
511  if (this->size_) this->v_ = new T[this->size_];
512  }
513 
514  forAll(*this, i)
515  {
516  this->operator[](i) = lst[i];
517  }
518 }
519 
520 
521 // Assignment operator. Takes linear time.
522 template<class T>
524 {
525  if (lst.size() != this->size_)
526  {
527  if (this->v_) delete[] this->v_;
528  this->v_ = 0;
529  this->size_ = lst.size();
530  if (this->size_) this->v_ = new T[this->size_];
531  }
532 
533  forAll(*this, i)
534  {
535  this->operator[](i) = lst[i];
536  }
537 }
538 
539 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
540 
541 #include "ListIO.C"
542 
543 // ************************************************************************* //
Non-intrusive singly-linked list.
Definition: SLList.H:47
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:92
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 templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
const iterator & end()
Definition: LList.H:273
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:390
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
#define List_END_FOR_ALL
Definition: ListLoopM.H:67
#define List_FOR_ALL(f, i)
Definition: ListLoopM.H:62
An STL-conforming const_iterator.
Definition: SLListBase.H:210
T & last()
Return the last element of the list.
Definition: UListI.H:131
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
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
T & first()
Return the first element of the list.
Definition: UListI.H:117
const volScalarField & T
Definition: createFields.H:25
List()
Null constructor.
Definition: ListI.H:29
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
T & operator[](const label)
Return element of UList.
Definition: UListI.H:163
void clear()
Clear the list, i.e. set size to zero.
Definition: List.C:379
void setSize(const label)
Reset size of List.
Definition: List.C:318
A List with indirect addressing.
Definition: fvMatrix.H:106
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: List.H:65
~List()
Destructor.
Definition: List.C:309
label size() const
Return the number of elements in the list.
#define forAll(list, i)
Definition: UList.H:421
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:258
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Template function to specify if the data of a type are contiguous.
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
error FatalError
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
label size() const
Return the number of elements in the UList.
Definition: ListI.H:83
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:249
#define List_ELEM(f, fp, i)
Definition: ListLoopM.H:73
List<T> is a 1D vector of objects of type T, where the size of the vector is known and used for subsc...
Indexes into negList (negative index) or posList (zero or positive index).
#define List_ACCESS(type, f, fp)
Definition: ListLoopM.H:75
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:87
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
label size() const
Return the number of elements in the list.
label size() const
Return number of elements in list.
Definition: SLListBaseI.H:67
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:78
void operator=(const UList< T > &)
Assignment from UList operator. Takes linear time.
Definition: List.C:429