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
91
use crate::nd_tools::ndindex::get_ndindex_array;
use crate::Neural;
use ndarray::{prelude::*, Shape};

pub type BoxedTopological<N> = Box<dyn Topological<N> + Send>;
/// Interface for structures encapsulating representations of network layer topologies.
pub trait Topological<N>
where
    N: Neural,
{
    fn init_lateral(&self, neurons: &mut N);

    // Todo can we make this non public?
    fn clone_dyn(&self) -> BoxedTopological<N>;
}

impl<N> Topological<N> for BoxedTopological<N>
where
    N: Neural,
{
    fn init_lateral(&self, neurons: &mut N) {
        (**self).init_lateral(neurons)
    }

    fn clone_dyn(&self) -> BoxedTopological<N> {
        panic!()
    }
}

impl<N> Clone for BoxedTopological<N>
where
    N: Neural,
{
    fn clone(&self) -> Self {
        (**self).clone_dyn()
    }
}

#[derive(Clone)]
pub struct CartesianTopology<D>
where
    D: Dimension,
{
    pub shape: Shape<D>,
}

impl<D> CartesianTopology<D>
where
    D: Dimension,
{
    pub fn new<Sh>(shape: Sh) -> Self
    where
        Sh: ShapeBuilder<Dim = D>,
    {
        CartesianTopology {
            shape: shape.into_shape(),
        }
    }
}

impl<D, N> Topological<N> for CartesianTopology<D>
where
    N: Neural,
    D: Dimension,
{
    fn init_lateral(&self, neurons: &mut N) {
        neurons
            .get_lateral_mut()
            .assign(&get_ndindex_array(&self.shape));
    }

    fn clone_dyn(&self) -> BoxedTopological<N> {
        // Cloning leads to "parameter may not live long enough"
        // Box::new(self.clone())
        // Convert into dyn object first. Unclear whether this is the "right" thing to do
        let dim = self.shape.raw_dim().clone().into_dyn();
        return Box::new(CartesianTopology {
            shape: Shape::from(dim),
        });
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn it_works() {
        let result = 2 + 2;
        assert_eq!(result, 4);
    }
}