OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

PackedList.C

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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "PackedList.H"
00028 
00029 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00030 
00031 template<unsigned nBits>
00032 Foam::PackedList<nBits>::PackedList(const label size, const unsigned int val)
00033 :
00034     StorageList(packedLength(size), 0u),
00035     size_(size)
00036 {
00037     operator=(val);
00038 }
00039 
00040 
00041 template<unsigned nBits>
00042 Foam::PackedList<nBits>::PackedList(const UList<label>& lst)
00043 :
00044     StorageList(packedLength(lst.size()), 0u),
00045     size_(lst.size())
00046 {
00047     forAll(lst, i)
00048     {
00049         set(i, lst[i]);
00050     }
00051 }
00052 
00053 
00054 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00055 
00056 
00057 #if (UINT_MAX == 0xFFFFFFFF)
00058 // 32-bit counting, Hamming weight method
00059 #   define COUNT_PACKEDBITS(sum, x)                                           \
00060 {                                                                             \
00061     x -= (x >> 1) & 0x55555555;                                               \
00062     x = (x & 0x33333333) + ((x >> 2) & 0x33333333);                           \
00063     sum += (((x + (x >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;                \
00064 }
00065 #elif (UINT_MAX == 0xFFFFFFFFFFFFFFFF)
00066 // 64-bit counting, Hamming weight method
00067 #   define COUNT_PACKEDBITS(sum, x)                                           \
00068 {                                                                             \
00069     x -= (x >> 1) & 0x5555555555555555;                                       \
00070     x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);           \
00071     sum += (((x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F) * 0x0101010101010101) >> 56;\
00072 }
00073 #else
00074 // Arbitrary number of bits, Brian Kernighan's method
00075 #   define COUNT_PACKEDBITS(sum, x)    for (; x; ++sum) { x &= x - 1; }
00076 #endif
00077 
00078 
00079 template<unsigned nBits>
00080 unsigned int Foam::PackedList<nBits>::count() const
00081 {
00082     register unsigned int c = 0;
00083 
00084     if (size_)
00085     {
00086         // mask value for complete segments
00087         unsigned int mask = maskLower(packing());
00088 
00089         const unsigned int endSeg = size_ / packing();
00090         const unsigned int endOff = size_ % packing();
00091 
00092         // count bits in complete segments
00093         for (unsigned i = 0; i < endSeg; ++i)
00094         {
00095             register unsigned int bits = StorageList::operator[](i) & mask;
00096             COUNT_PACKEDBITS(c, bits);
00097         }
00098 
00099         // count bits in partial segment
00100         if (endOff)
00101         {
00102             mask = maskLower(endOff);
00103 
00104             register unsigned int bits = StorageList::operator[](endSeg) & mask;
00105             COUNT_PACKEDBITS(c, bits);
00106         }
00107     }
00108 
00109     return c;
00110 }
00111 
00112 
00113 template<unsigned nBits>
00114 bool Foam::PackedList<nBits>::trim()
00115 {
00116     if (!size_)
00117     {
00118         return false;
00119     }
00120 
00121     // mask value for complete segments
00122     unsigned int mask = maskLower(packing());
00123 
00124     label currElem = packedLength(size_) - 1;
00125     unsigned int endOff = size_ % packing();
00126 
00127     // clear trailing bits on final segment
00128     if (endOff)
00129     {
00130         StorageList::operator[](currElem) &= maskLower(endOff);
00131     }
00132 
00133     // test entire segment
00134     while (currElem > 0 && !(StorageList::operator[](currElem) &= mask))
00135     {
00136         currElem--;
00137     }
00138 
00139     // test segment
00140     label newsize = (currElem + 1) * packing();
00141 
00142     // mask for the final segment
00143     mask = max_value() << (nBits * (packing() - 1));
00144 
00145     for (endOff = packing(); endOff >= 1; --endOff, --newsize)
00146     {
00147         if (StorageList::operator[](currElem) & mask)
00148         {
00149             break;
00150         }
00151 
00152         mask >>= nBits;
00153     }
00154 
00155     if (size_ == newsize)
00156     {
00157         return false;
00158     }
00159 
00160     size_ = newsize;
00161     return false;
00162 }
00163 
00164 
00165 template<unsigned nBits>
00166 void Foam::PackedList<nBits>::flip()
00167 {
00168     label packLen = packedLength(size_);
00169 
00170     for (label i=0; i < packLen; i++)
00171     {
00172         StorageList::operator[](i) = ~StorageList::operator[](i);
00173     }
00174 }
00175 
00176 
00177 template<unsigned nBits>
00178 Foam::labelList Foam::PackedList<nBits>::values() const
00179 {
00180     labelList elems(size_);
00181 
00182     forAll(*this, i)
00183     {
00184         elems[i] = get(i);
00185     }
00186     return elems;
00187 }
00188 
00189 
00190 template<unsigned nBits>
00191 Foam::Ostream& Foam::PackedList<nBits>::iteratorBase::print(Ostream& os) const
00192 {
00193     os  << "iterator<"  << label(nBits) << "> ["
00194         << this->index_ << "]"
00195         << " segment:"  << label(this->index_ / packing())
00196         << " offset:"   << label(this->index_ % packing())
00197         << " value:"    << this->get()
00198         << nl;
00199 
00200     return os;
00201 }
00202 
00203 
00204 template<unsigned nBits>
00205 Foam::Ostream& Foam::PackedList<nBits>::print(Ostream& os) const
00206 {
00207     const label packLen = packedLength(size_);
00208 
00209     os  << "PackedList<" << nBits << ">"
00210         << " max_value:" << max_value()
00211         << " packing:"   << packing() << nl
00212         << " count: "     << count() << nl
00213         << " size/capacity: " << size_ << "/" << capacity() << nl
00214         << " storage/capacity: " << packLen << "/" << StorageList::size()
00215         << "\n(\n";
00216 
00217     // mask value for complete segments
00218     unsigned int mask = maskLower(packing());
00219 
00220     for (label i=0; i < packLen; i++)
00221     {
00222         const StorageType& rawBits = StorageList::operator[](i);
00223 
00224         // the final segment may not be full, modify mask accordingly
00225         if (i+1 == packLen)
00226         {
00227             unsigned int endOff = size_ % packing();
00228 
00229             if (endOff)
00230             {
00231                 mask = maskLower(endOff);
00232             }
00233             else
00234             {
00235                 continue;
00236             }
00237         }
00238 
00239         for (unsigned int testBit = (1u << max_bits()); testBit; testBit >>= 1)
00240         {
00241             if (mask & testBit)
00242             {
00243                 if (rawBits & testBit)
00244                 {
00245                     os << '1';
00246                 }
00247                 else
00248                 {
00249                     os << '-';
00250                 }
00251             }
00252             else
00253             {
00254                 os << 'x';
00255             }
00256         }
00257         os << '\n';
00258     }
00259     os << ")\n";
00260 
00261     return os;
00262 }
00263 
00264 
00265 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00266 
00267 template<unsigned nBits>
00268 void Foam::PackedList<nBits>::operator=(const PackedList<nBits>& lst)
00269 {
00270     setCapacity(lst.size());
00271     StorageList::operator=(lst);
00272 }
00273 
00274 
00275 template<unsigned nBits>
00276 void Foam::PackedList<nBits>::operator=(const UList<label>& lst)
00277 {
00278     setCapacity(lst.size());
00279 
00280     forAll(lst, i)
00281     {
00282         set(i, lst[i]);
00283     }
00284 }
00285 
00286 
00287 // * * * * * * * * * * * * * * * Ostream Operator *  * * * * * * * * * * * * //
00288 
00289 //template<unsigned nBits>
00290 //Foam::Ostream& ::Foam::operator<<(Ostream& os, const PackedList<nBits>& lst)
00291 //{
00292 //    os << lst();
00293 //    return os;
00294 //}
00295 
00296 
00297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00298 
00299 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd