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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::prelude::TVec;

#[derive(Debug, Clone)]
pub struct AxesInfo(TVec<AxisInfo>);

impl AxesInfo {
    pub fn none() -> AxesInfo {
        AxesInfo(tvec!())
    }
}

impl From<TVec<AxisInfo>> for AxesInfo {
    fn from(axes: TVec<AxisInfo>) -> AxesInfo {
        AxesInfo(axes)
    }
}

impl std::iter::FromIterator<AxisInfo> for AxesInfo {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = AxisInfo>,
    {
        AxesInfo(iter.into_iter().collect())
    }
}

impl std::ops::Deref for AxesInfo {
    type Target = [AxisInfo];
    fn deref(&self) -> &[AxisInfo] {
        &self.0
    }
}

/// Translation invariance property.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AxisInfo {
    pub inputs: TVec<Option<usize>>,
    pub outputs: TVec<Option<usize>>,
    pub period: usize,
    pub disposable: bool,
}

impl AxisInfo {
    pub fn simple(axis: usize) -> AxisInfo {
        AxisInfo {
            inputs: tvec!(Some(axis)),
            outputs: tvec!(Some(axis)),
            period: 1,
            disposable: true,
        }
    }

    pub fn with_period(self, period: usize) -> AxisInfo {
        AxisInfo { period, ..self }
    }

    pub fn disposable(self, disposable: bool) -> AxisInfo {
        AxisInfo { disposable, ..self }
    }
}

impl AxesInfo {
    pub fn unary_track_axis_up(&self, axis: usize, only_disposable: bool) -> Option<usize> {
        self.0
            .iter()
            .find(|connection| {
                connection.outputs.get(0) == Some(&Some(axis)) && connection.period == 1
            })
            .filter(|conn| conn.disposable || !only_disposable)
            .and_then(|connection| connection.inputs.get(0))
            .and_then(|d| *d)
    }

    pub fn unary_track_axis_down(&self, axis: usize, only_disposable: bool) -> Option<usize> {
        self.0
            .iter()
            .find(|connection| {
                connection.inputs.get(0) == Some(&Some(axis)) && connection.period == 1
            })
            .filter(|conn| conn.disposable || !only_disposable)
            .and_then(|connection| connection.outputs.get(0))
            .and_then(|d| *d)
    }
}