00001 #ifndef SeqFrequent2Filter_CPP
00002 #define SeqFrequent2Filter_CPP
00003
00008 #include "common.hpp"
00009 #include <vector>
00010
00011
00012
00019 template <class IT_R>
00020 class SeqFrequent2Filter
00021 {
00022 public:
00023 SeqFrequent2Filter<IT_R>(IT_R* it_r):
00024 it_r(it_r), largest_item_p1(it_r->getLargestItem()+1) { }
00025
00026 void findFrequentPairs(
00027 std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >&
00028 freq_pairs_with_counters,
00029 counter_t min_supp);
00030
00031
00032 private:
00033 IT_R* it_r;
00034 const item_t largest_item_p1;
00035 };
00042 template <class IT_R> inline void SeqFrequent2Filter<IT_R>::
00043 findFrequentPairs(
00044 std::vector< std::pair<counter_t, std::pair<item_t, item_t> > >&
00045 freq_pairs_with_counters, counter_t min_supp)
00046 {
00047 std::vector< std::vector<counter_t> > temp_counter_array;
00048 std::vector< std::vector<bool> > pattern;
00049 temp_counter_array.reserve(largest_item_p1);
00050 temp_counter_array.resize(largest_item_p1);
00051 pattern.reserve(largest_item_p1);
00052 pattern.resize(largest_item_p1);
00053 for( item_t index = 0; index < largest_item_p1; ++index)
00054 {
00055 temp_counter_array[index].reserve(largest_item_p1);
00056 pattern[index].reserve(largest_item_p1);
00057 temp_counter_array[index].resize(largest_item_p1, 0);
00058 pattern[index].resize(largest_item_p1, true);
00059 }
00060
00061 freq_pairs_with_counters.clear();
00062 it_r->rewind();
00063
00064 std::vector<item_t> transaction;
00065 std::vector<item_t>::iterator it1, it2;
00066 counter_t counter;
00068 while( (counter = it_r->nextTransactionBIS( transaction )) != 0 )
00069 {
00070 if( transaction.size() > 1)
00071 {
00072 for( it1 = transaction.begin(); it1 != transaction.end()-1; ++it1)
00073 {
00074 for( it2 = it1+1; it2 != transaction.end(); ++it2)
00075 if(pattern[*it1][*it2])
00076 {
00077 temp_counter_array[*it1][*it2] += counter;
00078 pattern[*it1][*it2] = false;
00079 }
00080 }
00081 for( it1 = transaction.begin(); it1 != transaction.end()-1; ++it1)
00082 for( it2 = it1+1; it2 != transaction.end(); ++it2)
00083 pattern[*it1][*it2] = true;
00084 }
00085 }
00087 item_t index1, index2;
00088 std::pair<item_t, item_t> temp_pair;
00089 std::pair< counter_t, std::pair<item_t, item_t> > temp_pair2;
00090 for( index1 = 0; index1 < largest_item_p1 ; ++index1 )
00091 {
00092 for( index2 = 0; index2 < largest_item_p1; ++index2 )
00093 {
00094 if( temp_counter_array[index1][index2] >= min_supp )
00095 {
00096 temp_pair.first = index1;
00097 temp_pair.second = index2;
00098 temp_pair2.first = temp_counter_array[index1][index2];
00099 temp_pair2.second = temp_pair;
00100 freq_pairs_with_counters.push_back(temp_pair2);
00101 }
00102 }
00103 temp_counter_array[index1].clear();
00104 std::vector<counter_t>().swap(temp_counter_array[index1]);
00105
00106 }
00107 }
00108
00109 #endif