illegal use of List_ELEM macro
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / List / List.C
blobfb6d9a0678929e05f12888dc97361f578aadc32e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 \*---------------------------------------------------------------------------*/
27 #include "List.H"
28 #include "ListLoopM.H"
30 #include "FixedList.H"
31 #include "PtrList.H"
32 #include "SLList.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
43 template<class T>
44 Foam::List<T>::List(const label s)
46     UList<T>(NULL, s)
48     if (this->size_ < 0)
49     {
50         FatalErrorIn("List<T>::List(const label size)")
51             << "bad size " << this->size_
52             << abort(FatalError);
53     }
55     if (this->size_)
56     {
57         this->v_ = new T[this->size_];
58     }
62 // Construct with length and single value specified
63 template<class T>
64 Foam::List<T>::List(const label s, const T& a)
66     UList<T>(NULL, s)
68     if (this->size_ < 0)
69     {
70         FatalErrorIn("List<T>::List(const label size, const T&)")
71             << "bad size " << this->size_
72             << abort(FatalError);
73     }
75     if (this->size_)
76     {
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;
82         List_END_FOR_ALL
83     }
87 // Construct as copy
88 template<class T>
89 Foam::List<T>::List(const List<T>& a)
91     UList<T>(NULL, a.size_)
93     if (this->size_)
94     {
95         this->v_ = new T[this->size_];
97 #       ifdef USEMEMCPY
98         if (contiguous<T>())
99         {
100             memcpy(this->v_, a.v_, this->byteSize());
101         }
102         else
103 #       endif
104         {
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);
109             List_END_FOR_ALL
110         }
111     }
115 // Construct by transferring the parameter contents
116 template<class T>
117 Foam::List<T>::List(const Xfer< List<T> >& lst)
119     transfer(lst());
123 // Construct as copy or re-use as specified.
124 template<class T>
125 Foam::List<T>::List(List<T>& a, bool reUse)
127     UList<T>(NULL, a.size_)
129     if (reUse)
130     {
131         this->v_ = a.v_;
132         a.v_ = 0;
133         a.size_ = 0;
134     }
135     else if (this->size_)
136     {
137         this->v_ = new T[this->size_];
139 #       ifdef USEMEMCPY
140         if (contiguous<T>())
141         {
142             memcpy(this->v_, a.v_, this->byteSize());
143         }
144         else
145 #       endif
146         {
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);
151             List_END_FOR_ALL
152         }
153     }
157 // Construct as subset
158 template<class T>
159 Foam::List<T>::List(const UList<T>& a, const unallocLabelList& map)
161     UList<T>(NULL, map.size())
163     if (this->size_)
164     {
165         // Note:cannot use List_ELEM since third argument has to be index.
167         this->v_ = new T[this->size_];
169         forAll(*this, i)
170         {
171             this->v_[i] = a[map[i]];
172         }
173     }
177 // Construct given start and end iterators.
178 template<class T>
179 template<class InputIterator>
180 Foam::List<T>::List(InputIterator first, InputIterator last)
182     label s = 0;
183     for
184     (
185         InputIterator iter = first;
186         iter != last;
187         ++iter
188     )
189     {
190         s++;
191     }
193     setSize(s);
195     s = 0;
197     for
198     (
199         InputIterator iter = first;
200         iter != last;
201         ++iter
202     )
203     {
204         this->operator[](s++) = iter();
205     }
209 // Construct as copy of FixedList<T, Size>
210 template<class T>
211 template<unsigned Size>
212 Foam::List<T>::List(const FixedList<T, Size>& lst)
214     UList<T>(NULL, Size)
216     if (this->size_)
217     {
218         this->v_ = new T[this->size_];
220         forAll(*this, i)
221         {
222             this->operator[](i) = lst[i];
223         }
224     }
228 // Construct as copy of PtrList<T>
229 template<class T>
230 Foam::List<T>::List(const PtrList<T>& lst)
232     UList<T>(NULL, lst.size())
234     if (this->size_)
235     {
236         this->v_ = new T[this->size_];
238         forAll(*this, i)
239         {
240             this->operator[](i) = lst[i];
241         }
242     }
246 // Construct as copy of SLList<T>
247 template<class T>
248 Foam::List<T>::List(const SLList<T>& lst)
250     UList<T>(NULL, lst.size())
252     if (this->size_)
253     {
254         this->v_ = new T[this->size_];
256         label i = 0;
257         for
258         (
259             typename SLList<T>::const_iterator iter = lst.begin();
260             iter != lst.end();
261             ++iter
262         )
263         {
264             this->operator[](i++) = iter();
265         }
266     }
270 // Construct as copy of IndirectList<T>
271 template<class T>
272 Foam::List<T>::List(const IndirectList<T>& lst)
274     UList<T>(NULL, lst.size())
276     if (this->size_)
277     {
278         this->v_ = new T[this->size_];
280         forAll(*this, i)
281         {
282             this->operator[](i) = lst[i];
283         }
284     }
288 // Construct as copy of UIndirectList<T>
289 template<class T>
290 Foam::List<T>::List(const UIndirectList<T>& lst)
292     UList<T>(NULL, lst.size())
294     if (this->size_)
295     {
296         this->v_ = new T[this->size_];
298         forAll(*this, i)
299         {
300             this->operator[](i) = lst[i];
301         }
302     }
306 // Construct as copy of BiIndirectList<T>
307 template<class T>
308 Foam::List<T>::List(const BiIndirectList<T>& lst)
310     UList<T>(NULL, lst.size())
312     if (this->size_)
313     {
314         this->v_ = new T[this->size_];
316         forAll(*this, i)
317         {
318             this->operator[](i) = lst[i];
319         }
320     }
324 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
326 // Destroy list elements
327 template<class T>
328 Foam::List<T>::~List()
330     if (this->v_) delete[] this->v_;
334 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
336 template<class T>
337 void Foam::List<T>::setSize(const label newSize)
339     if (newSize < 0)
340     {
341         FatalErrorIn("List<T>::setSize(const label)")
342             << "bad set size " << newSize
343             << abort(FatalError);
344     }
346     if (newSize != this->size_)
347     {
348         if (newSize > 0)
349         {
350             T* nv = new T[label(newSize)];
352             if (this->size_)
353             {
354                 register label i = min(this->size_, newSize);
356 #               ifdef USEMEMCPY
357                 if (contiguous<T>())
358                 {
359                     memcpy(nv, this->v_, i*sizeof(T));
360                 }
361                 else
362 #               endif
363                 {
364                     register T* vv = &this->v_[i];
365                     register T* av = &nv[i];
366                     while (i--) *--av = *--vv;
367                 }
368             }
369             if (this->v_) delete[] this->v_;
371             this->size_ = newSize;
372             this->v_ = nv;
373         }
374         else
375         {
376             clear();
377         }
378     }
382 template<class T>
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)
389     {
390         register label i = newSize - oldSize;
391         register T* vv = &this->v_[newSize];
392         while (i--) *--vv = a;
393     }
397 template<class T>
398 void Foam::List<T>::clear()
400     if (this->v_) delete[] this->v_;
401     this->size_ = 0;
402     this->v_ = 0;
406 // Transfer the contents of the argument List into this List
407 // and anull the argument list
408 template<class T>
409 void Foam::List<T>::transfer(List<T>& a)
411     if (this->v_) delete[] this->v_;
412     this->size_ = a.size_;
413     this->v_ = a.v_;
415     a.size_ = 0;
416     a.v_ = 0;
420 // Transfer the contents of the argument DynamicList into this List
421 // and anull the argument list
422 template<class T>
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
427     a.shrink();
428     transfer(static_cast<List<T>&>(a));
429     a.clearStorage();
433 // Transfer the contents of the argument SortableList into this List
434 // and anull the argument list
435 template<class T>
436 void Foam::List<T>::transfer(SortableList<T>& a)
438     // shrink away the sort indices
439     a.shrink();
440     transfer(static_cast<List<T>&>(a));
444 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
446 // Assignment to UList operator. Takes linear time.
447 template<class T>
448 void Foam::List<T>::operator=(const UList<T>& a)
450     if (a.size_ != this->size_)
451     {
452         if (this->v_) delete[] this->v_;
453         this->v_ = 0;
454         this->size_ = a.size_;
455         if (this->size_) this->v_ = new T[this->size_];
456     }
458     if (this->size_)
459     {
460 #       ifdef USEMEMCPY
461         if (contiguous<T>())
462         {
463             memcpy(this->v_, a.v_, this->byteSize());
464         }
465         else
466 #       endif
467         {
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);
472             List_END_FOR_ALL
473         }
474     }
478 // Assignment operator. Takes linear time.
479 template<class T>
480 void Foam::List<T>::operator=(const List<T>& a)
482     if (this == &a)
483     {
484         FatalErrorIn("List<T>::operator=(const List<T>&)")
485             << "attempted assignment to self"
486             << abort(FatalError);
487     }
489     operator=(static_cast<const UList<T>&>(a));
493 // Assignment operator. Takes linear time.
494 template<class T>
495 void Foam::List<T>::operator=(const SLList<T>& lst)
497     if (lst.size() != this->size_)
498     {
499         if (this->v_) delete[] this->v_;
500         this->v_ = 0;
501         this->size_ = lst.size();
502         if (this->size_) this->v_ = new T[this->size_];
503     }
505     if (this->size_)
506     {
507         label i = 0;
508         for
509         (
510             typename SLList<T>::const_iterator iter = lst.begin();
511             iter != lst.end();
512             ++iter
513         )
514         {
515             this->operator[](i++) = iter();
516         }
517     }
521 // Assignment operator. Takes linear time.
522 template<class T>
523 void Foam::List<T>::operator=(const IndirectList<T>& lst)
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     }
533     forAll(*this, i)
534     {
535         this->operator[](i) = lst[i];
536     }
540 // Assignment operator. Takes linear time.
541 template<class T>
542 void Foam::List<T>::operator=(const UIndirectList<T>& lst)
544     if (lst.size() != this->size_)
545     {
546         if (this->v_) delete[] this->v_;
547         this->v_ = 0;
548         this->size_ = lst.size();
549         if (this->size_) this->v_ = new T[this->size_];
550     }
552     forAll(*this, i)
553     {
554         this->operator[](i) = lst[i];
555     }
559 // Assignment operator. Takes linear time.
560 template<class T>
561 void Foam::List<T>::operator=(const BiIndirectList<T>& lst)
563     if (lst.size() != this->size_)
564     {
565         if (this->v_) delete[] this->v_;
566         this->v_ = 0;
567         this->size_ = lst.size();
568         if (this->size_) this->v_ = new T[this->size_];
569     }
571     forAll(*this, i)
572     {
573         this->operator[](i) = lst[i];
574     }
577 // * * * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * //
579 #include "ListIO.C"
581 // ************************************************************************* //