initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / UList / UList.C
blob42d00bc41e7ede72f47b9e4dcf9490e0891c2f33
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 "error.H"
29 #include "UList.H"
30 #include "ListLoopM.H"
31 #include "contiguous.H"
33 #include <algorithm>
35 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
37 template<class T>
38 void Foam::UList<T>::assign(const UList<T>& a)
40     if (a.size_ != this->size_)
41     {
42         FatalErrorIn("UList<T>::assign(const UList<T>&)")
43             << "ULists have different sizes: "
44             << this->size_ << " " << a.size_
45             << abort(FatalError);
46     }
48     if (this->size_)
49     {
50 #       ifdef USEMEMCPY
51         if (contiguous<T>())
52         {
53             memcpy(this->v_, a.v_, this->byteSize());
54         }
55         else
56 #       endif
57         {
58             List_ACCESS(T, (*this), vp);
59             List_CONST_ACCESS(T, a, ap);
60             List_FOR_ALL((*this), i)
61                 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
62             List_END_FOR_ALL
63         }
64     }
68 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
70 template<class T>
71 void Foam::UList<T>::operator=(const T& t)
73     List_ACCESS(T, (*this), vp);
74     List_FOR_ALL((*this), i)
75         List_ELEM((*this), vp, i) = t;
76     List_END_FOR_ALL
80 // * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //
82 template<class T>
83 void Foam::UList<T>::swap(UList<T>& a)
85     if (a.size_ != this->size_)
86     {
87         FatalErrorIn("UList<T>::swap(const UList<T>&)")
88             << "ULists have different sizes: "
89             << this->size_ << " " << a.size_
90             << abort(FatalError);
91     }
93     List_ACCESS(T, (*this), vp);
94     List_ACCESS(T, a, ap);
95     T tmp;
96     List_FOR_ALL((*this), i)
97         tmp = List_ELEM((*this), vp, i);
98         List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
99         List_ELEM(a, ap, i) = tmp;
100     List_END_FOR_ALL
104 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
106 template<class T>
107 Foam::label Foam::UList<T>::byteSize() const
109     if (!contiguous<T>())
110     {
111         FatalErrorIn("UList<T>::byteSize()")
112             << "Cannot return the binary size of a list of "
113                "non-primitive elements"
114             << abort(FatalError);
115     }
117     return this->size_*sizeof(T);
121 template<class T>
122 void Foam::sort(UList<T>& a)
124     std::sort(a.begin(), a.end());
128 template<class T, class Cmp>
129 void Foam::sort(UList<T>& a, const Cmp& cmp)
131     std::sort(a.begin(), a.end(), cmp);
135 template<class T>
136 void Foam::stableSort(UList<T>& a)
138     std::stable_sort(a.begin(), a.end());
142 template<class T, class Cmp>
143 void Foam::stableSort(UList<T>& a, const Cmp& cmp)
145     std::stable_sort(a.begin(), a.end(), cmp);
149 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
151 template<class T>
152 bool Foam::UList<T>::operator==(const UList<T>& a) const
154     if (this->size_ != a.size_)
155     {
156         return false;
157     }
159     bool equal = true;
161     List_CONST_ACCESS(T, (*this), vp);
162     List_CONST_ACCESS(T, (a), ap);
164     List_FOR_ALL((*this), i)
165         equal = equal && (List_ELEM((*this), vp, i) == List_ELEM((a), ap, i));
166     List_END_FOR_ALL
168     return equal;
172 template<class T>
173 bool Foam::UList<T>::operator!=(const UList<T>& a) const
175     return !operator==(a);
179 template<class T>
180 bool Foam::UList<T>::operator<(const UList<T>& a) const
182     for
183     (
184         const_iterator vi = begin(), ai = a.begin();
185         vi < end() && ai < a.end();
186         vi++, ai++
187     )
188     {
189         if (*vi < *ai)
190         {
191             return true;
192         }
193         else if (*vi > *ai)
194         {
195             return false;
196         }
197     }
199     if (this->size_ < a.size_)
200     {
201         return true;
202     }
203     else
204     {
205         return false;
206     }
210 template<class T>
211 bool Foam::UList<T>::operator>(const UList<T>& a) const
213     return a.operator<(*this);
217 template<class T>
218 bool Foam::UList<T>::operator<=(const UList<T>& a) const
220     return !operator>(a);
224 template<class T>
225 bool Foam::UList<T>::operator>=(const UList<T>& a) const
227     return !operator<(a);
231 // * * * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * //
233 #include "UListIO.C"
235 // ************************************************************************* //