@@ -20,12 +20,6 @@ pub fn stride_offset(n: Ix, stride: Ix) -> isize {
20
20
( n as isize ) * ( ( stride as Ixs ) as isize )
21
21
}
22
22
23
- /// Check whether `stride` is strictly positive
24
- #[ inline]
25
- fn stride_is_positive ( stride : Ix ) -> bool {
26
- ( stride as Ixs ) > 0
27
- }
28
-
29
23
/// Check whether the given `dim` and `stride` lead to overlapping indices
30
24
///
31
25
/// There is overlap if, when iterating through the dimensions in the order
@@ -61,33 +55,42 @@ pub fn dim_stride_overlap<D: Dimension>(dim: &D, strides: &D) -> bool {
61
55
pub fn can_index_slice < A , D : Dimension > ( data : & [ A ] , dim : & D , strides : & D )
62
56
-> Result < ( ) , ShapeError >
63
57
{
64
- if strides. slice ( ) . iter ( ) . cloned ( ) . all ( stride_is_positive) {
65
- if dim. size_checked ( ) . is_none ( ) {
58
+ // check lengths of axes.
59
+ let len = match dim. size_checked ( ) {
60
+ Some ( l) => l,
61
+ None => return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ,
62
+ } ;
63
+ // check if strides are strictly positive (zero ok for len 0)
64
+ for & s in strides. slice ( ) {
65
+ let s = s as Ixs ;
66
+ if s < 1 && ( len != 0 || s < 0 ) {
67
+ return Err ( from_kind ( ErrorKind :: Unsupported ) ) ;
68
+ }
69
+ }
70
+ if len == 0 {
71
+ return Ok ( ( ) ) ;
72
+ }
73
+ // check that the maximum index is in bounds
74
+ let mut last_index = dim. clone ( ) ;
75
+ for mut index in last_index. slice_mut ( ) . iter_mut ( ) {
76
+ * index -= 1 ;
77
+ }
78
+ if let Some ( offset) = stride_offset_checked_arithmetic ( dim,
79
+ strides,
80
+ & last_index)
81
+ {
82
+ // offset is guaranteed to be positive so no issue converting
83
+ // to usize here
84
+ if ( offset as usize ) >= data. len ( ) {
66
85
return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ;
67
86
}
68
- let mut last_index = dim. clone ( ) ;
69
- for mut index in last_index. slice_mut ( ) . iter_mut ( ) {
70
- * index -= 1 ;
71
- }
72
- if let Some ( offset) = stride_offset_checked_arithmetic ( dim,
73
- strides,
74
- & last_index)
75
- {
76
- // offset is guaranteed to be positive so no issue converting
77
- // to usize here
78
- if ( offset as usize ) >= data. len ( ) {
79
- return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ;
80
- }
81
- if dim_stride_overlap ( dim, strides) {
82
- return Err ( from_kind ( ErrorKind :: Unsupported ) ) ;
83
- }
84
- } else {
85
- return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ;
87
+ if dim_stride_overlap ( dim, strides) {
88
+ return Err ( from_kind ( ErrorKind :: Unsupported ) ) ;
86
89
}
87
- Ok ( ( ) )
88
90
} else {
89
- return Err ( from_kind ( ErrorKind :: Unsupported ) ) ;
91
+ return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ;
90
92
}
93
+ Ok ( ( ) )
91
94
}
92
95
93
96
/// Return stride offset for this dimension and index.
0 commit comments