big changes
This commit is contained in:
51
src/misc.rs
51
src/misc.rs
@@ -135,6 +135,57 @@ pub fn test_func(function_string: String) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
pub struct SteppedVector {
|
||||
data: Vec<f64>, // Assumes data is sorted from min to maximum
|
||||
min: f64,
|
||||
max: f64,
|
||||
step: f64,
|
||||
}
|
||||
|
||||
impl SteppedVector {
|
||||
pub fn get_index(&self, x: f64) -> Option<usize> {
|
||||
if (x > self.max) | (self.min > x) {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Should work....
|
||||
let possible_i = ((x + self.min) / self.step) as usize;
|
||||
if self.data[possible_i] == x {
|
||||
return Some(possible_i);
|
||||
} else if (x >= self.min) && (self.max >= x) {
|
||||
panic!("possible_i did not result in a possible value, but x is in range");
|
||||
// Panic in case (no clue how this would happen). I may remove this check later.
|
||||
}
|
||||
|
||||
// Not really needed as the above code should handle everything
|
||||
/*
|
||||
for (i, ele) in self.data.iter().enumerate() {
|
||||
if ele > &x {
|
||||
return None;
|
||||
} else if &x == ele {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
*/
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<f64>> for SteppedVector {
|
||||
// Note: input `data` is assumed to be sorted from min to max
|
||||
fn from(data: Vec<f64>) -> SteppedVector {
|
||||
let max = data[0];
|
||||
let min = data[data.len() - 1];
|
||||
let step = (max - min).abs() / ((data.len() - 1) as f64);
|
||||
SteppedVector {
|
||||
data,
|
||||
min,
|
||||
max,
|
||||
step,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rounds f64 to specific number of digits
|
||||
pub fn digits_precision(x: f64, digits: usize) -> f64 {
|
||||
let large_number: f64 = 10.0_f64.powf(digits as f64);
|
||||
|
||||
Reference in New Issue
Block a user