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
00023 Trie::Trie( const unsigned long init_counter):
00024 counter(init_counter)
00025 {
00026 }
00027
00036 const Trie* Trie::is_included( const set<itemtype>& an_itemset,
00037 set<itemtype>::const_iterator item_it ) const
00038 {
00039 if( item_it == an_itemset.end() ) return this;
00040 else
00041 {
00042 vector<Edge>::const_iterator it_edgevector =
00043 lower_bound(edgevector.begin(), edgevector.end(),
00044 *item_it, Edge_point_less);
00045 if( it_edgevector != edgevector.end() &&
00046 (*it_edgevector).label == *item_it )
00047 return (*it_edgevector).subtrie->is_included( an_itemset, ++item_it );
00048 else return NULL;
00049 }
00050 }
00051
00061 void Trie::find_candidate( vector<itemtype>::const_iterator it_basket_upper_bound,
00062 const itemtype distance_from_candidate,
00063 vector<itemtype>::const_iterator it_basket,
00064 const unsigned long counter_incr)
00065 {
00066 if( distance_from_candidate )
00067 {
00068 vector<Edge>::iterator it_edge = edgevector.begin();
00069 while( it_edge != edgevector.end()
00070 && it_basket != it_basket_upper_bound )
00071 {
00072 if( (*it_edge).label < *it_basket) it_edge++;
00073 else if( (*it_edge).label > *it_basket) it_basket++;
00074 else
00075 {
00076 (*it_edge).subtrie->find_candidate( it_basket_upper_bound + 1,
00077 distance_from_candidate - 1,
00078 it_basket + 1, counter_incr);
00079 it_edge++;
00080 it_basket++;
00081 }
00082 }
00083 }
00084 else counter += counter_incr;
00085 }
00086
00093 void Trie::delete_infrequent( const unsigned long min_occurrence,
00094 const itemtype distance_from_candidate_parent,
00095 unsigned long& nr_of_freq_itemsets)
00096 {
00097 vector<Edge>::iterator itEdge = edgevector.begin();
00098 if( distance_from_candidate_parent )
00099 {
00100 while( itEdge != edgevector.end() )
00101 {
00102 (*itEdge).subtrie->delete_infrequent( min_occurrence,
00103 distance_from_candidate_parent - 1,
00104 nr_of_freq_itemsets );
00105 if( (*itEdge).subtrie->edgevector.empty() )
00106 {
00107 delete (*itEdge).subtrie;
00108 itEdge = edgevector.erase(itEdge);
00109 }
00110 else itEdge++;
00111 }
00112 }
00113 else
00114 {
00115 while ( itEdge != edgevector.end() )
00116 {
00117 if( (*itEdge).subtrie->counter < min_occurrence )
00118 {
00119 delete (*itEdge).subtrie;
00120 itEdge = edgevector.erase(itEdge);
00121 }
00122 else
00123 {
00124 itEdge++;
00125 nr_of_freq_itemsets++;
00126 }
00127 }
00128 }
00129 }
00130
00131 Trie::~Trie()
00132 {
00133 for( vector<Edge>::iterator itEdge = edgevector.begin();
00134 itEdge != edgevector.end(); itEdge++ )
00135 delete (*itEdge).subtrie;
00136 }
00137
00138
00143 void Trie::add_empty_state( const itemtype item, const unsigned long counter )
00144 {
00145 Edge temp_edge;
00146 temp_edge.label = item;
00147 temp_edge.subtrie = new Trie( counter );
00148 edgevector.push_back(temp_edge);
00149 }