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

Bitmap4.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 /// Bitmap4.cpp
00036 ///
00037 /////////////////////////////////////////////////////////////////////
00038 
00039 #include <iostream>
00040 #include "Bitmap4.h"
00041 
00042 const int Bitmap4::_lookupTableSize = 0x10000;
00043 
00044 int Bitmap4::_countOr = 0;
00045 int Bitmap4::_countAnd = 0;
00046 int Bitmap4::_countCount = 0;
00047 int Bitmap4::_countCreateSBitmap = 0;
00048 int Bitmap4::_countCreateCBitmap = 0;
00049 int* Bitmap4::_countLookupTable = 0;
00050 int* Bitmap4::_sBitmapLookupTable = 0;
00051 int* Bitmap4::_cBitmapLookupTable = 0;
00052 
00053 #define DEBUG_BITMAP4COUNT
00054 
00055 /////////////////////////////////////////////////////////////////////
00056 /// Initialize the counting tables
00057 /////////////////////////////////////////////////////////////////////
00058 void Bitmap4::Init()
00059 {
00060     int i, s;
00061     int i1, i2, i3, i4, a1, a2, a3, a4;
00062 
00063     // ------ initialize _countLookupTable -----------------------------
00064     _countLookupTable = new int[_lookupTableSize];
00065     memset(_countLookupTable, 0, SIZE_INT*_lookupTableSize);
00066 
00067     for (i = 1; i < _lookupTableSize; i++)
00068     {
00069         if (i & 0x000f)
00070             _countLookupTable[i]++;
00071         if (i & 0x00f0)
00072             _countLookupTable[i]++;
00073         if (i & 0x0f00)
00074             _countLookupTable[i]++;
00075         if (i & 0xf000)
00076             _countLookupTable[i]++;
00077 
00078         assert(1 == _countLookupTable[i] || 2 == _countLookupTable[i] ||
00079                3 == _countLookupTable[i] || 4 == _countLookupTable[i]);
00080     }
00081 
00082 
00083     // ------ initialize _sBitmapLookupTable -----------------------------
00084     // note: for a customer (4 bits), set the first bit
00085     // (after the first bit with a one) to one
00086     // recall that a SHORT is 16 bits, so we need to change
00087     // bits for 4 customers
00088 
00089     int Bit4SBitmapLookUp[16];
00090     Bit4SBitmapLookUp[0] = 0;
00091     Bit4SBitmapLookUp[1] = 0;
00092 
00093     int curValue = 0;
00094     int curIndex = 1;
00095     for (i = 2; i < 16; i++)
00096     {
00097         if (i % curIndex == 0)
00098         {
00099             curValue = curValue + curIndex;
00100             curIndex *= 2;
00101         }
00102         Bit4SBitmapLookUp[i] = curValue;
00103     }
00104 
00105 
00106     _sBitmapLookupTable = new int[_lookupTableSize];
00107     memset(_sBitmapLookupTable, 0, SIZE_INT*_lookupTableSize);
00108 
00109 
00110     s = 0;  // index into the sBitmapLookupTable
00111 
00112     for (i1 = 0; i1 < 16; i1++)
00113     {
00114         // first customer
00115         a1 = Bit4SBitmapLookUp[i1] << 12;
00116 
00117         for (i2 = 0; i2 < 16; i2++)
00118         {
00119             // second customer
00120             a2 = Bit4SBitmapLookUp[i2] << 8;
00121 
00122             for (i3 = 0; i3 < 16; i3++)
00123             {
00124                 // third customer
00125                 a3 = Bit4SBitmapLookUp[i3] << 4;
00126 
00127                 for (i4 = 0;i4 < 16;i4++)
00128                 {
00129                     // fourth customer
00130                     a4 = Bit4SBitmapLookUp[i4];
00131 
00132                     // now actually set the sBitmapLookupTable value
00133                     _sBitmapLookupTable[s] = a1 | a2 | a3 | a4;
00134                     s++;
00135 
00136                 } // for i4
00137             } // for i3
00138         } // for i2
00139     } // for i1
00140 
00141 
00142 
00143     // ------ initialize _cBitmapLookupTable -----------------------------
00144     int Bit4CBitmapLookUp[16] =
00145         {0, 1, 3, 3, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15, 15, 15};
00146 
00147     _cBitmapLookupTable = new int[_lookupTableSize];
00148     memset(_cBitmapLookupTable, 0, SIZE_INT*_lookupTableSize);
00149 
00150     s = 0;  // index into the cBitmapLookupTable
00151 
00152     for (i1 = 0; i1 < 16; i1++)
00153     {
00154         // first customer
00155         a1 = Bit4CBitmapLookUp[i1] << 12;
00156 
00157         for (i2 = 0; i2 < 16; i2++)
00158         {
00159             // second customer
00160             a2 = Bit4CBitmapLookUp[i2] << 8;
00161 
00162             for (i3 = 0; i3 < 16; i3++)
00163             {
00164                 // third customer
00165                 a3 = Bit4CBitmapLookUp[i3] << 4;
00166 
00167                 for (i4 = 0;i4 < 16;i4++)
00168                 {
00169                     // fourth customer
00170                     a4 = Bit4CBitmapLookUp[i4];
00171 
00172                     // now actually set the sBitmapLookupTable value
00173                     _cBitmapLookupTable[s] = a1 | a2 | a3 | a4;
00174                     s++;
00175 
00176                 } // for i4
00177             } // for i3
00178         } // for i2
00179     } // for i1
00180 
00181 }
00182 
00183 /////////////////////////////////////////////////////////////////////
00184 /// deallocate counting tables
00185 /////////////////////////////////////////////////////////////////////
00186 void Bitmap4::Destroy()
00187 {
00188     if (_countLookupTable != 0)
00189         delete [] _countLookupTable;
00190 
00191     if (_sBitmapLookupTable != 0)
00192         delete [] _sBitmapLookupTable;
00193 
00194     if (_cBitmapLookupTable != 0)
00195         delete [] _cBitmapLookupTable;
00196 }
00197 
00198 /////////////////////////////////////////////////////////////////////
00199 /// Bitwise OR 2 bitmap4s and store the result
00200 ///
00201 /// @param b1                the first Bitmap4
00202 /// @param b2                the second Bitmap4
00203 /////////////////////////////////////////////////////////////////////
00204 void Bitmap4::Or(const Bitmap4 &b1, const Bitmap4 &b2)
00205 {
00206 
00207 #ifdef DEBUG_BITMAP4COUNT
00208     _countOr++;
00209 #endif
00210 
00211     // OR each int bitwise
00212     for (int i = 0; i < b1._memSizeInt; i++)
00213         _memory[i] = b1._memory[i] | b2._memory[i];
00214 }
00215 
00216 
00217 /////////////////////////////////////////////////////////////////////
00218 /// Bitwise AND 2 bitmap4s and store the result
00219 ///
00220 /// @param b1                the first Bitmap4
00221 /// @param b2                the second Bitmap4
00222 /////////////////////////////////////////////////////////////////////
00223 void Bitmap4::And(const Bitmap4 &b1, const Bitmap4 &b2)
00224 {
00225 #ifdef DEBUG_BITMAP4COUNT
00226     _countAnd++;
00227 #endif
00228 
00229     // AND each int bitwise
00230     for (int i = 0; i < b1._memSizeInt; i++)
00231         _memory[i] = b1._memory[i] & b2._memory[i];
00232 
00233 }
00234 
00235 
00236 /////////////////////////////////////////////////////////////////////
00237 /// find the support of this bitmap4 in *number of customers*
00238 ///
00239 /// @return the number of customers that have some bit set among their 4 bits
00240 /////////////////////////////////////////////////////////////////////
00241 int Bitmap4::Count()
00242 {
00243 
00244 #ifdef DEBUG_BITMAP4COUNT
00245     _countCount++;
00246 #endif
00247 
00248     // we walk the memory in terms of shorts
00249     // ms: pointer to memory as a pointer to shorts
00250     // ss: size of the memory in short
00251     unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
00252     int support = 0;
00253 
00254     // we count the support
00255     // go thru the whole bitmap in steps of SHORT (but note that each
00256     // customer uses 4 bits)
00257     for (int i = 0; i < _memSizeShort; i++)
00258         support += _countLookupTable[ms[i]];
00259 
00260     return support;
00261 }
00262 
00263 
00264 /////////////////////////////////////////////////////////////////////
00265 /// Create a s-bitmap from an i-bitmap
00266 /// <p>
00267 /// Idea  : Again, we go thru each element of _memory. For each element,
00268 ///     if it is greater than 0, we look up the transformation table
00269 ///     (postProcessTable) to find the corresponding value for the s-bitmap,
00270 ///     and set the remaining SHORTs of the current custom to USHRT_MAX
00271 ///     (i.e. all 1's).
00272 /// <p>
00273 /// Note  : For example, if the bitmap is
00274 ///     [0001 | 1100 | 0011 | 1111 | 0000 | 0000] and
00275 ///     [00001111 | 00011111 | 11111111]. Refer to the paper for details.
00276 ///
00277 /// @param iBitmap           the bitmap4 from which we create s-bitmap
00278 /////////////////////////////////////////////////////////////////////
00279 void Bitmap4::CreateSBitmap(const Bitmap4 &iBitmap)
00280 {
00281 
00282 #ifdef DEBUG_BITMAP4COUNT
00283     _countCreateSBitmap++;
00284 #endif
00285 
00286     assert(_memory);
00287     assert(_memSizeInt == iBitmap._memSizeInt);
00288 
00289     // we walk the memory in terms of shorts
00290     // msib: pointer to memory of ibitmap in terms of short
00291     // ms: pointer to memory as a pointer to shorts
00292     // ss: size of the memory in short
00293 
00294     unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
00295     const unsigned short* msib =
00296         reinterpret_cast<const unsigned short*> (iBitmap._memory);
00297 
00298     // go thru each SHORT (note that a SHORT is 4 customers)
00299     for (int i = 0; i < _memSizeShort; i++)
00300         ms[i] = _sBitmapLookupTable[msib[i]];
00301 }
00302 
00303 /////////////////////////////////////////////////////////////////////
00304 /// create a c-bitmap for compression
00305 ///
00306 /// @param iBitmap           the bitmap4 from which we create c-bitmap
00307 /////////////////////////////////////////////////////////////////////
00308 void Bitmap4::CreateCBitmap(const Bitmap4 &iBitmap)
00309 {
00310     assert(_memory);
00311     assert(_memSizeInt == iBitmap._memSizeInt);
00312 
00313 #ifdef DEBUG_BITMAP4COUNT
00314 
00315     _countCreateCBitmap++;
00316 #endif
00317 
00318     // we walk the memory in terms of shorts
00319     // msib: pointer to memory of ibitmap in terms of short
00320     // ms: pointer to memory as a pointer to shorts
00321     // ss: size of the memory in short
00322 
00323     unsigned short* ms = reinterpret_cast<unsigned short*>(_memory);
00324     const unsigned short* msib =
00325         reinterpret_cast<const unsigned short*> (iBitmap._memory);
00326 
00327     // go thru each SHORT (note that a SHORT is 4 customers)
00328     for (int i = 0; i < _memSizeShort; i++)
00329         ms[i] = _cBitmapLookupTable[msib[i]];
00330 }
00331 

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