Commit 4c5cc91b authored by Jeroen Demeyer's avatar Jeroen Demeyer Committed by Stefan Behnel

Refactor SliceTupleAndList utility code (GH-3087)

parent de176c65
......@@ -735,6 +735,55 @@ bad:
}
/////////////// TupleAndListFromArray.proto ///////////////
#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n);
static CYTHON_INLINE PyObject* __Pyx_PyList_FromArray(PyObject *const *src, Py_ssize_t n);
#endif
/////////////// TupleAndListFromArray ///////////////
//@substitute: naming
#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE void __Pyx_copy_object_array(PyObject *const *CYTHON_RESTRICT src, PyObject** CYTHON_RESTRICT dest, Py_ssize_t length) {
PyObject *v;
Py_ssize_t i;
for (i = 0; i < length; i++) {
v = dest[i] = src[i];
Py_INCREF(v);
}
}
static CYTHON_INLINE PyObject *
__Pyx_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
{
PyObject *res;
if (n <= 0) {
Py_INCREF($empty_tuple);
return $empty_tuple;
}
res = PyTuple_New(n);
if (unlikely(res == NULL)) return NULL;
__Pyx_copy_object_array(src, ((PyTupleObject*)res)->ob_item, n);
return res;
}
static CYTHON_INLINE PyObject *
__Pyx_PyList_FromArray(PyObject *const *src, Py_ssize_t n)
{
PyObject *res;
if (n <= 0) {
return PyList_New(0);
}
res = PyList_New(n);
if (unlikely(res == NULL)) return NULL;
__Pyx_copy_object_array(src, ((PyListObject*)res)->ob_item, n);
return res;
}
#endif
/////////////// SliceTupleAndList.proto ///////////////
#if CYTHON_COMPILING_IN_CPYTHON
......@@ -746,6 +795,7 @@ static CYTHON_INLINE PyObject* __Pyx_PyTuple_GetSlice(PyObject* src, Py_ssize_t
#endif
/////////////// SliceTupleAndList ///////////////
//@requires: TupleAndListFromArray
#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE void __Pyx_crop_slice(Py_ssize_t* _start, Py_ssize_t* _stop, Py_ssize_t* _length) {
......@@ -766,32 +816,12 @@ static CYTHON_INLINE void __Pyx_crop_slice(Py_ssize_t* _start, Py_ssize_t* _stop
*_stop = stop;
}
static CYTHON_INLINE void __Pyx_copy_object_array(PyObject** CYTHON_RESTRICT src, PyObject** CYTHON_RESTRICT dest, Py_ssize_t length) {
PyObject *v;
Py_ssize_t i;
for (i = 0; i < length; i++) {
v = dest[i] = src[i];
Py_INCREF(v);
}
}
{{for type in ['List', 'Tuple']}}
static CYTHON_INLINE PyObject* __Pyx_Py{{type}}_GetSlice(
PyObject* src, Py_ssize_t start, Py_ssize_t stop) {
PyObject* dest;
Py_ssize_t length = Py{{type}}_GET_SIZE(src);
__Pyx_crop_slice(&start, &stop, &length);
if (unlikely(length <= 0))
return Py{{type}}_New(0);
dest = Py{{type}}_New(length);
if (unlikely(!dest))
return NULL;
__Pyx_copy_object_array(
((Py{{type}}Object*)src)->ob_item + start,
((Py{{type}}Object*)dest)->ob_item,
length);
return dest;
return __Pyx_Py{{type}}_FromArray(((Py{{type}}Object*)src)->ob_item + start, length);
}
{{endfor}}
#endif
......
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