Skip to content

Commit 2bf599d

Browse files
committed
add method to check if strides and dimension can cause overlap
These need to be tested and integrated into the constructor with strides.
1 parent 160201f commit 2bf599d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/dimension.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,45 @@ pub fn stride_is_positive(stride: Ix) -> bool
1717
(stride as Ixs) > 0
1818
}
1919

20+
/// Return the axis ordering corresponding to the fastest variation
21+
///
22+
/// Assumes that no stride value appears twice. This cannot yield the correct
23+
/// result the strides are not positive.
24+
fn fastest_varying_order<D: Dimension>(strides: &D) -> D
25+
{
26+
let mut sorted = strides.clone();
27+
sorted.slice_mut().sort();
28+
let mut res = strides.clone();
29+
for (ind, val) in sorted.slice().iter().enumerate() {
30+
res.slice_mut()[ind] = sorted.slice()
31+
.binary_search(val)
32+
.expect("should be present");
33+
}
34+
res
35+
}
36+
37+
/// Check whether the given `dim` and `stride` lead to overlapping indices
38+
///
39+
/// There is overlap if, when iterating through the dimensions in the order
40+
/// of maximum variation, the current stride is inferior to the sum of all
41+
/// preceding strides multiplied by their corresponding dimensions.
42+
///
43+
/// The current implementation assumes strides to be positive
44+
pub fn dim_stride_overlap<D: Dimension>(dim: &D, strides: &D) -> bool
45+
{
46+
let order = fastest_varying_order(strides);
47+
48+
let mut sum = 0;
49+
for &ind in order.slice().iter() {
50+
let s = strides.slice()[ind];
51+
if (s as isize) < sum {
52+
return true;
53+
}
54+
sum += stride_offset(dim.slice()[ind], s);
55+
}
56+
false
57+
}
58+
2059
/// Check whether the given dimension and strides are memory safe
2160
/// to index the provided slice.
2261
///

0 commit comments

Comments
 (0)