OpenFOAM logo
Open Source CFD Toolkit

FixedListI.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 "UList.H"
00028 #include "SLList.H"
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00036 
00037 template<class T, label Size>
00038 inline FixedList<T, Size>::FixedList()
00039 {}
00040 
00041 template<class T, label Size>
00042 inline FixedList<T, Size>::FixedList(const T v[Size])
00043 {
00044     for (register label i=0; i<Size; i++)
00045     {
00046         v_[i] = v[i];
00047     }
00048 }
00049 
00050 template<class T, label Size>
00051 inline FixedList<T, Size>::FixedList(const T& t)
00052 {
00053     for (register label i=0; i<Size; i++)
00054     {
00055         v_[i] = t;
00056     }
00057 }
00058 
00059 template<class T, label Size>
00060 inline FixedList<T, Size>::FixedList(const UList<T>& ul)
00061 {
00062     checkSize(ul.size());
00063 
00064     for (register label i=0; i<Size; i++)
00065     {
00066         v_[i] = ul[i];
00067     }
00068 }
00069 
00070 template<class T, label Size>
00071 inline FixedList<T, Size>::FixedList(const SLList<T>& sll)
00072 {
00073     checkSize(sll.size());
00074 
00075     register label i = 0;
00076     for
00077     (
00078         typename SLList<T>::const_iterator iter = sll.begin();
00079         iter != sll.end();
00080         ++iter
00081     )
00082     {
00083         operator[](i++) = iter();
00084     }
00085 }
00086 
00087 template<class T, label Size>
00088 inline FixedList<T, Size>::FixedList(const FixedList<T, Size>& fl)
00089 {
00090     for (register label i=0; i<Size; i++)
00091     {
00092         v_[i] = fl[i];
00093     }
00094 }
00095 
00096 template<class T, label Size>
00097 inline autoPtr<FixedList<T, Size> > FixedList<T, Size>::clone() const
00098 {
00099     return autoPtr<FixedList<T, Size> >(new FixedList<T, Size>(*this));
00100 }
00101 
00102 
00103 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00104 
00105 template<class T, label Size>
00106 inline label FixedList<T, Size>::fcIndex(const label i) const
00107 {
00108     return (i == Size-1 ? 0 : i+1); 
00109 }
00110 
00111 
00112 template<class T, label Size>
00113 inline label FixedList<T, Size>::rcIndex(const label i) const
00114 {
00115     return (i == 0 ? Size-1 : i-1); 
00116 }
00117 
00118 
00119 // Check start is within valid range (0 ... size-1).
00120 template<class T, label Size>
00121 inline void FixedList<T, Size>::checkStart(const label start) const
00122 {
00123     if (start<0 || (start && start>=Size))
00124     {
00125         FatalErrorIn("FixedList<T, Size>::checkStart(const label)")
00126             << "start " << start << " out of range 0 ... " << max(Size-1, 0)
00127             << abort(FatalError);
00128     }
00129 }
00130 
00131 
00132 // Check size is within valid range (0 ... size).
00133 template<class T, label Size>
00134 inline void FixedList<T, Size>::checkSize(const label size) const
00135 {
00136     if (size<0 || size>Size)
00137     {
00138         FatalErrorIn("FixedList<T, Size>::checkSize(const label)")
00139             << "size " << size << " out of range 0 ... " << Size
00140             << abort(FatalError);
00141     }
00142 }
00143 
00144 
00145 // Check index i is within valid range (0 ... size-1).
00146 template<class T, label Size>
00147 inline void FixedList<T, Size>::checkIndex(const label i) const
00148 {
00149     if (!Size)
00150     {
00151         FatalErrorIn("FixedList<T, Size>::checkIndex(const label)")
00152             << "attempt to access element from zero sized list"
00153             << abort(FatalError);
00154     }
00155     else if (i<0 || i>=Size)
00156     {
00157         FatalErrorIn("FixedList<T, Size>::checkIndex(const label)")
00158             << "index " << i << " out of range 0 ... " << Size-1
00159             << abort(FatalError);
00160     }
00161 }
00162 
00163 
00164 template<class T, label Size>
00165 inline void FixedList<T, Size>::setSize(const label s)
00166 {
00167 #   ifdef FULLDEBUG
00168     checkSize(s);
00169 #   endif
00170 }
00171 
00172 
00173 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00174 
00175 // Return subscript-checked element access
00176 template<class T, label Size>
00177 inline T& FixedList<T, Size>::operator[](const label i)
00178 {
00179 #   ifdef FULLDEBUG
00180     checkIndex(i);
00181 #   endif
00182     return v_[i];
00183 }
00184 
00185 
00186 // Return subscript-checked const element access
00187 template<class T, label Size>
00188 inline const T& FixedList<T, Size>::operator[](const label i) const
00189 {
00190 #   ifdef FULLDEBUG
00191     checkIndex(i);
00192 #   endif
00193     return v_[i];
00194 }
00195 
00196 
00197 template<class T, label Size>
00198 inline void FixedList<T, Size>::operator=(const T v[Size])
00199 {
00200     for (register label i=0; i<Size; i++)
00201     {
00202         v_[i] = v[i];
00203     }
00204 }
00205 
00206 template<class T, label Size>
00207 inline void FixedList<T, Size>::operator=(const UList<T>& ul)
00208 {
00209     checkSize(ul.size());
00210 
00211     for (register label i=0; i<Size; i++)
00212     {
00213         v_[i] = ul[i];
00214     }
00215 }
00216 
00217 template<class T, label Size>
00218 inline void FixedList<T, Size>::operator=(const SLList<T>& sll)
00219 {
00220     checkSize(sll.size());
00221 
00222     register label i = 0;
00223     for
00224     (
00225         typename SLList<T>::const_iterator iter = sll.begin();
00226         iter != sll.end();
00227         ++iter
00228     )
00229     {
00230         operator[](i++) = iter();
00231     }
00232 }
00233 
00234 template<class T, label Size>
00235 inline void FixedList<T, Size>::operator=(const T& t)
00236 {
00237     for (register label i=0; i<Size; i++)
00238     {
00239         v_[i] = t;
00240     }
00241 }
00242 
00243 
00244 // * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //
00245 
00246 template<class T, label Size>
00247 inline typename FixedList<T, Size>::iterator
00248 FixedList<T, Size>::begin()
00249 {
00250     return v_;
00251 }
00252 
00253 
00254 template<class T, label Size>
00255 inline typename FixedList<T, Size>::const_iterator
00256 FixedList<T, Size>::begin() const
00257 {
00258     return v_;
00259 }
00260 
00261 
00262 template<class T, label Size>
00263 inline typename FixedList<T, Size>::iterator
00264 FixedList<T, Size>::end()
00265 {
00266     return &v_[Size];
00267 }
00268 
00269 
00270 template<class T, label Size>
00271 inline typename FixedList<T, Size>::const_iterator
00272 FixedList<T, Size>::end() const
00273 {
00274     return &v_[Size];
00275 }
00276 
00277 template<class T, label Size>
00278 inline typename FixedList<T, Size>::iterator
00279 FixedList<T, Size>::rbegin()
00280 {
00281     return &v_[Size-1];
00282 }
00283 
00284 
00285 template<class T, label Size>
00286 inline typename FixedList<T, Size>::const_iterator
00287 FixedList<T, Size>::rbegin() const
00288 {
00289     return &v_[Size-1];
00290 }
00291 
00292 
00293 template<class T, label Size>
00294 inline typename FixedList<T, Size>::iterator
00295 FixedList<T, Size>::rend()
00296 {
00297     return &v_[-1];
00298 }
00299 
00300 
00301 template<class T, label Size>
00302 inline typename FixedList<T, Size>::const_iterator
00303 FixedList<T, Size>::rend() const
00304 {
00305     return &v_[-1];
00306 }
00307 
00308 
00309 template<class T, label Size>
00310 inline label FixedList<T, Size>::size() const
00311 {
00312     return Size;
00313 }
00314 
00315 
00316 template<class T, label Size>
00317 inline label FixedList<T, Size>::max_size() const
00318 {
00319     return Size;
00320 }
00321 
00322 
00323 template<class T, label Size>
00324 inline bool FixedList<T, Size>::empty() const
00325 {
00326     return false;
00327 }
00328 
00329 
00330 template<class T, label Size>
00331 template<class HashT>
00332 inline FixedList<T, Size>::Hash<HashT>::Hash()
00333 {}
00334 
00335 
00336 //- Rotating Hash. From http://burtleburtle.net/bob/hash/doobs.html.
00337 template<class T, label Size>
00338 template<class HashT>
00339 inline label FixedList<T, Size>::Hash<HashT>::operator()
00340 (
00341     const FixedList<T, Size>& fl
00342 ) const
00343 {
00344     static const label farbit(8*sizeof(label)-4);
00345 
00346     label val = Size;
00347 
00348     for (register int i=0; i<Size; i++)
00349     {
00350         val = (val<<4)^(val>>farbit)^HashT()(fl[i]);
00351     }
00352 
00353     return val;
00354 }
00355 
00356 
00357 template<class T, label Size>
00358 template<class HashT>
00359 inline label FixedList<T, Size>::Hash<HashT>::operator()
00360 (
00361     const FixedList<T, Size>& fl,
00362     const label tableSize
00363 ) const
00364 {
00365     return mag(operator()(fl)) % tableSize;
00366 }
00367 
00368 
00369 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00370 
00371 } // End namespace Foam
00372 
00373 // ************************************************************************* //
For further information go to www.openfoam.org