Skip to content
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
70 changes: 13 additions & 57 deletions src/_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -1244,68 +1244,24 @@ bool convert_to_string(PathIterator &path,
}

template<class T>
struct _is_sorted
bool is_sorted(PyArrayObject *array)
{
bool operator()(PyArrayObject *array)
{
npy_intp size;
npy_intp i;
T last_value = -INFINITY;
T current_value;

size = PyArray_DIM(array, 0);

for (i = 0; i < size; ++i) {
last_value = *((T *)PyArray_GETPTR1(array, i));
if (!std::isnan(last_value)) {
break;
}
}

if (i == size) {
// The whole array is non-finite
return false;
}

for (; i < size; ++i) {
current_value = *((T *)PyArray_GETPTR1(array, i));
if (!std::isnan(current_value)) {
if (current_value < last_value) {
return false;
}
last_value = current_value;
}
}

return true;
}
};


template<class T>
struct _is_sorted_int
{
bool operator()(PyArrayObject *array)
{
npy_intp size;
npy_intp i;
T last_value;
T current_value;

size = PyArray_DIM(array, 0);

last_value = *((T *)PyArray_GETPTR1(array, 0));

for (i = 1; i < size; ++i) {
current_value = *((T *)PyArray_GETPTR1(array, i));
if (current_value < last_value) {
npy_intp size = PyArray_DIM(array, 0);
using limits = std::numeric_limits<T>;
T last = limits::has_infinity ? -limits::infinity() : limits::min();

for (npy_intp i = 0; i < size; ++i) {
T current = *(T *)PyArray_GETPTR1(array, i);
// The following tests !isnan(current), but also works for integral
// types. (The isnan(IntegralType) overload is absent on MSVC.)
if (current == current) {
if (current < last) {
return false;
}
last_value = current_value;
last = current;
}

return true;
}
return true;
};


Expand Down
47 changes: 11 additions & 36 deletions src/_path_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,54 +807,29 @@ static PyObject *Py_is_sorted(PyObject *self, PyObject *obj)

/* Handle just the most common types here, otherwise coerce to
double */
switch(PyArray_TYPE(array)) {
switch (PyArray_TYPE(array)) {
case NPY_INT:
{
_is_sorted_int<npy_int> is_sorted;
result = is_sorted(array);
}
result = is_sorted<npy_int>(array);
break;

case NPY_LONG:
{
_is_sorted_int<npy_long> is_sorted;
result = is_sorted(array);
}
result = is_sorted<npy_long>(array);
break;

case NPY_LONGLONG:
{
_is_sorted_int<npy_longlong> is_sorted;
result = is_sorted(array);
}
result = is_sorted<npy_longlong>(array);
break;

case NPY_FLOAT:
{
_is_sorted<npy_float> is_sorted;
result = is_sorted(array);
}
result = is_sorted<npy_float>(array);
break;

case NPY_DOUBLE:
{
_is_sorted<npy_double> is_sorted;
result = is_sorted(array);
}
result = is_sorted<npy_double>(array);
break;

default:
{
Py_DECREF(array);
array = (PyArrayObject *)PyArray_FromObject(obj, NPY_DOUBLE, 1, 1);

if (array == NULL) {
return NULL;
}

_is_sorted<npy_double> is_sorted;
result = is_sorted(array);
Py_DECREF(array);
array = (PyArrayObject *)PyArray_FromObject(obj, NPY_DOUBLE, 1, 1);
if (array == NULL) {
return NULL;
}
result = is_sorted<npy_double>(array);
}

Py_DECREF(array);
Expand Down