1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
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 "BiIndirectList.H"
35 #include "contiguous.H"
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
46 // Construct with length specified
48 List<T>::List(const label s)
54 FatalErrorIn("List<T>::List(const label size)")
55 << "bad size " << this->size_
61 this->v_ = new T[this->size_];
70 // Construct with length and single value specified
72 List<T>::List(const label s, const T& a)
78 FatalErrorIn("List<T>::List(const label size, const T a)")
79 << "bad size " << this->size_
85 this->v_ = new T[this->size_];
87 List_ACCESS(T, (*this), vp);
88 List_FOR_ALL((*this), i)
89 List_ELEM((*this), vp, i) = a;
101 List<T>::List(const List<T>& a)
103 UList<T>(NULL, a.size_)
107 this->v_ = new T[this->size_];
112 memcpy(this->v_, a.v_, this->byteSize());
117 List_ACCESS(T, (*this), vp);
118 List_CONST_ACCESS(T, a, ap);
119 List_FOR_ALL((*this), i)
120 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
131 // Construct as copy or re-use as specified.
133 List<T>::List(List<T>& a, bool reUse)
135 UList<T>(NULL, a.size_)
143 else if (this->size_)
145 this->v_ = new T[this->size_];
150 memcpy(this->v_, a.v_, this->byteSize());
155 List_ACCESS(T, (*this), vp);
156 List_CONST_ACCESS(T, a, ap);
157 List_FOR_ALL((*this), i)
158 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
169 // Construct given size and start and end iterators.
171 template<class InputIterator>
172 List<T>::List(InputIterator first, InputIterator last)
177 InputIterator iter = first;
191 InputIterator iter = first;
196 this->operator[](s++) = iter();
201 // Construct as copy of FixedList<T, Size>
204 List<T>::List(const FixedList<T, Size>& fl)
210 this->v_ = new T[this->size_];
214 this->operator[](i) = fl[i];
224 // Construct as copy of PtrList<T>
226 List<T>::List(const PtrList<T>& sptrl)
228 UList<T>(NULL, sptrl.size())
232 this->v_ = new T[this->size_];
236 this->operator[](i) = sptrl[i];
246 // Construct as copy of SLList<T>
248 List<T>::List(const SLList<T>& sll)
250 UList<T>(NULL, sll.size())
254 this->v_ = new T[this->size_];
259 typename SLList<T>::const_iterator iter = sll.begin();
264 this->operator[](i++) = iter();
274 // Construct as copy of IndirectList<T>
276 List<T>::List(const IndirectList<T>& idl)
278 UList<T>(NULL, idl.size())
282 this->v_ = new T[this->size_];
286 this->operator[](i) = idl[i];
296 // Construct as copy of BiIndirectList<T>
298 List<T>::List(const BiIndirectList<T>& idl)
300 UList<T>(NULL, idl.size())
304 this->v_ = new T[this->size_];
308 this->operator[](i) = idl[i];
318 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
320 // Destroy list elements
324 if (this->size_) delete[] this->v_;
328 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
331 const List<T>& List<T>::null()
333 List<T>* nullPtr = reinterpret_cast<List<T>*>(NULL);
339 void List<T>::setSize(const label newSize)
343 FatalErrorIn("List<T>::setSize(const label)")
344 << "bad set size " << newSize
345 << abort(FatalError);
348 if (newSize != this->size_)
352 T* nv = new T[label(newSize)];
356 register label i = min(this->size_, newSize);
361 memcpy(nv, this->v_, i*sizeof(T));
366 register T* vv = &this->v_[i];
367 register T* av = &nv[i];
368 while (i--) *--av = *--vv;
374 this->size_ = newSize;
386 void List<T>::setSize(const label newSize, const T& a)
388 label oldSize = this->size_;
389 this->setSize(newSize);
391 if (newSize > oldSize)
393 register label i = newSize - oldSize;
394 register T* vv = &this->v_[newSize];
395 while (i--) *--vv = a;
401 void List<T>::clear()
403 if (this->size_) delete[] this->v_;
409 // Transfer the contents of the argument List into this List
410 // and anull the argument list
412 void List<T>::transfer(List<T>& a)
414 if (this->size_) delete[] this->v_;
416 this->size_ = a.size_;
425 void sort(List<T>& a)
427 std::sort(a.begin(), a.end());
431 template<class T, class Cmp>
432 void sort(List<T>& a, const Cmp& cmp)
434 std::sort(a.begin(), a.end(), cmp);
439 void stableSort(List<T>& a)
441 std::stable_sort(a.begin(), a.end());
445 template<class T, class Cmp>
446 void stableSort(List<T>& a, const Cmp& cmp)
448 std::stable_sort(a.begin(), a.end(), cmp);
452 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
454 // Assignment to UList operator. Takes linear time.
456 void List<T>::operator=(const UList<T>& a)
458 if (a.size_ != this->size_)
460 if (this->size_) delete[] this->v_;
461 this->size_ = a.size_;
462 if (this->size_) this->v_ = new T[this->size_];
470 memcpy(this->v_, a.v_, this->byteSize());
475 List_ACCESS(T, (*this), vp);
476 List_CONST_ACCESS(T, a, ap);
477 List_FOR_ALL((*this), i)
478 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
485 // Assignment operator. Takes linear time.
487 void List<T>::operator=(const List<T>& a)
491 FatalErrorIn("List<T>::operator=(const List<T>&)")
492 << "attempted assignment to self"
493 << abort(FatalError);
496 operator=(static_cast<const UList<T>&>(a));
500 // Assignment operator. Takes linear time.
502 void List<T>::operator=(const SLList<T>& sll)
504 if (sll.size() != this->size_)
506 if (this->size_) delete[] this->v_;
507 this->size_ = sll.size();
508 if (this->size_) this->v_ = new T[this->size_];
516 typename SLList<T>::const_iterator iter = sll.begin();
521 this->operator[](i++) = iter();
527 // Assignment operator. Takes linear time.
529 void List<T>::operator=(const IndirectList<T>& idl)
531 if (idl.size() != this->size_)
533 if (this->size_) delete[] this->v_;
534 this->size_ = idl.size();
535 if (this->size_) this->v_ = new T[this->size_];
542 this->operator[](i) = idl[i];
548 // Assignment operator. Takes linear time.
550 void List<T>::operator=(const BiIndirectList<T>& idl)
552 if (idl.size() != this->size_)
554 if (this->size_) delete[] this->v_;
555 this->size_ = idl.size();
556 if (this->size_) this->v_ = new T[this->size_];
563 this->operator[](i) = idl[i];
569 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
571 } // End namespace Foam
573 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
577 // ************************************************************************* //