Commit 5f6369ae authored by Stefan Behnel's avatar Stefan Behnel

Rewrite _unellipsify() helper function in memoryview code to speed it up and...

Rewrite _unellipsify() helper function in memoryview code to speed it up and reduce the generated C code.
parent 7491c235
...@@ -669,33 +669,28 @@ cdef tuple _unellipsify(object index, int ndim): ...@@ -669,33 +669,28 @@ cdef tuple _unellipsify(object index, int ndim):
Replace all ellipses with full slices and fill incomplete indices with Replace all ellipses with full slices and fill incomplete indices with
full slices. full slices.
""" """
if not isinstance(index, tuple): cdef Py_ssize_t idx
tup = (index,) tup = <tuple>index if isinstance(index, tuple) else (index,)
else:
tup = index
result = [] result = [slice(None)] * ndim
have_slices = False have_slices = False
seen_ellipsis = False seen_ellipsis = False
for idx, item in enumerate(tup): idx = 0
for item in tup:
if item is Ellipsis: if item is Ellipsis:
if not seen_ellipsis: if not seen_ellipsis:
result.extend([slice(None)] * (ndim - len(tup) + 1)) idx += ndim - len(tup)
seen_ellipsis = True seen_ellipsis = True
else:
result.append(slice(None))
have_slices = True have_slices = True
else: else:
if not isinstance(item, slice) and not PyIndex_Check(item): if isinstance(item, slice):
have_slices = True
elif not PyIndex_Check(item):
raise TypeError(f"Cannot index with type '{type(item)}'") raise TypeError(f"Cannot index with type '{type(item)}'")
result[idx] = item
idx += 1
have_slices = have_slices or isinstance(item, slice) nslices = ndim - idx
result.append(item)
nslices = ndim - len(result)
if nslices:
result.extend([slice(None)] * nslices)
return have_slices or nslices, tuple(result) return have_slices or nslices, tuple(result)
cdef assert_direct_dimensions(Py_ssize_t *suboffsets, int ndim): cdef assert_direct_dimensions(Py_ssize_t *suboffsets, int ndim):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment