improvements
General code improvements and optimizations.
This commit is contained in:
@@ -143,6 +143,8 @@ impl FunctionEntry {
|
||||
settings_opened: false,
|
||||
};
|
||||
|
||||
pub const fn is_some(&self) -> bool { !self.function.is_none() }
|
||||
|
||||
pub fn settings_window(&mut self, ctx: &Context) {
|
||||
let mut invalidate_nth = false;
|
||||
egui::Window::new(format!("Settings: {}", self.raw_func_str))
|
||||
@@ -310,34 +312,37 @@ impl FunctionEntry {
|
||||
.as_slice()
|
||||
.into();
|
||||
|
||||
let back_data: Vec<Value> = dyn_iter(&resolution_iter)
|
||||
.map(|x| {
|
||||
if let Some(i) = x_data.get_index(x) {
|
||||
self.back_data[i]
|
||||
} else {
|
||||
Value::new(*x, self.function.get(*x))
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let (back_data, derivative_data_1): (Vec<Value>, Vec<Option<Value>>) =
|
||||
dyn_iter(&resolution_iter)
|
||||
.map(|x| {
|
||||
if let Some(i) = x_data.get_index(x) {
|
||||
(
|
||||
self.back_data[i],
|
||||
derivative_required.then(|| self.derivative_data[i]),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
Value::new(*x, self.function.get(*x)),
|
||||
derivative_required
|
||||
.then(|| Value::new(*x, self.function.get_derivative_1(*x))),
|
||||
)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<(Value, Option<Value>)>>()
|
||||
.into_iter()
|
||||
.unzip();
|
||||
|
||||
debug_assert_eq!(back_data.len(), settings.plot_width + 1);
|
||||
debug_assert_eq!(derivative_data_1.len(), settings.plot_width + 1);
|
||||
|
||||
self.back_data = back_data;
|
||||
|
||||
if derivative_required {
|
||||
let new_derivative_data: Vec<Value> = dyn_iter(&resolution_iter)
|
||||
.map(|x| {
|
||||
if let Some(i) = x_data.get_index(x) {
|
||||
self.derivative_data[i]
|
||||
} else {
|
||||
Value::new(*x, self.function.get_derivative_1(*x))
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
debug_assert_eq!(new_derivative_data.len(), settings.plot_width + 1);
|
||||
|
||||
self.derivative_data = new_derivative_data;
|
||||
debug_assert!(derivative_data_1[0].is_some());
|
||||
self.derivative_data = derivative_data_1
|
||||
.into_iter()
|
||||
.map(|ele| unsafe { ele.unwrap_unchecked() })
|
||||
.collect::<Vec<Value>>();
|
||||
} else {
|
||||
self.invalidate_derivative();
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ impl FunctionManager {
|
||||
|
||||
// Toggle integral being enabled or not
|
||||
function.integral.bitxor_assign(
|
||||
ui.add(button_area_button("∫"))
|
||||
ui.add_enabled(function.is_some(), button_area_button("∫"))
|
||||
.on_hover_text(match function.integral {
|
||||
true => "Don't integrate",
|
||||
false => "Integrate",
|
||||
@@ -206,7 +206,7 @@ impl FunctionManager {
|
||||
|
||||
// Toggle showing the derivative (even though it's already calculated this option just toggles if it's displayed or not)
|
||||
function.derivative.bitxor_assign(
|
||||
ui.add(button_area_button("d/dx"))
|
||||
ui.add_enabled(function.is_some(), button_area_button("d/dx"))
|
||||
.on_hover_text(match function.derivative {
|
||||
true => "Don't Differentiate",
|
||||
false => "Differentiate",
|
||||
@@ -215,7 +215,7 @@ impl FunctionManager {
|
||||
);
|
||||
|
||||
function.settings_opened.bitxor_assign(
|
||||
ui.add(button_area_button("⚙"))
|
||||
ui.add_enabled(function.is_some(), button_area_button("⚙"))
|
||||
.on_hover_text(match function.settings_opened {
|
||||
true => "Close Settings",
|
||||
false => "Open Settings",
|
||||
|
||||
@@ -148,26 +148,23 @@ impl MathApp {
|
||||
|
||||
const DATA_NAME: &str = "YTBN-DECOMPRESSED";
|
||||
fn get_storage_decompressed() -> Option<Vec<u8>> {
|
||||
if let Ok(Some(data)) = get_localstorage().get_item(DATA_NAME) {
|
||||
let (commit, cached_data) = crate::misc::hashed_storage_read(data);
|
||||
let data = get_localstorage().get_item(DATA_NAME).ok()??;
|
||||
let (commit, cached_data) = crate::misc::hashed_storage_read(data);
|
||||
|
||||
if commit == build::SHORT_COMMIT {
|
||||
tracing::info!("Reading decompression cache. Bytes: {}, or: {}", cached_data.len(), crate::misc::format_bytes(cached_data.len()));
|
||||
return Some(cached_data.to_vec());
|
||||
} else {
|
||||
tracing::info!("Decompression cache are invalid due to differing commits (build: {}, previous: {})", build::SHORT_COMMIT, commit);
|
||||
|
||||
// is invalid
|
||||
None
|
||||
}
|
||||
if commit == build::SHORT_COMMIT {
|
||||
tracing::info!("Reading decompression cache. Bytes: {}, or: {}", cached_data.len(), crate::misc::format_bytes(cached_data.len()));
|
||||
return Some(cached_data.to_vec());
|
||||
} else {
|
||||
tracing::info!("Decompression cache are invalid (build: {}, previous: {})", build::SHORT_COMMIT, commit);
|
||||
|
||||
// is invalid
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn set_storage_decompressed(data: &Vec<u8>) {
|
||||
fn set_storage_decompressed(data: &[u8]) {
|
||||
tracing::info!("Setting decompression cache");
|
||||
let saved_data = &crate::misc::hashed_storage_create(&build::SHORT_COMMIT.chars().map(|c| c as u8).collect::<Vec<u8>>(), data.as_slice());
|
||||
let saved_data = &crate::misc::hashed_storage_create(&build::SHORT_COMMIT.as_bytes(), data);
|
||||
tracing::info!("Bytes: {}, or: {}", saved_data.len(), crate::misc::format_bytes(data.len()));
|
||||
get_localstorage().set_item(DATA_NAME, saved_data).expect("failed to set local storage cache");
|
||||
}
|
||||
@@ -610,7 +607,7 @@ impl App for MathApp {
|
||||
.collect::<Vec<u8>>(),
|
||||
bincode::serialize(&self.functions).unwrap().as_slice(),
|
||||
);
|
||||
tracing::info!("Bytes: {}", saved_data.len());
|
||||
// tracing::info!("Bytes: {}", saved_data.len());
|
||||
local_storage
|
||||
.set_item("YTBN-FUNCTIONS", saved_data)
|
||||
.expect("failed to set local function storage");
|
||||
|
||||
20
src/misc.rs
20
src/misc.rs
@@ -298,15 +298,29 @@ pub fn hashed_storage_create(hash: &[u8], data: &[u8]) -> String {
|
||||
debug_assert_eq!(hash.len(), HASH_LENGTH);
|
||||
debug_assert!(data.len() > 0);
|
||||
|
||||
unsafe {
|
||||
assume(data.len() > 0);
|
||||
assume(!data.is_empty());
|
||||
assume(hash.len() == HASH_LENGTH);
|
||||
}
|
||||
|
||||
let new_data = [hash, data].concat();
|
||||
debug_assert_eq!(new_data.len(), data.len() + hash.len());
|
||||
base64::encode(new_data)
|
||||
|
||||
// cannot use `from_utf8` seems to break on wasm. no clue why
|
||||
new_data.iter().map(|b| *b as char).collect::<String>()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn hashed_storage_read(data: String) -> (String, Vec<u8>) {
|
||||
let decoded_1 = base64::decode(data).expect("unable to read data");
|
||||
debug_assert!(decoded_1.len() > HASH_LENGTH);
|
||||
debug_assert!(data.len() > HASH_LENGTH);
|
||||
unsafe {
|
||||
assume(!data.is_empty());
|
||||
assume(data.len() > 0);
|
||||
}
|
||||
|
||||
// can't use data.as_bytes() here for some reason, seems to break on wasm?
|
||||
let decoded_1 = data.chars().map(|c| c as u8).collect::<Vec<u8>>();
|
||||
|
||||
let (hash, cached_data) = decoded_1.split_at(8);
|
||||
debug_assert_eq!(hash.len(), HASH_LENGTH);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::hash::Hash;
|
||||
|
||||
pub fn widgets_ontop<R>(
|
||||
ui: &egui::Ui, id: String, re: &egui::Response, y_offset: f32,
|
||||
ui: &egui::Ui, id: impl Hash, re: &egui::Response, y_offset: f32,
|
||||
add_contents: impl FnOnce(&mut egui::Ui) -> R,
|
||||
) -> R {
|
||||
let area = egui::Area::new(id)
|
||||
|
||||
Reference in New Issue
Block a user