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.h 00036 /// 00037 ///////////////////////////////////////////////////////////////////// 00038 #ifndef __Bitmap16_H__ 00039 #define __Bitmap16_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 16 transactions since it 00054 /// allocates exactly 16 bits per customer 00055 ///////////////////////////////////////////////////////////////////// 00056 class Bitmap16 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 if (numCust % 2 == 0) 00068 { 00069 return (numCust / 2) * SIZE_USHORT; 00070 } 00071 else 00072 return (numCust / 2 + 1) * SIZE_USHORT; 00073 } 00074 00075 ///////////////////////////////////////////////////////////////////// 00076 /// Allocate the memory for Bitmap16 for this many customers 00077 /// 00078 /// @param numCustomers # of customers 00079 /// @param memory 00080 ///////////////////////////////////////////////////////////////////// 00081 Bitmap16(int numCustomers, unsigned short*& memory) 00082 { 00083 _numCust = numCustomers; 00084 00085 if (_numCust % 2 == 0) 00086 _memSizeInt = _numCust / 2; 00087 else 00088 _memSizeInt = _numCust / 2 + 1; 00089 00090 _memSizeShort = _memSizeInt * SHORT_PER_INT; 00091 //_memory = new unsigned short[_memSizeShort]; 00092 _memory = memory; 00093 memory += _memSizeShort * SIZE_USHORT; 00094 memset(_memory, 0, _memSizeShort * SIZE_USHORT); 00095 }; 00096 00097 ///////////////////////////////////////////////////////////////////// 00098 /// Copy constructor 00099 /// 00100 /// @param b Bitmap to copy 00101 /// @param memory 00102 ///////////////////////////////////////////////////////////////////// 00103 Bitmap16(Bitmap16 &b, unsigned short *&memory) 00104 { 00105 // copy information from the source bitmap 00106 _memSizeShort = b._memSizeShort; 00107 _memSizeInt = b._memSizeInt; 00108 _numCust = b._numCust; 00109 //_memory = new unsigned short[b._memSizeShort]; 00110 _memory = memory; 00111 memory += _memSizeShort * SIZE_USHORT; 00112 memcpy(_memory, b._memory, _memSizeShort * SIZE_USHORT); 00113 }; 00114 00115 ///////////////////////////////////////////////////////////////////// 00116 /// Destructor (does nothing) 00117 ///////////////////////////////////////////////////////////////////// 00118 ~Bitmap16() 00119 { 00120 //if (_memory) 00121 // delete [] _memory; 00122 }; 00123 00124 ///////////////////////////////////////////////////////////////////// 00125 /// Pop this bitmap's memory from the global stack (allocation is done in 00126 /// the constructor, but deallocation is not done in the destructor 00127 ///////////////////////////////////////////////////////////////////// 00128 void Deallocate(unsigned short *&memory) 00129 { 00130 memory -= (_memSizeShort * SIZE_USHORT); 00131 }; 00132 00133 ///////////////////////////////////////////////////////////////////// 00134 /// Fill in a 1 in a certain position 00135 /// 00136 /// @param j bit position in Bitmap to be changed 00137 ///////////////////////////////////////////////////////////////////// 00138 void FillEmptyPosition(int j) 00139 { 00140 // Locate correct short in the bitmap 00141 int i = j / BITS_PER_SHORT; 00142 00143 // switch on correct bit 00144 _memory[i] = _memory[i] | Bit16Table[(j % BITS_PER_SHORT )]; 00145 }; 00146 00147 // --- anding and oring two bitmap16s 00148 void Or(const Bitmap16 &b1, const Bitmap16 &b2); 00149 void And(const Bitmap16 &b1, const Bitmap16 &b2); 00150 00151 // count the number of customers 00152 int Count(); 00153 void CreateSBitmap(const Bitmap16 &iBitmap); 00154 void CreateCBitmap(const Bitmap16 &iBitmap); 00155 00156 // ----------------------- static variables ---------------------- 00157 /// counters for performance measurements 00158 static int _countOr; 00159 static int _countAnd; 00160 static int _countCount; 00161 static int _countCreateSBitmap; 00162 static int _countCreateCBitmap; 00163 00164 /// lookup table for counting 00165 static const int _lookupTableSize; 00166 00167 /// lookup table for creating a s-bitmap from an i-bitmap 00168 static int* _sBitmapLookupTable; 00169 00170 /// lookup table for creating a c-bitmap for compression 00171 static int* _cBitmapLookupTable; 00172 00173 int getIntSize() const 00174 { 00175 return _memSizeInt; 00176 } 00177 00178 private: 00179 // --- private variables 00180 unsigned short* _memory; ///< where data is stored 00181 int _memSizeInt; ///< size of memory allocated in sizeof(int) 00182 int _memSizeShort; ///< size of memory allocated in sizeof(short) 00183 int _numCust; ///< number of customers 00184 00185 } 00186 ; // class Bitmap16 00187 00188 /** @} */ 00189 #endif