Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves https://github.com/pypr/cyarray/issues/13 #14

Merged
merged 6 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade pip setuptools wheel
python -m pip install -r requirements.txt
python setup.py install
pip install .
- name: Run tests
run: pytest -v --pyargs cyarray
1 change: 1 addition & 0 deletions cyarray/carray.pxd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This file (carray.pxd) has been generated automatically.
# DO NOT modify this file
# To make changes modify the source templates (carray.pxd.mako) and regenerate
#cython: language_level=3
"""
Implementation of resizeable arrays of different types in Cython.

Expand Down
1 change: 1 addition & 0 deletions cyarray/carray.pxd.mako
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type_info = [
%># This file (carray.pxd) has been generated automatically.
# DO NOT modify this file
# To make changes modify the source templates (carray.pxd.mako) and regenerate
#cython: language_level=3
"""
Implementation of resizeable arrays of different types in Cython.

Expand Down
28 changes: 20 additions & 8 deletions cyarray/carray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# DO NOT modify this file
# To make changes modify the source templates (carray.pxd.mako) and regenerate
# distutils: language=c++
# cython: embedsignature=True
# cython: embedsignature=True, language_level=3
"""
Implementation of resizeable arrays of different types in Cython.

Expand Down Expand Up @@ -65,6 +65,18 @@ cdef extern from "numpy/arrayobject.h":
cdef extern from "stdlib.h":
void *memcpy(void *dst, void *src, long n) nogil


# Cython>= 3.0 now disallows relpacing the guts of a numpy array.
# Workaround: Thanks https://github.com/rainwoodman/pandas/blob/05d3fe2402e4563124e7060837ded7513ab5bca7/pandas/_libs/reduction.pyx#L27 # noqa: E501
# FIXME: FIND A CLEANER SOLUTION
cdef extern from *:
"""
static void PyArray_SET_DATA(PyArrayObject *arr, char * data) {
arr->data = data;
}
"""
void PyArray_SET_DATA(np.ndarray arr, char * data) nogil

# numpy module initialization call
_import_array()

Expand All @@ -77,9 +89,9 @@ cdef inline long aligned(long n, int item_size) nogil:
return n
else:
if 64%item_size == 0:
return (n*item_size/64 + 1)*64/item_size
return (n*item_size//64 + 1)*64//item_size
else:
return (n*item_size/64 + 1)*64
return (n*item_size//64 + 1)*64

cpdef long py_aligned(long n, int item_size):
"""Align `n` items each having size (in bytes) `item_size` to
Expand Down Expand Up @@ -451,7 +463,7 @@ cdef class IntArray(BaseArray):
if self._old_data != NULL:
self.data = self._old_data
self._old_data = NULL
self._npy_array.data = <char *>self.data
PyArray_SET_DATA(self._npy_array, <char *>self.data)

cdef void c_resize(self, long size) nogil:
cdef PyArrayObject* arr = <PyArrayObject*>self._npy_array
Expand Down Expand Up @@ -956,7 +968,7 @@ cdef class UIntArray(BaseArray):
if self._old_data != NULL:
self.data = self._old_data
self._old_data = NULL
self._npy_array.data = <char *>self.data
PyArray_SET_DATA(self._npy_array, <char *>self.data)

cdef void c_resize(self, long size) nogil:
cdef PyArrayObject* arr = <PyArrayObject*>self._npy_array
Expand Down Expand Up @@ -1461,7 +1473,7 @@ cdef class LongArray(BaseArray):
if self._old_data != NULL:
self.data = self._old_data
self._old_data = NULL
self._npy_array.data = <char *>self.data
PyArray_SET_DATA(self._npy_array, <char *>self.data)

cdef void c_resize(self, long size) nogil:
cdef PyArrayObject* arr = <PyArrayObject*>self._npy_array
Expand Down Expand Up @@ -1966,7 +1978,7 @@ cdef class FloatArray(BaseArray):
if self._old_data != NULL:
self.data = self._old_data
self._old_data = NULL
self._npy_array.data = <char *>self.data
PyArray_SET_DATA(self._npy_array, <char *>self.data)

cdef void c_resize(self, long size) nogil:
cdef PyArrayObject* arr = <PyArrayObject*>self._npy_array
Expand Down Expand Up @@ -2471,7 +2483,7 @@ cdef class DoubleArray(BaseArray):
if self._old_data != NULL:
self.data = self._old_data
self._old_data = NULL
self._npy_array.data = <char *>self.data
PyArray_SET_DATA(self._npy_array, <char *>self.data)

