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/default.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'chain/src/default.rs') 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