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
use std::fmt::{self, Debug};
use std::mem;
use std::ops::Index;
use base;
use base::Stride as Base;
#[repr(C)]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct Stride<'a,T: 'a> {
base: Base<'a, T>,
}
impl<'a, T> Copy for Stride<'a, T> {}
impl<'a, T> Clone for Stride<'a, T> {
fn clone(&self) -> Stride<'a, T> { *self }
}
unsafe impl<'a, T: Sync> Sync for Stride<'a, T> {}
unsafe impl<'a, T: Sync> Send for Stride<'a, T> {}
impl<'a, T: Debug> Debug for Stride<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.base.fmt(f)
}
}
impl<'a, T> Stride<'a, T> {
#[inline(always)]
fn new_raw(base: Base<'a, T>) -> Stride<'a, T> {
Stride {
base: base,
}
}
#[inline(always)]
pub fn new(x: &'a [T]) -> Stride<'a, T> {
Stride::new_raw(Base::new(x.as_ptr() as *mut _, x.len(), 1))
}
#[inline(always)]
pub fn len(&self) -> usize {
self.base.len()
}
#[inline(always)]
pub fn stride(&self) -> usize {
self.base.stride() / mem::size_of::<T>()
}
#[inline(always)]
pub fn as_ptr(&self) -> *const T {
self.base.as_mut_ptr() as *const T
}
#[inline(always)]
#[cfg(test)]
fn reborrow<'b>(&'b self) -> Stride<'b, T> {
*self
}
#[inline]
pub fn substrides2(&self) -> (Stride<'a, T>, Stride<'a, T>) {
let (l, r) = self.base.substrides2();
(Stride::new_raw(l), Stride::new_raw(r))
}
#[inline]
pub fn substrides(&self, n: usize) -> Substrides<'a, T> {
Substrides {
base: self.base.substrides(n),
}
}
#[inline]
pub fn get(&self, n: usize) -> Option<&'a T> {
self.base.get(n)
}
#[inline]
pub fn iter(&self) -> ::Items<'a, T> {
self.base.iter()
}
#[inline]
pub fn slice(&self, from: usize, to: usize) -> Stride<'a, T> {
Stride::new_raw(self.base.slice(from, to))
}
#[inline]
pub fn slice_from(&self, from: usize) -> Stride<'a, T> {
Stride::new_raw(self.base.slice_from(from))
}
#[inline]
pub fn slice_to(&self, to: usize) -> Stride<'a, T> {
Stride::new_raw(self.base.slice_to(to))
}
#[inline]
pub fn split_at(&self, idx: usize) -> (Stride<'a, T>, Stride<'a, T>) {
let (l, r) = self.base.split_at(idx);
(Stride::new_raw(l), Stride::new_raw(r))
}
}
impl<'a, T> Index<usize> for Stride<'a, T> {
type Output = T;
fn index<'b>(&'b self, n: usize) -> &'b T {
self.get(n).expect("Stride.index: index out of bounds")
}
}
pub struct Substrides<'a, T: 'a> {
base: base::Substrides<'a, T>,
}
impl<'a, T> Iterator for Substrides<'a, T> {
type Item = Stride<'a, T>;
fn next(&mut self) -> Option<Stride<'a, T>> {
match self.base.next() {
Some(s) => Some(Stride::new_raw(s)),
None => None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.base.size_hint()
}
}
#[cfg(test)]
#[allow(unused_mut)]
mod tests {
use super::Stride;
make_tests!(substrides2, substrides, slice, slice_to, slice_from, split_at, get, iter, );
}