@@ -1244,68 +1244,24 @@ bool convert_to_string(PathIterator &path,
12441244}
12451245
12461246template <class T >
1247- struct _is_sorted
1247+ bool is_sorted (PyArrayObject *array)
12481248{
1249- bool operator ()(PyArrayObject *array)
1250- {
1251- npy_intp size;
1252- npy_intp i;
1253- T last_value = -INFINITY;
1254- T current_value;
1255-
1256- size = PyArray_DIM (array, 0 );
1257-
1258- for (i = 0 ; i < size; ++i) {
1259- last_value = *((T *)PyArray_GETPTR1 (array, i));
1260- if (!std::isnan (last_value)) {
1261- break ;
1262- }
1263- }
1264-
1265- if (i == size) {
1266- // The whole array is non-finite
1267- return false ;
1268- }
1269-
1270- for (; i < size; ++i) {
1271- current_value = *((T *)PyArray_GETPTR1 (array, i));
1272- if (!std::isnan (current_value)) {
1273- if (current_value < last_value) {
1274- return false ;
1275- }
1276- last_value = current_value;
1277- }
1278- }
1279-
1280- return true ;
1281- }
1282- };
1283-
1284-
1285- template <class T >
1286- struct _is_sorted_int
1287- {
1288- bool operator ()(PyArrayObject *array)
1289- {
1290- npy_intp size;
1291- npy_intp i;
1292- T last_value;
1293- T current_value;
1294-
1295- size = PyArray_DIM (array, 0 );
1296-
1297- last_value = *((T *)PyArray_GETPTR1 (array, 0 ));
1298-
1299- for (i = 1 ; i < size; ++i) {
1300- current_value = *((T *)PyArray_GETPTR1 (array, i));
1301- if (current_value < last_value) {
1249+ npy_intp size = PyArray_DIM (array, 0 );
1250+ using limits = std::numeric_limits<T>;
1251+ T last = limits::has_infinity ? -limits::infinity () : limits::min ();
1252+
1253+ for (npy_intp i = 0 ; i < size; ++i) {
1254+ T current = *(T *)PyArray_GETPTR1 (array, i);
1255+ // The following tests !isnan(current), but also works for integral
1256+ // types. (The isnan(IntegralType) overload is absent on MSVC.)
1257+ if (current == current) {
1258+ if (current < last) {
13021259 return false ;
13031260 }
1304- last_value = current_value ;
1261+ last = current ;
13051262 }
1306-
1307- return true ;
13081263 }
1264+ return true ;
13091265};
13101266
13111267
0 commit comments