use crate::prelude::*;
use alloc::boxed::Box;
pub struct Fx<'b, 'f, Y: Fitness, const DIM: usize> {
bound: &'b [[f64; 2]; DIM],
#[allow(clippy::type_complexity)]
func: Box<dyn Fn(&[f64; DIM]) -> Y + Sync + Send + 'f>,
}
impl<'b, 'f, Y: Fitness, const DIM: usize> Fx<'b, 'f, Y, DIM> {
pub fn new<F>(bound: &'b [[f64; 2]; DIM], func: F) -> Self
where
F: Fn(&[f64; DIM]) -> Y + Sync + Send + 'f,
{
Self { func: Box::new(func), bound }
}
}
impl<Y: Fitness, const DIM: usize> Bounded for Fx<'_, '_, Y, DIM> {
#[inline]
fn bound(&self) -> &[[f64; 2]] {
self.bound
}
}
impl<Y: Fitness, const DIM: usize> ObjFunc for Fx<'_, '_, Y, DIM> {
type Ys = Y;
fn fitness(&self, xs: &[f64]) -> Self::Ys {
(self.func)(xs.try_into().unwrap_or_else(|_| unreachable!()))
}
}