1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
pub trait Path: Copy + Clone {
    fn default(weight: f64, index: usize) -> Self;
    fn aggregate(&mut self, other: Self);
}

#[derive(Copy, Clone)]
pub struct FindMax {
    pub idx: usize,
    pub weight: f64,
}

impl Path for FindMax {
    fn default(weight: f64, index: usize) -> Self {
        FindMax { idx: index, weight }
    }

    fn aggregate(&mut self, other: Self) {
        if other.weight > self.weight {
            self.weight = other.weight;
            self.idx = other.idx;
        }
    }
}

#[derive(Copy, Clone)]
pub struct FindMin {
    pub idx: usize,
    pub weight: f64,
}

impl Path for FindMin {
    fn default(weight: f64, index: usize) -> Self {
        FindMin { idx: index, weight }
    }

    fn aggregate(&mut self, other: Self) {
        if other.weight < self.weight {
            self.weight = other.weight;
            self.idx = other.idx;
        }
    }
}

#[derive(Copy, Clone)]
pub struct FindSum {
    pub sum: f64,
}

impl Path for FindSum {
    fn default(weight: f64, _: usize) -> Self {
        FindSum { sum: weight }
    }

    fn aggregate(&mut self, other: Self) {
        self.sum += other.sum;
    }
}