use max length of functions

This commit is contained in:
Simon Gardling 2022-03-31 15:05:58 -04:00
parent 82f2263b1f
commit 4faf710128
3 changed files with 18 additions and 96 deletions

View File

@ -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!(

View File

@ -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<Value> = 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<Value> = 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);
}
}

View File

@ -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<HintEnum<'static>> {
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<String, Option<String>> {
let mut values: HashMap<String, Option<String>> = HashMap::new();
let processed_func: Vec<String> = [
"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<String>)> = 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<String> = 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
}
*/
}
*/