FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-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 Class
00025     Foam::UList
00026 
00027 Description
00028     A 1D vector of objects of type <T>, where the size of the vector is
00029     known and can be used for subscript bounds checking, etc.
00030 
00031     Storage is not allocated during construction or use but is supplied to
00032     the 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 <OpenFOAM/bool.H>
00046 #include <OpenFOAM/label.H>
00047 #include <OpenFOAM/uLabel.H>
00048 
00049 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00050 
00051 namespace Foam
00052 {
00053 
00054 // Forward declaration of friend classes
00055 template<class T> class List;
00056 template<class T> class SubList;
00057 
00058 // Forward declaration of friend functions and operators
00059 template<class T> class UList;
00060 template<class T> Ostream& operator<<(Ostream&, const UList<T>&);
00061 
00062 
00063 /*---------------------------------------------------------------------------*\
00064                            Class UList Declaration
00065 \*---------------------------------------------------------------------------*/
00066 
00067 template<class T>
00068 class UList
00069 {
00070     // Private data
00071 
00072         //- Number of elements in UList.
00073         label size_;
00074 
00075         //- Vector of values of type T.
00076         T* __restrict__ v_;
00077 
00078 
00079 public:
00080 
00081     // Related types
00082 
00083         //- Declare friendship with the List class
00084         friend class List<T>;
00085 
00086         //- Declare friendship with the SubList class
00087         friend class SubList<T>;
00088 
00089     // Static Member Functions
00090 
00091         //- Return a null UList
00092         inline static const UList<T>& null();
00093 
00094     // Public classes
00095 
00096         //- Less function class that can be used for sorting
00097         class less
00098         {
00099             const UList<T>& values_;
00100 
00101         public:
00102 
00103             less(const UList<T>& values)
00104             :
00105                 values_(values)
00106             {}
00107 
00108             bool operator()(const label a, const label b)
00109             {
00110                 return values_[a] < values_[b];
00111             }
00112         };
00113 
00114 
00115     // Constructors
00116 
00117         //- Null constructor.
00118         inline UList();
00119 
00120         //- Construct from components
00121         inline UList(T* __restrict__ v, label size);
00122 
00123 
00124     // Member Functions
00125 
00126 
00127         // Access
00128 
00129             //- Return the forward circular index, i.e. the next index
00130             //  which returns to the first at the end of the list
00131             inline label fcIndex(const label i) const;
00132 
00133             //- Return the reverse circular index, i.e. the previous index
00134             //  which returns to the last at the begining of the list
00135             inline label rcIndex(const label i) const;
00136 
00137             //- Return the binary size in number of characters of the UList
00138             //  if the element is a primitive type
00139             //  i.e. contiguous<T>() == true
00140             label byteSize() const;
00141 
00142 
00143             //- Return a const pointer to the first data element,
00144             //  similar to the STL front() method and the string::data() method
00145             //  This can be used (with caution) when interfacing with C code.
00146             inline const T* cdata() const;
00147 
00148             //- Return a pointer to the first data element,
00149             //  similar to the STL front() method and the string::data() method
00150             //  This can be used (with caution) when interfacing with C code.
00151             inline T* data();
00152 
00153 
00154         // Check
00155 
00156             //- Check start is within valid range (0 ... size-1).
00157             inline void checkStart(const label start) const;
00158 
00159             //- Check size is within valid range (0 ... size).
00160             inline void checkSize(const label size) const;
00161 
00162             //- Check index i is within valid range (0 ... size-1).
00163             inline void checkIndex(const label i) const;
00164 
00165 
00166         //- Write the UList as a dictionary entry.
00167         void writeEntry(Ostream&) const;
00168 
00169         //- Write the UList as a dictionary entry with keyword.
00170         void writeEntry(const word& keyword, Ostream&) const;
00171 
00172         //- Assign elements to those from UList.
00173         void assign(const UList<T>&);
00174 
00175 
00176     // Member operators
00177 
00178         //- Return element of UList.
00179         inline T& operator[](const label);
00180 
00181         //- Return element of constant UList.
00182         //  Note that the bool specialization adds lazy evaluation so reading
00183         //  an out-of-range element returns false without any ill-effects
00184         inline const T& operator[](const label) const;
00185 
00186         //- Allow cast to a const List<T>&
00187         inline operator const Foam::List<T>&() const;
00188 
00189         //- Assignment of all entries to the given value
00190         void operator=(const T&);
00191 
00192 
00193     // STL type definitions
00194 
00195         //- Type of values the UList contains.
00196         typedef T value_type;
00197 
00198         //- Type that can be used for storing into
00199         //  UList::value_type objects.
00200         typedef T& reference;
00201 
00202         //- Type that can be used for storing into
00203         //  constant UList::value_type objects
00204         typedef const T& const_reference;
00205 
00206         //- The type that can represent the difference between any two
00207         //  UList iterator objects.
00208         typedef label difference_type;
00209 
00210         //- The type that can represent the size of a UList.
00211         typedef label size_type;
00212 
00213 
00214     // STL iterator
00215 
00216         //- Random access iterator for traversing UList.
00217         typedef T* iterator;
00218 
00219         //- Return an iterator to begin traversing the UList.
00220         inline iterator begin();
00221 
00222         //- Return an iterator to end traversing the UList.
00223         inline iterator end();
00224 
00225 
00226     // STL const_iterator
00227 
00228         //- Random access iterator for traversing UList.
00229         typedef const T* const_iterator;
00230 
00231         //- Return const_iterator to begin traversing the constant UList.
00232         inline const_iterator cbegin() const;
00233 
00234         //- Return const_iterator to end traversing the constant UList.
00235         inline const_iterator cend() const;
00236 
00237         //- Return const_iterator to begin traversing the constant UList.
00238         inline const_iterator begin() const;
00239 
00240         //- Return const_iterator to end traversing the constant UList.
00241         inline const_iterator end() const;
00242 
00243 
00244     // STL reverse_iterator
00245 
00246         //- Reverse iterator for reverse traversal of UList.
00247         typedef T* reverse_iterator;
00248 
00249         //- Return reverse_iterator to begin reverse traversing the UList.
00250         inline reverse_iterator rbegin();
00251 
00252         //- Return reverse_iterator to end reverse traversing the UList.
00253         inline reverse_iterator rend();
00254 
00255 
00256     // STL const_reverse_iterator
00257 
00258         //- Reverse iterator for reverse traversal of constant UList.
00259         typedef const T* const_reverse_iterator;
00260 
00261         //- Return const_reverse_iterator to begin reverse traversing the UList.
00262         inline const_reverse_iterator crbegin() const;
00263 
00264         //- Return const_reverse_iterator to end reverse traversing the UList.
00265         inline const_reverse_iterator crend() const;
00266 
00267         //- Return const_reverse_iterator to begin reverse traversing the UList.
00268         inline const_reverse_iterator rbegin() const;
00269 
00270         //- Return const_reverse_iterator to end reverse traversing the UList.
00271         inline const_reverse_iterator rend() const;
00272 
00273 
00274     // STL member functions
00275 
00276         //- Return the number of elements in the UList.
00277         inline label size() const;
00278 
00279         //- Return size of the largest possible UList.
00280         inline label max_size() const;
00281 
00282         //- Return true if the UList is empty (ie, size() is zero).
00283         inline bool empty() const;
00284 
00285         //- Swap two ULists of the same type in constant time.
00286         void swap(UList<T>&);
00287 
00288 
00289     // STL member operators
00290 
00291         //- Equality operation on ULists of the same type.
00292         //  Returns true when the ULists are elementwise equal
00293         //  (using UList::value_type::operator==).  Takes linear time.
00294         bool operator==(const UList<T>&) const;
00295 
00296         //- The opposite of the equality operation. Takes linear time.
00297         bool operator!=(const UList<T>&) const;
00298 
00299         //- Compare two ULists lexicographically. Takes linear time.
00300         bool operator<(const UList<T>&) const;
00301 
00302         //- Compare two ULists lexicographically. Takes linear time.
00303         bool operator>(const UList<T>&) const;
00304 
00305         //- Return true if !(a > b). Takes linear time.
00306         bool operator<=(const UList<T>&) const;
00307 
00308         //- Return true if !(a < b). Takes linear time.
00309         bool operator>=(const UList<T>&) const;
00310 
00311 
00312     // Ostream operator
00313 
00314         // Write UList to Ostream.
00315         friend Ostream& operator<< <T>
00316         (
00317             Ostream&,
00318             const UList<T>&
00319         );
00320 };
00321 
00322 template<class T>
00323 void sort(UList<T>&);
00324 
00325 template<class T, class Cmp>
00326 void sort(UList<T>&, const Cmp&);
00327 
00328 template<class T>
00329 void stableSort(UList<T>&);
00330 
00331 template<class T, class Cmp>
00332 void stableSort(UList<T>&, const Cmp&);
00333 
00334 // Reverse the first n elements of the list
00335 template<class T>
00336 inline void reverse(UList<T>&, const label n);
00337 
00338 // Reverse all the elements of the list
00339 template<class T>
00340 inline void reverse(UList<T>&);
00341 
00342 
00343 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00344 
00345 } // End namespace Foam
00346 
00347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00348 
00349 #   include "UListI.H"
00350 
00351 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00352 
00377 #define forAll(list, i) \
00378     for (Foam::label i=0; i<(list).size(); i++)
00379 
00380 #define forAllReverse(list, i) \
00381     for (Foam::label i=(list).size()-1; i>=0; i--)
00382 
00396 #define forAllIter(Container,container,iter)                                   \
00397     for                                                                        \
00398     (                                                                          \
00399         Container::iterator iter = (container).begin();                        \
00400         iter != (container).end();                                             \
00401         ++iter                                                                 \
00402     )
00403 
00417 #define forAllConstIter(Container,container,iter)                              \
00418     for                                                                        \
00419     (                                                                          \
00420         Container::const_iterator iter = (container).begin();                  \
00421         iter != (container).end();                                             \
00422         ++iter                                                                 \
00423     )
00424 
00425 
00426 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00427 
00428 #ifdef NoRepository
00429 #   include "UList.C"
00430 #endif
00431 
00432 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00433 
00434 #endif
00435 
00436 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines