Skip to content

Commit 02cb20a

Browse files
committed
completed implementation
1 parent e2a0005 commit 02cb20a

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/_arraykit.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,7 +3337,7 @@ slice_to_ascending_slice(PyObject *Py_UNUSED(m), PyObject *args) {
33373337
return NULL;
33383338
}
33393339

3340-
Py_ssize_t len = -1;
3340+
Py_ssize_t step_count = -1;
33413341
Py_ssize_t start = 0;
33423342
Py_ssize_t stop = 0;
33433343
Py_ssize_t step = 0;
@@ -3349,17 +3349,15 @@ slice_to_ascending_slice(PyObject *Py_UNUSED(m), PyObject *args) {
33493349
Py_INCREF(slice);
33503350
return slice;
33513351
}
3352-
len = PySlice_AdjustIndices(
3352+
step_count = PySlice_AdjustIndices(
33533353
PyLong_AsSsize_t(size),
33543354
&start,
33553355
&stop,
33563356
step);
3357-
// AK_DEBUG_MSG_OBJ("resolved slice", Py_BuildValue("nnnn", pos, stop, step, len));
3358-
3359-
// real stop is (len-1); len is negative
33603357

33613358
PyObject* asc_stop = PyLong_FromSsize_t(start + 1);
3362-
PyObject* asc_start = PyLong_FromSsize_t(start + (step * (len - 1)));
3359+
// step will be negative; shift original start value down to find new start
3360+
PyObject* asc_start = PyLong_FromSsize_t(start + (step * (step_count - 1)));
33633361
PyObject* asc_step = PyLong_FromSsize_t(-step);
33643362

33653363
// might be NULL, let return

test/test_util.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,5 +730,20 @@ def test_slice_to_ascending_slice_d(self) -> None:
730730
slice(4, 11, 2),
731731
)
732732

733+
def test_slice_to_ascending_slice_e(self) -> None:
734+
for slc, size in (
735+
(slice(10, 2, -2), 12),
736+
(slice(12, 2, -3), 12),
737+
(slice(12, None, -4), 12),
738+
(slice(76, 12, -8), 100),
739+
(slice(81, 33, -12), 100),
740+
(slice(97, 6, -7), 101),
741+
):
742+
self.assertEqual(
743+
slice_to_ascending_slice(slc, size),
744+
slice_to_ascending_slice_ref(slc, size),
745+
)
746+
747+
733748
if __name__ == '__main__':
734749
unittest.main()

0 commit comments

Comments
 (0)