diff options
author | JSDurand <mmemmew@gmail.com> | 2023-01-20 14:47:06 +0800 |
---|---|---|
committer | JSDurand <mmemmew@gmail.com> | 2023-01-20 14:47:06 +0800 |
commit | 973c789dae479dd8383b0f7f9cfa5f167fdf3d38 (patch) | |
tree | 86a307d61eee86cef29080e59a0325caa4e099fd /chain/src/default.rs | |
parent | 18d7955b7d84c00467ede38baae53f4ce1fb6908 (diff) |
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.
Diffstat (limited to 'chain/src/default.rs')
-rw-r--r-- | chain/src/default.rs | 34 |
1 files changed, 30 insertions, 4 deletions
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<Self::DerIter, Self::Error> { + fn derive(&mut self, t: usize, _pos: usize) -> Result<Self::DerIter, Self::Error> { 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<usize, _> = 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")?; |