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
85
86
87
88
89
90
use crate::{DirectedBijectiveConnectionGraph, Lemma1, Lemma2, NodeToSet};
use gt_graph::{Graph, InterChangeUsize};
use gt_graph_path::GraphPath;
use std::ops::{BitAnd, Sub};

pub trait NodeToNode<G>
where
    G: Graph,
{
    #[allow(non_snake_case)]
    fn N2N(&self, s: G::Node, d: G::Node) -> Vec<GraphPath<G>>;
    fn node_to_node(&self, s: G::Node, d: G::Node) -> Vec<GraphPath<G>>;
    fn node_to_node_helper(&self, n: G::Dims, s: G::Node, d: G::Node) -> Vec<GraphPath<G>>;
}

impl<G, N, D> NodeToNode<G> for G
where
    N: Copy + PartialEq + BitAnd<Output = N> + InterChangeUsize,
    D: Copy + InterChangeUsize + Sub<Output = D>,
    G: DirectedBijectiveConnectionGraph
        + Lemma2<G>
        + Lemma1<G>
        + NodeToSet<G>
        + Graph<Node = N, Dims = D>,
{
    #[allow(non_snake_case)]
    #[inline(always)]
    fn N2N(&self, s: G::Node, d: G::Node) -> Vec<GraphPath<G>> {
        self.node_to_node(s, d)
    }

    #[inline(always)]
    fn node_to_node(&self, s: G::Node, d: G::Node) -> Vec<GraphPath<G>> {
        self.node_to_node_helper(self.dimension(), s, d)
    }

    fn node_to_node_helper(&self, n: G::Dims, s: G::Node, d: G::Node) -> Vec<GraphPath<G>> {
        let mut paths;

        let mask: N =
            InterChangeUsize::from_usize(1 << (n - InterChangeUsize::from_usize(1)).to_usize());

        if s & mask == d & mask {
            paths = self.node_to_node_helper(n - InterChangeUsize::from_usize(1), s, d);

            let mut path = GraphPath::new(self);
            path.push_back(s);
            let phi_s = self.phi(n, s);
            path.push_back(phi_s);
            self.R_helper(n, phi_s, self.psi(n, d), &mut path);
            path.push_back(d);

            paths.push(path);
        } else {
            let mut path = GraphPath::new(self);
            path.push_back(s);
            let phi_s = self.phi(n, s);
            path.push_back(phi_s);
            self.R_helper(n, phi_s, d, &mut path);

            let neighbor_node = path.inner_path()[path.inner_path().len() - 2];

            let lemma1_except_neighbor = self
                .lemma1(n, d)
                .into_iter()
                .filter(|path| !path.inner_path().contains(&neighbor_node))
                .collect::<Vec<_>>();

            let ds = lemma1_except_neighbor
                .iter()
                .map(|path| path.inner_path()[0])
                .collect::<Vec<_>>();

            let mut partial_paths = self.node_to_set(s, &ds);
            partial_paths
                .iter_mut()
                .zip(lemma1_except_neighbor.iter())
                .for_each(|(partial, lemma1)| {
                    partial
                        .inner_path_mut()
                        .extend(lemma1.inner_path().iter().skip(1))
                });

            paths = partial_paths;
            paths.push(path);
        }

        paths
    }
}