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