OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

UList.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "error.H"
00028 
00029 #include "UList.H"
00030 #include "ListLoopM.H"
00031 #include "contiguous.H"
00032 
00033 #include <algorithm>
00034 
00035 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00036 
00037 template<class T>
00038 void Foam::UList<T>::assign(const UList<T>& a)
00039 {
00040     if (a.size_ != this->size_)
00041     {
00042         FatalErrorIn("UList<T>::assign(const UList<T>&)")
00043             << "ULists have different sizes: "
00044             << this->size_ << " " << a.size_
00045             << abort(FatalError);
00046     }
00047 
00048     if (this->size_)
00049     {
00050 #       ifdef USEMEMCPY
00051         if (contiguous<T>())
00052         {
00053             memcpy(this->v_, a.v_, this->byteSize());
00054         }
00055         else
00056 #       endif
00057         {
00058             List_ACCESS(T, (*this), vp);
00059             List_CONST_ACCESS(T, a, ap);
00060             List_FOR_ALL((*this), i)
00061                 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
00062             List_END_FOR_ALL
00063         }
00064     }
00065 }
00066 
00067 
00068 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00069 
00070 template<class T>
00071 void Foam::UList<T>::operator=(const T& t)
00072 {
00073     List_ACCESS(T, (*this), vp);
00074     List_FOR_ALL((*this), i)
00075         List_ELEM((*this), vp, i) = t;
00076     List_END_FOR_ALL
00077 }
00078 
00079 
00080 // * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //
00081 
00082 template<class T>
00083 void Foam::UList<T>::swap(UList<T>& a)
00084 {
00085     if (a.size_ != this->size_)
00086     {
00087         FatalErrorIn("UList<T>::swap(const UList<T>&)")
00088             << "ULists have different sizes: "
00089             << this->size_ << " " << a.size_
00090             << abort(FatalError);
00091     }
00092 
00093     List_ACCESS(T, (*this), vp);
00094     List_ACCESS(T, a, ap);
00095     T tmp;
00096     List_FOR_ALL((*this), i)
00097         tmp = List_ELEM((*this), vp, i);
00098         List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
00099         List_ELEM(a, ap, i) = tmp;
00100     List_END_FOR_ALL
00101 }
00102 
00103 
00104 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00105 
00106 template<class T>
00107 Foam::label Foam::UList<T>::byteSize() const
00108 {
00109     if (!contiguous<T>())
00110     {
00111         FatalErrorIn("UList<T>::byteSize()")
00112             << "Cannot return the binary size of a list of "
00113                "non-primitive elements"
00114             << abort(FatalError);
00115     }
00116 
00117     return this->size_*sizeof(T);
00118 }
00119 
00120 
00121 template<class T>
00122 void Foam::sort(UList<T>& a)
00123 {
00124     std::sort(a.begin(), a.end());
00125 }
00126 
00127 
00128 template<class T, class Cmp>
00129 void Foam::sort(UList<T>& a, const Cmp& cmp)
00130 {
00131     std::sort(a.begin(), a.end(), cmp);
00132 }
00133 
00134 
00135 template<class T>
00136 void Foam::stableSort(UList<T>& a)
00137 {
00138     std::stable_sort(a.begin(), a.end());
00139 }
00140 
00141 
00142 template<class T, class Cmp>
00143 void Foam::stableSort(UList<T>& a, const Cmp& cmp)
00144 {
00145     std::stable_sort(a.begin(), a.end(), cmp);
00146 }
00147 
00148 
00149 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00150 
00151 template<class T>
00152 bool Foam::UList<T>::operator==(const UList<T>& a) const
00153 {
00154     if (this->size_ != a.size_)
00155     {
00156         return false;
00157     }
00158 
00159     bool equal = true;
00160 
00161     List_CONST_ACCESS(T, (*this), vp);
00162     List_CONST_ACCESS(T, (a), ap);
00163 
00164     List_FOR_ALL((*this), i)
00165         equal = equal && (List_ELEM((*this), vp, i) == List_ELEM((a), ap, i));
00166     List_END_FOR_ALL
00167 
00168     return equal;
00169 }
00170 
00171 
00172 template<class T>
00173 bool Foam::UList<T>::operator!=(const UList<T>& a) const
00174 {
00175     return !operator==(a);
00176 }
00177 
00178 
00179 template<class T>
00180 bool Foam::UList<T>::operator<(const UList<T>& a) const
00181 {
00182     for
00183     (
00184         const_iterator vi = begin(), ai = a.begin();
00185         vi < end() && ai < a.end();
00186         vi++, ai++
00187     )
00188     {
00189         if (*vi < *ai)
00190         {
00191             return true;
00192         }
00193         else if (*vi > *ai)
00194         {
00195             return false;
00196         }
00197     }
00198 
00199     if (this->size_ < a.size_)
00200     {
00201         return true;
00202     }
00203     else
00204     {
00205         return false;
00206     }
00207 }
00208 
00209 
00210 template<class T>
00211 bool Foam::UList<T>::operator>(const UList<T>& a) const
00212 {
00213     return a.operator<(*this);
00214 }
00215 
00216 
00217 template<class T>
00218 bool Foam::UList<T>::operator<=(const UList<T>& a) const
00219 {
00220     return !operator>(a);
00221 }
00222 
00223 
00224 template<class T>
00225 bool Foam::UList<T>::operator>=(const UList<T>& a) const
00226 {
00227     return !operator<(a);
00228 }
00229 
00230 
00231 // * * * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * //
00232 
00233 #include "UListIO.C"
00234 
00235 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd