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