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