00001 #ifndef Frequent2Filter_CPP
00002 #define Frequent2Filter_CPP
00003
00008 #include "common.hpp"
00009 #include <vector>
00010
00011
00016 template <class IT_R>
00017 class Frequent2Filter
00018 {
00019 public:
00020 Frequent2Filter<IT_R>(IT_R* it_r):
00021 it_r(it_r), largest_item(it_r->getLargestItem()) { }
00022
00024 void findFrequentPairs(
00025 std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >&
00026 freq_pairs_with_counters,
00027 counter_t min_supp);
00028
00029
00030 private:
00031 IT_R* it_r;
00032 const item_t largest_item;
00033 };
00034
00042 template <class IT_R> inline void Frequent2Filter<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 temp_counter_array.reserve(largest_item);
00049 temp_counter_array.resize(largest_item);
00050 for( item_t index = 0; index < largest_item; ++index)
00051 {
00052 temp_counter_array[index].reserve(largest_item - index );
00053 temp_counter_array[index].resize(largest_item - index, 0);
00054 }
00055
00056 freq_pairs_with_counters.clear();
00057 it_r->rewind();
00058
00059 std::vector<item_t> transaction;
00060 std::vector<item_t>::iterator it1, it2;
00061 counter_t counter;
00063 while( (counter = it_r->nextTransactionBIS( transaction )) != 0 )
00064 {
00065 if( transaction.size() > 1)
00066 {
00067 for( it1 = transaction.begin(); it1 != transaction.end()-1; ++it1)
00068 {
00069 for( it2 = it1+1; it2 != transaction.end(); ++it2)
00070 temp_counter_array[*it1][*it2 - *it1 - 1] += counter;
00071 }
00072 }
00073 }
00075 item_t index1, index2;
00076 std::pair<item_t, item_t> temp_pair;
00077 std::pair< counter_t, std::pair<item_t, item_t> > temp_pair2;
00078 for( index1 = 0; index1 < largest_item ; ++index1 )
00079 {
00080 for( index2 = 0; index2 < largest_item - index1; ++index2 )
00081 {
00082 if( temp_counter_array[index1][index2] >= min_supp )
00083 {
00084 temp_pair.first = index1;
00085 temp_pair.second = index1 + index2 + 1;
00086 temp_pair2.first = temp_counter_array[index1][index2];
00087 temp_pair2.second = temp_pair;
00088 freq_pairs_with_counters.push_back(temp_pair2);
00089 }
00090 }
00091 temp_counter_array[index1].clear();
00092 std::vector<counter_t>().swap(temp_counter_array[index1]);
00093
00094 }
00095 }
00096
00097 #endif