@@ -350,41 +350,26 @@ Py_is_sorted_and_has_non_nan(py::object obj)
350
350
{
351
351
bool result;
352
352
353
- PyArrayObject *array = (PyArrayObject *)PyArray_CheckFromAny (
354
- obj.ptr (), NULL , 1 , 1 , NPY_ARRAY_NOTSWAPPED, NULL );
355
-
356
- if (array == NULL ) {
357
- throw py::error_already_set ();
353
+ py::array array = py::array::ensure (obj);
354
+ if (array.ndim () != 1 ) {
355
+ throw std::invalid_argument (" array must be 1D" );
358
356
}
359
357
358
+ auto dtype = array.dtype ();
360
359
/* Handle just the most common types here, otherwise coerce to double */
361
- switch (PyArray_TYPE (array)) {
362
- case NPY_INT:
363
- result = is_sorted_and_has_non_nan<npy_int>(array);
364
- break ;
365
- case NPY_LONG:
366
- result = is_sorted_and_has_non_nan<npy_long>(array);
367
- break ;
368
- case NPY_LONGLONG:
369
- result = is_sorted_and_has_non_nan<npy_longlong>(array);
370
- break ;
371
- case NPY_FLOAT:
372
- result = is_sorted_and_has_non_nan<npy_float>(array);
373
- break ;
374
- case NPY_DOUBLE:
375
- result = is_sorted_and_has_non_nan<npy_double>(array);
376
- break ;
377
- default :
378
- Py_DECREF (array);
379
- array = (PyArrayObject *)PyArray_FromObject (obj.ptr (), NPY_DOUBLE, 1 , 1 );
380
- if (array == NULL ) {
381
- throw py::error_already_set ();
382
- }
383
- result = is_sorted_and_has_non_nan<npy_double>(array);
360
+ if (dtype.equal (py::dtype::of<std::int32_t >())) {
361
+ result = is_sorted_and_has_non_nan<int32_t >(array);
362
+ } else if (dtype.equal (py::dtype::of<std::int64_t >())) {
363
+ result = is_sorted_and_has_non_nan<int64_t >(array);
364
+ } else if (dtype.equal (py::dtype::of<float >())) {
365
+ result = is_sorted_and_has_non_nan<float >(array);
366
+ } else if (dtype.equal (py::dtype::of<double >())) {
367
+ result = is_sorted_and_has_non_nan<double >(array);
368
+ } else {
369
+ array = py::array_t <double >::ensure (obj);
370
+ result = is_sorted_and_has_non_nan<double >(array);
384
371
}
385
372
386
- Py_DECREF (array);
387
-
388
373
return result;
389
374
}
390
375
0 commit comments