1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
28 #include "ListLoopM.H"
30 #include "FixedList.H"
33 #include "IndirectList.H"
34 #include "UIndirectList.H"
35 #include "BiIndirectList.H"
36 #include "contiguous.H"
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
42 // Construct with length specified
44 Foam::List<T>::List(const label s)
50 FatalErrorIn("List<T>::List(const label size)")
51 << "bad size " << this->size_
57 this->v_ = new T[this->size_];
62 // Construct with length and single value specified
64 Foam::List<T>::List(const label s, const T& a)
70 FatalErrorIn("List<T>::List(const label size, const T&)")
71 << "bad size " << this->size_
77 this->v_ = new T[this->size_];
79 List_ACCESS(T, (*this), vp);
80 List_FOR_ALL((*this), i)
81 List_ELEM((*this), vp, i) = a;
89 Foam::List<T>::List(const List<T>& a)
91 UList<T>(NULL, a.size_)
95 this->v_ = new T[this->size_];
100 memcpy(this->v_, a.v_, this->byteSize());
105 List_ACCESS(T, (*this), vp);
106 List_CONST_ACCESS(T, a, ap);
107 List_FOR_ALL((*this), i)
108 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
115 // Construct by transferring the parameter contents
117 Foam::List<T>::List(const Xfer< List<T> >& lst)
123 // Construct as copy or re-use as specified.
125 Foam::List<T>::List(List<T>& a, bool reUse)
127 UList<T>(NULL, a.size_)
135 else if (this->size_)
137 this->v_ = new T[this->size_];
142 memcpy(this->v_, a.v_, this->byteSize());
147 List_ACCESS(T, (*this), vp);
148 List_CONST_ACCESS(T, a, ap);
149 List_FOR_ALL((*this), i)
150 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
157 // Construct as subset
159 Foam::List<T>::List(const UList<T>& a, const unallocLabelList& map)
161 UList<T>(NULL, map.size())
165 // Note:cannot use List_ELEM since third argument has to be index.
167 this->v_ = new T[this->size_];
171 this->v_[i] = a[map[i]];
177 // Construct given start and end iterators.
179 template<class InputIterator>
180 Foam::List<T>::List(InputIterator first, InputIterator last)
185 InputIterator iter = first;
199 InputIterator iter = first;
204 this->operator[](s++) = iter();
209 // Construct as copy of FixedList<T, Size>
211 template<unsigned Size>
212 Foam::List<T>::List(const FixedList<T, Size>& lst)
218 this->v_ = new T[this->size_];
222 this->operator[](i) = lst[i];
228 // Construct as copy of PtrList<T>
230 Foam::List<T>::List(const PtrList<T>& lst)
232 UList<T>(NULL, lst.size())
236 this->v_ = new T[this->size_];
240 this->operator[](i) = lst[i];
246 // Construct as copy of SLList<T>
248 Foam::List<T>::List(const SLList<T>& lst)
250 UList<T>(NULL, lst.size())
254 this->v_ = new T[this->size_];
259 typename SLList<T>::const_iterator iter = lst.begin();
264 this->operator[](i++) = iter();
270 // Construct as copy of IndirectList<T>
272 Foam::List<T>::List(const IndirectList<T>& lst)
274 UList<T>(NULL, lst.size())
278 this->v_ = new T[this->size_];
282 this->operator[](i) = lst[i];
288 // Construct as copy of UIndirectList<T>
290 Foam::List<T>::List(const UIndirectList<T>& lst)
292 UList<T>(NULL, lst.size())
296 this->v_ = new T[this->size_];
300 this->operator[](i) = lst[i];
306 // Construct as copy of BiIndirectList<T>
308 Foam::List<T>::List(const BiIndirectList<T>& lst)
310 UList<T>(NULL, lst.size())
314 this->v_ = new T[this->size_];
318 this->operator[](i) = lst[i];
324 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
326 // Destroy list elements
328 Foam::List<T>::~List()
330 if (this->v_) delete[] this->v_;
334 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
337 void Foam::List<T>::setSize(const label newSize)
341 FatalErrorIn("List<T>::setSize(const label)")
342 << "bad set size " << newSize
343 << abort(FatalError);
346 if (newSize != this->size_)
350 T* nv = new T[label(newSize)];
354 register label i = min(this->size_, newSize);
359 memcpy(nv, this->v_, i*sizeof(T));
364 register T* vv = &this->v_[i];
365 register T* av = &nv[i];
366 while (i--) *--av = *--vv;
369 if (this->v_) delete[] this->v_;
371 this->size_ = newSize;
383 void Foam::List<T>::setSize(const label newSize, const T& a)
385 label oldSize = this->size_;
386 this->setSize(newSize);
388 if (newSize > oldSize)
390 register label i = newSize - oldSize;
391 register T* vv = &this->v_[newSize];
392 while (i--) *--vv = a;
398 void Foam::List<T>::clear()
400 if (this->v_) delete[] this->v_;
406 // Transfer the contents of the argument List into this List
407 // and anull the argument list
409 void Foam::List<T>::transfer(List<T>& a)
411 if (this->v_) delete[] this->v_;
412 this->size_ = a.size_;
420 // Transfer the contents of the argument DynamicList into this List
421 // and anull the argument list
423 template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
424 void Foam::List<T>::transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>& a)
426 // shrink the allocated space to the number of elements used
428 transfer(static_cast<List<T>&>(a));
433 // Transfer the contents of the argument SortableList into this List
434 // and anull the argument list
436 void Foam::List<T>::transfer(SortableList<T>& a)
438 // shrink away the sort indices
440 transfer(static_cast<List<T>&>(a));
444 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
446 // Assignment to UList operator. Takes linear time.
448 void Foam::List<T>::operator=(const UList<T>& a)
450 if (a.size_ != this->size_)
452 if (this->v_) delete[] this->v_;
454 this->size_ = a.size_;
455 if (this->size_) this->v_ = new T[this->size_];
463 memcpy(this->v_, a.v_, this->byteSize());
468 List_ACCESS(T, (*this), vp);
469 List_CONST_ACCESS(T, a, ap);
470 List_FOR_ALL((*this), i)
471 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
478 // Assignment operator. Takes linear time.
480 void Foam::List<T>::operator=(const List<T>& a)
484 FatalErrorIn("List<T>::operator=(const List<T>&)")
485 << "attempted assignment to self"
486 << abort(FatalError);
489 operator=(static_cast<const UList<T>&>(a));
493 // Assignment operator. Takes linear time.
495 void Foam::List<T>::operator=(const SLList<T>& lst)
497 if (lst.size() != this->size_)
499 if (this->v_) delete[] this->v_;
501 this->size_ = lst.size();
502 if (this->size_) this->v_ = new T[this->size_];
510 typename SLList<T>::const_iterator iter = lst.begin();
515 this->operator[](i++) = iter();
521 // Assignment operator. Takes linear time.
523 void Foam::List<T>::operator=(const IndirectList<T>& lst)
525 if (lst.size() != this->size_)
527 if (this->v_) delete[] this->v_;
529 this->size_ = lst.size();
530 if (this->size_) this->v_ = new T[this->size_];
535 this->operator[](i) = lst[i];
540 // Assignment operator. Takes linear time.
542 void Foam::List<T>::operator=(const UIndirectList<T>& lst)
544 if (lst.size() != this->size_)
546 if (this->v_) delete[] this->v_;
548 this->size_ = lst.size();
549 if (this->size_) this->v_ = new T[this->size_];
554 this->operator[](i) = lst[i];
559 // Assignment operator. Takes linear time.
561 void Foam::List<T>::operator=(const BiIndirectList<T>& lst)
563 if (lst.size() != this->size_)
565 if (this->v_) delete[] this->v_;
567 this->size_ = lst.size();
568 if (this->size_) this->v_ = new T[this->size_];
573 this->operator[](i) = lst[i];
577 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
581 // ************************************************************************* //