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
/*
   Appellation: layout <module>
   Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::model::layout::{features, Features};
use nd::{Dimension, RemoveAxis, ShapeBuilder};

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]

pub struct Layout<D = nd::Ix2>
where
    D: Dimension,
{
    pub(crate) dim: D,
    pub(crate) features: Features,
}

impl<D> Layout<D>
where
    D: Dimension,
{
    pub fn new(dim: D) -> Self {
        let features = features(dim.clone()).expect("Invalid dimension");
        Self { dim, features }
    }

    pub fn from_shape<Sh>(shape: Sh) -> Self
    where
        D: RemoveAxis,
        Sh: ShapeBuilder<Dim = D>,
    {
        let shape = shape.into_shape();
        let dim = shape.raw_dim().clone();
        let features = Features::from_shape(shape);
        Self { dim, features }
    }

    pub fn as_slice(&self) -> &[usize] {
        self.dim.slice()
    }

    pub fn as_mut_slice(&mut self) -> &mut [usize] {
        self.dim.slice_mut()
    }

    pub fn features(&self) -> Features {
        self.features
    }

    pub fn ndim(&self) -> usize {
        self.dim.ndim()
    }

    pub fn pattern(&self) -> D::Pattern
    where
        D: Copy,
    {
        self.dim.into_pattern()
    }

    pub fn raw_dim(&self) -> D {
        self.dim.clone()
    }
}