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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
   Appellation: stride <mod>
   Contrib: FL03 <jo3mccain@icloud.com>
*/
use super::{Axis, Rank};
use core::borrow::{Borrow, BorrowMut};
use core::ops::{Deref, DerefMut, Index, IndexMut};
use core::slice::{Iter as SliceIter, IterMut as SliceIterMut};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub trait IntoStride {
    fn into_stride(self) -> Stride;
}

impl<S> IntoStride for S
where
    S: Into<Stride>,
{
    fn into_stride(self) -> Stride {
        self.into()
    }
}

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Stride(pub(crate) Vec<usize>);

impl Stride {
    pub fn new(stride: Vec<usize>) -> Self {
        Self(stride)
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Self(Vec::with_capacity(capacity))
    }

    pub fn zeros(rank: Rank) -> Self {
        Self(vec![0; *rank])
    }
    /// Returns a reference to the stride.
    pub fn as_slice(&self) -> &[usize] {
        &self.0
    }
    /// Returns a mutable reference to the stride.
    pub fn as_slice_mut(&mut self) -> &mut [usize] {
        &mut self.0
    }
    /// Returns the capacity of the stride.
    pub fn capacity(&self) -> usize {
        self.0.capacity()
    }
    /// Clears the stride, removing all elements.
    pub fn clear(&mut self) {
        self.0.clear()
    }
    /// Gets the element at the specified axis, returning None if the axis is out of bounds.
    pub fn get(&self, axis: Axis) -> Option<&usize> {
        self.0.get(*axis)
    }
    /// Returns an iterator over references to the elements of the stride.
    pub fn iter(&self) -> SliceIter<usize> {
        self.0.iter()
    }
    /// Returns an iterator over mutable references to the elements of the stride.
    pub fn iter_mut(&mut self) -> SliceIterMut<usize> {
        self.0.iter_mut()
    }
    /// Returns the rank of the stride; i.e., the number of dimensions.
    pub fn rank(&self) -> Rank {
        self.0.len().into()
    }
    /// Removes and returns the stride of the axis.
    pub fn remove(&mut self, axis: Axis) -> usize {
        self.0.remove(*axis)
    }
    /// Returns a new stride with the axis removed.
    pub fn remove_axis(&self, axis: Axis) -> Self {
        let mut stride = self.clone();
        stride.remove(axis);
        stride
    }
    /// Reverses the stride.
    pub fn reverse(&mut self) {
        self.0.reverse()
    }
    /// Swaps two elements in the stride, inplace.
    pub fn swap(&mut self, a: usize, b: usize) {
        self.0.swap(a, b)
    }
    /// Returns a new shape with the two axes swapped.
    pub fn swap_axes(&self, a: Axis, b: Axis) -> Self {
        let mut stride = self.clone();
        stride.swap(a.axis(), b.axis());
        stride
    }
}

// Internal methods
impl Stride {
    pub(crate) fn _fastest_varying_stride_order(&self) -> Self {
        let mut indices = self.clone();
        for (i, elt) in indices.as_slice_mut().into_iter().enumerate() {
            *elt = i;
        }
        let strides = self.as_slice();
        indices
            .as_slice_mut()
            .sort_by_key(|&i| (strides[i] as isize).abs());
        indices
    }
}

impl AsRef<[usize]> for Stride {
    fn as_ref(&self) -> &[usize] {
        &self.0
    }
}

impl AsMut<[usize]> for Stride {
    fn as_mut(&mut self) -> &mut [usize] {
        &mut self.0
    }
}

impl Borrow<[usize]> for Stride {
    fn borrow(&self) -> &[usize] {
        &self.0
    }
}

impl BorrowMut<[usize]> for Stride {
    fn borrow_mut(&mut self) -> &mut [usize] {
        &mut self.0
    }
}

impl Deref for Stride {
    type Target = [usize];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Stride {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Extend<usize> for Stride {
    fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I) {
        self.0.extend(iter)
    }
}

impl FromIterator<usize> for Stride {
    fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> Self {
        Stride(Vec::from_iter(iter))
    }
}

impl Index<usize> for Stride {
    type Output = usize;

    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

impl IndexMut<usize> for Stride {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.0[index]
    }
}

impl Index<Axis> for Stride {
    type Output = usize;

    fn index(&self, index: Axis) -> &Self::Output {
        &self.0[*index]
    }
}

impl IndexMut<Axis> for Stride {
    fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
        &mut self.0[*index]
    }
}

impl IntoIterator for Stride {
    type Item = usize;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl From<Vec<usize>> for Stride {
    fn from(v: Vec<usize>) -> Self {
        Stride(v)
    }
}

impl From<&[usize]> for Stride {
    fn from(v: &[usize]) -> Self {
        Stride(v.to_vec())
    }
}