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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::marker::PhantomData;

use super::LanesIter;
use super::LanesIterMut;
use crate::imp_prelude::*;
use crate::{Layout, NdProducer};

impl_ndproducer! {
    ['a, A, D: Dimension]
    [Clone => 'a, A, D: Clone ]
    Lanes {
        base,
        inner_len,
        inner_stride,
    }
    Lanes<'a, A, D> {
        type Item = ArrayView<'a, A, Ix1>;
        type Dim = D;

        unsafe fn item(&self, ptr) {
            ArrayView::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix))
        }
    }
}

/// See [`.lanes()`](ArrayBase::lanes)
/// for more information.
pub struct Lanes<'a, A, D> {
    base: ArrayView<'a, A, D>,
    inner_len: Ix,
    inner_stride: Ixs,
}

impl<'a, A, D: Dimension> Lanes<'a, A, D> {
    pub(crate) fn new<Di>(v: ArrayView<'a, A, Di>, axis: Axis) -> Self
    where
        Di: Dimension<Smaller = D>,
    {
        let ndim = v.ndim();
        let len;
        let stride;
        let iter_v = if ndim == 0 {
            len = 1;
            stride = 1;
            v.try_remove_axis(Axis(0))
        } else {
            let i = axis.index();
            len = v.dim[i];
            stride = v.strides[i] as isize;
            v.try_remove_axis(axis)
        };
        Lanes {
            inner_len: len,
            inner_stride: stride,
            base: iter_v,
        }
    }
}

impl_ndproducer! {
    ['a, A, D: Dimension]
    [Clone =>]
    LanesMut {
        base,
        inner_len,
        inner_stride,
    }
    LanesMut<'a, A, D> {
        type Item = ArrayViewMut<'a, A, Ix1>;
        type Dim = D;

        unsafe fn item(&self, ptr) {
            ArrayViewMut::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix))
        }
    }
}

impl<'a, A, D> IntoIterator for Lanes<'a, A, D>
where
    D: Dimension,
{
    type Item = <Self::IntoIter as Iterator>::Item;
    type IntoIter = LanesIter<'a, A, D>;
    fn into_iter(self) -> Self::IntoIter {
        LanesIter {
            iter: self.base.into_base_iter(),
            inner_len: self.inner_len,
            inner_stride: self.inner_stride,
            life: PhantomData,
        }
    }
}

/// See [`.lanes_mut()`](ArrayBase::lanes_mut)
/// for more information.
pub struct LanesMut<'a, A, D> {
    base: ArrayViewMut<'a, A, D>,
    inner_len: Ix,
    inner_stride: Ixs,
}

impl<'a, A, D: Dimension> LanesMut<'a, A, D> {
    pub(crate) fn new<Di>(v: ArrayViewMut<'a, A, Di>, axis: Axis) -> Self
    where
        Di: Dimension<Smaller = D>,
    {
        let ndim = v.ndim();
        let len;
        let stride;
        let iter_v = if ndim == 0 {
            len = 1;
            stride = 1;
            v.try_remove_axis(Axis(0))
        } else {
            let i = axis.index();
            len = v.dim[i];
            stride = v.strides[i] as isize;
            v.try_remove_axis(axis)
        };
        LanesMut {
            inner_len: len,
            inner_stride: stride,
            base: iter_v,
        }
    }
}

impl<'a, A, D> IntoIterator for LanesMut<'a, A, D>
where
    D: Dimension,
{
    type Item = <Self::IntoIter as Iterator>::Item;
    type IntoIter = LanesIterMut<'a, A, D>;
    fn into_iter(self) -> Self::IntoIter {
        LanesIterMut {
            iter: self.base.into_base_iter(),
            inner_len: self.inner_len,
            inner_stride: self.inner_stride,
            life: PhantomData,
        }
    }
}