@@ -3,6 +3,7 @@ use crate::imp_prelude::*;
3
3
use crate :: IntoDimension ;
4
4
use crate :: Layout ;
5
5
use crate :: NdProducer ;
6
+ use crate :: Slice ;
6
7
7
8
/// Window producer and iterable
8
9
///
@@ -24,16 +25,19 @@ impl<'a, A, D: Dimension> Windows<'a, A, D> {
24
25
25
26
let mut unit_stride = D :: zeros ( ndim) ;
26
27
unit_stride. slice_mut ( ) . fill ( 1 ) ;
27
-
28
+
28
29
Windows :: new_with_stride ( a, window, unit_stride)
29
30
}
30
31
31
- pub ( crate ) fn new_with_stride < E > ( a : ArrayView < ' a , A , D > , window_size : E , strides : E ) -> Self
32
+ pub ( crate ) fn new_with_stride < E > ( a : ArrayView < ' a , A , D > , window_size : E , axis_strides : E ) -> Self
32
33
where
33
34
E : IntoDimension < Dim = D > ,
34
35
{
35
36
let window = window_size. into_dimension ( ) ;
36
- let strides_d = strides. into_dimension ( ) ;
37
+
38
+ let strides = axis_strides. into_dimension ( ) ;
39
+ let window_strides = a. strides . clone ( ) ;
40
+
37
41
ndassert ! (
38
42
a. ndim( ) == window. ndim( ) ,
39
43
concat!(
@@ -44,45 +48,35 @@ impl<'a, A, D: Dimension> Windows<'a, A, D> {
44
48
a. ndim( ) ,
45
49
a. shape( )
46
50
) ;
51
+
47
52
ndassert ! (
48
- a. ndim( ) == strides_d . ndim( ) ,
53
+ a. ndim( ) == strides . ndim( ) ,
49
54
concat!(
50
55
"Stride dimension {} does not match array dimension {} " ,
51
56
"(with array of shape {:?})"
52
57
) ,
53
- strides_d . ndim( ) ,
58
+ strides . ndim( ) ,
54
59
a. ndim( ) ,
55
60
a. shape( )
56
61
) ;
57
- let mut size = a. dim ;
58
- for ( ( sz, & ws) , & stride) in size
59
- . slice_mut ( )
60
- . iter_mut ( )
61
- . zip ( window. slice ( ) )
62
- . zip ( strides_d. slice ( ) )
63
- {
64
- assert_ne ! ( ws, 0 , "window-size must not be zero!" ) ;
65
- assert_ne ! ( stride, 0 , "stride cannot have a dimension as zero!" ) ;
66
- // cannot use std::cmp::max(0, ..) since arithmetic underflow panics
67
- * sz = if * sz < ws {
68
- 0
69
- } else {
70
- ( ( * sz - ( ws - 1 ) - 1 ) / stride) + 1
71
- } ;
72
- }
73
- let window_strides = a. strides . clone ( ) ;
74
62
75
- let mut array_strides = a. strides . clone ( ) ;
76
- for ( arr_stride, ix_stride) in array_strides. slice_mut ( ) . iter_mut ( ) . zip ( strides_d. slice ( ) ) {
77
- * arr_stride *= ix_stride;
78
- }
63
+ let mut base = a;
64
+ base. slice_each_axis_inplace ( |ax_desc| {
65
+ let len = ax_desc. len ;
66
+ let wsz = window[ ax_desc. axis . index ( ) ] ;
67
+ let stride = strides[ ax_desc. axis . index ( ) ] ;
79
68
80
- unsafe {
81
- Windows {
82
- base : ArrayView :: new ( a. ptr , size, array_strides) ,
83
- window,
84
- strides : window_strides,
69
+ if len < wsz {
70
+ Slice :: new ( 0 , Some ( 0 ) , 1 )
71
+ } else {
72
+ Slice :: new ( 0 , Some ( ( len - wsz + 1 ) as isize ) , stride as isize )
85
73
}
74
+ } ) ;
75
+
76
+ Windows {
77
+ base,
78
+ window,
79
+ strides : window_strides,
86
80
}
87
81
}
88
82
}
0 commit comments