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

PackedListI.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 
00026 \*---------------------------------------------------------------------------*/
00027 
00028 #ifndef PackedList_I
00029 #define PackedList_I
00030 
00031 #include "IOstreams.H"
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 // Calculate underlying list size
00039 template<int nBits>
00040 inline label PackedList<nBits>::intSize(const label sz)
00041 {
00042     const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00043 
00044     return (sz+nElemsPerLabel-1)/nElemsPerLabel;
00045 }
00046 
00047 
00048 // Convert index into index in integer array
00049 template<int nBits>
00050 inline label PackedList<nBits>::intIndex(const label i)
00051 {
00052     const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00053 
00054     // Index in underlying int array
00055     label elemI = i/nElemsPerLabel;
00056 
00057     return elemI;
00058 }
00059 
00060 
00061 // Check index i is within valid range (0 ... size-1).
00062 template<int nBits>
00063 inline void PackedList<nBits>::checkIndex(const label i) const
00064 {
00065     if (!size_)
00066     {
00067         FatalErrorIn("PackedList<nBits>::checkIndex(const label)")
00068             << "attempt to access element from zero sized list"
00069             << abort(FatalError);
00070     }
00071     else if (i<0 || i>=size_)
00072     {
00073         FatalErrorIn("PackedList<nBits>::checkIndex(const label)")
00074             << "index " << i << " out of range 0 ... " << size_-1
00075             << abort(FatalError);
00076     }
00077 }
00078 
00079 
00080 // Check value is representable in nBits
00081 template<int nBits>
00082 inline void PackedList<nBits>::checkValue(const unsigned int val) const
00083 {
00084     if (val>=(1u << nBits))
00085     {
00086         FatalErrorIn("PackedList<T>::set(const unsigned int)")
00087             << "value " << label(val) << " out of range 0 ... "
00088             << label((1u << nBits)-1)
00089             << " representable by " << nBits << " bits"
00090             << abort(FatalError);
00091     }
00092 }
00093 
00094 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00095 
00096 // Null constructor
00097 template<int nBits>
00098 inline PackedList<nBits>::PackedList()
00099 :
00100     List<unsigned int>(0),
00101     size_(0)
00102 {}
00103 
00104 
00105 // Construct with given size.
00106 template<int nBits>
00107 inline PackedList<nBits>::PackedList(const label size)
00108 :
00109     List<unsigned int>(intSize(size), 0u),
00110     size_(size)
00111 {}
00112 
00113 
00114 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00115 
00116 template<int nBits>
00117 inline label PackedList<nBits>::size() const
00118 {
00119     return size_;
00120 }
00121 
00122 
00123 // Get value at i
00124 template<int nBits>
00125 inline unsigned int PackedList<nBits>::get(const label i) const
00126 {
00127 #   ifdef DEBUGList
00128     checkIndex(i);
00129 #   endif
00130 
00131     // Constant: number of elements that fit in an unsigned int
00132     const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00133 
00134     unsigned int mask = ((1u << nBits) - 1);
00135 
00136     label indexInLabel = i % nElemsPerLabel;
00137 
00138     // Starting bit in int.
00139     label startBit = nBits*indexInLabel;
00140 
00141     return (List<unsigned int>::operator[](intIndex(i)) >> startBit) & mask;
00142 }
00143 
00144 
00145 template<int nBits>
00146 inline unsigned int PackedList<nBits>::operator[](const label i) const
00147 {
00148     return get(i);
00149 }
00150 
00151 
00152 // Set value at i
00153 template<int nBits>
00154 inline void PackedList<nBits>::set(const label i, const unsigned int val)
00155 {
00156 #   ifdef DEBUGList
00157     checkIndex(i);
00158     checkValue(val);
00159 #   endif
00160 
00161     // Constant: number of elements that fit in an unsigned int
00162     const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00163 
00164     unsigned int mask = ((1u << nBits) - 1);
00165 
00166     label indexInLabel = i % nElemsPerLabel;
00167 
00168     // Starting bit in int.
00169     label startBit = nBits*indexInLabel;
00170 
00171 
00172     unsigned int shiftedMask = mask << startBit;
00173 
00174     unsigned int shiftedVal = val << startBit;
00175 
00176     unsigned int& elem = List<unsigned int>::operator[](intIndex(i));
00177 
00178     elem = (elem & ~shiftedMask) | shiftedVal;
00179 }
00180 
00181 
00182 template<int nBits>
00183 inline ::Foam::reference PackedList<nBits>::operator[](const label i)
00184 {
00185 #   ifdef DEBUGList
00186     checkIndex(i);
00187 #   endif
00188 
00189     // Constant: number of elements that fit in an unsigned int
00190     const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00191 
00192     unsigned int mask = ((1u << nBits) - 1);
00193 
00194     label indexInLabel = i % nElemsPerLabel;
00195 
00196     // Starting bit in int.
00197     label startBit = nBits*indexInLabel;
00198 
00199     unsigned int& elem = List<unsigned int>::operator[](intIndex(i));
00200 
00201     return ::Foam::reference(elem, mask, startBit);
00202 }
00203 
00204 
00205 // Set all to val
00206 template<int nBits>
00207 inline void PackedList<nBits>::operator=(const unsigned int val)
00208 {
00209 #   ifdef DEBUGList
00210     checkValue(val);
00211 #   endif
00212     List<unsigned int>::operator=(val);
00213 }
00214 
00215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00216 
00217 } // End namespace Foam
00218 
00219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00220 
00221 #endif
00222 
00223 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd