summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony LaTorre <tlatorre9@gmail.com>2011-09-06 23:57:18 -0400
committerAnthony LaTorre <tlatorre9@gmail.com>2011-09-06 23:57:18 -0400
commitc0d055366c18b2f3a71435c7c07ce0afe126f85e (patch)
treecd49fffa3414c2d45daaa832d16fc28fd86c2980
parent10de6b1fbba5cf906cafda1c4c704e6ade163956 (diff)
downloadchroma-c0d055366c18b2f3a71435c7c07ce0afe126f85e.tar.gz
chroma-c0d055366c18b2f3a71435c7c07ce0afe126f85e.tar.bz2
chroma-c0d055366c18b2f3a71435c7c07ce0afe126f85e.zip
fix devious assumption in searchsorted that if searching a length one array it assumed the array was meant to be in descending order; it now assumes ascending order and this assumption is documented.
-rw-r--r--src/sorting.h8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/sorting.h b/src/sorting.h
index 03eabc6..9fd52fe 100644
--- a/src/sorting.h
+++ b/src/sorting.h
@@ -55,6 +55,10 @@ piksrt2(int n, T *arr, U *brr)
}
}
+/* Returns the index in `arr` where `x` should be inserted in order to
+ maintain order. If `n` equals one, return the index such that, when
+ `x` is inserted, `arr` will be in ascending order.
+*/
template <class T>
__device__ unsigned long
searchsorted(unsigned long n, T *arr, const T &x)
@@ -65,12 +69,12 @@ searchsorted(unsigned long n, T *arr, const T &x)
jl = 0;
ju = n;
- ascnd = (arr[n-1] > arr[0]);
+ ascnd = (arr[n-1] >= arr[0]);
while (ju-jl > 1) {
jm = (ju+jl) >> 1;
- if (x >= arr[jm] == ascnd)
+ if ((x > arr[jm]) == ascnd)
jl = jm;
else
ju = jm;