00001 #ifndef FrequentPairInserterDyn_HPP
00002 #define FrequentPairInserterDyn_HPP
00003
00004
00005 #include "common.hpp"
00006 #include "common/Edge.hpp"
00007 #include "apriori/bodon/inhomogeneous_trie/trie_manipulators/ManipulatorBase.hpp"
00008 #include <vector>
00009
00010
00011
00012 namespace Bodon
00013 {
00014 namespace dynamic_trie
00015 {
00016 template <class DF_D, class TRIE_OEL, class TRIE_OI, class LEAF,
00017 class LEAF_ALLOCATOR, NEELevel NEE>
00018 class FrequentPairInserter : public Bodon::inhomogeneous_trie::
00019 ManipulatorBase<DF_D, TRIE_OEL, LEAF_ALLOCATOR>
00020 {
00021 private:
00022 typedef Bodon::inhomogeneous_trie::
00023 ManipulatorBase<DF_D, TRIE_OEL, LEAF_ALLOCATOR> PARENT;
00024 protected:
00025 std::vector<Edge> extenders;
00026 std::vector<Edge> replace_list;
00027
00028 public:
00029 FrequentPairInserter( TRIE_OEL& trie, DF_D& df_decoder,
00030 LEAF_ALLOCATOR& s_alloc ) :
00031 PARENT(trie, df_decoder, s_alloc){}
00032
00033
00035 void insertFrequentPairs(
00036 const std::vector< std::pair< counter_t,
00037 std::pair<item_t, item_t> > >& freq_pairs_with_counters )
00038 {
00039 if(NEE > NEE_Off)
00040 insertFrequentPairsNEE(freq_pairs_with_counters);
00041 else
00042 insertFrequentPairsNONEE(freq_pairs_with_counters);
00043 }
00044
00045 protected:
00046 void insertFrequentPairsNONEE(
00047 const std::vector< std::pair< counter_t,
00048 std::pair<item_t, item_t> > >& freq_pairs_with_counters );
00049 void insertFrequentPairsNEE(
00050 const std::vector< std::pair< counter_t,
00051 std::pair<item_t, item_t> > >& freq_pairs_with_counters );
00052
00053
00054 };
00055
00056
00057 template <class DF_D, class TRIE_OEL, class TRIE_OI, class LEAF, class LEAF_ALLOCATOR, NEELevel NEE> void
00058 FrequentPairInserter<DF_D, TRIE_OEL, TRIE_OI, LEAF, LEAF_ALLOCATOR, NEE>::insertFrequentPairsNONEE(
00059 const std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >&
00060 freq_pairs_with_counters )
00061 {
00062 if( freq_pairs_with_counters.empty() )
00063 PARENT::main_trie.edgelist.clear();
00064 else
00065 {
00066 std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >::
00067 const_iterator it = freq_pairs_with_counters.begin();
00068 typename TRIE_OEL::iterator mt_iter;
00069 for( mt_iter = PARENT::main_trie.edgelist.begin();
00070 mt_iter != PARENT::main_trie.edgelist.end(); ++mt_iter)
00071 {
00072 while (it != freq_pairs_with_counters.end() &&
00073 (*it).second.first < (*mt_iter).first)
00074 ++it;
00075 if(it == freq_pairs_with_counters.end() )
00076 {
00077 break;
00078 }
00079 extenders.clear();
00080 while ((*it).second.first == (*mt_iter).first)
00081 {
00082 extenders.push_back(Edge((*it).second.second, PARENT::s_alloc.allocate()));
00083 static_cast<LEAF*>(extenders.back().second)->setCounter((*it).first);
00084 ++it;
00085 }
00086 if(!extenders.empty())
00087 if( 2 * extenders.size() < extenders.back().first - extenders.front().first + 2 )
00088 {
00089 TRIE_OEL* toExtend = new TRIE_OEL(
00090 static_cast<LEAF*>((*mt_iter).second)->getCounter() | TWO_POW31 );
00091 toExtend->edgelist.insert(extenders);
00092 replace_list.push_back(Edge((*mt_iter).first, toExtend));
00093 }
00094 else
00095 {
00096 TRIE_OI* toExtend = new TRIE_OI(
00097 static_cast<LEAF*>((*mt_iter).second)->getCounter() );
00098 toExtend->edgelist.insert(extenders);
00099 replace_list.push_back(Edge((*mt_iter).first, toExtend));
00100 }
00101 delete static_cast<LEAF*>((*mt_iter).second);
00102 }
00103 while( mt_iter != PARENT::main_trie.edgelist.end() )
00104 {
00105 delete static_cast<LEAF*>((*mt_iter).second);
00106 ++mt_iter;
00107 }
00108 PARENT::main_trie.edgelist.clear();
00109 PARENT::main_trie.edgelist.insert(replace_list);
00110 }
00111 }
00112
00113 template <class DF_D, class TRIE_OEL, class TRIE_OI,
00114 class LEAF, class LEAF_ALLOCATOR, NEELevel NEE> void
00115 FrequentPairInserter<DF_D, TRIE_OEL, TRIE_OI, LEAF, LEAF_ALLOCATOR, NEE>::
00116 insertFrequentPairsNEE(
00117 const std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >&
00118 freq_pairs_with_counters )
00119 {
00120 PARENT::df_decoder.pushEquisupportItemSet(PARENT::main_trie.neelist);
00121 PARENT::df_decoder.write( PARENT::main_trie.getCounter() & TWO_POW31_1);
00122 if( freq_pairs_with_counters.empty() )
00123 PARENT::main_trie.edgelist.clear();
00124 else
00125 {
00126 unsigned int nr_of_prefix_equiitem=0;
00127 std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >::
00128 const_iterator it = freq_pairs_with_counters.begin();
00129 vector<item_t> neelist;
00130 typename TRIE_OEL::iterator mt_iter;
00131 for( mt_iter = PARENT::main_trie.edgelist.begin();
00132 mt_iter != PARENT::main_trie.edgelist.end(); ++mt_iter)
00133 {
00134 while (it != freq_pairs_with_counters.end() &&
00135 (*it).second.first < (*mt_iter).first)
00136 ++it;
00137 extenders.clear();
00138 neelist.clear();
00139 while( it != freq_pairs_with_counters.end() &&
00140 (*it).second.first == (*mt_iter).first )
00141 if( !PARENT::main_trie.neeFind((*it).second.second) )
00142 {
00143 if( (*it).first ==
00144 static_cast<LEAF*>((*mt_iter).second)->getCounter() )
00145 {
00146 neelist.push_back((*it).second.second);
00147 #if DEBUG_LEVEL >= LEVEL_PERF
00148 ++nr_of_prefix_equiitem;
00149 #endif
00150 }
00151 else
00152 {
00153 extenders.push_back(Edge((*it).second.second,
00154 PARENT::s_alloc.allocate()));
00155 static_cast<LEAF*>(extenders.back().second)->setCounter((*it).first);
00156 }
00157 ++it;
00158 }
00159 if(extenders.empty())
00160 {
00161 if(PARENT::main_trie.neelist.empty() && neelist.empty())
00162 {
00163 PARENT::df_decoder.pushItemWithWrite(
00164 (*mt_iter).first, static_cast<LEAF*>((*mt_iter).second)->getCounter() );
00165 PARENT::df_decoder.popItem();
00166 }
00167 else
00168 {
00169 TRIE_OEL* toExtend = new TRIE_OEL(
00170 static_cast<LEAF*>((*mt_iter).second)->getCounter() | TWO_POW31 );
00171 toExtend->neePushBackSorted(neelist);
00172 replace_list.push_back(Edge((*mt_iter).first, toExtend));
00173 }
00174 }
00175 else
00176 if( 2 * extenders.size() <
00177 extenders.back().first - extenders.front().first + 2 )
00178 {
00179 TRIE_OEL* toExtend = new TRIE_OEL(
00180 static_cast<LEAF*>((*mt_iter).second)->getCounter() | TWO_POW31 );
00181 toExtend->edgelist.insert(extenders);
00182 toExtend->neePushBackSorted(neelist);
00183 replace_list.push_back(Edge((*mt_iter).first, toExtend));
00184 }
00185 else
00186 {
00187 TRIE_OI* toExtend = new TRIE_OI(
00188 static_cast<LEAF*>((*mt_iter).second)->getCounter() );
00189 toExtend->edgelist.insert(extenders);
00190 toExtend->neePushBackSorted(neelist);
00191 replace_list.push_back(Edge((*mt_iter).first, toExtend));
00192 }
00193 delete static_cast<LEAF*>((*mt_iter).second);
00194 }
00195 PARENT::main_trie.edgelist.clear();
00196 PARENT::main_trie.edgelist.insert(replace_list);
00197
00198 }
00199 }
00200 }
00201 }
00202
00203 #endif