initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / Lists / UList / UList.C
blob85ee1bb4addad7ba6ae1359d661f90e0f1190f78
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 "error.H"
29 #include "UList.H"
30 #include "ListLoopM.H"
31 #include "contiguous.H"
33 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
35 template<class T>
36 const Foam::UList<T>& Foam::UList<T>::null()
38     UList<T>* nullPtr = reinterpret_cast<UList<T>*>(NULL);
39     return *nullPtr;
43 template<class T>
44 void Foam::UList<T>::assign(const UList<T>& a)
46     if (a.size_ != this->size_)
47     {
48         FatalErrorIn("UList<T>::operator=(const UList<T>&)")
49             << "ULists have different sizes: "
50             << this->size_ << " " << a.size_
51             << abort(FatalError);
52     }
54     if (this->size_)
55     {
56 #       ifdef USEMEMCPY
57         if (contiguous<T>())
58         {
59             memcpy(this->v_, a.v_, this->byteSize());
60         }
61         else
62 #       endif
63         {
64             List_ACCESS(T, (*this), vp);
65             List_CONST_ACCESS(T, a, ap);
66             List_FOR_ALL((*this), i)
67                 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
68             List_END_FOR_ALL
69         }
70     }
74 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
76 template<class T>
77 void Foam::UList<T>::operator=(const T& t)
79     List_ACCESS(T, (*this), vp);
80     List_FOR_ALL((*this), i)
81         List_ELEM((*this), vp, i) = t;
82     List_END_FOR_ALL
86 // * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //
88 template<class T>
89 void Foam::UList<T>::swap(UList<T>& a)
91     if (a.size_ != this->size_)
92     {
93         FatalErrorIn("UList<T>::swap(const UList<T>&)")
94             << "ULists have different sizes: "
95             << this->size_ << " " << a.size_
96             << abort(FatalError);
97     }
99     List_ACCESS(T, (*this), vp);
100     List_ACCESS(T, a, ap);
101     T tmp;
102     List_FOR_ALL((*this), i)
103         tmp = List_ELEM((*this), vp, i);
104         List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
105         List_ELEM(a, ap, i) = tmp;
106     List_END_FOR_ALL
110 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
112 template<class T>
113 Foam::label Foam::UList<T>::byteSize() const
115     if (!contiguous<T>())
116     {
117         FatalErrorIn("UList<T>::byteSize()")
118             << "Cannot return the binary size of a list of "
119                "non-primitive elements"
120             << abort(FatalError);
121     }
123     return this->size_*sizeof(T);
127 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
129 template<class T>
130 bool Foam::UList<T>::operator==(const UList<T>& a) const
132     if (this->size_ != a.size_)
133     {
134         return false;
135     }
137     bool equal = true;
139     List_CONST_ACCESS(T, (*this), vp);
140     List_CONST_ACCESS(T, (a), ap);
142     List_FOR_ALL((*this), i)
143         equal = equal && (List_ELEM((*this), vp, i) == List_ELEM((a), ap, i));
144     List_END_FOR_ALL
146     return equal;
150 template<class T>
151 bool Foam::UList<T>::operator!=(const UList<T>& a) const
153     return !operator==(a);
157 template<class T>
158 bool Foam::UList<T>::operator<(const UList<T>& a) const
160     for
161     (
162         const_iterator vi = begin(), ai = a.begin();
163         vi < end() && ai < a.end();
164         vi++, ai++
165     )
166     {
167         if (*vi < *ai)
168         {
169             return true;
170         }
171         else if (*vi > *ai)
172         {
173             return false;
174         }
175     }
177     if (this->size_ < a.size_)
178     {
179         return true;
180     }
181     else
182     {
183         return false;
184     }
188 template<class T>
189 bool Foam::UList<T>::operator>(const UList<T>& a) const
191     return a.operator<(*this);
195 template<class T>
196 bool Foam::UList<T>::operator<=(const UList<T>& a) const
198     return !operator>(a);
202 template<class T>
203 bool Foam::UList<T>::operator>=(const UList<T>& a) const
205     return !operator<(a);
209 // * * * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * //
211 #include "UListIO.C"
213 // ************************************************************************* //