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

FixedList.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::FixedList
00026 
00027 Description
00028     A 1D vector of objects of type <T> with a fixed size <Size>.
00029 
00030 SourceFiles
00031     FixedList.C
00032     FixedListI.H
00033     FixedListIO.C
00034 
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #ifndef FixedList_H
00038 #define FixedList_H
00039 
00040 #include <OpenFOAM/bool.H>
00041 #include <OpenFOAM/label.H>
00042 #include <OpenFOAM/uLabel.H>
00043 #include <OpenFOAM/Hash.H>
00044 #include <OpenFOAM/autoPtr.H>
00045 #include <OpenFOAM/StaticAssert.H>
00046 
00047 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00048 
00049 namespace Foam
00050 {
00051 
00052 // Forward declaration of friend functions and operators
00053 
00054 template<class T, unsigned Size> class FixedList;
00055 
00056 template<class T, unsigned Size>
00057 Istream& operator>>(Istream&, FixedList<T, Size>&);
00058 
00059 template<class T, unsigned Size>
00060 Ostream& operator<<(Ostream&, const FixedList<T, Size>&);
00061 
00062 template<class T> class UList;
00063 template<class T> class SLList;
00064 
00065 
00066 /*---------------------------------------------------------------------------*\
00067                            Class FixedList Declaration
00068 \*---------------------------------------------------------------------------*/
00069 
00070 template<class T, unsigned Size>
00071 class FixedList
00072 {
00073     //- Size must be positive (non-zero) and also fit as a signed value
00074     StaticAssert(Size && Size <= INT_MAX);
00075 
00076     // Private data
00077 
00078         //- Vector of values of type T of size Size.
00079         T v_[Size];
00080 
00081 
00082 public:
00083 
00084     //- Hashing function class.
00085     //  Use Hasher directly for contiguous data. Otherwise hash incrementally.
00086     template< class HashT=Hash<T> >
00087     class Hash
00088     {
00089     public:
00090         Hash()
00091         {}
00092 
00093         inline unsigned operator()
00094         (
00095             const FixedList<T, Size>&,
00096             unsigned seed = 0
00097         ) const;
00098     };
00099 
00100     // Static Member Functions
00101 
00102         //- Return a null FixedList
00103         inline static const FixedList<T, Size>& null();
00104 
00105 
00106     // Constructors
00107 
00108         //- Null constructor.
00109         inline FixedList();
00110 
00111         //- Construct from components
00112         inline FixedList(const T v[Size]);
00113 
00114         //- Construct from value
00115         inline FixedList(const T&);
00116 
00117         //- Construct from UList.
00118         inline FixedList(const UList<T>&);
00119 
00120         //- Construct from SLList.
00121         inline FixedList(const SLList<T>&);
00122 
00123         //- Copy constructor.
00124         inline FixedList(const FixedList<T, Size>&);
00125 
00126         //- Construct from Istream.
00127         FixedList(Istream&);
00128 
00129         //- Clone
00130         inline autoPtr< FixedList<T, Size> > clone() const;
00131 
00132 
00133     // Member Functions
00134 
00135         // Access
00136 
00137             //- Return the forward circular index, i.e. the next index
00138             //  which returns to the first at the end of the list
00139             inline label fcIndex(const label i) const;
00140 
00141             //- Return the reverse circular index, i.e. the previous index
00142             //  which returns to the last at the beginning of the list
00143             inline label rcIndex(const label i) const;
00144 
00145 
00146             //- Return a const pointer to the first data element,
00147             //  similar to the STL front() method and the string::data() method
00148             //  This can be used (with caution) when interfacing with C code.
00149             inline const T* cdata() const;
00150 
00151             //- Return a pointer to the first data element,
00152             //  similar to the STL front() method and the string::data() method
00153             //  This can be used (with caution) when interfacing with C code.
00154             inline T* data();
00155 
00156 
00157         // Check
00158 
00159             //- Check start is within valid range (0 ... size-1).
00160             inline void checkStart(const label start) const;
00161 
00162             //- Check size is within valid range (0 ... size).
00163             inline void checkSize(const label size) const;
00164 
00165             //- Check index i is within valid range (0 ... size-1).
00166             inline void checkIndex(const label i) const;
00167 
00168 
00169         // Edit
00170 
00171             //- Dummy resize function
00172             //  needed to make FixedList consistent with List
00173             inline void resize(const label);
00174 
00175             //- Dummy setSize function
00176             //  needed to make FixedList consistent with List
00177             inline void setSize(const label);
00178 
00179             //- Copy (not transfer) the argument contents
00180             //  needed to make FixedList consistent with List
00181             void transfer(const FixedList<T, Size>&);
00182 
00183         //- Write the FixedList as a dictionary entry
00184         void writeEntry(Ostream&) const;
00185 
00186         //- Write the FixedList as a dictionary entry with keyword
00187         void writeEntry(const word& keyword, Ostream&) const;
00188 
00189 
00190     // Member operators
00191 
00192         //- Return element of FixedList.
00193         inline T& operator[](const label);
00194 
00195         //- Return element of constant FixedList.
00196         inline const T& operator[](const label) const;
00197 
00198         //- Assignment from array operator. Takes linear time.
00199         inline void operator=(const T v[Size]);
00200 
00201         //- Assignment from UList operator. Takes linear time.
00202         inline void operator=(const UList<T>&);
00203 
00204         //- Assignment from SLList operator. Takes linear time.
00205         inline void operator=(const SLList<T>&);
00206 
00207         //- Assignment of all entries to the given value
00208         inline void operator=(const T&);
00209 
00210 
00211     // STL type definitions
00212 
00213         //- Type of values the FixedList contains.
00214         typedef T value_type;
00215 
00216         //- Type that can be used for storing into
00217         //  FixedList::value_type objects.
00218         typedef T& reference;
00219 
00220         //- Type that can be used for storing into
00221         //  constant FixedList::value_type objects
00222         typedef const T& const_reference;
00223 
00224         //- The type that can represent the difference between any two
00225         //  FixedList iterator objects.
00226         typedef label difference_type;
00227 
00228         //- The type that can represent the size of a FixedList.
00229         typedef label size_type;
00230 
00231 
00232     // STL iterator
00233 
00234         //- Random access iterator for traversing FixedList.
00235         typedef T* iterator;
00236 
00237         //- Return an iterator to begin traversing the FixedList.
00238         inline iterator begin();
00239 
00240         //- Return an iterator to end traversing the FixedList.
00241         inline iterator end();
00242 
00243 
00244     // STL const_iterator
00245 
00246         //- Random access iterator for traversing FixedList.
00247         typedef const T* const_iterator;
00248 
00249         //- Return const_iterator to begin traversing the constant FixedList.
00250         inline const_iterator cbegin() const;
00251 
00252         //- Return const_iterator to end traversing the constant FixedList.
00253         inline const_iterator cend() const;
00254 
00255         //- Return const_iterator to begin traversing the constant FixedList.
00256         inline const_iterator begin() const;
00257 
00258         //- Return const_iterator to end traversing the constant FixedList.
00259         inline const_iterator end() const;
00260 
00261 
00262     // STL reverse_iterator
00263 
00264         //- Reverse iterator for reverse traversal of FixedList.
00265         typedef T* reverse_iterator;
00266 
00267         //- Return reverse_iterator to begin reverse traversing the FixedList.
00268         inline reverse_iterator rbegin();
00269 
00270         //- Return reverse_iterator to end reverse traversing the FixedList.
00271         inline reverse_iterator rend();
00272 
00273 
00274     // STL const_reverse_iterator
00275 
00276         //- Reverse iterator for reverse traversal of constant FixedList.
00277         typedef const T* const_reverse_iterator;
00278 
00279         //- Return const_reverse_iterator to begin reverse traversing FixedList.
00280         inline const_reverse_iterator crbegin() const;
00281 
00282         //- Return const_reverse_iterator to end reverse traversing FixedList.
00283         inline const_reverse_iterator crend() const;
00284 
00285         //- Return const_reverse_iterator to begin reverse traversing FixedList.
00286         inline const_reverse_iterator rbegin() const;
00287 
00288         //- Return const_reverse_iterator to end reverse traversing FixedList.
00289         inline const_reverse_iterator rend() const;
00290 
00291 
00292     // STL member functions
00293 
00294         //- Return the number of elements in the FixedList.
00295         inline label size() const;
00296 
00297         //- Return size of the largest possible FixedList.
00298         inline label max_size() const;
00299 
00300         //- Return true if the FixedList is empty (ie, size() is zero).
00301         inline bool empty() const;
00302 
00303         //- Swap two FixedLists of the same type in constant time.
00304         void swap(FixedList<T, Size>&);
00305 
00306 
00307     // STL member operators
00308 
00309         //- Equality operation on FixedLists of the same type.
00310         //  Returns true when the FixedLists are elementwise equal
00311         //  (using FixedList::value_type::operator==).  Takes linear time.
00312         bool operator==(const FixedList<T, Size>&) const;
00313 
00314         //- The opposite of the equality operation. Takes linear time.
00315         bool operator!=(const FixedList<T, Size>&) const;
00316 
00317         //- Compare two FixedLists lexicographically. Takes linear time.
00318         bool operator<(const FixedList<T, Size>&) const;
00319 
00320         //- Compare two FixedLists lexicographically. Takes linear time.
00321         bool operator>(const FixedList<T, Size>&) const;
00322 
00323         //- Return true if !(a > b). Takes linear time.
00324         bool operator<=(const FixedList<T, Size>&) const;
00325 
00326         //- Return true if !(a < b). Takes linear time.
00327         bool operator>=(const FixedList<T, Size>&) const;
00328 
00329 
00330     // IOstream operators
00331 
00332         //- Read List from Istream, discarding contents of existing List.
00333         friend Istream& operator>> <T, Size>
00334         (Istream&, FixedList<T, Size>&);
00335 
00336         // Write FixedList to Ostream.
00337         friend Ostream& operator<< <T, Size>
00338         (
00339             Ostream&,
00340             const FixedList<T, Size>&
00341         );
00342 };
00343 
00344 
00345 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00346 
00347 } // End namespace Foam
00348 
00349 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00350 
00351 #include "FixedListI.H"
00352 
00353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00354 
00355 #ifdef NoRepository
00356 #   include "FixedList.C"
00357 #endif
00358 
00359 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00360 
00361 #endif
00362 
00363 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines