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

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-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     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         //- Number of nBits entries
00103         label size_;
00104 
00105 
00106     // Private Member Functions
00107 
00108         //- Calculate underlying list size
00109         inline static label intSize(const label sz);
00110 
00111         //- Calculate index into underlying List.
00112         inline static label intIndex(const label i);
00113 
00114         //- Check index i is within valid range (0 ... size-1).
00115         inline void checkIndex(const label i) const;
00116 
00117         //- Check value is representable in nBits
00118         inline void checkValue(const unsigned int val) const;
00119 
00120 public:
00121 
00122     // Constructors
00123 
00124         //- Null constructor
00125         inline PackedList();
00126 
00127         //- Construct with given size. Note: initializes intList to 0.
00128         inline PackedList(const label size);
00129 
00130         //- Construct with given size and value for all elements.
00131         PackedList(const label size, const unsigned int val);
00132 
00133         //- Copy constructor.
00134         PackedList(const PackedList<nBits>& PList);
00135 
00136         //- Construct from labelList.
00137         PackedList(const labelList&);
00138 
00139         //- Clone
00140         inline autoPtr<PackedList<nBits> > clone() const;
00141 
00142     // Member Functions
00143 
00144         // Edit
00145 
00146             //- Reset size of List.
00147             void setSize(const label);
00148 
00149             //- Clear the list, i.e. set size to zero.
00150             void clear();
00151 
00152             //- Transfer the contents of the argument List into this List
00153             //  and annull the argument list.
00154             void transfer(PackedList<nBits>&);
00155 
00156 
00157         // Access
00158 
00159             //- Number of packed elements
00160             inline label size() const;
00161 
00162 
00163             //- Get value at index I
00164             inline unsigned int get(const label i) const;
00165 
00166 
00167             //- Set value at index I
00168             inline void set(const label i, const unsigned int val);
00169 
00170 
00171     // Member operators
00172 
00173             //- Get value at index i
00174             inline unsigned int operator[](const label i) const;
00175 
00176             //- Set value at index i. Returns proxy which does actual operation
00177             inline ::Foam::reference operator[](const label i);
00178 
00179             //- Assignment operator. Takes linear time.
00180             void operator=(const PackedList<nBits>&);
00181 
00182             //- Assignment of all entries to the given value
00183             inline void operator=(const unsigned int val);
00184 
00185             //- Return as labelList
00186             labelList operator()() const;
00187 
00188 
00189     // Ostream operator
00190 
00191 //        // Write PackedList to Ostream.
00192 //        friend Ostream& operator<< <nBits> (Ostream&, const PackedList<nBits>&);
00193 }
00194 ;
00195 
00196 
00197 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00198 
00199 } // End namespace Foam
00200 
00201 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00202 
00203 #   include "PackedListI.H"
00204 
00205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00206 
00207 #ifdef NoRepository
00208 #   include "PackedList.C"
00209 #endif
00210 
00211 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00212 
00213 #endif
00214 
00215 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd