@@ -5,24 +5,18 @@ use crate::Layout;
5
5
use crate :: NdProducer ;
6
6
use crate :: Slice ;
7
7
8
- #[ derive( Clone ) ]
9
- pub struct GeneralWindow ;
10
- #[ derive( Clone ) ]
11
- pub struct AxisWindow { pub ( crate ) index : usize }
12
-
13
8
/// Window producer and iterable
14
9
///
15
10
/// See [`.windows()`](ArrayBase::windows) for more
16
11
/// information.
17
- pub struct Windows < ' a , A , D , V > {
12
+ pub struct Windows < ' a , A , D > {
18
13
base : ArrayView < ' a , A , D > ,
19
14
window : D ,
20
15
strides : D ,
21
- variant : V ,
22
16
}
23
17
24
- impl < ' a , A , D : Dimension , V > Windows < ' a , A , D , V > {
25
- pub ( crate ) fn new < E > ( a : ArrayView < ' a , A , D > , window_size : E , variant : V ) -> Self
18
+ impl < ' a , A , D : Dimension > Windows < ' a , A , D > {
19
+ pub ( crate ) fn new < E > ( a : ArrayView < ' a , A , D > , window_size : E ) -> Self
26
20
where
27
21
E : IntoDimension < Dim = D > ,
28
22
{
@@ -32,15 +26,10 @@ impl<'a, A, D: Dimension, V> Windows<'a, A, D, V> {
32
26
let mut unit_stride = D :: zeros ( ndim) ;
33
27
unit_stride. slice_mut ( ) . fill ( 1 ) ;
34
28
35
- Windows :: new_with_stride ( a, window, unit_stride, variant )
29
+ Windows :: new_with_stride ( a, window, unit_stride)
36
30
}
37
31
38
- pub ( crate ) fn new_with_stride < E > (
39
- a : ArrayView < ' a , A , D > ,
40
- window_size : E ,
41
- axis_strides : E ,
42
- variant : V ,
43
- ) -> Self
32
+ pub ( crate ) fn new_with_stride < E > ( a : ArrayView < ' a , A , D > , window_size : E , axis_strides : E ) -> Self
44
33
where
45
34
E : IntoDimension < Dim = D > ,
46
35
{
@@ -88,7 +77,6 @@ impl<'a, A, D: Dimension, V> Windows<'a, A, D, V> {
88
77
base,
89
78
window,
90
79
strides : window_strides,
91
- variant,
92
80
}
93
81
}
94
82
}
@@ -100,9 +88,8 @@ impl_ndproducer! {
100
88
base,
101
89
window,
102
90
strides,
103
- variant,
104
91
}
105
- Windows <' a, A , D , GeneralWindow > {
92
+ Windows <' a, A , D > {
106
93
type Item = ArrayView <' a, A , D >;
107
94
type Dim = D ;
108
95
@@ -113,7 +100,7 @@ impl_ndproducer! {
113
100
}
114
101
}
115
102
116
- impl < ' a , A , D , V > IntoIterator for Windows < ' a , A , D , V >
103
+ impl < ' a , A , D > IntoIterator for Windows < ' a , A , D >
117
104
where
118
105
D : Dimension ,
119
106
A : ' a ,
@@ -161,14 +148,55 @@ impl_iterator! {
161
148
}
162
149
}
163
150
164
- impl < ' a , A , D : Dimension > NdProducer for Windows < ' a , A , D , AxisWindow > {
151
+ /// Window producer and iterable
152
+ ///
153
+ /// See [`.axis_windows()`](ArrayBase::axis_windows) for more
154
+ /// information.
155
+ pub struct AxisWindows < ' a , A , D > {
156
+ base : ArrayView < ' a , A , D > ,
157
+ axis_idx : usize ,
158
+ window : D ,
159
+ strides : D ,
160
+ }
161
+
162
+ impl < ' a , A , D : Dimension > AxisWindows < ' a , A , D > {
163
+ pub ( crate ) fn new ( a : ArrayView < ' a , A , D > , axis : Axis , window_size : usize ) -> Self
164
+ {
165
+ let strides = a. strides . clone ( ) ;
166
+ let mut base = a;
167
+ let axis_idx = axis. index ( ) ;
168
+ let mut window = base. raw_dim ( ) ;
169
+ window[ axis_idx] = window_size;
170
+
171
+ base. slice_each_axis_inplace ( |ax_desc| {
172
+ let len = ax_desc. len ;
173
+ let wsz = window[ ax_desc. axis . index ( ) ] ;
174
+
175
+ if len < wsz {
176
+ Slice :: new ( 0 , Some ( 0 ) , 1 )
177
+ } else {
178
+ Slice :: new ( 0 , Some ( ( len - wsz + 1 ) as isize ) , 1 )
179
+ }
180
+ } ) ;
181
+
182
+ AxisWindows {
183
+ base,
184
+ axis_idx,
185
+ window,
186
+ strides,
187
+ }
188
+ }
189
+ }
190
+
191
+
192
+ impl < ' a , A , D : Dimension > NdProducer for AxisWindows < ' a , A , D > {
165
193
type Item = ArrayView < ' a , A , D > ;
166
194
type Dim = Ix1 ;
167
195
type Ptr = * mut A ;
168
196
type Stride = isize ;
169
197
170
198
fn raw_dim ( & self ) -> Ix1 {
171
- Ix1 ( self . base . raw_dim ( ) [ self . variant . index ] )
199
+ Ix1 ( self . base . raw_dim ( ) [ self . axis_idx ] )
172
200
}
173
201
174
202
fn layout ( & self ) -> Layout {
@@ -184,38 +212,54 @@ impl<'a, A, D: Dimension> NdProducer for Windows<'a, A, D, AxisWindow> {
184
212
}
185
213
186
214
unsafe fn as_ref ( & self , ptr : * mut A ) -> Self :: Item {
187
- ArrayView :: new_ ( ptr, self . window . clone ( ) , self . strides . clone ( ) )
215
+ ArrayView :: new_ ( ptr, self . window . clone ( ) ,
216
+ self . strides . clone ( ) )
188
217
}
189
218
190
219
unsafe fn uget_ptr ( & self , i : & Self :: Dim ) -> * mut A {
191
220
let mut d = D :: zeros ( self . base . ndim ( ) ) ;
192
- d[ self . variant . index ] = i[ 0 ] ;
221
+ d[ self . axis_idx ] = i[ 0 ] ;
193
222
self . base . uget_ptr ( & d)
194
223
}
195
224
196
225
fn stride_of ( & self , axis : Axis ) -> isize {
197
226
assert_eq ! ( axis, Axis ( 0 ) ) ;
198
- self . base . stride_of ( Axis ( self . variant . index ) )
227
+ self . base . stride_of ( Axis ( self . axis_idx ) )
199
228
}
200
229
201
230
fn split_at ( self , axis : Axis , index : usize ) -> ( Self , Self ) {
202
231
assert_eq ! ( axis, Axis ( 0 ) ) ;
203
- let ( a, b) = self . base . split_at ( Axis ( self . variant . index ) , index) ;
204
- (
205
- Windows {
206
- base : a,
207
- window : self . window . clone ( ) ,
208
- strides : self . strides . clone ( ) ,
209
- variant : self . variant . clone ( ) ,
210
- } ,
211
- Windows {
212
- base : b,
213
- window : self . window ,
214
- strides : self . strides ,
215
- variant : self . variant ,
216
- } ,
217
- )
218
- }
219
-
220
- private_impl ! { }
232
+ let ( a, b) = self . base . split_at ( Axis ( self . axis_idx ) , index) ;
233
+ ( AxisWindows {
234
+ base : a,
235
+ axis_idx : self . axis_idx ,
236
+ window : self . window . clone ( ) ,
237
+ strides : self . strides . clone ( )
238
+
239
+ } ,
240
+ AxisWindows {
241
+ base : b,
242
+ axis_idx : self . axis_idx ,
243
+ window : self . window ,
244
+ strides : self . strides ,
245
+ } )
246
+ }
247
+
248
+ private_impl ! { }
249
+ }
250
+
251
+ impl < ' a , A , D > IntoIterator for AxisWindows < ' a , A , D >
252
+ where
253
+ D : Dimension ,
254
+ A : ' a ,
255
+ {
256
+ type Item = <Self :: IntoIter as Iterator >:: Item ;
257
+ type IntoIter = WindowsIter < ' a , A , D > ;
258
+ fn into_iter ( self ) -> Self :: IntoIter {
259
+ WindowsIter {
260
+ iter : self . base . into_elements_base ( ) ,
261
+ window : self . window ,
262
+ strides : self . strides ,
263
+ }
264
+ }
221
265
}
0 commit comments