@@ -5,24 +5,18 @@ use crate::Layout;
55use crate :: NdProducer ;
66use crate :: Slice ;
77
8- #[ derive( Clone ) ]
9- pub struct GeneralWindow ;
10- #[ derive( Clone ) ]
11- pub struct AxisWindow { pub ( crate ) index : usize }
12-
138/// Window producer and iterable
149///
1510/// See [`.windows()`](ArrayBase::windows) for more
1611/// information.
17- pub struct Windows < ' a , A , D , V > {
12+ pub struct Windows < ' a , A , D > {
1813 base : ArrayView < ' a , A , D > ,
1914 window : D ,
2015 strides : D ,
21- variant : V ,
2216}
2317
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
2620 where
2721 E : IntoDimension < Dim = D > ,
2822 {
@@ -32,15 +26,10 @@ impl<'a, A, D: Dimension, V> Windows<'a, A, D, V> {
3226 let mut unit_stride = D :: zeros ( ndim) ;
3327 unit_stride. slice_mut ( ) . fill ( 1 ) ;
3428
35- Windows :: new_with_stride ( a, window, unit_stride, variant )
29+ Windows :: new_with_stride ( a, window, unit_stride)
3630 }
3731
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
4433 where
4534 E : IntoDimension < Dim = D > ,
4635 {
@@ -88,7 +77,6 @@ impl<'a, A, D: Dimension, V> Windows<'a, A, D, V> {
8877 base,
8978 window,
9079 strides : window_strides,
91- variant,
9280 }
9381 }
9482}
@@ -100,9 +88,8 @@ impl_ndproducer! {
10088 base,
10189 window,
10290 strides,
103- variant,
10491 }
105- Windows <' a, A , D , GeneralWindow > {
92+ Windows <' a, A , D > {
10693 type Item = ArrayView <' a, A , D >;
10794 type Dim = D ;
10895
@@ -113,7 +100,7 @@ impl_ndproducer! {
113100 }
114101}
115102
116- impl < ' a , A , D , V > IntoIterator for Windows < ' a , A , D , V >
103+ impl < ' a , A , D > IntoIterator for Windows < ' a , A , D >
117104where
118105 D : Dimension ,
119106 A : ' a ,
@@ -161,14 +148,55 @@ impl_iterator! {
161148 }
162149}
163150
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 > {
165193 type Item = ArrayView < ' a , A , D > ;
166194 type Dim = Ix1 ;
167195 type Ptr = * mut A ;
168196 type Stride = isize ;
169197
170198 fn raw_dim ( & self ) -> Ix1 {
171- Ix1 ( self . base . raw_dim ( ) [ self . variant . index ] )
199+ Ix1 ( self . base . raw_dim ( ) [ self . axis_idx ] )
172200 }
173201
174202 fn layout ( & self ) -> Layout {
@@ -184,38 +212,54 @@ impl<'a, A, D: Dimension> NdProducer for Windows<'a, A, D, AxisWindow> {
184212 }
185213
186214 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 ( ) )
188217 }
189218
190219 unsafe fn uget_ptr ( & self , i : & Self :: Dim ) -> * mut A {
191220 let mut d = D :: zeros ( self . base . ndim ( ) ) ;
192- d[ self . variant . index ] = i[ 0 ] ;
221+ d[ self . axis_idx ] = i[ 0 ] ;
193222 self . base . uget_ptr ( & d)
194223 }
195224
196225 fn stride_of ( & self , axis : Axis ) -> isize {
197226 assert_eq ! ( axis, Axis ( 0 ) ) ;
198- self . base . stride_of ( Axis ( self . variant . index ) )
227+ self . base . stride_of ( Axis ( self . axis_idx ) )
199228 }
200229
201230 fn split_at ( self , axis : Axis , index : usize ) -> ( Self , Self ) {
202231 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+ }
221265}
0 commit comments