OpenFOAM logo
Open Source CFD Toolkit

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-2005 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 "Swap.H"
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00036 
00037 template<class T>
00038 inline UList<T>::UList(T* restrict v, label size)
00039 :
00040     size_(size),
00041     v_(v)
00042 {}
00043 
00044 
00045 template<class T>
00046 inline UList<T>::UList()
00047 :
00048     size_(0),
00049     v_(0)
00050 {}
00051 
00052 
00053 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00054 
00055 template<class T>
00056 inline label UList<T>::fcIndex(const label i) const
00057 {
00058     return (i == size()-1 ? 0 : i+1); 
00059 }
00060 
00061 
00062 template<class T>
00063 inline label UList<T>::rcIndex(const label i) const
00064 {
00065     return (i == 0 ? size()-1 : i-1); 
00066 }
00067 
00068 
00069 // Check start is within valid range (0 ... size-1).
00070 template<class T>
00071 inline void UList<T>::checkStart(const label start) const
00072 {
00073     if (start<0 || (start && start>=size_))
00074     {
00075         FatalErrorIn("UList<T>::checkStart(const label)")
00076             << "start " << start << " out of range 0 ... " << max(size_-1, 0)
00077             << abort(FatalError);
00078     }
00079 }
00080 
00081 
00082 // Check size is within valid range (0 ... size).
00083 template<class T>
00084 inline void UList<T>::checkSize(const label size) const
00085 {
00086     if (size<0 || size>size_)
00087     {
00088         FatalErrorIn("UList<T>::checkSize(const label)")
00089             << "size " << size << " out of range 0 ... " << size_
00090             << abort(FatalError);
00091     }
00092 }
00093 
00094 
00095 // Check index i is within valid range (0 ... size-1).
00096 template<class T>
00097 inline void UList<T>::checkIndex(const label i) const
00098 {
00099     if (!size_)
00100     {
00101         FatalErrorIn("UList<T>::checkIndex(const label)")
00102             << "attempt to access element from zero sized list"
00103             << abort(FatalError);
00104     }
00105     else if (i<0 || i>=size_)
00106     {
00107         FatalErrorIn("UList<T>::checkIndex(const label)")
00108             << "index " << i << " out of range 0 ... " << size_-1
00109             << abort(FatalError);
00110     }
00111 }
00112 
00113 
00114 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00115 
00116 // Return subscript-checked element access
00117 template<class T>
00118 inline T& UList<T>::operator[](const label i)
00119 {
00120 #   ifdef FULLDEBUG
00121     checkIndex(i);
00122 #   endif
00123     return v_[i];
00124 }
00125 
00126 
00127 // Return subscript-checked const element access
00128 template<class T>
00129 inline const T& UList<T>::operator[](const label i) const
00130 {
00131 #   ifdef FULLDEBUG
00132     checkIndex(i);
00133 #   endif
00134     return v_[i];
00135 }
00136 
00137 
00138 // Allow cast to a const List<T>&
00139 template<class T>
00140 inline UList<T>::operator const List<T>&() const
00141 {
00142     return *reinterpret_cast<const List<T>*>(this);
00143 }
00144 
00145 
00146 // * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //
00147 
00148 template<class T>
00149 inline typename UList<T>::iterator
00150 UList<T>::begin()
00151 {
00152     return v_;
00153 }
00154 
00155 
00156 template<class T>
00157 inline typename UList<T>::const_iterator
00158 UList<T>::begin() const
00159 {
00160     return v_;
00161 }
00162 
00163 
00164 template<class T>
00165 inline typename UList<T>::iterator
00166 UList<T>::end()
00167 {
00168     return &v_[size_];
00169 }
00170 
00171 
00172 template<class T>
00173 inline typename UList<T>::const_iterator
00174 UList<T>::end() const
00175 {
00176     return &v_[size_];
00177 }
00178 
00179 template<class T>
00180 inline typename UList<T>::iterator
00181 UList<T>::rbegin()
00182 {
00183     return &v_[size_-1];
00184 }
00185 
00186 
00187 template<class T>
00188 inline typename UList<T>::const_iterator
00189 UList<T>::rbegin() const
00190 {
00191     return &v_[size_-1];
00192 }
00193 
00194 
00195 template<class T>
00196 inline typename UList<T>::iterator
00197 UList<T>::rend()
00198 {
00199     return &v_[-1];
00200 }
00201 
00202 
00203 template<class T>
00204 inline typename UList<T>::const_iterator
00205 UList<T>::rend() const
00206 {
00207     return &v_[-1];
00208 }
00209 
00210 
00211 template<class T>
00212 inline label UList<T>::size() const
00213 {
00214     return size_;
00215 }
00216 
00217 
00218 template<class T>
00219 inline label UList<T>::max_size() const
00220 {
00221     return labelMax;
00222 }
00223 
00224 
00225 template<class T>
00226 inline bool UList<T>::empty() const
00227 {
00228     return (size_ == 0);
00229 }
00230 
00231 
00232 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00233 
00234 template<class T>
00235 inline void reverse(UList<T>& ul, const label n)
00236 {
00237     for (int i=0; i<n/2; i++)
00238     {
00239         Swap(ul[i], ul[n-1-i]);
00240     }
00241 }
00242 
00243 template<class T>
00244 inline void reverse(UList<T>& ul)
00245 {
00246     reverse(ul, ul.size());
00247 }
00248 
00249 
00250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00251 
00252 } // End namespace Foam
00253 
00254 // ************************************************************************* //
For further information go to www.openfoam.org