2
* Copyright 2008-2009 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/device/dereference.h>
22
#include <thrust/iterator/iterator_traits.h>
39
// XXX generalize these upon implementation of scalar::distance & scalar::advance
41
template<typename RandomAccessIterator, typename T, typename BinaryPredicate>
43
RandomAccessIterator lower_bound(RandomAccessIterator first, RandomAccessIterator last,
47
typedef typename thrust::iterator_difference<RandomAccessIterator>::type difference_type;
49
// XXX should read len = distance(first,last)
50
difference_type len = last - first;
54
difference_type half = len >> 1;
55
RandomAccessIterator middle = first;
57
// XXX should read advance(middle,half)
60
if(comp(dereference(middle), val))
75
template<typename RandomAccessIterator, typename T, typename BinaryPredicate>
77
RandomAccessIterator upper_bound(RandomAccessIterator first, RandomAccessIterator last,
81
typedef typename thrust::iterator_difference<RandomAccessIterator>::type difference_type;
83
// XXX should read len = distance(first,last)
84
difference_type len = last - first;
88
difference_type half = len >> 1;
89
RandomAccessIterator middle = first;
91
// XXX should read advance(middle,half)
94
if(comp(val, dereference(middle)))
102
len = len - half - 1;
109
template<typename RandomAccessIterator, typename T, typename BinaryPredicate>
111
pair<RandomAccessIterator,RandomAccessIterator>
112
equal_range(RandomAccessIterator first, RandomAccessIterator last,
114
BinaryPredicate comp)
116
RandomAccessIterator lb = thrust::detail::device::generic::scalar::lower_bound(first, last, val, comp);
117
return thrust::make_pair(lb, thrust::detail::device::generic::scalar::upper_bound(lb, last, val, comp));
121
template<typename RandomAccessIterator, typename T, typename Compare>
123
bool binary_search(RandomAccessIterator first, RandomAccessIterator last, const T &value, Compare comp)
125
RandomAccessIterator iter = thrust::detail::device::generic::scalar::lower_bound(first,last,value,comp);
126
return iter != last && !comp(value, *iter);
139
#include <thrust/detail/device/generic/scalar/binary_search.inl>