Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

StatOutput.hpp

Go to the documentation of this file.
00001 
00014 #ifndef StatOutput_HPP
00015 #define StatOutput_HPP
00016 
00017 #include "common.hpp"  
00018 #include "common/log.h"
00019 #include "io/output/normal/BufferedNormalOutput.hpp"
00020 #include <iostream>
00021 #include <vector>
00022 
00023 template <class OUTPUT = BufferedNormalOutput<>, bool VERBOSE = false > 
00024 class StatOutput : public OUTPUT
00025 {
00026       std::vector<counter_t> lengthsp;
00027       size_t buffer_size;
00028 public:
00029       class params_t : public OUTPUT::params_t
00030       {
00031          public:
00033             item_t numfreq;
00034             params_t() : OUTPUT::params_t()
00035             {
00036                numfreq = 0;
00037             }
00038       };
00039       StatOutput( const params_t* par ):
00040          OUTPUT(par), lengthsp(par->numfreq), buffer_size(0){ }
00041       
00042       template <class InputIterator> inline 
00043       void DINLINE writeItemsetAndCounter( 
00044          InputIterator first, InputIterator last, 
00045          const counter_t support)
00046       {
00047          size_t slen = last - first; //will work only with randomaccess iterators
00048          lengthsp[slen]++;
00049          OUTPUT::writeItemsetAndCounter(first,last,support);
00050       }
00051 
00052       void pushItem(item_t item)
00053       {
00054          ++buffer_size;
00055          OUTPUT::pushItem(item);
00056       }
00057       void write(counter_t support)
00058       {
00059          lengthsp[buffer_size]++;
00060          OUTPUT::write(support);
00061       }
00062 
00063       void pushItemWithWrite(item_t item, counter_t support)
00064       {
00065          ++buffer_size;
00066          lengthsp[buffer_size]++;
00067          OUTPUT::pushItemWithWrite(item, support);
00068       }
00069 
00070       void pushItemWithPrevSupport(item_t item)
00071       {
00072          ++buffer_size;
00073          lengthsp[buffer_size]++;
00074          OUTPUT::pushItemWithPrevSupport(item);
00075       }
00076 
00077       void popItem()
00078       {
00079          --buffer_size;
00080          OUTPUT::popItem();
00081       }
00082 
00083       void popAll()
00084       {
00085          buffer_size = 0;
00086          OUTPUT::popAll();
00087       }
00088 
00090       ~StatOutput( )
00091       {
00092          int maxs=-1;
00093          unsigned int total_nr_of_freq_items = 0;
00094          for(size_t i=0; i< lengthsp.size(); ++i) 
00095          {
00096             if( lengthsp[i] )
00097                maxs=i;
00098             total_nr_of_freq_items += lengthsp[i];
00099          }
00100          if (VERBOSE)
00101          {
00102             std::cerr<<"Total number of frequent itemsets: \t\t\t"<<total_nr_of_freq_items<<std::endl;
00103             std::cerr<<"Total number of frequent itemsets except 0,1,2-itemsets: "<<
00104             total_nr_of_freq_items-lengthsp[0]-lengthsp[1]-lengthsp[2]<<std::endl;
00105             std::cerr<<"The size of the largest frequent itemset: \t\t"<<maxs<<std::endl;
00106             std::cerr<<"The distribution of the frequent itemsets:"<<std::endl;
00107          }
00108          unsigned int sum_of_sizes=0;
00109          for(int i=0; i <= maxs; ++i) 
00110          {
00111             if(VERBOSE)
00112             {
00113                std::cerr<<i<<':';
00114                sum_of_sizes += i*lengthsp[i];
00115             }
00116             std::cerr<<lengthsp[i]<<std::endl;
00117          }
00118          if(VERBOSE)
00119          {
00120             std::cerr<<"The average size of the frequent itemsets: \t\t"<<
00121                static_cast<double>(sum_of_sizes)/total_nr_of_freq_items<<std::endl;
00122          }
00123          
00124       }
00125 };
00126 
00131 template <class OUTPUT>
00132 class LStatOutput {
00133 private:
00134   OUTPUT& out;
00135   size_t totlen;
00136   size_t totsup;
00137 public:
00138 
00144   LStatOutput( OUTPUT& out, item_t numfreq )
00145     throw(std::ios_base::failure) : out(out), totlen(0), totsup(0){
00146         
00147   }
00148       
00159   template <class InputIterator> inline void DINLINE writeItemsetAndCounter( 
00160                                                                             InputIterator first, InputIterator last, 
00161                                                                             const counter_t support){
00162     totlen+=last-first; //will work only with randomaccessiterators
00163     totsup+=support; //to avoid the compiler optimizing it away
00164     out.writeItemsetAndCounter(first,last,support);
00165   }
00166 
00167   void flush() {
00168     out.flush();
00169   }
00170 
00172   ~LStatOutput( ){
00173     log_perf(0,"Total length of itemsets output: %u",totlen);
00174     log_perf(0,"Total support (ignore this fool statistics): %u",totsup);
00175   }
00176 };
00177 
00178 
00179 #endif //StatOutput_CPP

Generated on Sun Sep 17 17:50:40 2006 for FIM environment by  doxygen 1.4.4