Skip to content

Commit b0974ce

Browse files
committed
Refactor dimension::do_slice()
1 parent 0bdce23 commit b0974ce

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

src/dimension/mod.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use {Ix, Ixs};
9+
use {Ix, Ixs, Slice};
1010
use error::{from_kind, ErrorKind, ShapeError};
1111

1212
pub use self::dim::*;
@@ -222,25 +222,18 @@ pub fn abs_index(len: Ix, index: Ixs) -> Ix {
222222
}
223223
}
224224

225-
/// Modify dimension, stride and return data pointer offset
225+
/// Determines nonnegative start and end indices, and performs sanity checks.
226+
///
227+
/// The return value is (start, end, step).
226228
///
227229
/// **Panics** if stride is 0 or if any index is out of bounds.
228-
pub fn do_slice(
229-
dim: &mut Ix,
230-
stride: &mut Ix,
231-
start: Ixs,
232-
end: Option<Ixs>,
233-
step: Ixs,
234-
) -> isize {
235-
let mut offset = 0;
236-
237-
let axis_len = *dim;
230+
fn to_abs_slice(axis_len: usize, slice: Slice) -> (usize, usize, isize) {
231+
let Slice { start, end, step } = slice;
238232
let start = abs_index(axis_len, start);
239-
let mut end = abs_index(axis_len, end.unwrap_or(axis_len as Ixs));
233+
let mut end = abs_index(axis_len, end.unwrap_or(axis_len as isize));
240234
if end < start {
241235
end = start;
242236
}
243-
244237
ndassert!(
245238
start <= axis_len,
246239
"Slice begin {} is past end of axis of length {}",
@@ -253,15 +246,23 @@ pub fn do_slice(
253246
end,
254247
axis_len,
255248
);
249+
ndassert!(step != 0, "Slice stride must not be zero");
250+
(start, end, step)
251+
}
252+
253+
/// Modify dimension, stride and return data pointer offset
254+
///
255+
/// **Panics** if stride is 0 or if any index is out of bounds.
256+
pub fn do_slice(dim: &mut usize, stride: &mut usize, slice: Slice) -> isize {
257+
let (start, end, step) = to_abs_slice(*dim, slice);
256258

257259
let m = end - start;
258-
// stride
259-
let s = (*stride) as Ixs;
260+
let s = (*stride) as isize;
260261

261262
// Data pointer offset
262-
offset += stride_offset(start, *stride);
263+
let mut offset = stride_offset(start, *stride);
263264
// Adjust for strides
264-
ndassert!(step != 0, "Slice stride must not be zero");
265+
//
265266
// How to implement negative strides:
266267
//
267268
// Increase start pointer by
@@ -273,13 +274,13 @@ pub fn do_slice(
273274

274275
let s_prim = s * step;
275276

276-
let d = m / step.abs() as Ix;
277-
let r = m % step.abs() as Ix;
277+
let d = m / step.abs() as usize;
278+
let r = m % step.abs() as usize;
278279
let m_prim = d + if r > 0 { 1 } else { 0 };
279280

280281
// Update dimension and stride coordinate
281282
*dim = m_prim;
282-
*stride = s_prim as Ix;
283+
*stride = s_prim as usize;
283284

284285
offset
285286
}

src/impl_methods.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
357357
let offset = do_slice(
358358
&mut self.dim.slice_mut()[axis.index()],
359359
&mut self.strides.slice_mut()[axis.index()],
360-
indices.start,
361-
indices.end,
362-
indices.step,
360+
indices,
363361
);
364362
unsafe {
365363
self.ptr = self.ptr.offset(offset);

0 commit comments

Comments
 (0)