use crate::root::*;
pub type WItem<V,W> = WeightedItem<V,W>;
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub struct WeightedItem<V, W: Weight>
{
pub weight: W,
pub value: V,
}
impl<V, W: Weight> WeightedItem<V,W>
{
pub fn unit(value: V) -> Self
{
Self {
weight: W::one(),
value
}
}
pub fn new(weight: W, value: V) -> Self
{
Self { weight, value }
}
pub fn from((weight, value): (W, V)) -> Self
{
Self { weight, value }
}
}
#[macro_export]
macro_rules! wit {
($weight: expr, $value: expr) => {
WeightedItem::new($weight, $value)
};
}
impl<V, W: Weight> From<WeightedItem<V,W>> for (W, V)
{
fn from(item: WeightedItem<V,W>) -> Self {
(item.weight, item.value)
}
}
impl<V, W: Weight> Ord for WeightedItem<V,W>
where
V: Eq,
W: Ord,
{
fn cmp(&self, other: &Self) -> std::cmp::Ordering
{
self.weight.cmp(&other.weight)
}
}
impl<V, W: Weight> PartialOrd for WeightedItem<V,W>
where
V: Eq,
{
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering>
{
self.weight.partial_cmp(&other.weight)
}
}
impl<V, W: Weight> Default for WeightedItem<V,W>
where
V: Default,
W: Default,
{
fn default() -> Self {
Self { weight: W::default(), value: V::default() }
}
}
impl<V, W: Weight> std::fmt::Display for WeightedItem<V,W>
where
V: std::fmt::Display,
W: std::fmt::Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result
{
write!(f, "{{ {}, {} }}", self.weight, self.value)
}
}