Skip to content

Commit

Permalink
added method to export all nodes, tests for export node method
Browse files Browse the repository at this point in the history
  • Loading branch information
sriram98v committed Oct 2, 2023
1 parent 8197002 commit 59a8dd9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/suffix_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ where

impl<T, U> KGST<T, U>
where
T: Display + Debug + Eq + PartialEq + Hash + Clone,
U: Display + Debug + Eq + PartialEq + Hash + Clone,
T: Display + Debug + Eq + PartialEq + Hash + Clone + Serialize,
U: Display + Debug + Eq + PartialEq + Hash + Clone + Serialize,
{
/// Creates a new empty K-Truncated Generalized Suffix tree, with a constant end symbol.
///
Expand Down Expand Up @@ -423,5 +423,51 @@ where
pub fn print_tree(&self){
todo!()
}

fn export_node(&self, node_id: &usize)->(Vec<T>, HashMap<T, usize>, HashMap<U, HashSet<usize>>, usize){
let node = self.get_node(node_id);
let children: HashMap<T, usize> = node.get_children().clone();
let start = self.get_node_start(node_id);
let edge_length: usize = self.get_node_edge_length(node_id);
let node_label: Vec<T>;
if node_id==&0{
node_label = Vec::new();
}
else{
node_label = self.get_node_string(node_id)[start.clone()..start+edge_length].to_vec();
}

let data: HashMap<U, HashSet<usize>> = self.get_node_data(node_id);

return (node_label, children, data, edge_length)
}

pub fn export_all_nodes(&self)->String{
#[derive(Debug, Serialize, Deserialize)]
pub struct Enode<T, U>
where
T: Display + Debug + Eq + PartialEq + Hash + Clone,
U: Display + Debug + Eq + PartialEq + Hash + Clone,
{
node_label: Vec<T>,
children: HashMap<T, usize>,
data: HashMap<U, HashSet<usize>>,
edge_length: usize,
}

let mut out_string: String = String::new();
for node_id in self.nodes.keys(){
let exp_node_data = self.export_node(node_id);
let exp_node = Enode{
node_label: exp_node_data.0,
children: exp_node_data.1,
data: exp_node_data.2,
edge_length: exp_node_data.3,
};
out_string.push_str(&serde_json::to_string(&exp_node).unwrap());
}

return out_string;
}

}
15 changes: 15 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ fn serialize_deserialize_tree(){
assert_eq!(tree.get_nodes(), tree_2.get_nodes());
}

