use backshift::{ArimaSpec, Filter};
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
struct Counting;
static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCATIONS.fetch_add(1, Ordering::SeqCst);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
fn main() {
let history = [0.3, -0.4, 1.1, 0.2, -0.9, 0.5, 0.0, 0.7];
let mut mean = [0.0; 6];
let mut variance = [0.0; 6];
let before = ALLOCATIONS.load(Ordering::SeqCst);
let spec = ArimaSpec {
ar: &[0.5, -0.2],
d: 1,
ma: &[0.4],
mean: 0.1,
sigma2: 1.3,
};
let mut filter = Filter::<4>::new(&spec).expect("stationary model");
filter.warm_up(&history);
for y in [0.1, -0.2, f64::NAN, 0.4] {
std::hint::black_box(filter.update(y));
}
filter.forecast_into(&mut mean, &mut variance);
let (mut lower, mut upper) = ([0.0; 6], [0.0; 6]);
filter.forecast_interval_into(0.95, &mut mean, &mut lower, &mut upper);
let after = ALLOCATIONS.load(Ordering::SeqCst);
std::hint::black_box((&mean, &variance, &lower, &upper));
assert_eq!(
after - before,
0,
"the device path allocated {} times",
after - before
);
assert!(variance.iter().all(|v| v.is_finite() && *v > 0.0));
println!("no_alloc: ok, 0 allocations");
}