2 * Copyright 2008-2013 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <thrust/detail/config.h>
20 #include <thrust/pair.h>
21 #include <thrust/detail/function.h>
22 #include <thrust/iterator/iterator_traits.h>
39 template<typename RandomAccessIterator, typename Size, typename T, typename BinaryPredicate>
41 RandomAccessIterator lower_bound_n(RandomAccessIterator first,
47 thrust::detail::host_device_function<
56 if(wrapped_comp(first[i], val))
69 // XXX generalize these upon implementation of scalar::distance & scalar::advance
71 template<typename RandomAccessIterator, typename T, typename BinaryPredicate>
73 RandomAccessIterator lower_bound(RandomAccessIterator first, RandomAccessIterator last,
77 typename thrust::iterator_difference<RandomAccessIterator>::type n = last - first;
78 return lower_bound_n(first, n, val, comp);
81 template<typename RandomAccessIterator, typename Size, typename T, typename BinaryPredicate>
83 RandomAccessIterator upper_bound_n(RandomAccessIterator first,
89 thrust::detail::host_device_function<
98 if(wrapped_comp(val, first[i]))
108 return first + start;
111 template<typename RandomAccessIterator, typename T, typename BinaryPredicate>
113 RandomAccessIterator upper_bound(RandomAccessIterator first, RandomAccessIterator last,
115 BinaryPredicate comp)
117 typename thrust::iterator_difference<RandomAccessIterator>::type n = last - first;
118 return upper_bound_n(first, n, val, comp);
121 template<typename RandomAccessIterator, typename T, typename BinaryPredicate>
123 pair<RandomAccessIterator,RandomAccessIterator>
124 equal_range(RandomAccessIterator first, RandomAccessIterator last,
126 BinaryPredicate comp)
128 RandomAccessIterator lb = thrust::system::detail::generic::scalar::lower_bound(first, last, val, comp);
129 return thrust::make_pair(lb, thrust::system::detail::generic::scalar::upper_bound(lb, last, val, comp));
133 template<typename RandomAccessIterator, typename T, typename Compare>
135 bool binary_search(RandomAccessIterator first, RandomAccessIterator last, const T &value, Compare comp)
137 RandomAccessIterator iter = thrust::system::detail::generic::scalar::lower_bound(first, last, value, comp);
140 thrust::detail::host_device_function<
143 > wrapped_comp(comp);
145 return iter != last && !wrapped_comp(value,*iter);
158 #include <thrust/system/detail/generic/scalar/binary_search.inl>