00001 /* 00002 00003 Copyright (c) 2003, 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 /// Transaction.cpp 00035 /// 00036 ///////////////////////////////////////////////////////////////////// 00037 00038 #include "Transaction.h" 00039 #include <vector> 00040 #include <algorithm> 00041 using namespace std; 00042 00043 ///////////////////////////////////////////////////////////////////// 00044 /// @addtogroup InputOutput 00045 /** @{ */ 00046 00047 ///////////////////////////////////////////////////////////////////// 00048 /// Get next transaction from the input file 00049 /// 00050 /// @return pointer to a new Transaction 00051 ///////////////////////////////////////////////////////////////////// 00052 Transaction *InputData::getNextTransaction() { 00053 char c; 00054 00055 int itemIndex = 0; 00056 if (isAsciiFile) { 00057 // read list of items 00058 do { 00059 int item = 0, pos = 0; 00060 inputFile.get(c); 00061 while (!inputFile.eof() && (c >= '0') && (c <= '9')) { 00062 item *= 10; 00063 item += int(c) - int('0'); 00064 inputFile.get(c); 00065 pos++; 00066 } 00067 00068 if (pos) { 00069 itembuffer[itemIndex] = item; 00070 itemIndex++; 00071 } 00072 } while (!inputFile.eof() && c != '\n'); 00073 00074 // if end of file is reached 00075 if (itemIndex == 0) 00076 return 0; 00077 } else { 00078 int custid; // customer id (NOT used currently) 00079 int transid; // transaction id 00080 int nitems = 0; // number of items in the transaction 00081 00082 if (!inputFile.eof()) { 00083 // read in the transaction 00084 inputFile.read((char *)&custid, sizeof(int)); 00085 inputFile.read((char *)&transid, sizeof(int)); 00086 inputFile.read((char *)&nitems, sizeof(int)); 00087 00088 // ensure that there are not too many items 00089 if (nitems >= MAX_NUM_ITEMS) { 00090 cout << "More than " << MAX_NUM_ITEMS 00091 << " items in customer id: " << custid 00092 << " transaction id: " << transid; 00093 exit(1); 00094 } 00095 00096 // Read in the items of the transaction 00097 inputFile.read((char *)itembuffer, nitems * sizeof(int)); 00098 } 00099 00100 itemIndex = nitems; 00101 if (itemIndex == 0) 00102 return 0; 00103 } 00104 00105 // Note, also last transaction must end with newline, 00106 // else, it will be ignored 00107 00108 // sort list of items (this is not necessary for the workshop test datasets) 00109 // sort(list.begin(),list.end()); 00110 00111 // put items in Transaction structure 00112 Transaction *newTransaction = new Transaction(itembuffer, itemIndex); 00113 return newTransaction; 00114 } 00115 00116 ///////////////////////////////////////////////////////////////////// 00117 /// Open the input file 00118 /// 00119 /// @param filename input filename 00120 /// @param ITEMBUFFER pointer to buffer for storing a transaction 00121 /// @param IS_ASCII true if file is in ASCII format 00122 ///////////////////////////////////////////////////////////////////// 00123 InputData::InputData(char *filename, int *ITEMBUFFER, bool IS_ASCII) { 00124 if (IS_ASCII) 00125 inputFile.open(filename); 00126 else 00127 inputFile.open(filename, ios::binary); 00128 00129 isAsciiFile = IS_ASCII; 00130 itembuffer = ITEMBUFFER; 00131 } 00132 00133 ///////////////////////////////////////////////////////////////////// 00134 /// Close the input file 00135 ///////////////////////////////////////////////////////////////////// 00136 InputData::~InputData() { 00137 inputFile.close(); 00138 } 00139 00140 ///////////////////////////////////////////////////////////////////// 00141 /// Check if the input file is open 00142 /// 00143 /// @return true if file is open 00144 ///////////////////////////////////////////////////////////////////// 00145 int InputData::isOpen() { 00146 return inputFile.is_open(); 00147 } 00148 /** @} */