Main Page | Modules | Namespace List | Data Structures | File List | Data Fields | Globals

Bitmap8.cpp

Go to the documentation of this file.
00001 /*
00002  
00003 Copyright (c) 2004, Cornell University
00004 All rights reserved.
00005  
00006 Redistribution and use in source and binary forms, with or without
00007 modification, are permitted provided that the following conditions are met:
00008  
00009    - Redistributions of source code must retain the above copyright notice,
00010        this list of conditions and the following disclaimer.
00011    - Redistributions in binary form must reproduce the above copyright
00012        notice, this list of conditions and the following disclaimer in the
00013        documentation and/or other materials provided with the distribution.
00014    - Neither the name of Cornell University nor the names of its
00015        contributors may be used to endorse or promote products derived from
00016        this software without specific prior written permission.
00017  
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00028 THE POSSIBILITY OF SUCH DAMAGE.
00029  
00030 */
00031 
00032 
00033 /////////////////////////////////////////////////////////////////////
00034 ///
00035 /// Bitmap8.cpp
00036 ///
00037 /////////////////////////////////////////////////////////////////////
00038 
00039 #include <stdio.h>
00040 #include "Bitmap8.h"
00041 
00042 const int Bitmap8::_lookupTableSize = 0x10000;
00043 
00044 int Bitmap8::_countOr = 0;
00045 int Bitmap8::_countAnd = 0;
00046 int Bitmap8::_countCount = 0;
00047 int Bitmap8::_countCreateSBitmap = 0;
00048 int Bitmap8::_countCreateCBitmap = 0;
00049 int* Bitmap8::_countLookupTable = 0;
00050 int* Bitmap8::_sBitmapLookupTable = 0;
00051 int* Bitmap8::_cBitmapLookupTable = 0;
00052 
00053 #define DEBUG_BITMAP8COUNT
00054 
00055 /////////////////////////////////////////////////////////////////////
00056 /// Initialize the counting tables
00057 /////////////////////////////////////////////////////////////////////
00058 void Bitmap8::Init()
00059 {
00060     int i, s;
00061     int i1, i2, a1, a2;
00062 
00063     // ------ initialize _countLookupTable -----------------------------
00064     _countLookupTable = new int[_lookupTableSize];
00065     memset(_countLookupTable, 0, SIZE_INT*_lookupTableSize);
00066     for (i = 1; i < _lookupTableSize; i++)
00067     {
00068         if (i & 0x00ff)
00069             _countLookupTable[i]++;
00070         if (i & 0xff00)
00071             _countLookupTable[i]++;
00072         assert(1 == _countLookupTable[i] || 2 == _countLookupTable[i]);
00073     }
00074 
00075 
00076 
00077     // ------ initialize _sBitmapLookupTable -----------------------------
00078     // note: for a customer (4 bits), set the first bit
00079     // (after the first bit with a one) to one
00080     // recall that a SHORT is 16 bits, so we need to
00081     // change bits for 4 customers
00082 
00083     int Bit8SBitmapLookUp[256];
00084     Bit8SBitmapLookUp[0] = 0;
00085     Bit8SBitmapLookUp[1] = 0;
00086 
00087     int curValue = 0;
00088     int curIndex = 1;
00089     for (i = 2; i < 256; i++)
00090     {
00091         if (i % curIndex == 0)
00092         {
00093             curValue = curValue + curIndex;
00094             curIndex *= 2;
00095         }
00096         Bit8SBitmapLookUp[i] = curValue;
00097     }
00098 
00099 
00100     _sBitmapLookupTable = new int[_lookupTableSize];
00101     memset(_sBitmapLookupTable, 0, SIZE_INT*_lookupTableSize);
00102 
00103     s = 0;  // index into the sBitmapLookupTable
00104     for (i1 = 0; i1 < 256; i1++)
00105     {
00106         // first customer
00107         a1 = Bit8SBitmapLookUp[i1] << 8;
00108 
00109         for (i2 = 0; i2 < 256; i2++)
00110         {
00111             // second customer
00112             a2 = Bit8SBitmapLookUp[i2];
00113 
00114             // now actually set the sBitmapLookupTable value
00115             _sBitmapLookupTable[s] = a1 | a2;
00116             s++;
00117 
00118         } // for i2
00119     } // for i1
00120 
00121 
00122     // ------ initialize cBitmapLookupTable -----------------------------
00123 
00124     int Bit8CBitmapLookUp[256];
00125     Bit8CBitmapLookUp[0] = 0;
00126     Bit8CBitmapLookUp[1] = 1;
00127 
00128     curValue = 1;
00129     curIndex = 1;
00130     for (i = 2; i < 256; i++)
00131     {
00132         if (i % curIndex == 0)
00133         {
00134             curIndex *= 2;
00135             curValue = curValue + curIndex;
00136         }
00137         Bit8CBitmapLookUp[i] = curValue;
00138     }
00139 
00140     _cBitmapLookupTable = new int[_lookupTableSize];
00141     memset(_cBitmapLookupTable, 0, SIZE_INT*_lookupTableSize);
00142 
00143 
00144     s = 0;  // index into the sBitmapLookupTable
00145     // these are 16 bits: 4 customers
00146     for (i1 = 0; i1 < 256; i1++)
00147     {
00148         // first customer
00149         a1 = Bit8CBitmapLookUp[i1] << 8;
00150 
00151         for (i2 = 0; i2 < 256; i2++)
00152         {
00153             // second customer
00154             a2 = Bit8CBitmapLookUp[i2];
00155 
00156             // now actually set the sBitmapLookupTable value
00157             _cBitmapLookupTable[s] = a1 | a2;
00158             s++;
00159 
00160         } // for i2
00161     } // for i1
00162 
00163 
00164 }
00165 
00166 
00167 /////////////////////////////////////////////////////////////////////
00168 /// deallocate counting tables
00169 /////////////////////////////////////////////////////////////////////
00170 void Bitmap8::Destroy()
00171 {
00172     if (_countLookupTable != 0)
00173         delete [] _countLookupTable;
00174 
00175     if (_sBitmapLookupTable != 0)
00176         delete [] _sBitmapLookupTable;
00177 
00178     if (_cBitmapLookupTable != 0)
00179         delete [] _cBitmapLookupTable;
00180 }
00181 
00182 /////////////////////////////////////////////////////////////////////
00183 /// Bitwise OR 2 bitmap8s and store the result
00184 ///
00185 /// @param b1                the first Bitmap8
00186 /// @param b2                the second Bitmap8
00187 /////////////////////////////////////////////////////////////////////
00188 void Bitmap8::Or(const Bitmap8 &b1, const Bitmap8 &b2)
00189 {
00190 #ifdef DEBUG_BITMAP8COUNT
00191     _countOr++;
00192 #endif
00193 
00194     // OR each int bitwise
00195     for (int i = 0; i < b1._memSizeInt; i++)
00196         _memory[i] = b1._memory[i] | b2._memory[i];
00197 }
00198 
00199 
00200 /////////////////////////////////////////////////////////////////////
00201 /// Bitwise AND 2 bitmap8s and store the result
00202 ///
00203 /// @param b1                the first Bitmap8
00204 /// @param b2                the second Bitmap8
00205 /////////////////////////////////////////////////////////////////////
00206 void Bitmap8::And(const Bitmap8 &b1, const Bitmap8 &b2)
00207 {
00208 
00209 #ifdef DEBUG_BITMAP8COUNT
00210     _countAnd++;
00211 #endif
00212 
00213     // OR each int bitwise
00214     for (int i = 0; i < b1._memSizeInt; i++)
00215         _memory[i] = b1._memory[i] & b2._memory[i];
00216 }
00217 
00218 
00219 /////////////////////////////////////////////////////////////////////
00220 /// find the support of this bitmap in *number of customers*
00221 ///
00222 /// @return the number of customers that have some bit set among their 8 bits
00223 /////////////////////////////////////////////////////////////////////
00224 int Bitmap8::Count()
00225 {
00226 
00227 #ifdef DEBUG_BITMAP8COUNT
00228     _countCount++;
00229 #endif
00230 
00231 
00232     unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
00233     int support = 0;
00234 
00235     // we count the support
00236     // go thru the whole bitmap in steps of SHORT (but note that each
00237     // customer uses 4 bits)
00238     for (int i = 0; i < _memSizeShort; i++)
00239         support += _countLookupTable[ms[i]];
00240 
00241     return support;
00242 }
00243 
00244 /////////////////////////////////////////////////////////////////////
00245 /// Create a s-bitmap from an i-bitmap
00246 /// <p>
00247 /// Idea  : Again, we go thru each element of _memory. For each element,
00248 ///     if it is greater than 0, we look up the transformation table
00249 ///     (postProcessTable) to find the corresponding value for the s-bitmap,
00250 ///     and set the remaining SHORTs of the current custom to USHRT_MAX
00251 ///     (i.e. all 1's).
00252 /// <p>
00253 /// Note  : For example, if the bitmap is
00254 ///     [0001 | 1100 | 0011 | 1111 | 0000 | 0000] and
00255 ///     [00001111 | 00011111 | 11111111]. Refer to the paper for details.
00256 ///
00257 /// @param iBitmap           the bitmap8 from which we create s-bitmap
00258 /////////////////////////////////////////////////////////////////////
00259 void Bitmap8::CreateSBitmap(const Bitmap8 &iBitmap)
00260 {
00261 
00262 #ifdef DEBUG_BITMAP8COUNT
00263     _countCreateSBitmap++;
00264 #endif
00265 
00266     assert(_memory);
00267     assert(_memSizeInt == iBitmap._memSizeInt);
00268 
00269     // we walk the memory in terms of chars
00270     // msib: pointer to memory of ibitmap in terms of char
00271     // ms: pointer to memory as a pointer to char
00272     // ss: size of the memory in char
00273     unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
00274     const unsigned short* msib =
00275         reinterpret_cast<const unsigned short*> (iBitmap._memory);
00276 
00277     // go thru each SHORT (note that a SHORT is 4 customers)
00278     for (int i = 0; i < _memSizeShort; i++)
00279         ms[i] = _sBitmapLookupTable[msib[i]];
00280 }
00281 
00282 /////////////////////////////////////////////////////////////////////
00283 /// create a c-bitmap for compression
00284 ///
00285 /// @param iBitmap           the bitmap8 from which we create c-bitmap
00286 /////////////////////////////////////////////////////////////////////
00287 void Bitmap8::CreateCBitmap(const Bitmap8 &iBitmap)
00288 {
00289 
00290 #ifdef DEBUG_BITMAP8COUNT
00291     _countCreateCBitmap++;
00292 #endif
00293 
00294     assert(_memory);
00295     assert(_memSizeInt == iBitmap._memSizeInt);
00296 
00297     // we walk the memory in terms of chars
00298     // msib: pointer to memory of ibitmap in terms of char
00299     // ms: pointer to memory as a pointer to char
00300     // ss: size of the memory in char
00301     unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
00302     const unsigned short* msib =
00303         reinterpret_cast<const unsigned short*> (iBitmap._memory);
00304 
00305     // go thru each SHORT (note that a SHORT is 4 customers)
00306     for (int i = 0; i < _memSizeShort; i++)
00307         ms[i] = _cBitmapLookupTable[msib[i]];
00308 }
00309 

Generated on Thu Mar 11 12:01:51 2004 for SPAM by doxygen 1.3.4