OpenFOAM logo
Open Source CFD Toolkit

UList.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 Class
00026     UList<T>
00027 
00028 Description
00029     UList<T> is a 1D vector of objects of type T, where the size of
00030     the vector is known and used for subscript bounds checking, etc.  Storage
00031     is not allocated during construction or use but is supplied to the
00032     constructor as an argument.  This type of list is particularly useful
00033     for lists that refer to parts of existing lists such as SubList.
00034 
00035 SourceFiles
00036     UList.C
00037     UListI.H
00038     UListIO.C
00039 
00040 \*---------------------------------------------------------------------------*/
00041 
00042 #ifndef UList_H
00043 #define UList_H
00044 
00045 #include "label.H"
00046 #include "bool.H"
00047 
00048 #ifndef cray
00049 #    define restrict
00050 #endif
00051 
00052 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00053 
00054 namespace Foam
00055 {
00056 
00057 //- Pre-declare related List type
00058 template<class T>
00059 class List;
00060 
00061 //- Pre-declare related SubList type
00062 template<class T>
00063 class SubList;
00064 
00065 // * * * * * * Forward declaration of template friend fuctions * * * * * * * //
00066 
00067 template<class T> class UList;
00068 
00069 template<class T> Ostream& operator<<(Ostream&, const UList<T>&);
00070 
00071 
00072 /*---------------------------------------------------------------------------*\
00073                            Class UList Declaration
00074 \*---------------------------------------------------------------------------*/
00075 
00076 template<class T>
00077 class UList
00078 {
00079     // Private data
00080 
00081         //- Number of elements in UList.
00082         label size_;
00083 
00084         //- Vector of values of type T.
00085         T* restrict v_;
00086 
00087 
00088 public:
00089 
00090     // Related types
00091 
00092         //- Declare friendship with the List class
00093         friend class List<T>;
00094 
00095         //- Declare friendship with the SubList class
00096         friend class SubList<T>;
00097 
00098 
00099     // Constructors
00100 
00101         //- Null constructor.
00102         inline UList();
00103 
00104         //- Construct from components
00105         inline UList(T* restrict v, label size);
00106 
00107 
00108     // Member functions
00109 
00110         //- Return a null UList
00111         static UList<T>& null();
00112 
00113 
00114         // Access
00115 
00116             //- Return the forward circular index, i.e. the next index
00117             //  which returns to the first at the end of the list
00118             inline label fcIndex(const label i) const;
00119 
00120             //- Return the reverse circular index, i.e. the previous index
00121             //  which returns to the last at the begining of the list
00122             inline label rcIndex(const label i) const;
00123 
00124 
00125         // Check
00126 
00127             //- Check start is within valid range (0 ... size-1).
00128             inline void checkStart(const label start) const;
00129 
00130             //- Check size is within valid range (0 ... size).
00131             inline void checkSize(const label size) const;
00132 
00133             //- Check index i is within valid range (0 ... size-1).
00134             inline void checkIndex(const label i) const;
00135 
00136 
00137         //- Write the UList as a dictionary entry
00138         void writeEntry(Ostream& os) const;
00139 
00140         //- Write the UList as a dictionary entry with keyword
00141         void writeEntry(const word& keyword, Ostream& os) const;
00142 
00143 
00144     // Member operators
00145 
00146         //- Return subscript-checked element of UList.
00147         inline T& operator[](const label);
00148 
00149         //- Return subscript-checked element of constant UList.
00150         inline const T& operator[](const label) const;
00151 
00152         //- Allow cast to a const List<T>&
00153         inline operator const List<T>&() const;
00154 
00155         //- Assignment of all entries to the given value
00156         void operator=(const T&);
00157 
00158 
00159     // STL type definitions
00160 
00161         //- Type of values the UList contains.
00162         typedef T value_type;
00163 
00164         //- Type that can be used for storing into
00165         //  UList::value_type objects.
00166         typedef T& reference;
00167 
00168         //- Type that can be used for storing into
00169         //  constant UList::value_type objects
00170         typedef const T& const_reference;
00171 
00172         //- The type that can represent the difference between any two
00173         //  UList iterator objects.
00174         typedef label difference_type;
00175 
00176         //- The type that can represent the size of a UList.
00177         typedef label size_type;
00178 
00179 
00180     // STL iterator
00181 
00182         //- Random access iterator for traversing UList.
00183         typedef T* iterator;
00184 
00185         //- Return an iterator to begin traversing the UList.
00186         inline iterator begin();
00187 
00188         //- Return an iterator to end traversing the UList.
00189         inline iterator end();
00190 
00191 
00192     // STL const_iterator
00193 
00194         //- Random access iterator for traversing UList.
00195         typedef const T* const_iterator;
00196 
00197         //- Return a const_iterator to begin traversing the
00198         //  constant UList.
00199         inline const_iterator begin() const;
00200 
00201         //- Return a const_iterator to end traversing the
00202         //  constant UList.
00203         inline const_iterator end() const;
00204 
00205 
00206     // STL reverse_iterator
00207 
00208         //- Reverse iterator for reverse traversal of UList.
00209         typedef T* reverse_iterator;
00210 
00211         //- Return a reverse_iterator to begin reverse traversing the
00212         //  UList.
00213         inline reverse_iterator rbegin();
00214 
00215         //- Return a reverse_iterator to end reverse traversing the
00216         //  UList.
00217         inline reverse_iterator rend();
00218 
00219 
00220     // STL const_reverse_iterator
00221 
00222         //- Reverse iterator for reverse traversal of constant UList.
00223         typedef const T* const_reverse_iterator;
00224 
00225         //- Return a const_reverse_iterator to begin reverse traversing the
00226         //  UList.
00227         inline const_reverse_iterator rbegin() const;
00228 
00229         //- Return a const_reverse_iterator to end reverse traversing the
00230         //  UList.
00231         inline const_reverse_iterator rend() const;
00232 
00233 
00234     // STL member functions
00235 
00236         //- Return the number of elements in the UList.
00237         inline label size() const;
00238 
00239         //- Return size of the largest possible UList.
00240         inline label max_size() const;
00241 
00242         //- Return true if the UList is empty (i.e., if size() == 0).
00243         inline bool empty() const;
00244 
00245         //- Swap two ULists of the same type in constant time.
00246         void swap(UList<T>&);
00247 
00248 
00249     // STL member operators
00250 
00251         //- Equality operation on ULists of the same type.
00252         //  Returns true when the ULists are elementwise equal
00253         //  (using UList::value_type::operator==).  Takes linear time.
00254         bool operator==(const UList<T>&) const;
00255 
00256         //- The opposite of the equality operation. Takes linear time.
00257         bool operator!=(const UList<T>&) const;
00258 
00259         //- Compare two ULists lexicographically. Takes linear time.
00260         bool operator<(const UList<T>&) const;
00261 
00262         //- Compare two ULists lexicographically. Takes linear time.
00263         bool operator>(const UList<T>&) const;
00264 
00265         //- Return true if !(a > b). Takes linear time.
00266         bool operator<=(const UList<T>&) const;
00267 
00268         //- Return true if !(a < b). Takes linear time.
00269         bool operator>=(const UList<T>&) const;
00270 
00271 
00272     // Ostream operator
00273 
00274         // Write UList to Ostream.
00275         friend Ostream& operator<< <T>(Ostream&, const UList<T>&);
00276 };
00277 
00278 
00279 // Allow binary copying and writing of arrays of primitive types
00280 inline bool writeBinary(const bool*)        {return true;}
00281 inline bool writeBinary(const char*)        {return true;}
00282 inline bool writeBinary(const short*)       {return true;}
00283 inline bool writeBinary(const int*)         {return true;}
00284 inline bool writeBinary(const long*)        {return true;}
00285 inline bool writeBinary(const float*)       {return true;}
00286 inline bool writeBinary(const double*)      {return true;}
00287 inline bool writeBinary(const long double*) {return true;}
00288 
00289 // Disallow binary copying and writing of arrays of everything else
00290 template<class T>
00291 inline bool writeBinary(const T*)           {return false;}
00292 
00293 
00294 // Reverse the first n elements of the list
00295 template<class T>
00296 inline void reverse(UList<T>& ul, const label n);
00297 
00298 // Reverse all the elements of the list
00299 template<class T>
00300 inline void reverse(UList<T>& ul);
00301 
00302 
00303 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00304 
00305 } // End namespace Foam
00306 
00307 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00308 
00309 #   include "UListI.H"
00310 
00311 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00312 
00313 /*  Usage of forAll macro :
00314 
00315     forAll(anyList, i)
00316     {
00317         statements;
00318     }
00319 */
00320 
00321 #define forAll(list, i) for (Foam::label i=0; i<(list).size(); i++)
00322 #define forAllReverse(list, i) for (Foam::label i=(list).size()-1; i>=0; i--)
00323 
00324 
00325 /*  Usage of forAllIter macro :
00326 
00327     forAll(ContainerType, container, iter)
00328     {
00329         statements;
00330     }
00331 */
00332 
00333 #define forAllIter(Container,container,iter)                                   \
00334     for                                                                        \
00335     (                                                                          \
00336         Container::iterator iter = (container).begin();                        \
00337         iter != (container).end();                                             \
00338         ++iter                                                                 \
00339     )
00340 
00341 #define forAllConstIter(Container,container,iter)                              \
00342     for                                                                        \
00343     (                                                                          \
00344         Container::const_iterator iter = (container).begin();                  \
00345         iter != (container).end();                                             \
00346         ++iter                                                                 \
00347     )
00348 
00349 
00350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00351 
00352 #ifdef NoRepository
00353 #   include "UList.C"
00354 #endif
00355 
00356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00357 
00358 #endif
00359 
00360 // ************************************************************************* //
For further information go to www.openfoam.org