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

InfreqRemoverOrderPresAssumption.hpp

Go to the documentation of this file.
00001 #ifndef InfreqRemoverOrderPresAssumption_HPP
00002 #define InfreqRemoverOrderPresAssumption_HPP
00003 
00004 
00005 #include "common.hpp"
00006 #include "common/Edge.hpp"
00007 #include "apriori/bodon/inhomogeneous_trie/trie_manipulators/InfreqRemover.hpp"
00008 #include <vector>
00009 //#include <cstdio>
00010 //#include <iterator>   //for test
00011 
00012 
00013 
00014 
00015 namespace Bodon
00016 {
00017 namespace inhomogeneous_trie
00018 {
00019    template <class DF_D, class TRIE, class LEAF, class LEAF_ALLOCATOR>
00020    class InfreqRemoverOrderPresAssumption : 
00021          public InfreqRemover<DF_D, TRIE, LEAF, LEAF_ALLOCATOR, NEE_Off, false>
00022    {
00023       private:
00024          typedef InfreqRemover<DF_D, TRIE, LEAF, LEAF_ALLOCATOR, NEE_Off, false> 
00025          PARENT;
00026       public:
00027          InfreqRemoverOrderPresAssumption( TRIE& main_trie, DF_D& df_decoder,
00028                                            LEAF_ALLOCATOR& s_alloc ) : 
00029             PARENT(main_trie, df_decoder, s_alloc)
00030          {
00031             float alphas_array[] = {1.00, 0.95, 0.9, 0.85};
00032             int size = sizeof(alphas_array) / sizeof(float);
00033             alphas.insert(alphas.end(), alphas_array, alphas_array + size);         
00034             holds.resize(size);
00035             not_holds.resize(size);
00036          }      
00037          void afterWorkDel();
00038          float getOrderPreservingRatio(const int index) const
00039          {
00040             return static_cast<float>(holds[index])/(holds[index] + not_holds[index]);
00041          }
00042       protected:
00043          std::vector<float> alphas;
00044          std::vector<unsigned int> holds;
00045          std::vector<unsigned int> not_holds;
00046 
00047          void calculateOrderPresRatio(TRIE* trie1,
00048                                       std::vector<item_t>& itemset1 );
00049 
00050          void calculateOrderPresRatioAssist(
00051             const TRIE* trie1, TRIE* trie2, const std::vector<item_t>& itemset1, 
00052             std::vector<item_t>& itemset2, std::vector<item_t>& intersection,
00053             std::vector<item_t>& diff_1, std::vector<item_t>& diff_2);
00054 
00055    };
00056 
00057 
00058    template <class DF_D, class TRIE, class LEAF, class LEAF_ALLOCATOR>
00059    void InfreqRemoverOrderPresAssumption<DF_D, TRIE, LEAF, LEAF_ALLOCATOR>::
00060    afterWorkDel()
00061    {
00062       std::vector<item_t> itemset1;
00063       calculateOrderPresRatio( &this->main_trie, itemset1);
00064       for(unsigned int index = 0; index<holds.size(); ++index)
00065       {
00066          std::cout<<"The "<<alphas[index]<<"-order-preserving ratio is: "<<
00067             getOrderPreservingRatio(index)<<std::endl;
00068       }
00069       PARENT::destroy(&this->main_trie);
00070    }
00071    template <class DF_D, class TRIE, class LEAF, class LEAF_ALLOCATOR>
00072    void InfreqRemoverOrderPresAssumption<DF_D, TRIE, LEAF, LEAF_ALLOCATOR>::
00073    calculateOrderPresRatio(TRIE* trie1,
00074                            std::vector<item_t>& itemset1) 
00075    {
00076       std::vector<item_t> itemset2, intersection,
00077          diff1(itemset1), diff2;
00078       calculateOrderPresRatioAssist( trie1, &this->main_trie,
00079                                      itemset1, itemset2, intersection,
00080                                      diff1, diff2);
00081       for(typename TRIE::iterator itEdge( trie1->edgelist.begin()); 
00082           itEdge != trie1->edgelist.end(); ++itEdge )
00083       {
00084          itemset1.push_back((*itEdge).first);
00085          calculateOrderPresRatio(static_cast<TRIE*>((*itEdge).second), itemset1 );
00086          itemset1.pop_back();
00087       }
00088    }
00089    template <class DF_D, class TRIE, class LEAF, class LEAF_ALLOCATOR>
00090    void InfreqRemoverOrderPresAssumption<DF_D, TRIE, LEAF, LEAF_ALLOCATOR>::
00091    calculateOrderPresRatioAssist(
00092       const TRIE* trie1, TRIE* trie2, const std::vector<item_t>& itemset1, 
00093       std::vector<item_t>& itemset2, std::vector<item_t>& intersection,
00094       std::vector<item_t>& diff1, std::vector<item_t>& diff2)
00095    {
00096       if( trie1 < trie2)
00097       {
00098          if(!intersection.empty() && !diff1.empty() && !diff2.empty())
00099          {
00100             const LEAF* subtrie1 = static_cast<const LEAF*>(
00101                this->main_trie.isIncluded( diff1, diff1.begin()));
00102             const LEAF* subtrie2 = static_cast<const LEAF*>(
00103                this->main_trie.isIncluded( diff2, diff2.begin()));
00104             if(subtrie1->getCounter() != subtrie2->getCounter())
00105             {
00106                for(unsigned int index = 0; index< holds.size(); ++index)
00107                {
00108                   if( subtrie1->getCounter() < subtrie2->getCounter() )
00109                   {
00110                      if(alphas[index] * trie1->getCounter() > trie2->getCounter())                
00111                         ++not_holds[index];
00112                      else
00113                         ++holds[index];
00114                   }
00115                   else
00116                   {
00117                      if(trie1->getCounter() < alphas[index] * trie2->getCounter())                
00118                         ++not_holds[index];
00119                      else
00120                         ++holds[index];
00121                   }
00122                }
00123             }
00124          }
00125       }
00126       for(typename TRIE::iterator itEdge( trie2->edgelist.begin()); 
00127           itEdge != trie2->edgelist.end(); ++itEdge )
00128       {
00129          itemset2.push_back((*itEdge).first);
00130          std::vector<item_t>::size_type index = 
00131             std::find(diff1.begin(), diff1.end(), (*itEdge).first) 
00132             - diff1.begin();
00133          if ( index != diff1.size())
00134          {
00135             intersection.push_back((*itEdge).first);
00136             diff1.erase(diff1.begin() + index);
00137             calculateOrderPresRatioAssist(trie1, static_cast<TRIE*>((*itEdge).second), 
00138                                           itemset1, itemset2, intersection,
00139                                           diff1, diff2);
00140             diff1.insert(diff1.begin() + index, (*itEdge).first);
00141             intersection.pop_back();
00142          }
00143          else
00144          {
00145             diff2.push_back((*itEdge).first);
00146             calculateOrderPresRatioAssist(trie1, static_cast<TRIE*>((*itEdge).second), 
00147                                           itemset1, itemset2, intersection,
00148                                           diff1, diff2);
00149             diff2.pop_back();
00150          }
00151          itemset2.pop_back();
00152       }
00153    }
00154 
00155 }
00156 }
00157 #endif

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