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
58
59
60
61
62
63
use semirings::Semiring;
use std::hash::{Hash, Hasher};
use Label;
use EPS_LABEL;

#[derive(PartialEq, Debug, Clone, PartialOrd)]
pub struct Path<W: Semiring> {
    pub ilabels: Vec<Label>,
    pub olabels: Vec<Label>,
    pub weight: W,
}

impl<W: Semiring> Path<W> {
    pub fn new(ilabels: Vec<Label>, olabels: Vec<Label>, weight: W) -> Self {
        Path {
            ilabels,
            olabels,
            weight,
        }
    }

    pub fn add_to_path(&mut self, ilabel: Label, olabel: Label, weight: W) {
        if ilabel != EPS_LABEL {
            self.ilabels.push(ilabel);
        }

        if olabel != EPS_LABEL {
            self.olabels.push(olabel);
        }

        self.weight *= weight
    }

    pub fn add_weight(&mut self, weight: W) {
        self.weight *= weight
    }

    pub fn concat(&mut self, other: Path<W>) {
        self.ilabels.extend(other.ilabels);
        self.olabels.extend(other.olabels);
        self.weight *= other.weight;
    }
}

impl<W: Semiring> Default for Path<W> {
    fn default() -> Self {
        Path {
            ilabels: vec![],
            olabels: vec![],
            weight: W::one(),
        }
    }
}

impl<W: Semiring + Hash + Eq> Hash for Path<W> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.ilabels.hash(state);
        self.olabels.hash(state);
        self.weight.hash(state);
    }
}

impl<W: Semiring + Hash + Eq> Eq for Path<W> {}