Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

dynamic_trie/trie_manipulators/FrequentPairInserterNoprune.hpp

Go to the documentation of this file.
00001 #ifndef FrequentPairInserterNopruneDyn_HPP
00002 #define FrequentPairInserterNopruneDyn_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 //#include <cstdio>
00009 //#include <iterator>   //for test
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 FrequentPairInserterNoprune : 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       FrequentPairInserterNoprune( 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, 
00058           class LEAF_ALLOCATOR, NEELevel NEE> void 
00059 FrequentPairInserterNoprune<DF_D, TRIE_OEL, TRIE_OI, LEAF, LEAF_ALLOCATOR, NEE>::
00060 insertFrequentPairsNONEE(
00061    const std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >& 
00062    freq_pairs_with_counters )
00063 {
00064    if( freq_pairs_with_counters.empty() )
00065       PARENT::main_trie.edgelist.clear();
00066    else
00067    {
00068       std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >::
00069          const_iterator it = freq_pairs_with_counters.begin();
00070       typename TRIE_OEL::iterator mt_iter = PARENT::main_trie.edgelist.begin(); ;
00071       while( mt_iter != PARENT::main_trie.edgelist.end() )
00072       {
00073          while (it != freq_pairs_with_counters.end() && 
00074                 (*it).second.first < (*mt_iter).first)
00075             ++it;
00076          if(it == freq_pairs_with_counters.end() )
00077          {
00078             break;
00079          }
00080          extenders.clear();
00081          while ((*it).second.first == (*mt_iter).first)
00082          {
00083             extenders.push_back(Edge((*it).second.second, PARENT::s_alloc.allocate()));
00084             static_cast<LEAF*>(extenders.back().second)->setCounter((*it).first);
00085             ++it;
00086          }
00087          if(extenders.size() > 1)
00088          {
00089             if( 2 * extenders.size() < extenders.back().first - extenders.front().first + 2 )
00090             {
00091                TRIE_OEL* toExtend = new TRIE_OEL(
00092                   static_cast<LEAF*>((*mt_iter).second)->getCounter() | TWO_POW31 );
00093                toExtend->edgelist.insert(extenders);
00094                replace_list.push_back(Edge((*mt_iter).first, toExtend));
00095             }
00096             else
00097             {
00098                TRIE_OI* toExtend = new TRIE_OI(
00099                   static_cast<LEAF*>((*mt_iter).second)->getCounter() );
00100                toExtend->edgelist.insert(extenders);
00101                replace_list.push_back(Edge((*mt_iter).first, toExtend));
00102             }
00103             ++mt_iter;
00104          }
00105          else
00106          {
00107             if(extenders.size() == 1)
00108             {
00109                PARENT::df_decoder.pushItem((*mt_iter).first);
00110                PARENT::df_decoder.pushItemWithWrite(
00111                   extenders.front().first, static_cast<LEAF*>(extenders.front().second)->getCounter());
00112                PARENT::s_alloc.free(static_cast<LEAF*>(extenders.front().second));
00113                PARENT::df_decoder.popItem();
00114                PARENT::df_decoder.popItem();
00115             }
00116             delete static_cast<LEAF*>((*mt_iter).second);
00117             mt_iter = PARENT::main_trie.edgelist.erase(mt_iter);
00118          }
00119       }
00120       while( mt_iter != PARENT::main_trie.edgelist.end() )
00121       {
00122          delete static_cast<LEAF*>((*mt_iter).second);
00123          ++mt_iter;
00124       }
00125       PARENT::main_trie.edgelist.clear();
00126       PARENT::main_trie.edgelist.insert(replace_list);
00127    }
00128 }
00129 
00130 template <class DF_D, class TRIE_OEL, class TRIE_OI, 
00131           class LEAF, class LEAF_ALLOCATOR, NEELevel NEE> void 
00132 FrequentPairInserterNoprune<DF_D, TRIE_OEL, TRIE_OI, LEAF, LEAF_ALLOCATOR, NEE>::
00133 insertFrequentPairsNEE(
00134    const std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >& 
00135    freq_pairs_with_counters )
00136 {
00137    PARENT::df_decoder.pushEquisupportItemSet(PARENT::main_trie.neelist);
00138    PARENT::df_decoder.write( PARENT::main_trie.getCounter() & TWO_POW31_1);
00139    if( freq_pairs_with_counters.empty() )
00140       PARENT::main_trie.edgelist.clear();
00141    else
00142    {
00143       unsigned int nr_of_prefix_equiitem=0;
00144       std::vector< std::pair< counter_t, std::pair<item_t, item_t> > >::
00145          const_iterator it = freq_pairs_with_counters.begin();
00146       vector<item_t> neelist;
00147       for( typename TRIE_OEL::iterator mt_iter = PARENT::main_trie.edgelist.begin(); 
00148            mt_iter != PARENT::main_trie.edgelist.end(); ++mt_iter )
00149       {
00150          while (it != freq_pairs_with_counters.end() && 
00151                 (*it).second.first < (*mt_iter).first)
00152             ++it;
00153          extenders.clear();
00154          neelist.clear();
00155          while( it != freq_pairs_with_counters.end() && 
00156                 (*it).second.first == (*mt_iter).first )
00157             if( !PARENT::main_trie.neeFind((*it).second.second) )
00158          {
00159             if( (*it).first == 
00160                 static_cast<LEAF*>((*mt_iter).second)->getCounter() )
00161             {
00162                neelist.push_back((*it).second.second);
00163 #if DEBUG_LEVEL >= LEVEL_PERF
00164                ++nr_of_prefix_equiitem;
00165 #endif
00166             }
00167             else
00168             {
00169                extenders.push_back(Edge((*it).second.second, 
00170                                         PARENT::s_alloc.allocate()));
00171                static_cast<LEAF*>(extenders.back().second)->setCounter((*it).first);
00172             }
00173             ++it;
00174          }
00175          if(extenders.size() > 1)
00176          {
00177             if( 2 * extenders.size() < 
00178                 extenders.back().first - extenders.front().first + 2 )
00179             {
00180                TRIE_OEL* toExtend = new TRIE_OEL(
00181                   static_cast<LEAF*>((*mt_iter).second)->getCounter() | TWO_POW31 );
00182                toExtend->edgelist.insert(extenders);
00183                toExtend->neePushBackSorted(neelist);
00184                replace_list.push_back(Edge((*mt_iter).first, toExtend));
00185             }
00186             else
00187             {
00188                TRIE_OI* toExtend = new TRIE_OI(
00189                   static_cast<LEAF*>((*mt_iter).second)->getCounter() );
00190                toExtend->edgelist.insert(extenders);
00191                toExtend->neePushBackSorted(neelist);
00192                replace_list.push_back(Edge((*mt_iter).first, toExtend));
00193             }
00194          }
00195          else
00196          {
00197             PARENT::df_decoder.pushItem((*mt_iter).first);
00198             PARENT::df_decoder.pushEquisupportItemSet(neelist);
00199             if(extenders.size() == 1)
00200             {
00201                PARENT::df_decoder.pushItem( extenders.front().first );
00202                PARENT::df_decoder.write(
00203                   static_cast<LEAF*>(extenders.front().second)->getCounter());
00204                PARENT::s_alloc.free(static_cast<LEAF*>(extenders.front().second));
00205                PARENT::df_decoder.popItem();
00206             }
00207             PARENT::df_decoder.write( 
00208                static_cast<LEAF*>((*mt_iter).second)->getCounter() );
00209             PARENT::df_decoder.popItem();
00210          }
00211          delete static_cast<LEAF*>((*mt_iter).second);
00212       }
00213       PARENT::main_trie.edgelist.clear();
00214       PARENT::main_trie.edgelist.insert(replace_list);
00215 //      log_perf(0,"Number of prefix equisupport items: %d", nr_of_prefix_equiitem); 
00216    }
00217 }
00218 }
00219 }
00220 
00221 #endif

Generated on Sun Sep 17 17:50:38 2006 for FIM environment by  doxygen 1.4.4