00001 00021 #ifndef Coder_HPP 00022 #define Coder_HPP 00023 00024 #include "common.hpp" 00025 #include <vector> 00026 #include <set> 00027 #include "io/input/transaction_reader/brBufferedTransactionReader.hpp" 00028 #include "io/codec/decoder/df/CacheDFDecoder.hpp" 00029 00030 enum CodeMode 00031 { 00033 ASC, 00035 DESC, 00037 NOCARE 00038 }; 00039 00040 template <class T_R, class D> 00041 class Coder : public T_R 00042 { 00043 public: 00044 class params_t : public T_R::params_t 00045 { 00046 public: 00047 D* decoder; 00048 std::vector< std::pair<counter_t, item_t> >* 00049 freq_items_with_counters; 00050 std::vector<counter_t>* freq_counters; 00051 CodeMode codemode; 00052 params_t() 00053 { 00054 decoder = NULL; 00055 freq_items_with_counters = NULL; 00056 freq_counters = new std::vector<counter_t>(); 00057 codemode = NOCARE; 00058 } 00059 ~params_t() 00060 { 00061 delete freq_counters; 00062 } 00063 }; 00064 00065 Coder(const params_t* par); 00066 template <class BIS> counter_t nextTransactionBIS( BIS& transaction ); 00067 template <class UAC> counter_t nextTransactionUAC( UAC& transaction ); 00068 00069 protected: 00075 std::vector<item_t> code; 00076 }; 00077 00078 template <class T_R, class D> Coder<T_R, D>:: 00079 Coder(const params_t* par) : T_R(par) 00080 { 00081 code.reserve( T_R::getLargestItem() + 1 ); 00082 code.resize( T_R::getLargestItem() + 1, 0 ); 00083 if( par->codemode != NOCARE ) 00084 { 00085 sort( par->freq_items_with_counters->begin(), 00086 par->freq_items_with_counters->end() ); 00087 if( par->codemode == DESC) 00088 reverse( par->freq_items_with_counters->begin(), 00089 par->freq_items_with_counters->end() ); 00090 } 00091 par->freq_counters->reserve( par->freq_items_with_counters->size() ); 00092 std::vector<item_t> code_inverse; 00093 code_inverse.reserve(par->freq_items_with_counters->size()); 00094 std::vector< std::pair<counter_t, item_t> >::size_type index; 00095 for( index = 0; index < par->freq_items_with_counters->size(); ++index ) 00096 { 00097 code_inverse.push_back( (*par->freq_items_with_counters) 00098 [index].second ); 00099 par->freq_counters->push_back( 00100 (*par->freq_items_with_counters)[index].first ); 00101 code[(*par->freq_items_with_counters)[index].second] = index+1; 00102 } 00103 par->decoder->setCodeInverse(code_inverse); 00104 T_R::largest_item = par->freq_items_with_counters->size() - 1; 00105 } 00106 00107 template <class T_R, class D> template <class BIS> inline counter_t 00108 Coder<T_R, D>::nextTransactionBIS( BIS& transaction ) 00109 { 00110 counter_t return_value = T_R::nextTransactionBIS(transaction); 00111 TYPENAME BIS::iterator it1,it2; 00112 it1 = it2 = transaction.begin(); 00113 while( it1 != transaction.end() ) 00114 { 00115 register item_t new_code = code[*it1]; 00116 if(new_code) 00117 { 00118 *it2 = new_code - 1; 00119 ++it2; 00120 } 00121 ++it1; 00122 } 00123 transaction.erase(it2, transaction.end()); 00124 return return_value; 00125 } 00126 00127 template <class T_R, class D> template <class UAC> inline counter_t 00128 Coder<T_R, D>::nextTransactionUAC( UAC& transaction ) 00129 { 00130 std::set<item_t> raw_transaction; 00131 counter_t return_value = T_R::nextTransactionUAC(raw_transaction); 00132 if(return_value) 00133 { 00134 00135 transaction.clear(); 00136 for( std::set<item_t>::iterator it = raw_transaction.begin(); 00137 it != raw_transaction.end(); ++it ) 00138 if( code[*it] ) transaction.insert( code[*it]-1 ); 00139 } 00140 return return_value; 00141 } 00142 00143 #endif