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
/*
   Appellation: convert <mod>
   Contrib: FL03 <jo3mccain@icloud.com>
*/
use nd::Axis;

pub trait IntoAxis {
    fn into_axis(self) -> Axis;
}

pub trait NdArray<T> {
    type Dim;

    fn as_slice(&self) -> &[T];

    fn dim(&self) -> Self::Dim;

    fn shape(&self) -> &[usize];

    fn len(&self) -> usize {
        self.as_slice().len()
    }

    fn is_empty(&self) -> bool {
        self.as_slice().is_empty()
    }

    fn is_scalar(&self) -> bool {
        self.shape().is_empty()
    }
}

/*
 ******** implementations ********
*/

impl<S> IntoAxis for S
where
    S: AsRef<usize>,
{
    fn into_axis(self) -> Axis {
        Axis(*self.as_ref())
    }
}

use nd::{ArrayBase, Dimension};

impl<A, S, D> NdArray<A> for ArrayBase<S, D>
where
    S: nd::Data<Elem = A>,
    D: Dimension,
{
    type Dim = D;

    fn as_slice(&self) -> &[A] {
        ArrayBase::as_slice(self).unwrap()
    }

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

    fn shape(&self) -> &[usize] {
        self.shape()
    }
}