OpenFOAM logo
Open Source CFD Toolkit

PackedList.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     PackedList
00027 
00028 Description
00029     List of packed unsigned ints. Gets given the number of bits per item.
00030 
00031 SourceFiles
00032     PackedListI.H
00033     PackedList.C
00034 
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #ifndef PackedList_H
00038 #define PackedList_H
00039 
00040 #include "labelList.H"
00041 #include "List.H"
00042 
00043 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00044 
00045 namespace Foam
00046 {
00047 
00048 
00049 /*---------------------------------------------------------------------------*\
00050                         Class PackedListName Declaration
00051 \*---------------------------------------------------------------------------*/
00052 
00053 TemplateName(PackedList);
00054 
00055 
00056 /*---------------------------------------------------------------------------*\
00057                            Class PackedList Declaration
00058 \*---------------------------------------------------------------------------*/
00059 
00060 class reference
00061 {
00062     // private data
00063     unsigned int& elem_;
00064 
00065     unsigned int mask_;
00066 
00067     label startBit_;
00068 
00069 public:
00070 
00071     inline reference(unsigned int& elem, unsigned int mask, label startBit)
00072     :
00073         elem_(elem),
00074         mask_(mask),
00075         startBit_(startBit)
00076     {}
00077 
00078     inline void operator=(const unsigned int val)
00079     {
00080         unsigned int shiftedMask = mask_ << startBit_;
00081 
00082         unsigned int shiftedVal = val << startBit_;
00083 
00084         elem_ = (elem_ & ~shiftedMask) | shiftedVal;
00085     }
00086 
00087     inline operator unsigned int () const
00088     {
00089         return (elem_ >> startBit_) & mask_;
00090     }
00091 
00092 };
00093 
00094 
00095 template <int nBits>
00096 class PackedList
00097 :
00098     private List<unsigned int>
00099 {
00100     // Private data
00101 
00102         label size_;
00103 
00104 
00105     // Private Member Functions
00106 
00107         //- Number of integers needed for i elements of size nBits.
00108         inline static label intIndex(const label i);
00109 
00110         //- Check index i is within valid range (0 ... size-1).
00111         inline void checkIndex(const label i) const;
00112 
00113         //- Check value is representable in nBits
00114         inline void checkValue(const unsigned int val) const;
00115 
00116 public:
00117 
00118 
00119 
00120     // Constructors
00121 
00122         //- Null constructor
00123         inline PackedList();
00124 
00125         //- Construct with given size. Note: initializes intList to 0.
00126         inline PackedList(const label size);
00127 
00128         //- Construct with given size and value for all elements.
00129         PackedList(const label size, const unsigned int val);
00130 
00131         //- Copy constructor.
00132         PackedList(const PackedList<nBits>& PList);
00133 
00134         //- Construct from labelList.
00135         PackedList(const labelList&);
00136 
00137         //- Clone
00138         inline autoPtr<PackedList<nBits> > clone() const;
00139 
00140     // Member Functions
00141 
00142         // Edit
00143 
00144             //- Reset size of List.
00145             void setSize(const label);
00146 
00147             //- Clear the list, i.e. set size to zero.
00148             void clear();
00149 
00150             //- Transfer the contents of the argument List into this List
00151             //  and annull the argument list.
00152             void transfer(PackedList<nBits>&);
00153 
00154 
00155         // Access
00156 
00157             //- Number of packed elements
00158             inline label size() const;
00159 
00160 
00161             //- Get value at index I
00162             inline unsigned int get(const label i) const;
00163 
00164 
00165             //- Set value at index I
00166             inline void set(const label i, const unsigned int val);
00167 
00168 
00169     // Member operators
00170 
00171             //- Get value at index i
00172             inline unsigned int operator[](const label i) const;
00173 
00174             //- Set value at index i. Returns proxy which does actual operation
00175             inline ::Foam::reference operator[](const label i);
00176 
00177             //- Assignment operator. Takes linear time.
00178             void operator=(const PackedList<nBits>&);
00179 
00180             //- Assignment of all entries to the given value
00181             inline void operator=(const unsigned int val);
00182 
00183             //- Return as labelList
00184             labelList operator()() const;
00185 
00186 
00187     // Ostream operator
00188 
00189 //        // Write PackedList to Ostream.
00190 //        friend Ostream& operator<< <nBits> (Ostream&, const PackedList<nBits>&);
00191 }
00192 ;
00193 
00194 
00195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00196 
00197 } // End namespace Foam
00198 
00199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00200 
00201 #   include "PackedListI.H"
00202 
00203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00204 
00205 #ifdef NoRepository
00206 #   include "PackedList.C"
00207 #endif
00208 
00209 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00210 
00211 #endif
00212 
00213 // ************************************************************************* //
For further information go to www.openfoam.org