From e0a7c61baa6f524dac5a957f3610a625fe2e089b Mon Sep 17 00:00:00 2001 From: elvis Date: Thu, 3 Jul 2025 11:09:58 +0200 Subject: [PATCH] Fixing bugs Bug in parsing single set in .experiment and bug in printing frequency --- src/main.rs | 2 +- src/rsprocess/grammar.lalrpop | 24 ++++++++++----------- src/rsprocess/translator.rs | 40 +++++++++++++++++++++-------------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index f934a3b..f7cebaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ fn main() -> std::io::Result<()> { // examples::run()?; - examples::limit_freq()?; + // examples::limit_freq()?; examples::fast_freq()?; diff --git a/src/rsprocess/grammar.lalrpop b/src/rsprocess/grammar.lalrpop index 15e13d5..c2807c9 100644 --- a/src/rsprocess/grammar.lalrpop +++ b/src/rsprocess/grammar.lalrpop @@ -37,6 +37,11 @@ Separated: Vec = { } }; +Separated_Or: Vec = { + => vec![v], + > => v +} + // ----------------------------------------------------------------------------- // SetParser // ----------------------------------------------------------------------------- @@ -46,8 +51,7 @@ pub Set: RSset = { Set_of_entities: RSset = { "{" "}" => RSset::from(vec![]), - "{" "}" => RSset::from(vec![translator.encode(t)]), - "{" > "}" => + "{" > "}" => RSset::from(t.into_iter().map(|t| translator.encode(t)).collect::>()) }; @@ -58,8 +62,7 @@ Set_of_entities: RSset = { pub Reactions: Vec = { "(" ")" => vec![], - "(" ")" => vec![r], - "(" > ")" => s + "(" > ")" => s } Reaction: RSreaction = { @@ -73,9 +76,7 @@ Reaction: RSreaction = { // ----------------------------------------------------------------------------- pub Context: RSprocess = { "[" "]" => RSprocess::NondeterministicChoice{ children: vec![] }, - "[" "]" => - RSprocess::NondeterministicChoice{children: vec![Rc::new(t)]}, - "[" > "]" => + "[" > "]" => RSprocess::NondeterministicChoice{ children: t } }; @@ -107,8 +108,7 @@ CTX_process: RSprocess = { // ----------------------------------------------------------------------------- pub Environment: Box = { "[" "]" => Box::new(RSenvironment::new()), - "[" "]" => Box::new(RSenvironment::from(vec![t])), - "[" > "]" => Box::new(RSenvironment::from(t)) + "[" > "]" => Box::new(RSenvironment::from(t)) }; Env_term: (IdType, RSprocess) = { @@ -181,7 +181,7 @@ pub System: RSsystem = { // entities of equal length. pub Experiment: (Vec, Vec) = { - "Weights:" > - "Sets:" > - => (w.into_iter().map(|x| x as u32).collect::>(), s) + "Weights:" > + "Sets:" > + => (w.into_iter().map(|x| x as u32).collect::>(), s), } diff --git a/src/rsprocess/translator.rs b/src/rsprocess/translator.rs index 7faabfd..3f86d34 100644 --- a/src/rsprocess/translator.rs +++ b/src/rsprocess/translator.rs @@ -1,5 +1,7 @@ // translate and keeps track of strings -use std::collections::HashMap; +use std::{cmp::max, collections::HashMap}; + +static PRECISION: &usize = &2; pub type IdType = u32; @@ -403,30 +405,36 @@ fn print_frequency( write!(f, "[")?; let mut freq_it = frequency.frequency_map.iter().peekable(); + let totals = &frequency.totals; + let weights = &frequency.weights; + while let Some((e, freq)) = freq_it.next() { write!(f, "{} -> ", translator.decode(*e).unwrap_or("Missing".into()))?; - let mut iter = freq - .iter() - .zip(frequency.totals.iter().zip(frequency.weights.iter())) - .peekable(); - let mut total_freq = 0.; - while let Some((freq_e, (total, weight))) = iter.next() { - let weighted_freq = (*freq_e as f32 * *weight as f32 * 100.) / (*total as f32); + let end = max(freq.len(), max(totals.len(), weights.len())); - if iter.peek().is_none() { - write!(f, "{weighted_freq:.2}")?; - } else { - write!(f, "{weighted_freq:.2}, ")?; - } - total_freq += weighted_freq; - } + for pos in 0..end { + let freq_e = freq.get(pos).copied().unwrap_or(0) as f32; + let weight = weights.get(pos).copied().unwrap_or(1) as f32; + let total = totals.get(pos).copied().unwrap_or(1) as f32; + + let weighted_freq = (freq_e * weight * 100.) / (total); + if pos == end-1 { + #[allow(clippy::uninlined_format_args)] + write!(f, "{weighted_freq:.*}", PRECISION)?; + } else { + #[allow(clippy::uninlined_format_args)] + write!(f, "{weighted_freq:.*}, ", PRECISION)?; + } + total_freq += weighted_freq; + } total_freq /= frequency.total_weights() as f32; - write!(f, "(total: {total_freq:.2})")?; + #[allow(clippy::uninlined_format_args)] + write!(f, " (total: {total_freq:.*})", PRECISION)?; if freq_it.peek().is_some() { writeln!(f, ",")?;