00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "Trie.hpp"
00011 #include <cstdlib>
00012 #include <iostream>
00013 #include <algorithm>
00014
00015 bool Edge_point_less(const Edge edge, const itemtype label)
00016 {
00017 return edge.label < label;
00018 };
00019
00024 Trie::Trie(Trie* parent_trie,
00025 const unsigned long init_counter):
00026 parent(parent_trie),
00027 counter(init_counter),
00028 maxpath(0)
00029 {
00030 }
00031
00038 const Trie* Trie::is_included( const set<itemtype>& an_itemset,
00039 set<itemtype>::const_iterator item_it ) const
00040 {
00041 if( item_it == an_itemset.end() ) return this;
00042 else
00043 {
00044 vector<Edge>::const_iterator it_edgevector =
00045 lower_bound(edgevector.begin(), edgevector.end(), *item_it, Edge_point_less);
00046 if( it_edgevector != edgevector.end() && (*it_edgevector).label == *item_it)
00047 return (*it_edgevector).subtrie->is_included( an_itemset, ++item_it );
00048 else return NULL;
00049 }
00050 }
00051
00058 void Trie::find_candidate( const vector<itemtype>& basket, const itemtype distance_from_candidate,
00059 vector<itemtype>::const_iterator it_basket, const unsigned long counter_incr)
00060 {
00061 if( distance_from_candidate )
00062 {
00063 if (distance_from_candidate < (itemtype)(basket.end() - it_basket) + 1)
00064 {
00065 vector<itemtype>::const_iterator it_basket_upper_bound = basket.end() - distance_from_candidate + 1;
00066 vector<Edge>::iterator it_edge = edgevector.begin();
00067
00068 while( it_edge != edgevector.end() && it_basket != it_basket_upper_bound )
00069 {
00070 if( (*it_edge).label < *it_basket) it_edge++;
00071 else if( (*it_edge).label > *it_basket) it_basket++;
00072 else
00073 {
00074 if( (*it_edge).subtrie->maxpath + 1 == distance_from_candidate )
00075 (*it_edge).subtrie->find_candidate( basket, distance_from_candidate - 1, it_basket + 1, counter_incr);
00076 it_edge++;
00077 it_basket++;
00078 }
00079 }
00080 }
00081 }
00082 else counter += counter_incr;
00083 }
00084
00089 void Trie::delete_infrequent( const double min_occurrence, const itemtype distance_from_candidate_parent )
00090 {
00091 vector<Edge>::iterator itEdge = edgevector.begin();
00092 if( distance_from_candidate_parent )
00093 {
00094 for( ;itEdge != edgevector.end(); itEdge++ )
00095 if( (*itEdge).subtrie->maxpath == distance_from_candidate_parent )
00096 (*itEdge).subtrie->delete_infrequent( min_occurrence, distance_from_candidate_parent - 1 );
00097 }
00098 else
00099 {
00100 while ( itEdge != edgevector.end() )
00101 {
00102 if( (*itEdge).subtrie->counter < min_occurrence )
00103 {
00104 delete (*itEdge).subtrie;
00105 itEdge = edgevector.erase(itEdge);
00106 }
00107 else itEdge++;
00108 }
00109 if( edgevector.empty() )
00110 {
00111 maxpath = 0;
00112 parent->max_path_set();
00113 }
00114 }
00115 }
00116
00117 void Trie::show_content_preorder( ) const
00118 {
00119 cout<<"\ncounter: "<<counter<<" maxpath: "<<maxpath;
00120 for( vector<Edge>::const_iterator itEdge = edgevector.begin(); itEdge != edgevector.end(); itEdge++ )
00121 {
00122 cout<<"\nitem"<<(*itEdge).label<<" leads to the Trie, where";
00123 (*itEdge).subtrie->show_content_preorder();
00124 }
00125 cout<<"\nNo more edges. Let's go back to the parent!";
00126 }
00127
00128 Trie::~Trie()
00129 {
00130 for( vector<Edge>::iterator itEdge = edgevector.begin(); itEdge != edgevector.end(); itEdge++ )
00131 delete (*itEdge).subtrie;
00132 }
00133
00134 void Trie::max_path_set( )
00135 {
00136 itemtype temp_max_path = 0;
00137 for( vector<Edge>::iterator itEdge = edgevector.begin(); itEdge != edgevector.end(); itEdge++ )
00138 if( temp_max_path < (*itEdge).subtrie->maxpath ) temp_max_path=(*itEdge).subtrie->maxpath;
00139 if( maxpath != temp_max_path + 1)
00140 {
00141 maxpath = temp_max_path + 1;
00142 if( parent!=NULL ) parent->max_path_set( );
00143 }
00144 }
00145
00150 void Trie::add_empty_state( const itemtype item, const unsigned long counter )
00151 {
00152 Edge temp_edge;
00153 temp_edge.label = item;
00154 temp_edge.subtrie = new Trie( this, counter );
00155 edgevector.push_back(temp_edge);
00156 }