00001 #ifndef FrequentFilter_CPP 00002 #define FrequentFilter_CPP 00003 00008 #include "common.hpp" 00009 #include <vector> 00010 00011 00017 template <class IT_R> 00018 class FrequentFilter 00019 { 00020 public: 00021 FrequentFilter<IT_R>(IT_R& it_r):it_r(it_r){} 00022 00028 void findFrequentItems( 00029 std::vector< std::pair<counter_t, item_t> >& freq_items_with_counters, 00030 counter_t& nr_of_transactions, counter_t& min_supp, 00031 bool relative=false, double relminsupp=0.0); 00032 private: 00033 IT_R& it_r; 00034 00035 00036 }; 00045 template <class IT_R> void FrequentFilter<IT_R>::findFrequentItems( 00046 std::vector< std::pair<counter_t, item_t> >& freq_items_with_counters, 00047 counter_t& nr_of_transactions, counter_t& min_supp, 00048 bool relative, double relminsupp) 00049 { 00050 freq_items_with_counters.clear(); 00051 nr_of_transactions = 0; 00052 it_r.rewind(); 00053 00054 std::vector<item_t> transaction; 00055 std::vector< counter_t > temp_counter_vector; 00056 00058 std::vector<item_t>::iterator it_transaction; 00059 while( it_r.nextTransactionBIS( transaction ) ) 00060 { 00061 if( !transaction.empty() ) 00062 { 00063 nr_of_transactions++; 00064 for( it_transaction = transaction.begin(); 00065 it_transaction != transaction.end(); ++it_transaction ) 00066 { 00067 if( *it_transaction + 1 > temp_counter_vector.size() ) 00068 temp_counter_vector.resize( *it_transaction + 1, 0 ); 00069 ++temp_counter_vector[*it_transaction]; 00070 } 00071 } 00072 } 00073 it_r.setLargestItem( temp_counter_vector.size() - 1 ); 00074 00075 if(relative) 00076 min_supp = static_cast<unsigned int>(relminsupp * nr_of_transactions); 00078 for( std::vector< counter_t >::size_type index = 0; 00079 index < temp_counter_vector.size(); ++index ) 00080 if(temp_counter_vector[index] >= min_supp) 00081 { 00082 std::pair<counter_t, item_t> temp_pair(temp_counter_vector[index],index); 00083 freq_items_with_counters.push_back(temp_pair); 00084 } 00085 } 00086 00087 #endif