weighted_list/
weighted_item.rs1use std::{
2 fmt
3};
4
5use crate::root::*;
6
7
8#[derive(Debug, Clone, Eq, PartialEq, Hash)]
14pub struct WeightedItem<V, W: Weight>
15{
16 pub weight: W,
18
19 pub value: V,
21}
22
23impl<V, W: Weight> WeightedItem<V,W>
25{
26 pub fn unit(value: V) -> Self
28 {
29 Self {
30 weight: W::one(),
31 value
32 }
33 }
34
35 pub fn new(weight: W, value: V) -> Self
37 {
38 Self { weight, value }
39 }
40
41 pub fn from((weight, value): (W, V)) -> Self
43 {
44 Self { weight, value }
45 }
46}
47
48#[macro_export]
58macro_rules! wit {
59 ( $weight: expr, $value: expr ) => {
60 WeightedItem::new($weight, $value)
61 };
62}
63
64impl<V, W: Weight> From<WeightedItem<V,W>> for (W, V)
66{
67 fn from(item: WeightedItem<V,W>) -> Self {
68 (item.weight, item.value)
69 }
70}
71
72impl<V: fmt::Display, W: Weight> fmt::Display for WeightedItem<V,W>
74{
75 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
76 {
77 write!(f, "{{ {}, {} }}", self.weight, self.value)
78 }
79}
80
81impl<V: Eq, W: Weight + Ord> Ord for WeightedItem<V,W>
82{
83 fn cmp(&self, other: &Self) -> std::cmp::Ordering
84 {
85 self.weight.cmp(&other.weight)
86 }
88}
89
90impl<V: Eq, W: Weight> PartialOrd for WeightedItem<V,W>
91{
92 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering>
93 {
94 self.weight.partial_cmp(&other.weight)
95 }
96}