pcw_fn 0.1.0

Generic piecewise function trait and impls.
Documentation

Generic piecewise functions that allow for different internal buffers.

Examples

use pcw_fn::{VecPcwFn, PcwFn};

let pcw_poly: VecPcwFn<_, &dyn Fn(i32) -> i32> = VecPcwFn::try_from_iters(
vec![5, 10, 15],
vec![
&(|x| x-10) as &dyn Fn(i32) -> i32,
&(|x| 10) as &dyn Fn(i32) -> i32,
&(|x| x*x + 5) as &dyn Fn(i32) -> i32,
&(|x| x) as &dyn Fn(i32) -> i32,
],
)
.unwrap();
assert_eq!(pcw_poly.eval(0), -10);
assert_eq!(pcw_poly.eval(12), 12*12 + 5);
assert_eq!(pcw_poly.eval(500), 500);

let pcw_const = VecPcwFn::try_from_iters(
vec![5, 10, 15],
vec!["hi", "how", "are", "you"],
)
.unwrap();
assert_eq!(pcw_const.func_at(&0), &"hi");

let f: VecPcwFn<_, String> = pcw_const.combine(VecPcwFn::global(2), |s, x| format!("{s} {x}"));
assert_eq!(f.func_at(&15), &"you 2");

TODO: Add SmallVec and StaticVec backed variants. TODO: Add normalization / removal of redundant jumps. TODO: remove is_sorted dependency once is_sorted is stabilized.