#[test]
fn export_all_nodes(){
let mut tree: KGST<char, String> = KGST::new('$');
let string_set: Vec<Vec<char>> = vec![
"MKAILVVLLYTFTTADADTLCIGYHANNSTDTVDTVLEKNVTVTHSVNLLENRHNGKLCKLRGVAPLHLGKCNIAGWILGNPECESLSTAGSWSYIVETSNPDNGTCYPGDFINYEELREQLSSVSSFEKFEIFPKTSSWPNHDTNRGVTAACPHDGAKSFYRNLLWLVKKEKENSYPMINKSYTNNKGKEVLVLWAIHHPATSADQQSLYQNANAYVFVGSSKYSKKFEPEIAARPKVRDQAGRMKYYWTLVEPGDKITFEATGNLVVPIYAFALKRNSGSGIIISDTSVHDCDTTCQTPNGAINTSLPFQNIHPVTIGECPKYVKSTKLRMATGLRNIPSIQSRGLFGAIAGFIEGGWTGMIDGWYGYHHQNEQGSGYAADLKSTQNAIDGITNKVNSVIEKMNTQFTAVGKEFNHLERRIENLNKKVDDGFLDIWTYNAELLVLLENERTLDYHDSNVKNLYEKVRSQLKNNAKEIGNGCFEFYHKCDDTCMESVKNGTYDYPKYSEEAKLNREEIDGVKLESTRIYQILAIYSTVASSLVLVVSLGAISFWMCSNGSLQCRICI".to_string().chars().collect(),
"MKAILVVLLYTFTTANADTLCIGYHANNSTDTVDTVLEKNVTVTHSVNLLEDKHNGKLCKLRGVAPLHLGKCNIAGWILGNPECESLSTARSWSYIVETSNSDNGTCYPGDFINYEELREQLSSVSSFERFEIFPKTSSWPNHDSNKGVTAACPHAGAKSFYKNLVWLVKKEKENSYPKLNKTYINDKGKEVLVLWGIHHPPTTADQQSLYQNADAYVFVGTSRYSKKFKPEIAKRPKVRDQEGRMNYYWTLVEPGDKITFEATGNLVVPRYAFTMERNAGSGIIISDTPVHDCNTTCQTPEGAINTSLPFQNVHPITIGKCPKYVKSTKLRLATGLRNVPSIQSRGLFGAIAGFIEGGWTGMVDGWYGYHHQNEQGSGYAADLKSTQNAIDEITNKVNSVIEKMNTQFTAVGKEFNHLEKRIENLNKKVDDGFLDIWTYNAXLLVLLENERTLDYHDSNVKNLYEKVRTQLKNNAKEIGNGCFEFYHKCDNTCMESVKNGTYDYPKYSEEAKLNREKIDGVKLESTRIYQILAIYSTVASSLVLVVSLGAISFWMCSNGSLQCRICI".to_string().chars().collect(),
"MKAIIVVVLLYTFTTANADTLCIGYHANNSTDTVDTVLEKNVTVTHSVNLLKNRHNGKLCKLRGVAPLHLGKCNIAGWLLGNPECEPLSTASSWAYIVETSNSDNGTCYPGDFINYEELTEHLSSVSSFERFEIFPKTNSWPNHDTNKGVTAACPHAGTNSFYRNGIWLVKKENIYPKKSKSYKNKKKKEVLVLWAIHHPSTSADQQSLYQNADAYVFVGSSRYSRKFEPEIATRPKVRDQAGRMNYYWTLVEPGDKITFEATGNLVAPRYAFALKRNSGSGIIISDTSVHDCDTTCQTPNGAINTSLPFQNIHPVTIGECPKYVKSTKLRMATGLRNIPSIQSRGLFGAIAGFIEGGWTGMIDGWYGYHHQNEQGSGYAADLKSTQNAIDGITNKVNSVIEKMNTQFTAVGKEFSHLERRIENLNKKVDDGFLDIWTYNAELLVLLENERTLDYHDSNVKNLYEKVRSQLKNNAKEIGNGCFEFYHKCDDMCMESVKNGTYDYPKYSEEAKLNREEIDGVKLESTRIYQILAIYSTVASSLVLVVSLGAISFWMCSNGSLQCRICI".to_string().chars().collect(),
"MKAIIVVYLLHLQTATYADTLCIGYHANNSTDTVDTVLEKNVTVTHSVNLLENRHNGKLCKLRGVAPLHLGKCNIAGWLLGNPECESLSTASSWSYIVETSNSDNGTCYPGDFINYEELREQLSSVSSFERFEIFPKTSSWPNHDTNRGVTAACPHAGTNSFYRNLVWLVKKGNSYPKINKSYINNKEKEVLVLWAIHHPSTSADQQSLYQNADAYVFVGSSRYSKKFEPEIATRPKVRDQAGRMNYYWTLVEPGDKITFEATGNLVAPRYAFALKRNSGSGIIISDTSVHDCDTTCQTPNGAINTSLPFQNIHPVTIGECPKYVKSTKLRMATGLRNIPSIQSRGLFGAIAGFIEGGWTGMIDGWYGYHHQNEQGSGYAADLKSTQNAIDGITNKVNSVIEKMNTQFTAVGKEFSHLERRIENLNKKVDDGFLDIWTYNAELLVLLENERTLDYHDSNVKNLYEKVRSQLKNNAKEIGNGCFEFYHKCDDMCMESVKNGTYDYPKYSEEAKLNREGIDGVKLESTRIYQILAIYSTVASSLVLVVSLGAISFWMCSNGSLQCRICI".to_string().chars().collect(),
];
for item_idx in 0..string_set.len(){
tree.insert(item_idx.to_string(), string_set[item_idx].clone(), &0);
}
tree.export_all_nodes();
}


#[test]
fn insert_trunc(){
Expand Down

0 comments on commit 59a8dd9

Please sign in to comment.