From de405d168a8372ca02425865fd280cc736db4dd1 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 23 May 2022 09:49:31 -0400 Subject: [PATCH] optimize SteppedVector --- src/data.rs | 6 ++++-- src/function_manager.rs | 8 ++------ src/math_app.rs | 2 +- src/misc.rs | 42 ++++++++++++++++------------------------- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/data.rs b/src/data.rs index 707c514..e76bdd7 100644 --- a/src/data.rs +++ b/src/data.rs @@ -18,10 +18,12 @@ pub struct TextDataRaw { pub welcome: String, } +pub const FONT_SIZE: f32 = 14.0; +// ui.fonts().crate::data::FONT_SIZE(&egui::FontSelection::default().resolve(ui.style())); + impl TextDataRaw { #[allow(dead_code)] fn into_rich(self) -> TextData { - const SIZE: f32 = 14.0; use egui::RichText; TextData { help_expr: RichText::from(self.help_expr), @@ -29,7 +31,7 @@ impl TextDataRaw { help_panel: RichText::from(self.help_panel), help_function: RichText::from(self.help_function), help_other: RichText::from(self.help_other), - welcome: RichText::from(self.welcome).size(SIZE + 1.0), + welcome: RichText::from(self.welcome).size(FONT_SIZE + 1.0), } } } diff --git a/src/function_manager.rs b/src/function_manager.rs index 04ded7f..92dba47 100644 --- a/src/function_manager.rs +++ b/src/function_manager.rs @@ -77,13 +77,9 @@ impl FunctionManager { // ui.label("Functions:"); let can_remove = self.functions.len() > 1; - // Update if font settings are ever changed - const ROW_HEIGHT: f32 = 14.0; - // ui.fonts().row_height(&egui::FontSelection::default().resolve(ui.style())); - let available_width = ui.available_width(); let mut remove_i: Option = None; - let target_size = vec2(available_width, ROW_HEIGHT); + let target_size = vec2(available_width, crate::data::FONT_SIZE); for (i, (te_id, function)) in self.functions.iter_mut().enumerate() { let mut new_string = function.autocomplete.string.clone(); function.update_string(&new_string); @@ -181,7 +177,7 @@ impl FunctionManager { ui, format!("buttons_area_{}", i), &re, - ROW_HEIGHT * BUTTONS_Y_OFFSET, + crate::data::FONT_SIZE * BUTTONS_Y_OFFSET, |ui| { ui.horizontal(|ui| { // There's more than 1 function! Functions can now be deleted diff --git a/src/math_app.rs b/src/math_app.rs index fb5b67b..fbd272a 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -380,7 +380,7 @@ impl MathApp { self.functions.display_entries(ui); // Only render if there's enough space - if ui.available_height() > 14.0 { + if ui.available_height() > crate::data::FONT_SIZE { ui.with_layout(Layout::bottom_up(Align::Min), |ui| { // Contents put in reverse order from bottom to top due to the 'buttom_up' layout diff --git a/src/misc.rs b/src/misc.rs index 38ae3d0..4770ede 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -1,4 +1,4 @@ -use std::intrinsics::assume; +use std::{intrinsics::assume, ops::RangeInclusive}; use egui::plot::{Line, Points, Value, Values}; use itertools::Itertools; @@ -44,6 +44,8 @@ pub struct SteppedVector<'a> { /// Since all entries in `data` are evenly spaced, this field stores the step between 2 adjacent elements step: f64, + + range: RangeInclusive, } impl<'a> SteppedVector<'a> { @@ -61,24 +63,18 @@ impl<'a> SteppedVector<'a> { assume(self.data.len() >= 2); } - let max = self.get_max(); - if &x > max { + if !self.range.contains(&x) { return None; } - let min = self.get_min(); - if min > &x { - return None; - } - - if &x == min { + if &x == self.get_min() { return Some(0); - } else if &x == max { + } else if &x == self.get_max() { return Some(self.data.len() - 1); } // Do some math in order to calculate the expected index value - let possible_i = ((x - min).abs() / self.step) as usize; + let possible_i = ((x - self.get_min()).abs() / self.step) as usize; // Make sure that the index is valid by checking the data returned vs the actual data (just in case) if self.data.get(possible_i) == Some(&x) { @@ -91,22 +87,12 @@ impl<'a> SteppedVector<'a> { } #[inline] - pub const fn get_min(&self) -> &f64 { - debug_assert!(self.data.len() >= 2); - unsafe { - assume(!self.data.is_empty()); - self.data.get_unchecked(0) - } - } + #[allow(dead_code)] + pub const fn get_min(&self) -> &f64 { self.range.start() } #[inline] - pub const fn get_max(&self) -> &f64 { - debug_assert!(self.data.len() >= 2); - unsafe { - assume(!self.data.is_empty()); - self.data.last().unwrap_unchecked() - } - } + #[allow(dead_code)] + pub const fn get_max(&self) -> &f64 { self.range.end() } #[allow(dead_code)] pub fn get_data(&self) -> &'a [f64] { self.data } @@ -140,7 +126,11 @@ impl<'a> From<&'a [f64]> for SteppedVector<'a> { debug_assert!(step.is_finite()); // Create and return the struct - SteppedVector { data, step } + SteppedVector { + data, + step, + range: min..=max, + } } }