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 /// Bitmap16.cpp 00036 /// 00037 ///////////////////////////////////////////////////////////////////// 00038 00039 #include <stdio.h> 00040 #include "Bitmap16.h" 00041 00042 const int Bitmap16::_lookupTableSize = 0x10000; 00043 00044 int Bitmap16::_countOr = 0; 00045 int Bitmap16::_countAnd = 0; 00046 int Bitmap16::_countCount = 0; 00047 int Bitmap16::_countCreateSBitmap = 0; 00048 int Bitmap16::_countCreateCBitmap = 0; 00049 int* Bitmap16::_sBitmapLookupTable = 0; 00050 int* Bitmap16::_cBitmapLookupTable = 0; 00051 00052 #define DEBUG_BITMAP16COUNT 00053 00054 ///////////////////////////////////////////////////////////////////// 00055 /// Initialize the counting tables 00056 ///////////////////////////////////////////////////////////////////// 00057 void Bitmap16::Init() 00058 { 00059 00060 int i; 00061 00062 // ------ initialize sBitmapLookupTable ----------------------------- 00063 _sBitmapLookupTable = new int[_lookupTableSize]; 00064 memset(_sBitmapLookupTable, 0, SIZE_UINT*_lookupTableSize); 00065 00066 _sBitmapLookupTable[0] = 0; 00067 _sBitmapLookupTable[1] = 0; 00068 00069 int curValue = 0; 00070 int curIndex = 1; 00071 for (i = 2; i < _lookupTableSize; i++) 00072 { 00073 if (i % curIndex == 0) 00074 { 00075 curValue = curValue + curIndex; 00076 curIndex *= 2; 00077 } 00078 _sBitmapLookupTable[i] = curValue; 00079 } 00080 00081 00082 // ------ initialize sBitmapLookupTable ----------------------------- 00083 _cBitmapLookupTable = new int[_lookupTableSize]; 00084 memset(_cBitmapLookupTable, 0, SIZE_UINT*_lookupTableSize); 00085 00086 _cBitmapLookupTable[0] = 0; 00087 _cBitmapLookupTable[1] = 1; 00088 00089 curValue = 1; 00090 curIndex = 1; 00091 for (i = 2; i < _lookupTableSize; i++) 00092 { 00093 if (i % curIndex == 0) 00094 { 00095 curIndex *= 2; 00096 curValue = curValue + curIndex; 00097 } 00098 _cBitmapLookupTable[i] = curValue; 00099 } 00100 } 00101 00102 ///////////////////////////////////////////////////////////////////// 00103 /// deallocate counting tables 00104 ///////////////////////////////////////////////////////////////////// 00105 void Bitmap16::Destroy() 00106 { 00107 if (_sBitmapLookupTable != 0) 00108 delete [] _sBitmapLookupTable; 00109 00110 if (_cBitmapLookupTable != 0) 00111 delete [] _cBitmapLookupTable; 00112 } 00113 00114 ///////////////////////////////////////////////////////////////////// 00115 /// Bitwise OR 2 bitmaps and store the result 00116 /// 00117 /// @param b1 the first Bitmap16 00118 /// @param b2 the second Bitmap16 00119 ///////////////////////////////////////////////////////////////////// 00120 void Bitmap16::Or(const Bitmap16 &b1, const Bitmap16 &b2) 00121 { 00122 00123 #ifdef DEBUG_BITMAP16COUNT 00124 _countOr++; 00125 #endif 00126 00127 unsigned int* const b1ptr = 00128 reinterpret_cast<unsigned int * const>(b1._memory); 00129 unsigned int* const b2ptr = 00130 reinterpret_cast<unsigned int * const>(b2._memory); 00131 unsigned int* memory = 00132 reinterpret_cast<unsigned int *>(_memory); 00133 00134 // AND each int bitwise 00135 for (int i = 0; i < b1._memSizeInt; i++) 00136 memory[i] = b1ptr[i] | b2ptr[i]; 00137 } 00138 00139 00140 ///////////////////////////////////////////////////////////////////// 00141 /// Bitwise AND 2 bitmaps and store the result 00142 /// 00143 /// @param b1 the first Bitmap16 00144 /// @param b2 the second Bitmap16 00145 ///////////////////////////////////////////////////////////////////// 00146 void Bitmap16::And(const Bitmap16 &b1, const Bitmap16 &b2) 00147 { 00148 #ifdef DEBUG_BITMAP16COUNT 00149 _countAnd++; 00150 #endif 00151 00152 // AND each short bitwise 00153 unsigned int* const b1ptr = 00154 reinterpret_cast<unsigned int * const>(b1._memory); 00155 unsigned int* const b2ptr = 00156 reinterpret_cast<unsigned int * const>(b2._memory); 00157 unsigned int* memory = reinterpret_cast<unsigned int *>(_memory); 00158 00159 // AND each int bitwise 00160 for (int i = 0; i < b1._memSizeInt; i++) 00161 memory[i] = b1ptr[i] & b2ptr[i]; 00162 } 00163 00164 00165 ///////////////////////////////////////////////////////////////////// 00166 /// find the support of this bitmap in *number of customers* 00167 /// 00168 /// @return the number of customers that have some bit set among their 16 bits 00169 ///////////////////////////////////////////////////////////////////// 00170 int Bitmap16::Count() 00171 { 00172 00173 #ifdef DEBUG_BITMAP16COUNT 00174 _countCount++; 00175 #endif 00176 00177 int support = 0; 00178 00179 // we count the support 00180 // go thru the whole bitmap in steps of SHORT 00181 for (int i = 0; i < _memSizeShort; i++) 00182 if (_memory[i] > 0) 00183 support++; 00184 00185 return support; 00186 } 00187 00188 ///////////////////////////////////////////////////////////////////// 00189 /// Create a s-bitmap from an i-bitmap 00190 /// <p> 00191 /// Idea : Again, we go thru each element of _memory. For each element, 00192 /// if it is greater than 0, we look up the transformation table 00193 /// (postProcessTable) to find the corresponding value for the s-bitmap, 00194 /// and set the remaining SHORTs of the current custom to USHRT_MAX 00195 /// (i.e. all 1's). 00196 /// <p> 00197 /// Note : For example, if the bitmap is 00198 /// [0001 | 1100 | 0011 | 1111 | 0000 | 0000] and 00199 /// [00001111 | 00011111 | 11111111]. Refer to the paper for details. 00200 /// 00201 /// @param iBitmap the bitmap from which we create s-bitmap 00202 ///////////////////////////////////////////////////////////////////// 00203 void Bitmap16::CreateSBitmap(const Bitmap16 &iBitmap) 00204 { 00205 00206 #ifdef DEBUG_BITMAP16COUNT 00207 _countCreateSBitmap++; 00208 #endif 00209 00210 assert(_memory); 00211 assert(_memSizeShort == iBitmap._memSizeShort); 00212 00213 // go thru each SHORT 00214 for (int i = 0; i < _memSizeShort; i++) 00215 _memory[i] = _sBitmapLookupTable[iBitmap._memory[i]]; 00216 00217 } 00218 00219 ///////////////////////////////////////////////////////////////////// 00220 /// create a c-bitmap for compression 00221 /// 00222 /// @param iBitmap the bitmap16 from which we create c-bitmap 00223 ///////////////////////////////////////////////////////////////////// 00224 void Bitmap16::CreateCBitmap(const Bitmap16 &iBitmap) 00225 { 00226 00227 #ifdef DEBUG_BITMAP16COUNT 00228 _countCreateCBitmap++; 00229 #endif 00230 00231 assert(_memory); 00232 assert(_memSizeShort == iBitmap._memSizeShort); 00233 00234 // go thru each SHORT 00235 for (int i = 0; i < _memSizeShort; i++) 00236 _memory[i] = _cBitmapLookupTable[iBitmap._memory[i]]; 00237 00238 } 00239