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.h 00036 /// 00037 ///////////////////////////////////////////////////////////////////// 00038 #ifndef __Bitmap8_H__ 00039 #define __Bitmap8_H__ 00040 00041 #include <assert.h> 00042 #include <memory.h> 00043 #include "Tables.h" 00044 00045 ///////////////////////////////////////////////////////////////////// 00046 /// @addtogroup BitmapProcessingGroup Bitmap Processing 00047 /** @{ */ 00048 00049 ///////////////////////////////////////////////////////////////////// 00050 /// A vertical representation of the data. It handles both uncompressed 00051 /// and compressed data. 00052 /// <p> 00053 /// This bitmap can represent customers with up to 8 transactions since it 00054 /// allocates exactly 8 bits per customer 00055 ///////////////////////////////////////////////////////////////////// 00056 class Bitmap8 00057 { 00058 00059 friend class SeqBitmap; 00060 00061 public: 00062 00063 static void Init(); 00064 static void Destroy(); 00065 00066 static int CalcSize(int numCust) 00067 { 00068 // calculate the total number of bits 00069 int n = (8 * numCust); 00070 00071 // round the size in bits up to a multiple of BITS_PER_SHORT 00072 int k = n % BITS_PER_INT; 00073 if (k) 00074 { 00075 n += BITS_PER_INT - k; 00076 } 00077 00078 return (n / BITS_PER_INT) * SIZE_UINT; 00079 } 00080 00081 ///////////////////////////////////////////////////////////////////// 00082 /// Allocate the memory for Bitmap8 for this many customers 00083 /// 00084 /// @param numCustomers # of customers 00085 /// @param memory 00086 ///////////////////////////////////////////////////////////////////// 00087 Bitmap8(int numCustomers, unsigned int *&memory) 00088 { 00089 // calculate the total number of bits 00090 int n = (8 * numCustomers); 00091 00092 // round the size in bits up to a multiple of BITS_PER_SHORT 00093 int k = n % BITS_PER_INT; 00094 if (k) 00095 { 00096 n += BITS_PER_INT - k; 00097 } 00098 00099 assert(0 == (n % BITS_PER_INT)); 00100 00101 // translate this to a number of shorts 00102 _numCust = numCustomers; 00103 _memSizeInt = n / BITS_PER_INT; 00104 _memSizeShort = _memSizeInt * SHORT_PER_INT; 00105 //_memory = new unsigned int[_memSizeInt]; 00106 _memory = memory; 00107 00108 memory += _memSizeInt * SIZE_UINT; 00109 00110 memset(_memory, 0, _memSizeInt * SIZE_UINT); 00111 }; 00112 00113 ///////////////////////////////////////////////////////////////////// 00114 /// Copy constructor 00115 /// 00116 /// @param b Bitmap8 to copy 00117 /// @param memory 00118 ///////////////////////////////////////////////////////////////////// 00119 Bitmap8(Bitmap8 &b, unsigned int *&memory): _memSizeInt(b._memSizeInt) 00120 { 00121 // copy information from the source bitmap 00122 _numCust = b._numCust; 00123 _memSizeShort = b._memSizeShort; 00124 //_memory = new unsigned int[_memSizeInt]; 00125 _memory = memory; 00126 00127 // Update the global stack pointer 00128 memory += _memSizeInt * SIZE_UINT; 00129 memcpy(_memory, b._memory, _memSizeInt * SIZE_UINT); 00130 }; 00131 00132 ///////////////////////////////////////////////////////////////////// 00133 /// Destructor (does nothing) 00134 ///////////////////////////////////////////////////////////////////// 00135 ~Bitmap8() 00136 { 00137 //if (_memory) 00138 // delete [] _memory; 00139 }; 00140 00141 ///////////////////////////////////////////////////////////////////// 00142 /// Pop this bitmap's memory from the global stack (allocation is done in 00143 /// the constructor, but deallocation is not done in the destructor 00144 ///////////////////////////////////////////////////////////////////// 00145 void Deallocate(unsigned int *&memory) 00146 { 00147 memory -= (_memSizeInt * SIZE_UINT); 00148 }; 00149 00150 ///////////////////////////////////////////////////////////////////// 00151 /// Fill in a 1 in a certain position 00152 /// 00153 /// @param j bit position in Bitmap to be changed 00154 ///////////////////////////////////////////////////////////////////// 00155 void FillEmptyPosition(int j) 00156 { 00157 // Locate correct int in the Bitmap 00158 int i = j / BITS_PER_INT; 00159 00160 // switch on correct bit 00161 _memory[i] = _memory[i] | Bit32Table[(j % BITS_PER_INT)]; 00162 }; 00163 00164 00165 // --- anding and oring two bitmaps 00166 void Or(const Bitmap8 &b1, const Bitmap8 &b2); 00167 void And(const Bitmap8 &b1, const Bitmap8 &b2); 00168 00169 // count the number of customers 00170 int Count(); 00171 void CreateSBitmap(const Bitmap8 &iBitmap); 00172 void CreateCBitmap(const Bitmap8 &iBitmap); 00173 00174 int getIntSize() const 00175 { 00176 return _memSizeInt; 00177 } 00178 00179 // ----------------------- static variables ---------------------- 00180 // counters for performance measurements 00181 static int _countOr; 00182 static int _countAnd; 00183 static int _countCount; 00184 static int _countCreateSBitmap; 00185 static int _countCreateCBitmap; 00186 00187 /// lookup table for counting 00188 static const int _lookupTableSize; 00189 00190 /// lookup table for the counting 00191 static int* _countLookupTable; 00192 00193 /// lookup table for creating a s-bitmap from an i-bitmap 00194 static int* _sBitmapLookupTable; 00195 00196 /// lookup table for creating a c-bitmap for compression 00197 static int* _cBitmapLookupTable; 00198 00199 00200 private: 00201 // --- private variables 00202 unsigned int* _memory; ///< where data is stored 00203 int _memSizeInt; ///< size of memory allocated in sizeof(int) 00204 int _memSizeShort; ///< size of memory allocated in sizeof(short) 00205 int _numCust; ///< number of customers 00206 } 00207 ; // class Bitmap8 00208 00209 /** @} */ 00210 #endif