summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-01-20 14:47:06 +0800
committerJSDurand <mmemmew@gmail.com>2023-01-20 14:47:06 +0800
commit973c789dae479dd8383b0f7f9cfa5f167fdf3d38 (patch)
tree86a307d61eee86cef29080e59a0325caa4e099fd
parent18d7955b7d84c00467ede38baae53f4ce1fb6908 (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.
-rw-r--r--chain/src/atom/default.rs14
-rw-r--r--chain/src/default.rs34
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, (HashSet<(LabelType<TNT>, usize)>, bool)> =
- HashMap::new();
+ type TerminalsValue = (HashSet<(LabelType<TNT>, usize)>, bool);
+
+ let mut terminals_map: HashMap<usize, TerminalsValue> = 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<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")?;