improve caching
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
use crate::misc::{add_asterisks, Cache, Function};
|
||||
use crate::misc::{add_asterisks, Function};
|
||||
|
||||
|
||||
pub enum UpdateType {
|
||||
FULL,
|
||||
FRONT,
|
||||
BACK,
|
||||
NONE,
|
||||
}
|
||||
|
||||
// Manages Chart generation and caching of values
|
||||
pub struct ChartManager {
|
||||
@@ -7,8 +15,6 @@ pub struct ChartManager {
|
||||
max_x: f64,
|
||||
num_interval: usize,
|
||||
resolution: usize,
|
||||
back_cache: Cache<Vec<(f64, f64)>>,
|
||||
front_cache: Cache<(Vec<(f64, f64)>, f64)>,
|
||||
}
|
||||
|
||||
impl ChartManager {
|
||||
@@ -21,71 +27,37 @@ impl ChartManager {
|
||||
max_x,
|
||||
num_interval,
|
||||
resolution,
|
||||
back_cache: Cache::new_empty(),
|
||||
front_cache: Cache::new_empty(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn draw(&mut self) -> (Vec<(f64, f64)>, Vec<(f64, f64)>, f64) {
|
||||
pub fn draw_back(&mut self) -> Vec<(f64, f64)> {
|
||||
let absrange = (self.max_x - self.min_x).abs();
|
||||
let data: Vec<(f64, f64)> = match self.back_cache.is_valid() {
|
||||
true => self.back_cache.get().clone(),
|
||||
false => {
|
||||
let output: Vec<(f64, f64)> = (1..=self.resolution)
|
||||
let output: Vec<(f64, f64)> = (1..=self.resolution)
|
||||
.map(|x| ((x as f64 / self.resolution as f64) * absrange) + self.min_x)
|
||||
.map(|x| (x, self.function.run(x)))
|
||||
.collect();
|
||||
self.back_cache.set(output.clone());
|
||||
output
|
||||
}
|
||||
};
|
||||
|
||||
let filtered_data: Vec<(f64, f64)> = data.iter().map(|(x, y)| (*x, *y)).collect();
|
||||
|
||||
let (rect_data, area): (Vec<(f64, f64)>, f64) = match self.front_cache.is_valid() {
|
||||
true => self.front_cache.get().clone(),
|
||||
false => {
|
||||
let step = absrange / (self.num_interval as f64);
|
||||
let output: (Vec<(f64, f64)>, f64) = self.integral_rectangles(step);
|
||||
self.front_cache.set(output.clone());
|
||||
output
|
||||
}
|
||||
};
|
||||
|
||||
(filtered_data, rect_data, area)
|
||||
output
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn draw_front(&mut self) -> (Vec<(f64, f64)>, f64) {
|
||||
self.integral_rectangles(self.get_step())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_step(&self) -> f64 { (self.max_x - self.min_x).abs() / (self.num_interval as f64) }
|
||||
|
||||
pub fn do_update_front(&self, resolution: usize, num_interval: usize) -> bool {
|
||||
(self.resolution != resolution) | (num_interval != self.num_interval)
|
||||
}
|
||||
|
||||
pub fn do_update_back(&self, func_str_new: String, min_x: f64, max_x: f64) -> bool {
|
||||
let func_str: String = add_asterisks(func_str_new);
|
||||
let update_func: bool = !self.function.str_compare(func_str);
|
||||
|
||||
update_func | (min_x != self.min_x) | (max_x != self.max_x)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn update(
|
||||
&mut self, func_str_new: String, min_x: f64, max_x: f64, num_interval: usize,
|
||||
resolution: usize,
|
||||
) -> (Vec<(f64, f64)>, Vec<(f64, f64)>, f64) {
|
||||
) -> UpdateType {
|
||||
let func_str: String = add_asterisks(func_str_new);
|
||||
let update_func: bool = !self.function.str_compare(func_str.clone());
|
||||
|
||||
let underlying_update = update_func | (min_x != self.min_x) | (max_x != self.max_x);
|
||||
|
||||
if underlying_update | (self.resolution != resolution) {
|
||||
self.back_cache.invalidate();
|
||||
}
|
||||
|
||||
if underlying_update | (num_interval != self.num_interval) {
|
||||
self.front_cache.invalidate();
|
||||
}
|
||||
let update_back = update_func | (min_x != self.min_x) | (max_x != self.max_x);
|
||||
let update_front = update_back | (self.resolution != resolution) | (num_interval != self.num_interval);
|
||||
|
||||
if update_func {
|
||||
self.function = Function::from_string(func_str);
|
||||
@@ -96,7 +68,15 @@ impl ChartManager {
|
||||
self.num_interval = num_interval;
|
||||
self.resolution = resolution;
|
||||
|
||||
self.draw()
|
||||
if update_back && update_front {
|
||||
UpdateType::FULL
|
||||
} else if update_back {
|
||||
UpdateType::BACK
|
||||
} else if update_front {
|
||||
UpdateType::FRONT
|
||||
} else {
|
||||
UpdateType::NONE
|
||||
}
|
||||
}
|
||||
|
||||
// Creates and does the math for creating all the rectangles under the graph
|
||||
|
||||
Reference in New Issue
Block a user