11#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
22#include <Python.h>
33#include <math.h>
4- #define PY_SSIZE_T_CLEAN
54#include <numpy/arrayobject.h>
65#include <numpy/npy_math.h>
76
@@ -57,25 +56,27 @@ static PyObject *largest_triangle_one_bucket(PyObject *self, PyObject *args) {
5756 double * x = (double * )PyArray_DATA (x_array );
5857 double * y = (double * )PyArray_DATA (y_array );
5958
60- npy_intp dims [1 ] = {threshold };
61- PyArrayObject * x_result = (PyArrayObject * )PyArray_Empty (
62- 1 , dims , PyArray_DescrFromType (NPY_DOUBLE ), 0 );
63- PyArrayObject * y_result = (PyArrayObject * )PyArray_Empty (
64- 1 , dims , PyArray_DescrFromType (NPY_DOUBLE ), 0 );
65-
66- double * x_result_data = (double * )PyArray_DATA ((PyArrayObject * )x_result );
67- double * y_result_data = (double * )PyArray_DATA ((PyArrayObject * )y_result );
59+ double * result_x = (double * )malloc (threshold * sizeof (double ));
60+ double * result_y = (double * )malloc (threshold * sizeof (double ));
61+ if (!result_x || !result_y ) {
62+ PyErr_SetString (PyExc_MemoryError ,
63+ "Failed to allocate memory for result arrays." );
64+ free (result_x );
65+ free (result_y );
66+ goto fail ;
67+ }
6868
6969 // Add the first point and last
70- x_result_data [0 ] = npy_isfinite (x [0 ]) ? x [0 ] : 0.0 ;
71- y_result_data [0 ] = npy_isfinite (y [0 ]) ? y [0 ] : 0.0 ;
72- x_result_data [threshold - 1 ] =
70+ result_x [0 ] = npy_isfinite (x [0 ]) ? x [0 ] : 0.0 ;
71+ result_y [0 ] = npy_isfinite (y [0 ]) ? y [0 ] : 0.0 ;
72+ result_x [threshold - 1 ] =
7373 npy_isfinite (x [len_points - 1 ]) ? x [len_points - 1 ] : 0.0 ;
74- y_result_data [threshold - 1 ] =
74+ result_y [threshold - 1 ] =
7575 npy_isfinite (y [len_points - 1 ]) ? y [len_points - 1 ] : 0.0 ;
7676
7777 double bucket_size = (double )(len_points - 2 ) / (double )(threshold - 2 );
7878
79+ Py_BEGIN_ALLOW_THREADS ;
7980 // Main loop
8081 for (npy_intp i = 1 ; i < threshold - 1 ; i ++ ) {
8182 npy_intp start_index = (npy_intp )floor ((double )i * bucket_size );
@@ -95,18 +96,24 @@ static PyObject *largest_triangle_one_bucket(PyObject *self, PyObject *args) {
9596 max_area_index = j ;
9697 }
9798 }
98- x_result_data [i ] = (double )x [max_area_index ];
99- y_result_data [i ] = (double )y [max_area_index ];
99+ result_x [i ] = (double )x [max_area_index ];
100+ result_y [i ] = (double )y [max_area_index ];
100101 }
102+ Py_END_ALLOW_THREADS ;
101103
102- // Return x and y arrays as a tuple
103- PyObject * result = PyTuple_Pack (2 , x_result , y_result );
104- // Clean up references
104+ npy_intp dims [1 ] = {threshold };
105+ PyObject * npx =
106+ PyArray_SimpleNewFromData (1 , dims , NPY_DOUBLE , (void * )result_x );
107+ PyObject * npy =
108+ PyArray_SimpleNewFromData (1 , dims , NPY_DOUBLE , (void * )result_y );
109+ PyArray_ENABLEFLAGS ((PyArrayObject * )npx , NPY_ARRAY_OWNDATA );
110+ PyArray_ENABLEFLAGS ((PyArrayObject * )npy , NPY_ARRAY_OWNDATA );
111+
112+ PyObject * result = PyTuple_Pack (2 , npx , npy );
105113 Py_DECREF (x_array );
106114 Py_DECREF (y_array );
107- Py_DECREF (x_result );
108- Py_DECREF (y_result );
109-
115+ Py_DECREF (npx );
116+ Py_DECREF (npy );
110117 return result ;
111118
112119fail :
0 commit comments