dyn_mut_iter

This commit is contained in:
Simon Gardling 2022-03-24 19:22:28 -04:00
parent e6b87a178e
commit 723d61d1b5
3 changed files with 43 additions and 20 deletions

View File

@ -1,5 +1,5 @@
use crate::function::{FunctionEntry, Riemann, DEFAULT_FUNCTION_ENTRY}; use crate::function::{FunctionEntry, Riemann, DEFAULT_FUNCTION_ENTRY};
use crate::misc::{option_vec_printer, JsonFileOutput, SerdeValueHelper}; use crate::misc::{dyn_mut_iter, option_vec_printer, JsonFileOutput, SerdeValueHelper};
use crate::parsing::{process_func_str, test_func}; use crate::parsing::{process_func_str, test_func};
use crate::consts::*; use crate::consts::*;
@ -15,6 +15,9 @@ use instant::Duration;
use shadow_rs::shadow; use shadow_rs::shadow;
use std::{collections::BTreeMap, io::Read, ops::BitXorAssign, str}; use std::{collections::BTreeMap, io::Read, ops::BitXorAssign, str};
#[cfg(not(target_arch = "wasm32"))]
use rayon::iter::{IndexedParallelIterator, ParallelIterator};
shadow!(build); shadow!(build);
// Constant string that has a string containing information about the build. // Constant string that has a string containing information about the build.
@ -688,23 +691,25 @@ impl epi::App for MathApp {
let minx_bounds: f64 = bounds.min()[0]; let minx_bounds: f64 = bounds.min()[0];
let maxx_bounds: f64 = bounds.max()[0]; let maxx_bounds: f64 = bounds.max()[0];
area_list = self dyn_mut_iter(&mut self.functions)
.functions // .iter_mut()
.iter_mut()
.enumerate() .enumerate()
.map(|(i, function)| { .filter(|(i, _)| !self.func_strs[*i].is_empty())
if self.func_strs[i].is_empty() { .for_each(|(_, function)| {
return None; function.calculate(
}
function.display(
plot_ui,
minx_bounds, minx_bounds,
maxx_bounds, maxx_bounds,
width_changed, width_changed,
settings_copy, settings_copy,
) )
}) });
area_list = self
.functions
.iter()
.enumerate()
.filter(|(i, _)| !self.func_strs[*i].is_empty())
.map(|(_, function)| function.display(plot_ui, settings_copy))
.collect(); .collect();
}); });
}); });

View File

@ -288,11 +288,8 @@ impl FunctionEntry {
} }
/// Calculates and displays the function on PlotUI `plot_ui` /// Calculates and displays the function on PlotUI `plot_ui`
pub fn display( pub fn display(&self, plot_ui: &mut PlotUi, settings: AppSettings) -> Option<f64> {
&mut self, plot_ui: &mut PlotUi, min_x: f64, max_x: f64, width_changed: bool, // self.calculate(min_x, max_x, width_changed, settings);
settings: AppSettings,
) -> Option<f64> {
self.calculate(min_x, max_x, width_changed, settings);
let func_str = self.get_func_str(); let func_str = self.get_func_str();
let derivative_str = self.function.get_derivative_str(); let derivative_str = self.function.get_derivative_str();

View File

@ -6,16 +6,37 @@ use serde_json::Value as JsonValue;
use rayon::prelude::*; use rayon::prelude::*;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pub fn dyn_iter<'a, T>(input: &'a Vec<T>) -> impl Iterator<Item = &'a T> { input.iter() } pub fn dyn_iter<'a, T>(input: &'a Vec<T>) -> impl Iterator<Item = &'a T>
where
&'a [T]: IntoIterator,
{
input.iter()
}
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
pub fn dyn_iter<'a, T>(input: &'a Vec<T>) -> <&'a [T] as IntoParallelIterator>::Iter pub fn dyn_iter<'a, I>(input: &'a I) -> <&'a I as IntoParallelIterator>::Iter
where where
&'a [T]: IntoParallelIterator, &'a I: IntoParallelIterator,
{ {
input.par_iter() input.par_iter()
} }
#[cfg(target_arch = "wasm32")]
pub fn dyn_mut_iter<'a, T>(input: &'a mut Vec<T>) -> impl Iterator<Item = &'a mut T>
where
&'a mut [T]: IntoIterator,
{
input.iter_mut()
}
#[cfg(not(target_arch = "wasm32"))]
pub fn dyn_mut_iter<'a, I>(input: &'a mut I) -> <&'a mut I as IntoParallelIterator>::Iter
where
&'a mut I: IntoParallelIterator,
{
input.par_iter_mut()
}
pub trait VecValueToTuple { pub trait VecValueToTuple {
fn to_tuple(&self) -> Vec<(f64, f64)>; fn to_tuple(&self) -> Vec<(f64, f64)>;
} }