FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <OpenFOAM/error.H>
00027 
00028 #include "UList.H"
00029 #include <OpenFOAM/ListLoopM.H>
00030 #include <OpenFOAM/contiguous.H>
00031 
00032 #include <algorithm>
00033 
00034 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00035 
00036 template<class T>
00037 void Foam::UList<T>::assign(const UList<T>& a)
00038 {
00039     if (a.size_ != this->size_)
00040     {
00041         FatalErrorIn("UList<T>::assign(const UList<T>&)")
00042             << "ULists have different sizes: "
00043             << this->size_ << " " << a.size_
00044             << abort(FatalError);
00045     }
00046 
00047     if (this->size_)
00048     {
00049 #       ifdef USEMEMCPY
00050         if (contiguous<T>())
00051         {
00052             memcpy(this->v_, a.v_, this->byteSize());
00053         }
00054         else
00055 #       endif
00056         {
00057             List_ACCESS(T, (*this), vp);
00058             List_CONST_ACCESS(T, a, ap);
00059             List_FOR_ALL((*this), i)
00060                 List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
00061             List_END_FOR_ALL
00062         }
00063     }
00064 }
00065 
00066 
00067 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00068 
00069 template<class T>
00070 void Foam::UList<T>::operator=(const T& t)
00071 {
00072     List_ACCESS(T, (*this), vp);
00073     List_FOR_ALL((*this), i)
00074         List_ELEM((*this), vp, i) = t;
00075     List_END_FOR_ALL
00076 }
00077 
00078 
00079 // * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //
00080 
00081 template<class T>
00082 void Foam::UList<T>::swap(UList<T>& a)
00083 {
00084     if (a.size_ != this->size_)
00085     {
00086         FatalErrorIn("UList<T>::swap(const UList<T>&)")
00087             << "ULists have different sizes: "
00088             << this->size_ << " " << a.size_
00089             << abort(FatalError);
00090     }
00091 
00092     List_ACCESS(T, (*this), vp);
00093     List_ACCESS(T, a, ap);
00094     T tmp;
00095     List_FOR_ALL((*this), i)
00096         tmp = List_ELEM((*this), vp, i);
00097         List_ELEM((*this), vp, i) = List_ELEM(a, ap, i);
00098         List_ELEM(a, ap, i) = tmp;
00099     List_END_FOR_ALL
00100 }
00101 
00102 
00103 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00104 
00105 template<class T>
00106 Foam::label Foam::UList<T>::byteSize() const
00107 {
00108     if (!contiguous<T>())
00109     {
00110         FatalErrorIn("UList<T>::byteSize()")
00111             << "Cannot return the binary size of a list of "
00112                "non-primitive elements"
00113             << abort(FatalError);
00114     }
00115 
00116     return this->size_*sizeof(T);
00117 }
00118 
00119 
00120 template<class T>
00121 void Foam::sort(UList<T>& a)
00122 {
00123     std::sort(a.begin(), a.end());
00124 }
00125 
00126 
00127 template<class T, class Cmp>
00128 void Foam::sort(UList<T>& a, const Cmp& cmp)
00129 {
00130     std::sort(a.begin(), a.end(), cmp);
00131 }
00132 
00133 
00134 template<class T>
00135 void Foam::stableSort(UList<T>& a)
00136 {
00137     std::stable_sort(a.begin(), a.end());
00138 }
00139 
00140 
00141 template<class T, class Cmp>
00142 void Foam::stableSort(UList<T>& a, const Cmp& cmp)
00143 {
00144     std::stable_sort(a.begin(), a.end(), cmp);
00145 }
00146 
00147 
00148 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00149 
00150 template<class T>
00151 bool Foam::UList<T>::operator==(const UList<T>& a) const
00152 {
00153     if (this->size_ != a.size_)
00154     {
00155         return false;
00156     }
00157 
00158     bool equal = true;
00159 
00160     List_CONST_ACCESS(T, (*this), vp);
00161     List_CONST_ACCESS(T, (a), ap);
00162 
00163     List_FOR_ALL((*this), i)
00164         equal = equal && (List_ELEM((*this), vp, i) == List_ELEM((a), ap, i));
00165     List_END_FOR_ALL
00166 
00167     return equal;
00168 }
00169 
00170 
00171 template<class T>
00172 bool Foam::UList<T>::operator!=(const UList<T>& a) const
00173 {
00174     return !operator==(a);
00175 }
00176 
00177 
00178 template<class T>
00179 bool Foam::UList<T>::operator<(const UList<T>& a) const
00180 {
00181     for
00182     (
00183         const_iterator vi = begin(), ai = a.begin();
00184         vi < end() && ai < a.end();
00185         vi++, ai++
00186     )
00187     {
00188         if (*vi < *ai)
00189         {
00190             return true;
00191         }
00192         else if (*vi > *ai)
00193         {
00194             return false;
00195         }
00196     }
00197 
00198     if (this->size_ < a.size_)
00199     {
00200         return true;
00201     }
00202     else
00203     {
00204         return false;
00205     }
00206 }
00207 
00208 
00209 template<class T>
00210 bool Foam::UList<T>::operator>(const UList<T>& a) const
00211 {
00212     return a.operator<(*this);
00213 }
00214 
00215 
00216 template<class T>
00217 bool Foam::UList<T>::operator<=(const UList<T>& a) const
00218 {
00219     return !operator>(a);
00220 }
00221 
00222 
00223 template<class T>
00224 bool Foam::UList<T>::operator>=(const UList<T>& a) const
00225 {
00226     return !operator<(a);
00227 }
00228 
00229 
00230 // * * * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * //
00231 
00232 #include "UListIO.C"
00233 
00234 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines