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

UListI.H

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 #include "pTraits.H"
00029 #include "Swap.H"
00030 
00031 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00032 
00033 template<class T>
00034 inline Foam::UList<T>::UList()
00035 :
00036     size_(0),
00037     v_(0)
00038 {}
00039 
00040 
00041 template<class T>
00042 inline Foam::UList<T>::UList(T* __restrict__ v, label size)
00043 :
00044     size_(size),
00045     v_(v)
00046 {}
00047 
00048 
00049 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00050 
00051 template<class T>
00052 inline const Foam::UList<T>& Foam::UList<T>::null()
00053 {
00054     return *reinterpret_cast< UList<T>* >(0);
00055 }
00056 
00057 
00058 template<class T>
00059 inline Foam::label Foam::UList<T>::fcIndex(const label i) const
00060 {
00061     return (i == size()-1 ? 0 : i+1);
00062 }
00063 
00064 
00065 template<class T>
00066 inline Foam::label Foam::UList<T>::rcIndex(const label i) const
00067 {
00068     return (i ? i-1 : size()-1);
00069 }
00070 
00071 
00072 // Check start is within valid range (0 ... size-1).
00073 template<class T>
00074 inline void Foam::UList<T>::checkStart(const label start) const
00075 {
00076     if (start<0 || (start && start>=size_))
00077     {
00078         FatalErrorIn("UList<T>::checkStart(const label)")
00079             << "start " << start << " out of range 0 ... " << max(size_-1, 0)
00080             << abort(FatalError);
00081     }
00082 }
00083 
00084 
00085 // Check size is within valid range (0 ... size).
00086 template<class T>
00087 inline void Foam::UList<T>::checkSize(const label size) const
00088 {
00089     if (size<0 || size>size_)
00090     {
00091         FatalErrorIn("UList<T>::checkSize(const label)")
00092             << "size " << size << " out of range 0 ... " << size_
00093             << abort(FatalError);
00094     }
00095 }
00096 
00097 
00098 // Check index i is within valid range (0 ... size-1).
00099 template<class T>
00100 inline void Foam::UList<T>::checkIndex(const label i) const
00101 {
00102     if (!size_)
00103     {
00104         FatalErrorIn("UList<T>::checkIndex(const label)")
00105             << "attempt to access element from zero sized list"
00106             << abort(FatalError);
00107     }
00108     else if (i<0 || i>=size_)
00109     {
00110         FatalErrorIn("UList<T>::checkIndex(const label)")
00111             << "index " << i << " out of range 0 ... " << size_-1
00112             << abort(FatalError);
00113     }
00114 }
00115 
00116 
00117 template<class T>
00118 inline const T* Foam::UList<T>::cdata() const
00119 {
00120     return v_;
00121 }
00122 
00123 
00124 template<class T>
00125 inline T* Foam::UList<T>::data()
00126 {
00127     return v_;
00128 }
00129 
00130 
00131 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00132 
00133 
00134 // element access
00135 template<class T>
00136 inline T& Foam::UList<T>::operator[](const label i)
00137 {
00138 #   ifdef FULLDEBUG
00139     checkIndex(i);
00140 #   endif
00141     return v_[i];
00142 }
00143 
00144 
00145 namespace Foam
00146 {
00147 
00148     // Template specialization for bool
00149     template<>
00150     inline const bool& Foam::UList<bool>::operator[](const label i) const
00151     {
00152         // lazy evaluation - return false for out-of-range
00153         if (i < size_)
00154         {
00155             return v_[i];
00156         }
00157         else
00158         {
00159             return Foam::pTraits<bool>::zero;
00160         }
00161     }
00162 
00163 } // end of namespace Foam
00164 
00165 
00166 // const element access
00167 template<class T>
00168 inline const T& Foam::UList<T>::operator[](const label i) const
00169 {
00170 #   ifdef FULLDEBUG
00171     checkIndex(i);
00172 #   endif
00173     return v_[i];
00174 }
00175 
00176 
00177 // Allow cast to a const List<T>&
00178 template<class T>
00179 inline Foam::UList<T>::operator const Foam::List<T>&() const
00180 {
00181     return *reinterpret_cast<const List<T>*>(this);
00182 }
00183 
00184 
00185 // * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //
00186 
00187 template<class T>
00188 inline typename Foam::UList<T>::iterator
00189 Foam::UList<T>::begin()
00190 {
00191     return v_;
00192 }
00193 
00194 template<class T>
00195 inline typename Foam::UList<T>::const_iterator
00196 Foam::UList<T>::begin() const
00197 {
00198     return v_;
00199 }
00200 
00201 template<class T>
00202 inline typename Foam::UList<T>::const_iterator
00203 Foam::UList<T>::cbegin() const
00204 {
00205     return v_;
00206 }
00207 
00208 template<class T>
00209 inline typename Foam::UList<T>::iterator
00210 Foam::UList<T>::end()
00211 {
00212     return &v_[size_];
00213 }
00214 
00215 template<class T>
00216 inline typename Foam::UList<T>::const_iterator
00217 Foam::UList<T>::end() const
00218 {
00219     return &v_[size_];
00220 }
00221 
00222 template<class T>
00223 inline typename Foam::UList<T>::const_iterator
00224 Foam::UList<T>::cend() const
00225 {
00226     return &v_[size_];
00227 }
00228 
00229 template<class T>
00230 inline typename Foam::UList<T>::iterator
00231 Foam::UList<T>::rbegin()
00232 {
00233     return &v_[size_-1];
00234 }
00235 
00236 template<class T>
00237 inline typename Foam::UList<T>::const_iterator
00238 Foam::UList<T>::rbegin() const
00239 {
00240     return &v_[size_-1];
00241 }
00242 
00243 template<class T>
00244 inline typename Foam::UList<T>::const_iterator
00245 Foam::UList<T>::crbegin() const
00246 {
00247     return &v_[size_-1];
00248 }
00249 
00250 template<class T>
00251 inline typename Foam::UList<T>::iterator
00252 Foam::UList<T>::rend()
00253 {
00254     return &v_[-1];
00255 }
00256 
00257 template<class T>
00258 inline typename Foam::UList<T>::const_iterator
00259 Foam::UList<T>::rend() const
00260 {
00261     return &v_[-1];
00262 }
00263 
00264 template<class T>
00265 inline typename Foam::UList<T>::const_iterator
00266 Foam::UList<T>::crend() const
00267 {
00268     return &v_[-1];
00269 }
00270 
00271 template<class T>
00272 inline Foam::label Foam::UList<T>::size() const
00273 {
00274     return size_;
00275 }
00276 
00277 
00278 template<class T>
00279 inline Foam::label Foam::UList<T>::max_size() const
00280 {
00281     return labelMax;
00282 }
00283 
00284 
00285 template<class T>
00286 inline bool Foam::UList<T>::empty() const
00287 {
00288     return !size_;
00289 }
00290 
00291 
00292 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00293 
00294 template<class T>
00295 inline void Foam::reverse(UList<T>& ul, const label n)
00296 {
00297     for (int i=0; i<n/2; i++)
00298     {
00299         Swap(ul[i], ul[n-1-i]);
00300     }
00301 }
00302 
00303 template<class T>
00304 inline void Foam::reverse(UList<T>& ul)
00305 {
00306     reverse(ul, ul.size());
00307 }
00308 
00309 
00310 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd