From 973c789dae479dd8383b0f7f9cfa5f167fdf3d38 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Fri, 20 Jan 2023 14:47:06 +0800 Subject: minor refactoring It seems the performance is indeed linear for a simple grammar. This is such a historical moment, for me, that I think it deserves a separate commit, haha. --- chain/src/atom/default.rs | 14 ++++++++------ chain/src/default.rs | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/chain/src/atom/default.rs b/chain/src/atom/default.rs index 0dc24c3..14b7a9f 100644 --- a/chain/src/atom/default.rs +++ b/chain/src/atom/default.rs @@ -404,8 +404,9 @@ impl DefaultAtom { .map(|(label, target_iter)| (*label, target_iter)) .collect(); - let mut terminals_map: HashMap, usize)>, bool)> = - HashMap::new(); + type TerminalsValue = (HashSet<(LabelType, usize)>, bool); + + let mut terminals_map: HashMap = HashMap::new(); for (label, children_iter) in children.into_iter() { if let Some(TNT::Ter(t)) = *label.get_value() { @@ -422,10 +423,11 @@ impl DefaultAtom { let mut accepting = false; for child in children_iter { - accepting = accepting - || *accepting_vec.get(child).ok_or_else(|| { - GrammarError::IndexOutOfBounds(child, accepting_vec.len()) - })?; + accepting = + accepting + || *accepting_vec.get(child).ok_or( + GrammarError::IndexOutOfBounds(child, accepting_vec.len()), + )?; if nt == 3 && nfa.degree(child).map_err(index_out_of_bounds_conversion)? == 0 diff --git a/chain/src/default.rs b/chain/src/default.rs index 697b997..b9d7fe6 100644 --- a/chain/src/default.rs +++ b/chain/src/default.rs @@ -432,7 +432,7 @@ impl Chain for DefaultChain { type DerIter = DerIter; - fn derive(&mut self, t: usize, pos: usize) -> Result { + fn derive(&mut self, t: usize, _pos: usize) -> Result { use TNT::*; /// Convert an error telling us that an index is out of bounds. @@ -518,7 +518,7 @@ impl Chain for DefaultChain { let parent = Parent::new(0, 0); - for atom_child in atom_child_iter.clone() { + for atom_child in atom_child_iter { let atom_child_accepting = chain.atom.is_accepting(atom_child).unwrap(); let atom_child_empty_node = chain.atom.is_empty_node(atom_child).unwrap(); @@ -763,6 +763,7 @@ mod test_chain { for arg in std::env::args() { let parse_as_digit: Result = arg.parse(); + // just use the first number in the arguments if let Ok(parse_result) = parse_as_digit { result = parse_result; @@ -798,11 +799,36 @@ mod test_chain { dbg!(elapsed); dbg!(chain.current()); - println!("index: terminal, history"); + assert_eq!(input.len(), chain.history().len()); + + if std::fs::metadata("output/history").is_ok() { + std::fs::remove_file("output/history")?; + } + + let mut history_file = std::fs::OpenOptions::new() + .create(true) + .write(true) + .open("output/history")?; + + use std::fmt::Write; + use std::io::Write as IOWrite; + + let mut log_string = String::new(); + + writeln!(&mut log_string, "index: terminal, history")?; + for (index, t) in input.iter().copied().enumerate().take(input.len() - 1) { - println!("{index}: {t}, {}", chain.history().get(index).unwrap()); + writeln!( + &mut log_string, + "{index}: {t}, {}", + chain.history().get(index).unwrap() + )?; } + println!("Successfully logged to output/history"); + + history_file.write_all(log_string.as_bytes())?; + #[cfg(feature = "test-print-viz")] { chain.graph.print_viz("chain.gv")?; -- cgit v1.2.3-18-g5258