Skip to content

Commit d02268f

Browse files
committed
implemented, tested reversed slice selector iterators
1 parent a054c63 commit d02268f

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/_arraykit.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4253,12 +4253,12 @@ BIIterSeq_new(BlockIndexObject *bi, PyObject* selector, int8_t reversed) {
42534253
PyArrayObject *a = (PyArrayObject *)selector;
42544254
if (PyArray_NDIM(a) != 1) {
42554255
PyErr_SetString(PyExc_TypeError, "Arrays must be 1-dimensional");
4256-
return NULL;
4256+
goto error;
42574257
}
42584258
char kind = PyArray_DESCR(a)->kind;
42594259
if (kind != 'i' && kind != 'u') {
42604260
PyErr_SetString(PyExc_TypeError, "Arrays must integer kind");
4261-
return NULL;
4261+
goto error;
42624262
}
42634263
bii->selector_len = PyArray_SIZE(a);
42644264
bii->selector_is_array = 1;
@@ -4269,9 +4269,13 @@ BIIterSeq_new(BlockIndexObject *bi, PyObject* selector, int8_t reversed) {
42694269
}
42704270
else {
42714271
PyErr_SetString(PyExc_TypeError, "Input type not supported");
4272-
return NULL;
4272+
goto error;
42734273
}
42744274
return (PyObject *)bii;
4275+
error:
4276+
Py_DECREF(bii->bi);
4277+
Py_DECREF(bii->selector);
4278+
return NULL;
42754279
}
42764280

42774281
static void
@@ -4387,7 +4391,6 @@ typedef struct BIIterSliceObject {
43874391
Py_ssize_t count; // count of , mutated in-place
43884392
// these are the normalized values truncated to the span of the bir_count; len is the realized length after extraction; step is always set to 1 if missing; len is 0 if no realized values
43894393
Py_ssize_t start;
4390-
Py_ssize_t stop;
43914394
Py_ssize_t step;
43924395
Py_ssize_t len;
43934396
} BIIterSliceObject;
@@ -4405,14 +4408,14 @@ BIIterSlice_new(BlockIndexObject *bi, PyObject* slice, int8_t reversed) {
44054408
Py_INCREF(slice);
44064409
bii->slice = slice;
44074410

4408-
bii->reversed = reversed;
44094411
bii->count = 0;
4412+
Py_ssize_t stop; // not needed
44104413

44114414
if (PySlice_Check(slice)) {
44124415
if (PySlice_GetIndicesEx(slice,
44134416
bi->bir_count,
44144417
&bii->start,
4415-
&bii->stop,
4418+
&stop,
44164419
&bii->step,
44174420
&bii->len)) {
44184421
goto error;
@@ -4424,6 +4427,10 @@ BIIterSlice_new(BlockIndexObject *bi, PyObject* slice, int8_t reversed) {
44244427
PyErr_SetString(PyExc_TypeError, "Input type not supported");
44254428
goto error;
44264429
}
4430+
if ((bii->reversed = reversed)) {
4431+
bii->start += (bii->step * (bii->len - 1));
4432+
bii->step *= -1;
4433+
}
44274434
return (PyObject *)bii;
44284435
error:
44294436
Py_DECREF(bii->bi);

test/test_block_index.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,5 +383,19 @@ def test_block_index_iter_select_slice_b(self) -> None:
383383
)
384384

385385
self.assertEqual(list(bi1.iter_select((slice(None, None, -1)))),
386-
[(2, 4), (2, 3), (2, 2), (2, 1)]
386+
[(2, 4), (2, 3), (2, 2), (2, 1), (2, 0), (1, 0), (0, 1), (0, 0)]
387+
)
388+
389+
def test_block_index_iter_select_slice_c(self) -> None:
390+
bi1 = BlockIndex()
391+
bi1.register(np.arange(4).reshape(2,2))
392+
bi1.register(np.arange(2))
393+
bi1.register(np.arange(6).reshape(2,3))
394+
395+
self.assertEqual(list(bi1.iter_select(slice(1,5))),
396+
[(0, 1), (1, 0), (2, 0), (2, 1)]
397+
)
398+
399+
self.assertEqual(list(reversed(bi1.iter_select(slice(1,5)))),
400+
[(2, 1), (2, 0), (1, 0), (0, 1)]
387401
)

0 commit comments

Comments
 (0)