cdef void c_resize(self, long size) nogil:
cdef PyArrayObject* arr = <PyArrayObject*>self._npy_array
Expand Down
20 changes: 16 additions & 4 deletions cyarray/carray.pyx.mako
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type_info = [
# DO NOT modify this file
# To make changes modify the source templates (carray.pxd.mako) and regenerate
# distutils: language=c++
# cython: embedsignature=True
# cython: embedsignature=True, language_level=3
"""
Implementation of resizeable arrays of different types in Cython.

Expand Down Expand Up @@ -73,6 +73,18 @@ cdef extern from "numpy/arrayobject.h":
cdef extern from "stdlib.h":
void *memcpy(void *dst, void *src, long n) nogil


# Cython>= 3.0 now disallows relpacing the guts of a numpy array.
# Workaround: Thanks https://github.com/rainwoodman/pandas/blob/05d3fe2402e4563124e7060837ded7513ab5bca7/pandas/_libs/reduction.pyx#L27 # noqa: E501
# FIXME: FIND A CLEANER SOLUTION
cdef extern from *:
"""
static void PyArray_SET_DATA(PyArrayObject *arr, char * data) {
arr->data = data;
}
"""
void PyArray_SET_DATA(np.ndarray arr, char * data) nogil

# numpy module initialization call
_import_array()

Expand All @@ -85,9 +97,9 @@ cdef inline long aligned(long n, int item_size) nogil:
return n
else:
if 64%item_size == 0:
return (n*item_size/64 + 1)*64/item_size
return (n*item_size//64 + 1)*64//item_size
else:
return (n*item_size/64 + 1)*64
return (n*item_size//64 + 1)*64

cpdef long py_aligned(long n, int item_size):
"""Align `n` items each having size (in bytes) `item_size` to
Expand Down Expand Up @@ -463,7 +475,7 @@ cdef class ${CLASSNAME}(BaseArray):
if self._old_data != NULL:
self.data = self._old_data
self._old_data = NULL
self._npy_array.data = <char *>self.data
PyArray_SET_DATA(self._npy_array, <char *>self.data)

cdef void c_resize(self, long size) nogil:
cdef PyArrayObject* arr = <PyArrayObject*>self._npy_array
Expand Down
12 changes: 6 additions & 6 deletions cyarray/tests/test_carray.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,22 @@ def test_remove(self):
l1 = LongArray(10)
l1.set_data(numpy.arange(10))
rem = [0, 4, 3]
l1.remove(numpy.array(rem, dtype=numpy.int))
l1.remove(numpy.array(rem, dtype=int))
self.assertEqual(l1.length, 7)
self.assertEqual(numpy.allclose([7, 1, 2, 8, 9, 5, 6],
l1.get_npy_array()), True)

l1.remove(numpy.array(rem, dtype=numpy.int))
l1.remove(numpy.array(rem, dtype=int))
self.assertEqual(l1.length, 4)
self.assertEqual(numpy.allclose(
[6, 1, 2, 5], l1.get_npy_array()), True)

rem = [0, 1, 3]
l1.remove(numpy.array(rem, dtype=numpy.int))
l1.remove(numpy.array(rem, dtype=int))
self.assertEqual(l1.length, 1)
self.assertEqual(numpy.allclose([2], l1.get_npy_array()), True)

l1.remove(numpy.array([0], dtype=numpy.int))
l1.remove(numpy.array([0], dtype=int))
self.assertEqual(l1.length, 0)
self.assertEqual(len(l1.get_npy_array()), 0)

Expand All @@ -250,7 +250,7 @@ def test_remove_with_strides(self):

# When
rem = [3, 1]
l1.remove(numpy.array(rem, dtype=numpy.int), stride=3)
l1.remove(numpy.array(rem, dtype=int), stride=3)

# Then
self.assertEqual(l1.length, 6)
Expand All @@ -263,7 +263,7 @@ def test_remove_with_strides(self):

# When
rem = [0, 2]
l1.remove(numpy.array(rem, dtype=numpy.int), stride=3)
l1.remove(numpy.array(rem, dtype=int), stride=3)

# Then
self.assertEqual(l1.length, 6)
Expand Down