diff options
author | jaimefrio <jaime.frio@gmail.com> | 2014-02-11 16:37:56 -0800 |
---|---|---|
committer | jaimefrio <jaime.frio@gmail.com> | 2014-02-11 16:38:18 -0800 |
commit | c8975ff262292d760b533bb433de50b686b15f9e (patch) | |
tree | ef14646da99621a16ffd60bd256b655f4a96cc5d /numpy/lib/src | |
parent | f65593dd69cae9b0fb30c06c07868dafb8436d11 (diff) | |
download | numpy-c8975ff262292d760b533bb433de50b686b15f9e.tar.gz |
MAINT: rewrote `check_array_monotonic` following @charris suggestion to
minimize indexing.
Diffstat (limited to 'numpy/lib/src')
-rw-r--r-- | numpy/lib/src/_compiled_base.c | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/numpy/lib/src/_compiled_base.c b/numpy/lib/src/_compiled_base.c index c99a14975..c91855e81 100644 --- a/numpy/lib/src/_compiled_base.c +++ b/numpy/lib/src/_compiled_base.c @@ -68,37 +68,40 @@ decr_slot_right_(double x, double * bins, npy_intp lbins) static int check_array_monotonic(const double *a, npy_int lena) { - npy_intp idx = 0; - npy_intp next_idx = 1; + npy_intp i; + double next; + double last = a[0]; - /* - * After decrementing lena, next_idx < lena means both indices will - * still be in bounds after incrementing them. - */ - lena--; + /* Skip repeated values at the beginning of the array */ + for (i = 1; (i < lena) && (a[i] == last); i++); - /* Ignore repeating values at the beginning of the array */ - for (; (next_idx < lena) && a[idx] == a[next_idx]; idx = next_idx++); + if (i == lena) { + /* all bin edges hold the same value */ + return 1; + } - if (a[idx] > a[next_idx]) { - /* Possibly monotonic decreasing */ - while(next_idx < lena) { - idx = next_idx++; - if (a[idx] < a[next_idx]) { + next = a[i]; + if (last < next) { + /* Possibly monotonic increasing */ + for (i += 1; i < lena; i++) { + last = next; + next = a[i]; + if (last > next) { return 0; } } - return -1; + return 1; } else { - /* Possibly monotonic increasing, including all bins equal */ - while(next_idx < lena) { - idx = next_idx++; - if (a[idx] > a[next_idx]) { + /* last > next, possibly monotonic decreasing */ + for (i += 1; i < lena; i++) { + last = next; + next = a[i]; + if (last < next) { return 0; } } - return 1; + return -1; } } |