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