refactor SteppedVector
refactored and optimized SteppedVector
This commit is contained in:
parent
020064a79e
commit
1c8198103e
@ -57,7 +57,7 @@ uuid = { version = "1", features = ["v4", "fast-rng", "js"] }
|
||||
bincode = "1.3"
|
||||
serde = "1"
|
||||
base64 = { git = "https://github.com/marshallpierce/rust-base64.git" }
|
||||
byte-unit = "4.0.14"
|
||||
byte-unit = { version = "4", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
benchmarks = { path = "./benchmarks" }
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
#![feature(core_intrinsics)]
|
||||
#![feature(const_default_impls)]
|
||||
#![feature(const_mut_refs)]
|
||||
|
||||
#![feature(const_for)]
|
||||
mod autocomplete;
|
||||
mod autocomplete_hashmap;
|
||||
mod parsing;
|
||||
|
||||
@ -36,7 +36,7 @@ pub enum SplitType {
|
||||
}
|
||||
|
||||
pub fn split_function_chars(chars: &[char], split: SplitType) -> Vec<String> {
|
||||
// catch some basic cases
|
||||
// Catch some basic cases
|
||||
match chars.len() {
|
||||
0 => return Vec::new(),
|
||||
1 => return vec![chars[0].to_string()],
|
||||
@ -49,10 +49,7 @@ pub fn split_function_chars(chars: &[char], split: SplitType) -> Vec<String> {
|
||||
}
|
||||
|
||||
// Resulting split-up data
|
||||
let mut data: Vec<String> = Vec::with_capacity(chars.len());
|
||||
|
||||
// Need to start out with an empty string
|
||||
data.push(String::new());
|
||||
let mut data: Vec<String> = vec![chars[0].to_string()];
|
||||
|
||||
/// Used to store info about a character
|
||||
struct BoolSlice {
|
||||
@ -85,6 +82,7 @@ pub fn split_function_chars(chars: &[char], split: SplitType) -> Vec<String> {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const fn is_variable(&self) -> bool { self.variable && !self.masked_var }
|
||||
|
||||
const fn is_number(&self) -> bool { self.number && !self.masked_num }
|
||||
@ -108,8 +106,8 @@ pub fn split_function_chars(chars: &[char], split: SplitType) -> Vec<String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn splitable(&self, c: &char, other: &BoolSlice, split: &SplitType) -> bool {
|
||||
if (c == &'*') | ((split == &SplitType::Term) && other.open_parens) {
|
||||
const fn splitable(&self, c: &char, other: &BoolSlice, split: &SplitType) -> bool {
|
||||
if (*c == '*') | (matches!(split, &SplitType::Term) && other.open_parens) {
|
||||
return true;
|
||||
} else if other.closing_parens {
|
||||
// Cases like `)x`, `)2`, and `)(`
|
||||
@ -144,7 +142,6 @@ pub fn split_function_chars(chars: &[char], split: SplitType) -> Vec<String> {
|
||||
let mut prev_char: BoolSlice = BoolSlice::from_char(&chars[0], false, false);
|
||||
|
||||
let mut last = unsafe { data.last_mut().unwrap_unchecked() };
|
||||
last.push(chars[0]);
|
||||
|
||||
// Iterate through all chars excluding the first one
|
||||
for c in chars.iter().skip(1) {
|
||||
|
||||
@ -307,6 +307,7 @@ impl FunctionEntry {
|
||||
.iter()
|
||||
.map(|ele| ele.x)
|
||||
.collect::<Vec<f64>>()
|
||||
.as_slice()
|
||||
.into();
|
||||
|
||||
let back_data: Vec<Value> = dyn_iter(&resolution_iter)
|
||||
|
||||
36
src/misc.rs
36
src/misc.rs
@ -1,3 +1,5 @@
|
||||
use std::intrinsics::assume;
|
||||
|
||||
use eframe::egui::plot::{Line, Points, Value as EguiValue, Values};
|
||||
use itertools::Itertools;
|
||||
|
||||
@ -90,7 +92,7 @@ pub struct SteppedVector {
|
||||
impl SteppedVector {
|
||||
/// Returns `Option<usize>` with index of element with value `x`. and `None` if `x` does not exist in `data`
|
||||
pub fn get_index(&self, x: &f64) -> Option<usize> {
|
||||
// if `x` is outside range, just go ahead and return `None` as it *shouldn't* be in `data`
|
||||
// If `x` is outside range, just go ahead and return `None` as it *shouldn't* be in `data`
|
||||
if (x > &self.max) | (&self.min > x) {
|
||||
return None;
|
||||
}
|
||||
@ -126,37 +128,39 @@ impl SteppedVector {
|
||||
pub fn get_data(&self) -> &Vec<f64> { &self.data }
|
||||
}
|
||||
|
||||
// Convert `Vec<f64>` into [`SteppedVector`]
|
||||
impl From<Vec<f64>> for SteppedVector {
|
||||
fn from(input_data: Vec<f64>) -> SteppedVector {
|
||||
let mut data = input_data;
|
||||
// length of data
|
||||
let data_length = data.len();
|
||||
|
||||
// Convert `&[f64]` into [`SteppedVector`]
|
||||
impl From<&[f64]> for SteppedVector {
|
||||
fn from(data: &[f64]) -> SteppedVector {
|
||||
// Ensure data is of correct length
|
||||
if data_length < 2 {
|
||||
if data.len() < 2 {
|
||||
panic!("SteppedVector: data should have a length longer than 2");
|
||||
}
|
||||
|
||||
// length of data subtracted by 1 (represents the maximum index value)
|
||||
let data_i_length = data_length - 1;
|
||||
unsafe {
|
||||
assume(data.len() > 2);
|
||||
assume(!data.is_empty());
|
||||
}
|
||||
|
||||
let mut max: f64 = data[data_i_length]; // The max value should be the first element
|
||||
let mut min: f64 = data[0]; // The minimum value should be the last element
|
||||
// length of data subtracted by 1 (represents the maximum index value)
|
||||
let max: f64 = data[data.len() - 1]; // The max value should be the first element
|
||||
let min: f64 = data[0]; // The minimum value should be the last element
|
||||
|
||||
if min > max {
|
||||
panic!("SteppedVector: min is larger than max");
|
||||
/*
|
||||
tracing::debug!("SteppedVector: min is larger than max, sorting.");
|
||||
data.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
max = data[data_i_length];
|
||||
min = data[0];
|
||||
*/
|
||||
}
|
||||
|
||||
// Calculate the step between elements
|
||||
let step = (max - min).abs() / (data_length as f64);
|
||||
let step = (max - min).abs() / (data.len() as f64);
|
||||
|
||||
// Create and return the struct
|
||||
SteppedVector {
|
||||
data,
|
||||
data: data.to_vec(),
|
||||
min,
|
||||
max,
|
||||
step,
|
||||
@ -316,7 +320,7 @@ pub fn hashed_storage_read(data: String) -> (String, Vec<u8>) {
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn format_bytes(bytes: usize) -> String {
|
||||
byte_unit::Byte::from_bytes(bytes as u128)
|
||||
byte_unit::Byte::from_bytes(bytes as u64)
|
||||
.get_appropriate_unit(false)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ fn stepped_vector() {
|
||||
let max: i32 = 1000;
|
||||
let data: Vec<f64> = (min..=max).map(|x| x as f64).collect();
|
||||
let len_data = data.len();
|
||||
let stepped_vector: SteppedVector = data.into();
|
||||
let stepped_vector: SteppedVector = data.as_slice().into();
|
||||
|
||||
assert_eq!(stepped_vector.get_min(), min as f64);
|
||||
assert_eq!(stepped_vector.get_max(), max as f64);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user