fix cache

This commit is contained in:
Simon Gardling 2022-02-28 11:31:57 -05:00
parent 0f4b235139
commit 253b070a29

View File

@ -167,31 +167,31 @@ impl Function {
let range_new: f64 = max_x.abs() + min_x.abs(); let range_new: f64 = max_x.abs() + min_x.abs();
let resolution: f64 = (self.pixel_width as f64 / range_new) as f64; let resolution: f64 = (self.pixel_width as f64 / range_new) as f64;
let movement_right = min_x > self.min_x; let back_cache = self.back_cache.as_ref().expect("");
let mut new_back: Vec<Value> = self
let x_data: Vec<f64> = self
.back_cache .back_cache
.as_ref() .as_ref()
.expect("") .expect("")
.clone() .clone()
.iter() .iter()
.filter(|ele| (ele.x >= min_x) && (min_x >= ele.x)) .map(|ele| ele.x)
.copied()
.collect(); .collect();
let x_to_go = match movement_right { let back_data: Vec<Value> = (1..=self.pixel_width)
true => ((self.max_x - max_x) * resolution) as usize, .map(|x| (x as f64 / resolution as f64) + min_x)
false => ((self.min_x - min_x) * resolution) as usize, .map(|x| {
}; let i = x_data.iter().position(|&r| r == x);
new_back.append( if i.is_some() {
&mut (1..x_to_go) back_cache[i.expect("i is None")]
.map(|x| (x as f64 / resolution as f64) + min_x) } else {
.map(|x| (x, self.run_func(x))) Value::new(x, self.run_func(x))
.map(|(x, y)| Value::new(x, y)) }
.collect(), })
); .collect();
self.back_cache = Some(new_back); self.back_cache = Some(back_data);
} else { } else {
self.back_cache = None; self.back_cache = None;
self.min_x = min_x; self.min_x = min_x;
@ -207,7 +207,7 @@ impl Function {
pub fn is_integral(&self) -> bool { self.integral } pub fn is_integral(&self) -> bool { self.integral }
pub fn run(&mut self) -> FunctionOutput { pub fn run(&mut self) -> FunctionOutput {
let front_values: Vec<Value> = match self.back_cache.is_some() { let back_values: Vec<Value> = match self.back_cache.is_some() {
true => { true => {
// debug_log("back_cache: using"); // debug_log("back_cache: using");
self.back_cache.as_ref().expect("").clone() self.back_cache.as_ref().expect("").clone()
@ -216,20 +216,20 @@ impl Function {
// debug_log("back_cache: regen"); // debug_log("back_cache: regen");
let absrange = (self.max_x - self.min_x).abs(); let absrange = (self.max_x - self.min_x).abs();
let resolution: f64 = (self.pixel_width as f64 / absrange) as f64; let resolution: f64 = (self.pixel_width as f64 / absrange) as f64;
let front_data: Vec<Value> = (1..=self.pixel_width) let back_data: Vec<Value> = (1..=self.pixel_width)
.map(|x| (x as f64 / resolution as f64) + self.min_x) .map(|x| (x as f64 / resolution as f64) + self.min_x)
.map(|x| (x, self.run_func(x))) .map(|x| (x, self.run_func(x)))
.map(|(x, y)| Value::new(x, y)) .map(|(x, y)| Value::new(x, y))
.collect(); .collect();
// println!("{} {}", front_data.len(), front_data.len() as f64/absrange); // println!("{} {}", back_data.len(), back_data.len() as f64/absrange);
self.back_cache = Some(front_data.clone()); self.back_cache = Some(back_data.clone());
front_data back_data
} }
}; };
if self.integral { if self.integral {
let back_bars: (Vec<Bar>, f64) = match self.front_cache.is_some() { let front_bars: (Vec<Bar>, f64) = match self.front_cache.is_some() {
true => { true => {
// debug_log("front_cache: using"); // debug_log("front_cache: using");
let cache = self.front_cache.as_ref().expect(""); let cache = self.front_cache.as_ref().expect("");
@ -246,9 +246,9 @@ impl Function {
output output
} }
}; };
FunctionOutput::new(front_values, Some(back_bars)) FunctionOutput::new(back_values, Some(front_bars))
} else { } else {
FunctionOutput::new(front_values, None) FunctionOutput::new(back_values, None)
} }
} }