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
00028 const Trie* Trie::is_included( const set<itemtype>& an_itemset,
00029 set<itemtype>::const_iterator item_it ) const
00030 {
00031 if( item_it == an_itemset.end() ) return this;
00032 else
00033 {
00034 vector<Edge>::const_iterator it_edgevector =
00035 lower_bound(edgevector.begin(), edgevector.end(),
00036 *item_it, Edge_point_less);
00037 if( it_edgevector != edgevector.end() &&
00038 (*it_edgevector).label == *item_it )
00039 return (*it_edgevector).subtrie->is_included( an_itemset, ++item_it );
00040 else return NULL;
00041 }
00042 }
00043
00051 void Trie::find_candidate(
00052 vector<itemtype>::const_iterator it_basket_upper_bound,
00053 vector<itemtype>::const_iterator it_basket,
00054 const countertype counter_incr )
00055 {
00056 if( edgevector.empty() )
00057 counter += counter_incr;
00058 else
00059 {
00060
00061 vector<Edge>::iterator it_edge = edgevector.begin();
00062 while( it_edge != edgevector.end() &&
00063 it_basket != it_basket_upper_bound )
00064 {
00065 if( (*it_edge).label < *it_basket) ++it_edge;
00066 else if( (*it_edge).label > *it_basket) ++it_basket;
00067 else
00068 {
00069 (*it_edge).subtrie->find_candidate( it_basket_upper_bound + 1,
00070 it_basket + 1,
00071 counter_incr);
00072 ++it_edge;
00073 ++it_basket;
00074 }
00075 }
00076 }
00077 }
00078
00082 void Trie::delete_infrequent( const double min_occurrence )
00083 {
00084 vector<Edge>::iterator itEdge = edgevector.begin();
00085 while(itEdge != edgevector.end() )
00086 {
00087 if( (*itEdge).subtrie->edgevector.empty() )
00088 {
00089 if( (*itEdge).subtrie->counter < min_occurrence )
00090 {
00091 delete (*itEdge).subtrie;
00092 itEdge = edgevector.erase(itEdge);
00093 }
00094 else ++itEdge;
00095 }
00096 else
00097 {
00098 (*itEdge).subtrie->delete_infrequent( min_occurrence );
00099
00100 if( (*itEdge).subtrie->edgevector.empty() )
00101 {
00102 delete (*itEdge).subtrie;
00103 itEdge = edgevector.erase(itEdge);
00104 }
00105 else ++itEdge;
00106 }
00107 }
00108 }
00109
00110
00111 Trie::~Trie()
00112 {
00113 for( vector<Edge>::iterator itEdge = edgevector.begin();
00114 itEdge != edgevector.end(); ++itEdge )
00115 delete (*itEdge).subtrie;
00116 }
00117
00118
00123 void Trie::add_empty_state( const itemtype item, const countertype counter )
00124 {
00125 Edge temp_edge;
00126 temp_edge.label = item;
00127 temp_edge.subtrie = new Trie( counter );
00128 edgevector.push_back(temp_edge);
00129 }