iset_vat.h

00001 /*
00002  *  Copyright (C) 2005 M.J. Zaki <zaki@cs.rpi.edu> Rensselaer Polytechnic Institute 
00003  *  Written by parimi@cs.rpi.edu
00004  *  Updated by chaojv@cs.rpi.edu, alhasan@cs.rpi.edu, salems@cs.rpi.edu
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 #ifndef _ISET_VAT_H_
00021 #define _ISET_VAT_H_
00022 
00023 #include <iterator>
00024 #include "pattern.h"
00025 #include "pat_support.h"
00026 #include "generic_classes.h"
00027 #include "typedefs.h"
00028 
00029 template<class PP, class MP, template <typename> class ALLOC, template <typename P, typename> class ST >
00030 ostream& operator<< (ostream& , const vat<PP, MP, ALLOC, ST >&);
00031  
00032 template<class PP, class MP, template <typename> class ALLOC, template <typename P, typename> class ST >
00033 istream& operator>> (istream& , vat<PP, MP, ALLOC, ST >&);
00034 
00035 
00044 template<class PP, class MP, template <typename> class ALLOC, template <typename, typename> class ST >
00045 class vat<ISET_PROP, V_Fkk_MINE_PROP, ALLOC, ST >
00046 {
00047  public:
00048   typedef pattern_support<V_Fkk_MINE_PROP> PAT_SUP;
00049   typedef vat<ISET_PROP, V_Fkk_MINE_PROP, ALLOC, ST > VAT;
00050   
00051   typedef ST<int, ALLOC<int> > IDLIST_T; // Definition of a itemset VAT.
00052   typedef typename IDLIST_T::const_iterator CONST_IT;
00053   typedef typename IDLIST_T::iterator IT;
00054  
00055 
00056   void* operator new(size_t size) {
00057     ALLOC<VAT> va;
00058     return va.allocate(size);
00059   }
00060 
00061   void  operator delete(void *p, size_t size) {
00062     if (p) {
00063       ALLOC<VAT> va;
00064       va.deallocate(static_cast<VAT*> (p), size);
00065     }
00066   }
00067  
00068   vat() {}
00069   
00070   IT begin() {
00071     return _idlist.begin();
00072   }
00073 
00074   CONST_IT begin() const {
00075     return _idlist.begin();
00076   }
00077 
00078   IT end() {
00079     return _idlist.end();
00080   }
00081 
00082   CONST_IT end() const {
00083     return _idlist.end();
00084   }
00085 
00086   bool empty() const {
00087     return _idlist.empty;
00088   }
00089 
00090   int size() {
00091     return _idlist.size();
00092   }
00093   
00094   void make_empty() { 
00095     _idlist.clear();
00096   }        
00097 
00099   void push_back(const int& val) {
00100     _idlist.push_back(val);
00101   }
00102     
00104   int& operator[](int n) {
00105     return *(begin()+n);
00106   } 
00107 
00109   void resize(int x) {
00110     _idlist.resize(x);
00111   }
00112 
00113   //clear the vector
00114   void clear() {
00115     _idlist.clear();
00116   }
00117   
00118   
00129   template<class PAT>
00130     static VAT** intersection(const VAT* const& vat_i, const VAT* const& vat_j, PAT_SUP** cand_sups, PAT**, bool& is_l2) {
00131     
00132     VAT** cand_vats = new VAT*[1]; // This memory is reclaimed in count() routine of count_support.h file
00133     cand_vats[0] = new VAT();      // This memory is reclaimed in count() routine of count_support.h file, only if the 
00134     // corresponding pattern is NOT frequent, otherwise, Storage Manager get a hold of this
00135     // VAT.
00136     
00137     if(cand_sups[0] == 0) { // No pattern_support class associated to return support value, ERROR
00138       delete cand_vats[0];
00139       delete[] cand_vats;
00140       
00141       cerr << "iset_vat: No candidates VATs to be generated." << endl;
00142       return NULL;
00143     }
00144     
00145     CONST_IT it_i=vat_i->begin(), it_j=vat_j->begin();
00146     
00147     while(it_i!=vat_i->end() && it_j!=vat_j->end()) {
00148       if(*it_i < *it_j) {
00149         it_i++;
00150         continue;
00151       }
00152       
00153       if(*it_j < *it_i) {
00154         it_j++;
00155         continue;
00156       }
00157       
00158       // Execution reach is this point only if both TIDs are equal
00159       cand_vats[0]->push_back(*it_i);
00160       it_i++;
00161       it_j++;
00162     }//end while
00163     
00164     // setting the support value that will be checked in the count method of count_support class.
00165     cand_sups[0]->set_sup(make_pair(cand_vats[0]->size(), cand_vats[0]->size()));
00166     
00167     return cand_vats;
00168   }
00169   
00174   unsigned long size() const {
00175     return _idlist.size();
00176   }
00177 
00181   vat(const VAT &orig) {
00182     *this= orig;
00183   }
00184 
00185   ~vat(){
00186     _idlist.clear();
00187   }
00188   
00192   const VAT &operator=(const VAT &right) {
00193     if ( &right != this ) {         // avoid self assignment
00194       _idlist=right._idlist;
00195     }
00196     else
00197       cout << "Attempted assignment of a Vat to itself\n";
00198     
00199     return *this;   // enables cascaded assignments
00200   }
00201   
00202   // Return Vat length
00203   unsigned long int byte_size() const {
00204     
00205     return _idlist.size()* sizeof(int);
00206   }
00207   
00208   // Serialize an Itemset Vat to an Output stream
00209   void write_file(ostream & output_file) const {
00210     //cout<<"writing this:"<<*this;                                                                                              
00211     ostringstream output;
00212     CONST_IT iter;
00213     iter =_idlist.begin();
00214     for (;iter!=_idlist.end();iter++){
00215       // output.write( reinterpret_cast<const char *>( & (*iter) ), sizeof(T));                                                 
00216       output.write( reinterpret_cast<const char *>( & (*iter) ), sizeof(int));
00217     }
00218     output_file.write(output.str().c_str(), output.str().size());
00219   } // //end write_file
00220 
00221   //De-Serialize an Itemset Vat from an Input stream
00222   void read_file (istream & input, unsigned long int size) {
00223     
00224     int ITSZ=sizeof(int);
00225     int buf_size=size/ITSZ;   
00226     int *buf = new int[buf_size];
00227     input.read((char *)buf, size); 
00228     int current=0;
00229     _idlist.clear();
00230     while( current< buf_size){
00231       _idlist.push_back(buf[current++]); 
00232     }
00233     delete [] buf;
00234   } 
00235   
00236   friend ostream& operator<< <>(ostream& , const VAT&);
00237   
00238   friend istream& operator>> <>(istream& , VAT&);
00239   
00240  private:
00241   IDLIST_T _idlist;
00242   
00243 };
00244 
00245 
00246 template<class T, class MP, template <typename> class ALLOC, template <typename P, typename> class ST >
00247 ostream& operator<<(ostream& output, vat<T, MP, ALLOC, ST> const& orig) {
00248   std::ostream_iterator<int> out(output, " ");
00249   std::copy(orig.begin(), orig.end(), out);
00250   output <<'\n';
00251   return output;   // enables cascading
00252 }
00253 
00254 template<class T, class MP, template <typename> class ALLOC, template <typename P, typename> class ST >
00255 istream& operator>>(istream& input,  vat<T, MP, ALLOC, ST >& orig) {
00256   const unsigned int MAXLINE=20000;
00257   char line[MAXLINE];
00258   input.getline(line,MAXLINE);
00259   stringstream s(line);
00260   std::istream_iterator<int> in(s);
00261   std::istream_iterator<int> eof;
00262   
00263   orig._idlist.clear();
00264   std::copy (in,eof, std::back_inserter(orig._idlist));
00265   return input;   // enables cascading
00266 }
00267 
00268 #endif

Generated on Wed Jul 26 14:01:08 2006 for DMTL by  doxygen 1.4.7