OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen OpenCFD Solutions Contact OpenFOAM

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