mop-facades 0.0.3

Facades for MOP
use core::fmt::Debug;
use mop_blocks::{
    prelude::{Cstr, Obj},
    ObjDirection,
};
use mop_common::{num_traits::NumCast, TraitCfg};

#[allow(missing_debug_implementations)]
pub struct MinSoftCstrsResults<'a, S> {
    obj_direction: ObjDirection,
    soft_cstrs: Vec<&'a Cstr<S>>,
}

impl<'a, S> MinSoftCstrsResults<'a, S> {
    pub fn new(soft_cstrs: Vec<&'a Cstr<S>>) -> Self {
        let obj_direction = ObjDirection::Min;
        MinSoftCstrsResults {
            obj_direction,
            soft_cstrs,
        }
    }
}

impl<'a, OR, S> Obj<OR, S> for MinSoftCstrsResults<'a, S>
where
    OR: Debug + NumCast + TraitCfg,
    S: Copy + Debug + TraitCfg,
{
    fn obj_direction(&self) -> ObjDirection {
        self.obj_direction
    }

    fn result(&self, s: &S) -> OR {
        let sum: usize = self
            .soft_cstrs
            .iter()
            .map(|sc| sc.weight(s) * sc.multiply_weight_by())
            .sum();
        OR::from(sum).unwrap()
    }
}