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

trie/trie_manipulators/sequence/CandidateGeneratorPrune.hpp

Go to the documentation of this file.
00001 #ifndef CandidateGeneratorPrune_HPP
00002 #define CandidateGeneratorPrune_HPP
00003 
00004 
00005 #include "common.hpp"
00006 #include "common/Edge.hpp"
00007 #include <vector>
00008 //#include <cstdio>
00009 //#include <iterator>   //for test
00010 
00011 
00012 
00013 
00014 namespace Bodon
00015 {
00016 namespace sequence
00017 {
00018    template <class P, class DF_D, class TRIE>
00019    class CandidateGeneratorPrune : public P
00020    {
00021       protected:
00022          unsigned int nr_of_new_candidates;
00023          unsigned int nr_of_all_candidates;
00024       public:
00025          CandidateGeneratorPrune( TRIE& main_trie, DF_D& df_decoder ) : 
00026             P(main_trie, df_decoder), 
00027             nr_of_all_candidates(0){}
00028 
00029 
00035          void generateCandidate(unsigned int candidate_size);
00036       
00037          void afterWorkCandGen();
00038       protected:
00039 
00041          void generateCandidateFindParent( 
00042             TRIE* trie, std::vector<item_t>& maybe_candidate,
00043             unsigned int step_to_freq_par);
00044          void removeNonCandPaths( TRIE* trie, unsigned int step_to_freq);
00045    };
00046 
00047 
00048    template <class P, class DF_D, class TRIE>
00049    void CandidateGeneratorPrune<P, DF_D, TRIE>::
00050    generateCandidate(unsigned int candidate_size)
00051    {
00052       assert(candidate_size > 1);
00053       nr_of_new_candidates = 0;
00054       std::vector<item_t> maybe_candidate;
00055       generateCandidateFindParent( &main_trie, maybe_candidate,
00056                                    candidate_size -2 );
00057       removeNonCandPaths(&main_trie, candidate_size - 2);
00058       log_dbg(0,"Number of newly generated candidates: %d", 
00059               nr_of_new_candidates);
00060 
00061 #if DEBUG_LEVEL >= LEVEL_DBG
00062       nr_of_all_candidates += nr_of_new_candidates;
00063 #endif
00064    }
00065    template <class P, class DF_D, class TRIE>
00066    void CandidateGeneratorPrune<P, DF_D, TRIE>::
00067    afterWorkCandGen()
00068    {
00069       log_dbg(0,"The total number of generated candidates (in del. p.): %d", 
00070               nr_of_all_candidates);
00071    }
00072 
00073    template <class P, class DF_D, class TRIE>
00074    void CandidateGeneratorPrune<P, DF_D, TRIE>::
00075    generateCandidateFindParent( TRIE* trie, 
00076                                 std::vector<item_t>& maybe_candidate,
00077                                 unsigned int step_to_freq_par)
00078    {
00079       typename TRIE::iterator itEdge( trie->edgelist.begin() );
00080       if(step_to_freq_par)
00081       {
00082          --step_to_freq_par;
00083          while( itEdge != trie->edgelist.end() )
00084          {
00085             maybe_candidate.push_back((*itEdge).first);
00086             df_decoder.pushItem( (*itEdge).first );
00087             generateCandidateFindParent( static_cast<TRIE*>((*itEdge).second), 
00088                                          maybe_candidate,
00089                                          step_to_freq_par);
00090             df_decoder.popItem();
00091             maybe_candidate.pop_back();
00092             ++itEdge;
00093          }
00094       }
00095       else
00096       {
00097          generateCandidateAtParent(trie, maybe_candidate);
00098 #if DEBUG_LEVEL >= LEVEL_DBG
00099          for( typename TRIE::iterator it( trie->edgelist.begin() );
00100               it != trie->edgelist.end(); ++it)
00101             nr_of_new_candidates += 
00102                static_cast<TRIE*>((*it).second)->edgelist.size();
00103 #endif
00104       }
00105    }
00106 
00107    template <class P, class DF_D, class TRIE>
00108    void CandidateGeneratorPrune<P, DF_D, TRIE>::
00109    removeNonCandPaths( TRIE* trie, unsigned int step_to_freq_par)
00110    {
00111       typename TRIE::iterator itEdge( trie->edgelist.begin() );
00112       while( itEdge != trie->edgelist.end() )
00113       {
00114          if(step_to_freq_par)
00115             removeNonCandPaths( static_cast<TRIE*>((*itEdge).second), 
00116                                 step_to_freq_par-1);
00117          if(static_cast<TRIE*>((*itEdge).second)->edgelist.empty())
00118          {
00119             delete static_cast<TRIE*>((*itEdge).second);
00120             itEdge = trie->edgelist.erase(itEdge);
00121          }
00122          else ++itEdge;
00123       }
00124    }
00125 }
00126 }
00127 #endif

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