cleanup + .cargo/config.toml

This commit is contained in:
Simon Gardling 2022-03-01 09:08:22 -05:00
parent 985e2cfec5
commit b28b81950d
4 changed files with 30 additions and 58 deletions

2
.cargo/config.toml Normal file
View File

@ -0,0 +1,2 @@
[target.wasm32-unknown-unknown]
rustflags = ["-C", "linker-plugin-lto=yes", "-C", "linker=/usr/bin/lld", "-C", "inline-threshold=275"]

View File

@ -4,7 +4,6 @@ use crate::function::Function;
use crate::misc::{add_asterisks, digits_precision, test_func}; use crate::misc::{add_asterisks, digits_precision, test_func};
use eframe::{egui, epi}; use eframe::{egui, epi};
use egui::plot::Plot; use egui::plot::Plot;
use egui::widgets::plot::BarChart;
use egui::widgets::Button; use egui::widgets::Button;
use egui::{Color32, FontData, FontFamily, Vec2}; use egui::{Color32, FontData, FontFamily, Vec2};
use git_version::git_version; use git_version::git_version;
@ -183,9 +182,9 @@ impl epi::App for MathApp {
}); });
let integral: bool = if integral_toggle { let integral: bool = if integral_toggle {
!function.is_integral() !function.integral
} else { } else {
function.is_integral() function.integral
}; };
if !self.func_strs[i].is_empty() { if !self.func_strs[i].is_empty() {
@ -264,8 +263,8 @@ impl epi::App for MathApp {
plot_ui.line(back_values.color(Color32::RED)); plot_ui.line(back_values.color(Color32::RED));
if let Some(bars_data) = bars { if let Some(bars_data) = bars {
let (bars, area) = bars_data; let (bar_chart, area) = bars_data;
plot_ui.bar_chart(BarChart::new(bars).color(Color32::BLUE).width(step)); plot_ui.bar_chart(bar_chart.color(Color32::BLUE).width(step));
area_list.push(digits_precision(area, 8)) area_list.push(digits_precision(area, 8))
} else { } else {
area_list.push(f64::NAN); area_list.push(f64::NAN);

View File

@ -2,7 +2,7 @@
use crate::misc::debug_log; use crate::misc::debug_log;
use eframe::egui::{ use eframe::egui::{
plot::{Line, Value, Values}, plot::{BarChart, Line, Value, Values},
widgets::plot::Bar, widgets::plot::Bar,
}; };
use meval::Expr; use meval::Expr;
@ -17,7 +17,7 @@ pub struct Function {
back_cache: Option<Vec<Value>>, back_cache: Option<Vec<Value>>,
front_cache: Option<(Vec<Bar>, f64)>, front_cache: Option<(Vec<Bar>, f64)>,
integral: bool, pub(crate) integral: bool,
integral_min_x: f64, integral_min_x: f64,
integral_max_x: f64, integral_max_x: f64,
integral_num: usize, integral_num: usize,
@ -30,13 +30,12 @@ impl Function {
) -> Self { ) -> Self {
// Makes sure proper arguments are passed when integral is enabled // Makes sure proper arguments are passed when integral is enabled
if integral { if integral {
if integral_min_x.is_none() { integral_min_x
panic!("Invalid arguments: integral_min_x is None, but integral is enabled.") .expect("Invalid arguments: integral_min_x is None, but integral is enabled.");
} else if integral_max_x.is_none() { integral_max_x
panic!("Invalid arguments: integral_max_x is None, but integral is enabled.") .expect("Invalid arguments: integral_max_x is None, but integral is enabled.");
} else if integral_num.is_none() { integral_num
panic!("Invalid arguments: integral_num is None, but integral is enabled.") .expect("Invalid arguments: integral_num is None, but integral is enabled.");
}
} }
let expr: Expr = func_str.parse().unwrap(); let expr: Expr = func_str.parse().unwrap();
@ -95,14 +94,6 @@ impl Function {
// Makes sure proper arguments are passed when integral is enabled // Makes sure proper arguments are passed when integral is enabled
if integral { if integral {
if integral_min_x.is_none() {
panic!("Invalid arguments: integral_min_x is None, but integral is enabled.")
} else if integral_max_x.is_none() {
panic!("Invalid arguments: integral_max_x is None, but integral is enabled.")
} else if integral_num.is_none() {
panic!("Invalid arguments: integral_num is None, but integral is enabled.")
}
if (integral_min_x != Some(self.integral_min_x)) if (integral_min_x != Some(self.integral_min_x))
| (integral_max_x != Some(self.integral_max_x)) | (integral_max_x != Some(self.integral_max_x))
| (integral_num != Some(self.integral_num)) | (integral_num != Some(self.integral_num))
@ -152,13 +143,7 @@ impl Function {
} }
} }
pub fn get_step(&self) -> f64 { pub fn run(&mut self) -> (Line, Option<(BarChart, f64)>) {
(self.integral_min_x - self.integral_max_x).abs() / (self.integral_num as f64)
}
pub fn is_integral(&self) -> bool { self.integral }
pub fn run(&mut self) -> (Line, Option<(Vec<Bar>, f64)>) {
let back_values: Line = Line::new(Values::from_values(match self.back_cache.is_some() { let back_values: Line = Line::new(Values::from_values(match self.back_cache.is_some() {
true => { true => {
debug_log("back_cache: using"); debug_log("back_cache: using");
@ -168,25 +153,23 @@ 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 back_data: Vec<Value> = (1..=self.pixel_width) self.back_cache = Some(
(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!("{} {}", back_data.len(), back_data.len() as f64/absrange); );
self.back_cache.as_ref().unwrap().clone()
self.back_cache = Some(back_data.clone());
back_data
} }
})); }));
if self.integral { if self.integral {
let front_bars: (Vec<Bar>, f64) = match self.front_cache.is_some() { let front_bars: (BarChart, 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().unwrap(); let cache = self.front_cache.as_ref().unwrap();
let vec_bars: Vec<Bar> = cache.0.to_vec(); (BarChart::new(cache.0.clone()), cache.1)
(vec_bars, cache.1)
} }
false => { false => {
debug_log("front_cache: regen"); debug_log("front_cache: regen");
@ -195,7 +178,7 @@ impl Function {
let output = (bars, area); let output = (bars, area);
self.front_cache = Some(output.clone()); self.front_cache = Some(output.clone());
output (BarChart::new(output.0), output.1)
} }
}; };
(back_values, Some(front_bars)) (back_values, Some(front_bars))
@ -206,19 +189,13 @@ impl Function {
// Creates and does the math for creating all the rectangles under the graph // Creates and does the math for creating all the rectangles under the graph
fn integral_rectangles(&self) -> (Vec<(f64, f64)>, f64) { fn integral_rectangles(&self) -> (Vec<(f64, f64)>, f64) {
if !self.integral {
panic!("integral_rectangles called, but self.integral is false!");
}
if self.integral_min_x.is_nan() { if self.integral_min_x.is_nan() {
panic!("integral_min_x is NaN") panic!("integral_min_x is NaN")
} } else if self.integral_max_x.is_nan() {
if self.integral_max_x.is_nan() {
panic!("integral_max_x is NaN") panic!("integral_max_x is NaN")
} }
let step = self.get_step(); let step = (self.integral_min_x - self.integral_max_x).abs() / (self.integral_num as f64);
let half_step = step / 2.0; let half_step = step / 2.0;
let data2: Vec<(f64, f64)> = (0..self.integral_num) let data2: Vec<(f64, f64)> = (0..self.integral_num)

View File

@ -54,19 +54,13 @@ pub fn add_asterisks(function_in: String) -> String {
let prev_chars_len = prev_chars.len(); let prev_chars_len = prev_chars.len();
let prev_prev_char = if prev_chars_len >= 2 { let prev_prev_char = if prev_chars_len >= 2 {
match prev_chars.get(prev_chars_len - 2) { *prev_chars.get(prev_chars_len - 2).unwrap()
Some(x) => *x,
None => panic!(),
}
} else { } else {
' ' ' '
}; };
let prev_char = if prev_chars_len >= 1 { let prev_char = if prev_chars_len >= 1 {
match prev_chars.get(prev_chars_len - 1) { *prev_chars.get(prev_chars_len - 1).unwrap()
Some(x) => *x,
None => panic!(),
}
} else { } else {
' ' ' '
}; };