From 4faf71012882f72a00a57ae6d12b29693d984d38 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 31 Mar 2022 15:05:58 -0400 Subject: [PATCH] use max length of functions --- build.rs | 9 +++++ src/function.rs | 8 ++-- src/suggestions.rs | 97 +++------------------------------------------- 3 files changed, 18 insertions(+), 96 deletions(-) diff --git a/build.rs b/build.rs index 9e00bee..bb72f82 100644 --- a/build.rs +++ b/build.rs @@ -24,6 +24,13 @@ fn main() { fn generate_hashmap() { let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs"); let mut file = BufWriter::new(File::create(&path).unwrap()); + let max_len: usize = SUPPORTED_FUNCTIONS + .to_vec() + .iter() + .map(|func| func.len()) + .max() + .unwrap(); + let string_hashmap = compile_hashmap( SUPPORTED_FUNCTIONS .to_vec() @@ -45,6 +52,8 @@ fn generate_hashmap() { ) .unwrap(); writeln!(&mut file, ";").unwrap(); + + write!(&mut file, "const MAX_FUNC_LEN: usize = {};", max_len).unwrap(); } include!(concat!( diff --git a/src/function.rs b/src/function.rs index a3c3af9..c8caacd 100644 --- a/src/function.rs +++ b/src/function.rs @@ -368,7 +368,7 @@ impl FunctionEntry { } }) .collect(); - // assert_eq!(back_data.len(), settings.plot_width + 1); + debug_assert_eq!(back_data.len(), settings.plot_width + 1); self.back_data = Some(back_data); if derivative_required { @@ -383,7 +383,7 @@ impl FunctionEntry { }) .collect(); - // assert_eq!(new_derivative_data.len(), settings.plot_width + 1); + debug_assert_eq!(new_derivative_data.len(), settings.plot_width + 1); self.derivative_data = Some(new_derivative_data); } else { @@ -401,7 +401,7 @@ impl FunctionEntry { let data: Vec = dyn_iter(&resolution_iter) .map(|x| Value::new(*x, self.function.get(*x))) .collect(); - assert_eq!(data.len(), settings.plot_width + 1); + debug_assert_eq!(data.len(), settings.plot_width + 1); self.back_data = Some(data); } @@ -410,7 +410,7 @@ impl FunctionEntry { let data: Vec = dyn_iter(&resolution_iter) .map(|x| Value::new(*x, self.function.get_derivative_1(*x))) .collect(); - assert_eq!(data.len(), settings.plot_width + 1); + debug_assert_eq!(data.len(), settings.plot_width + 1); self.derivative_data = Some(data); } } diff --git a/src/suggestions.rs b/src/suggestions.rs index 8569484..bce3926 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -22,7 +22,7 @@ pub fn generate_hint(input: String) -> HintEnum<'static> { let len = chars.len(); - for i in (1..=5).rev().filter(|i| len >= *i) { + for i in (1..=MAX_FUNC_LEN).rev().filter(|i| len >= *i) { if let Some(output) = get_completion(chars_take(&chars, i)) { return output; } @@ -99,7 +99,6 @@ pub fn get_completion(key: String) -> Option> { COMPLETION_HASHMAP.get(&key).cloned() } -/* #[cfg(test)] mod tests { use std::collections::HashMap; @@ -110,99 +109,13 @@ mod tests { #[test] fn hint_test() { let values = HashMap::from([ - ("", "x^2"), - ("sin(x", ")"), - ("sin(x)", ""), - ("x^x", ""), - ("(x+1)(x-1", ")"), - ("lo", "g"), - ("log", ""), // because there are multiple log functions - ("asi", "n("), - ("asin", "("), - ("fl", "oor("), - ("ata", "n("), - ("at", "an("), - ("roun", "d("), - ("floo", "r("), - ("flo", "or("), + ("", HintEnum::Single("x^2")), + ("si", HintEnum::Many(&["gnum(", "n(", "nh("])), ]); for (key, value) in values { - println!("{} + {}", key, value); - assert_eq!(generate_hint(key).unwrap_or_default(), value.to_owned()); + println!("{} + {:?}", key, value); + assert_eq!(generate_hint(key.to_string()), value.to_owned()); } } - - /* - #[test] - fn completion_hashmap_test() { - let values = hashmap_test_gen(); - for (key, value) in values { - println!( - "{} + {}", - key, - match value.clone() { - Some(x) => x.clone(), - None => "(No completion)".to_string(), - } - ); - - assert_eq!( - get_completion(key.to_string()) - - .unwrap_or(String::new()), - value.unwrap_or(String::new()) - ); - } - } - - fn hashmap_test_gen() -> HashMap> { - let mut values: HashMap> = HashMap::new(); - - let processed_func: Vec = [ - "abs", "signum", "sin", "cos", "tan", "asin", "acos", "atan", "sinh", "cosh", "tanh", - "floor", "round", "ceil", "trunc", "fract", "exp", "sqrt", "cbrt", "ln", "log2", - "log10", - ] - .iter() - .map(|ele| ele.to_string() + "(") - .collect(); - - let mut data_tuple: Vec<(String, Option)> = Vec::new(); - for func in processed_func.iter() { - for i in 1..=func.len() { - let (first, last) = func.split_at(i); - let value = match last { - "" => None, - x => Some(x.to_string()), - }; - data_tuple.push((first.to_string(), value)); - } - } - - let key_list: Vec = data_tuple.iter().map(|(a, _)| a.clone()).collect(); - - for (key, value) in data_tuple { - if key_list.iter().filter(|a| **a == key).count() == 1 { - values.insert(key, value); - } - } - - let values_old = values.clone(); - values = values - .iter() - .filter(|(key, _)| values_old.iter().filter(|(a, _)| a == key).count() == 1) - .map(|(a, b)| (a.to_string(), b.clone())) - .collect(); - - let manual_values: Vec<(&str, Option<&str>)> = - vec![("sin", None), ("cos", None), ("tan", None)]; - - for (key, value) in manual_values { - values.insert(key.to_string(), value.map(|x| x.to_string())); - } - values - } - */ } -*/