competitive_hpp/total_ord/
total.rs1use std::cmp::Ordering;
2
3#[derive(PartialEq, PartialOrd, Clone, Debug)]
4pub struct Total<T>(pub T);
5
6impl<T> Total<T>
7where
8 T: Clone,
9{
10 pub fn unwrap(&self) -> T {
11 self.0.clone()
12 }
13}
14impl<T: PartialEq> Eq for Total<T> {}
15
16impl<T: PartialOrd> Ord for Total<T> {
17 fn cmp(&self, other: &Total<T>) -> Ordering {
18 self.0.partial_cmp(&other.0).unwrap()
19 }
20}
21
22#[cfg(test)]
23mod test {
24 use super::*;
25
26 #[test]
27 fn test_total_ord() {
28 let test = vec![2.2f64, 1.0f64, 8.5f64, 0.5f64, -3.5f64];
29 let sorted: Vec<f64> = vec![-3.5f64, 0.5f64, 1.0f64, 2.2f64, 8.5f64];
30
31 let mut total: Vec<Total<f64>> = test.iter().map(|&i| Total(i)).collect();
32 total.sort();
33 let total: Vec<f64> = total.iter().map(|&Total(i)| i).collect();
34
35 assert_eq!(sorted, total);
36 }
37}