thrust::upper_bound
Defined in thrust/binary_search.h
-
template<typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable>
ForwardIterator thrust::upper_bound(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, ForwardIterator first, ForwardIterator last, const LessThanComparable &value) upper_boundis a version of binary search: it attempts to find the element value in an ordered range[first, last). Specifically, it returns the last position where value could be inserted without violating the ordering. This version ofupper_boundusesoperator<for comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[first, i),value < *jisfalse.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
upper_boundto search for values in a ordered range using thethrust::deviceexecution policy for parallelism:#include <thrust/binary_search.h> #include <thrust/device_vector.h> #include <thrust/execution_policy.h> ... thrust::device_vector<int> input(5); input[0] = 0; input[1] = 2; input[2] = 5; input[3] = 7; input[4] = 8; thrust::upper_bound(thrust::device, input.begin(), input.end(), 0); // returns input.begin() + 1 thrust::upper_bound(thrust::device, input.begin(), input.end(), 1); // returns input.begin() + 1 thrust::upper_bound(thrust::device, input.begin(), input.end(), 2); // returns input.begin() + 2 thrust::upper_bound(thrust::device, input.begin(), input.end(), 3); // returns input.begin() + 2 thrust::upper_bound(thrust::device, input.begin(), input.end(), 8); // returns input.end() thrust::upper_bound(thrust::device, input.begin(), input.end(), 9); // returns input.end()
See also
lower_boundSee also
equal_rangeSee also
Binary Search- Parameters
exec – The execution policy to use for parallelization.
first – The beginning of the ordered sequence.
last – The end of the ordered sequence.
value – The value to be searched.
- Template Parameters
DerivedPolicy – The name of the derived execution policy.
ForwardIterator – is a model of Forward Iterator.
LessThanComparable – is a model of LessThanComparable.
- Returns
The furthermost iterator
i, such thatvalue < *iisfalse.