optimize SteppedVector

This commit is contained in:
Simon Gardling 2022-05-23 09:49:31 -04:00
parent a012deac4e
commit de405d168a
4 changed files with 23 additions and 35 deletions

View File

@ -18,10 +18,12 @@ pub struct TextDataRaw {
pub welcome: String, pub welcome: String,
} }
pub const FONT_SIZE: f32 = 14.0;
// ui.fonts().crate::data::FONT_SIZE(&egui::FontSelection::default().resolve(ui.style()));
impl TextDataRaw { impl TextDataRaw {
#[allow(dead_code)] #[allow(dead_code)]
fn into_rich(self) -> TextData { fn into_rich(self) -> TextData {
const SIZE: f32 = 14.0;
use egui::RichText; use egui::RichText;
TextData { TextData {
help_expr: RichText::from(self.help_expr), help_expr: RichText::from(self.help_expr),
@ -29,7 +31,7 @@ impl TextDataRaw {
help_panel: RichText::from(self.help_panel), help_panel: RichText::from(self.help_panel),
help_function: RichText::from(self.help_function), help_function: RichText::from(self.help_function),
help_other: RichText::from(self.help_other), 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),
} }
} }
} }

View File

@ -77,13 +77,9 @@ impl FunctionManager {
// ui.label("Functions:"); // ui.label("Functions:");
let can_remove = self.functions.len() > 1; 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 available_width = ui.available_width();
let mut remove_i: Option<usize> = None; let mut remove_i: Option<usize> = 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() { for (i, (te_id, function)) in self.functions.iter_mut().enumerate() {
let mut new_string = function.autocomplete.string.clone(); let mut new_string = function.autocomplete.string.clone();
function.update_string(&new_string); function.update_string(&new_string);
@ -181,7 +177,7 @@ impl FunctionManager {
ui, ui,
format!("buttons_area_{}", i), format!("buttons_area_{}", i),
&re, &re,
ROW_HEIGHT * BUTTONS_Y_OFFSET, crate::data::FONT_SIZE * BUTTONS_Y_OFFSET,
|ui| { |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
// There's more than 1 function! Functions can now be deleted // There's more than 1 function! Functions can now be deleted

View File

@ -380,7 +380,7 @@ impl MathApp {
self.functions.display_entries(ui); self.functions.display_entries(ui);
// Only render if there's enough space // 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| { ui.with_layout(Layout::bottom_up(Align::Min), |ui| {
// Contents put in reverse order from bottom to top due to the 'buttom_up' layout // Contents put in reverse order from bottom to top due to the 'buttom_up' layout

View File

@ -1,4 +1,4 @@
use std::intrinsics::assume; use std::{intrinsics::assume, ops::RangeInclusive};
use egui::plot::{Line, Points, Value, Values}; use egui::plot::{Line, Points, Value, Values};
use itertools::Itertools; 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 /// Since all entries in `data` are evenly spaced, this field stores the step between 2 adjacent elements
step: f64, step: f64,
range: RangeInclusive<f64>,
} }
impl<'a> SteppedVector<'a> { impl<'a> SteppedVector<'a> {
@ -61,24 +63,18 @@ impl<'a> SteppedVector<'a> {
assume(self.data.len() >= 2); assume(self.data.len() >= 2);
} }
let max = self.get_max(); if !self.range.contains(&x) {
if &x > max {
return None; return None;
} }
let min = self.get_min(); if &x == self.get_min() {
if min > &x {
return None;
}
if &x == min {
return Some(0); return Some(0);
} else if &x == max { } else if &x == self.get_max() {
return Some(self.data.len() - 1); return Some(self.data.len() - 1);
} }
// Do some math in order to calculate the expected index value // 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) // 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) { if self.data.get(possible_i) == Some(&x) {
@ -91,22 +87,12 @@ impl<'a> SteppedVector<'a> {
} }
#[inline] #[inline]
pub const fn get_min(&self) -> &f64 { #[allow(dead_code)]
debug_assert!(self.data.len() >= 2); pub const fn get_min(&self) -> &f64 { self.range.start() }
unsafe {
assume(!self.data.is_empty());
self.data.get_unchecked(0)
}
}
#[inline] #[inline]
pub const fn get_max(&self) -> &f64 { #[allow(dead_code)]
debug_assert!(self.data.len() >= 2); pub const fn get_max(&self) -> &f64 { self.range.end() }
unsafe {
assume(!self.data.is_empty());
self.data.last().unwrap_unchecked()
}
}
#[allow(dead_code)] #[allow(dead_code)]
pub fn get_data(&self) -> &'a [f64] { self.data } 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()); debug_assert!(step.is_finite());
// Create and return the struct // Create and return the struct
SteppedVector { data, step } SteppedVector {
data,
step,
range: min..=max,
}
} }
} }