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.
18 /*! \file binary_search.inl
19 * \brief Inline file for binary_search.h.
22 #include <thrust/detail/config.h>
23 #include <thrust/binary_search.h>
24 #include <thrust/iterator/iterator_traits.h>
25 #include <thrust/system/detail/generic/select_system.h>
26 #include <thrust/system/detail/generic/binary_search.h>
27 #include <thrust/system/detail/adl/binary_search.h>
33 template <typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable>
34 ForwardIterator lower_bound(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
35 ForwardIterator first,
37 const LessThanComparable &value)
39 using thrust::system::detail::generic::lower_bound;
40 return lower_bound(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, value);
44 template<typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering>
45 ForwardIterator lower_bound(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
46 ForwardIterator first,
49 StrictWeakOrdering comp)
51 using thrust::system::detail::generic::lower_bound;
52 return lower_bound(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, value, comp);
56 template<typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable>
57 ForwardIterator upper_bound(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
58 ForwardIterator first,
60 const LessThanComparable &value)
62 using thrust::system::detail::generic::upper_bound;
63 return upper_bound(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, value);
67 template<typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering>
68 ForwardIterator upper_bound(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
69 ForwardIterator first,
72 StrictWeakOrdering comp)
74 using thrust::system::detail::generic::upper_bound;
75 return upper_bound(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, value, comp);
79 template <typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable>
80 bool binary_search(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
81 ForwardIterator first,
83 const LessThanComparable& value)
85 using thrust::system::detail::generic::binary_search;
86 return binary_search(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, value);
90 template <typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering>
91 bool binary_search(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
92 ForwardIterator first,
95 StrictWeakOrdering comp)
97 using thrust::system::detail::generic::binary_search;
98 return binary_search(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, value, comp);
102 template <typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering>
103 thrust::pair<ForwardIterator, ForwardIterator>
104 equal_range(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
105 ForwardIterator first,
106 ForwardIterator last,
108 StrictWeakOrdering comp)
110 using thrust::system::detail::generic::equal_range;
111 return equal_range(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, value, comp);
115 template <typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable>
116 thrust::pair<ForwardIterator, ForwardIterator>
117 equal_range(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
118 ForwardIterator first,
119 ForwardIterator last,
120 const LessThanComparable& value)
122 using thrust::system::detail::generic::equal_range;
123 return equal_range(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, value);
127 template <typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator>
128 OutputIterator lower_bound(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
129 ForwardIterator first,
130 ForwardIterator last,
131 InputIterator values_first,
132 InputIterator values_last,
133 OutputIterator output)
135 using thrust::system::detail::generic::lower_bound;
136 return lower_bound(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, values_first, values_last, output);
140 template <typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering>
141 OutputIterator lower_bound(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
142 ForwardIterator first,
143 ForwardIterator last,
144 InputIterator values_first,
145 InputIterator values_last,
146 OutputIterator output,
147 StrictWeakOrdering comp)
149 using thrust::system::detail::generic::lower_bound;
150 return lower_bound(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, values_first, values_last, output, comp);
154 template <typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator>
155 OutputIterator upper_bound(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
156 ForwardIterator first,
157 ForwardIterator last,
158 InputIterator values_first,
159 InputIterator values_last,
160 OutputIterator output)
162 using thrust::system::detail::generic::upper_bound;
163 return upper_bound(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, values_first, values_last, output);
167 template <typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering>
168 OutputIterator upper_bound(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
169 ForwardIterator first,
170 ForwardIterator last,
171 InputIterator values_first,
172 InputIterator values_last,
173 OutputIterator output,
174 StrictWeakOrdering comp)
176 using thrust::system::detail::generic::upper_bound;
177 return upper_bound(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, values_first, values_last, output, comp);
181 template <typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator>
182 OutputIterator binary_search(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
183 ForwardIterator first,
184 ForwardIterator last,
185 InputIterator values_first,
186 InputIterator values_last,
187 OutputIterator output)
189 using thrust::system::detail::generic::binary_search;
190 return binary_search(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, values_first, values_last, output);
194 template <typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering>
195 OutputIterator binary_search(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
196 ForwardIterator first,
197 ForwardIterator last,
198 InputIterator values_first,
199 InputIterator values_last,
200 OutputIterator output,
201 StrictWeakOrdering comp)
203 using thrust::system::detail::generic::binary_search;
204 return binary_search(thrust::detail::derived_cast(thrust::detail::strip_const(exec)), first, last, values_first, values_last, output, comp);
208 //////////////////////
209 // Scalar Functions //
210 //////////////////////
212 template <typename ForwardIterator, typename LessThanComparable>
213 ForwardIterator lower_bound(ForwardIterator first,
214 ForwardIterator last,
215 const LessThanComparable& value)
217 using thrust::system::detail::generic::select_system;
219 typedef typename thrust::iterator_system<ForwardIterator>::type System;
223 return thrust::lower_bound(select_system(system), first, last, value);
226 template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
227 ForwardIterator lower_bound(ForwardIterator first,
228 ForwardIterator last,
230 StrictWeakOrdering comp)
232 using thrust::system::detail::generic::select_system;
234 typedef typename thrust::iterator_system<ForwardIterator>::type System;
238 return thrust::lower_bound(select_system(system), first, last, value, comp);
241 template <typename ForwardIterator, typename LessThanComparable>
242 ForwardIterator upper_bound(ForwardIterator first,
243 ForwardIterator last,
244 const LessThanComparable& value)
246 using thrust::system::detail::generic::select_system;
248 typedef typename thrust::iterator_system<ForwardIterator>::type System;
252 return thrust::upper_bound(select_system(system), first, last, value);
255 template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
256 ForwardIterator upper_bound(ForwardIterator first,
257 ForwardIterator last,
259 StrictWeakOrdering comp)
261 using thrust::system::detail::generic::select_system;
263 typedef typename thrust::iterator_system<ForwardIterator>::type System;
267 return thrust::upper_bound(select_system(system), first, last, value, comp);
270 template <typename ForwardIterator, typename LessThanComparable>
271 bool binary_search(ForwardIterator first,
272 ForwardIterator last,
273 const LessThanComparable& value)
275 using thrust::system::detail::generic::select_system;
277 typedef typename thrust::iterator_system<ForwardIterator>::type System;
281 return thrust::binary_search(select_system(system), first, last, value);
284 template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
285 bool binary_search(ForwardIterator first,
286 ForwardIterator last,
288 StrictWeakOrdering comp)
290 using thrust::system::detail::generic::select_system;
292 typedef typename thrust::iterator_system<ForwardIterator>::type System;
296 return thrust::binary_search(select_system(system), first, last, value, comp);
299 template <typename ForwardIterator, typename LessThanComparable>
300 thrust::pair<ForwardIterator, ForwardIterator>
301 equal_range(ForwardIterator first,
302 ForwardIterator last,
303 const LessThanComparable& value)
305 using thrust::system::detail::generic::select_system;
307 typedef typename thrust::iterator_system<ForwardIterator>::type System;
311 return thrust::equal_range(select_system(system), first, last, value);
314 template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
315 thrust::pair<ForwardIterator, ForwardIterator>
316 equal_range(ForwardIterator first,
317 ForwardIterator last,
319 StrictWeakOrdering comp)
321 using thrust::system::detail::generic::select_system;
323 typedef typename thrust::iterator_system<ForwardIterator>::type System;
327 return thrust::equal_range(select_system(system), first, last, value, comp);
330 //////////////////////
331 // Vector Functions //
332 //////////////////////
334 template <typename ForwardIterator, typename InputIterator, typename OutputIterator>
335 OutputIterator lower_bound(ForwardIterator first,
336 ForwardIterator last,
337 InputIterator values_first,
338 InputIterator values_last,
339 OutputIterator output)
341 using thrust::system::detail::generic::select_system;
343 typedef typename thrust::iterator_system<ForwardIterator>::type System1;
344 typedef typename thrust::iterator_system<InputIterator>::type System2;
345 typedef typename thrust::iterator_system<OutputIterator>::type System3;
351 return thrust::lower_bound(select_system(system1,system2,system3), first, last, values_first, values_last, output);
354 template <typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering>
355 OutputIterator lower_bound(ForwardIterator first,
356 ForwardIterator last,
357 InputIterator values_first,
358 InputIterator values_last,
359 OutputIterator output,
360 StrictWeakOrdering comp)
362 using thrust::system::detail::generic::select_system;
364 typedef typename thrust::iterator_system<ForwardIterator>::type System1;
365 typedef typename thrust::iterator_system<InputIterator>::type System2;
366 typedef typename thrust::iterator_system<OutputIterator>::type System3;
372 return thrust::lower_bound(select_system(system1,system2,system3), first, last, values_first, values_last, output, comp);
375 template <typename ForwardIterator, typename InputIterator, typename OutputIterator>
376 OutputIterator upper_bound(ForwardIterator first,
377 ForwardIterator last,
378 InputIterator values_first,
379 InputIterator values_last,
380 OutputIterator output)
382 using thrust::system::detail::generic::select_system;
384 typedef typename thrust::iterator_system<ForwardIterator>::type System1;
385 typedef typename thrust::iterator_system<InputIterator>::type System2;
386 typedef typename thrust::iterator_system<OutputIterator>::type System3;
392 return thrust::upper_bound(select_system(system1,system2,system3), first, last, values_first, values_last, output);
395 template <typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering>
396 OutputIterator upper_bound(ForwardIterator first,
397 ForwardIterator last,
398 InputIterator values_first,
399 InputIterator values_last,
400 OutputIterator output,
401 StrictWeakOrdering comp)
403 using thrust::system::detail::generic::select_system;
405 typedef typename thrust::iterator_system<ForwardIterator>::type System1;
406 typedef typename thrust::iterator_system<InputIterator>::type System2;
407 typedef typename thrust::iterator_system<OutputIterator>::type System3;
413 return thrust::upper_bound(select_system(system1,system2,system3), first, last, values_first, values_last, output, comp);
416 template <typename ForwardIterator, typename InputIterator, typename OutputIterator>
417 OutputIterator binary_search(ForwardIterator first,
418 ForwardIterator last,
419 InputIterator values_first,
420 InputIterator values_last,
421 OutputIterator output)
423 using thrust::system::detail::generic::select_system;
425 typedef typename thrust::iterator_system<ForwardIterator>::type System1;
426 typedef typename thrust::iterator_system<InputIterator>::type System2;
427 typedef typename thrust::iterator_system<OutputIterator>::type System3;
433 return thrust::binary_search(select_system(system1,system2,system3), first, last, values_first, values_last, output);
436 template <typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering>
437 OutputIterator binary_search(ForwardIterator first,
438 ForwardIterator last,
439 InputIterator values_first,
440 InputIterator values_last,
441 OutputIterator output,
442 StrictWeakOrdering comp)
444 using thrust::system::detail::generic::select_system;
446 typedef typename thrust::iterator_system<ForwardIterator>::type System1;
447 typedef typename thrust::iterator_system<InputIterator>::type System2;
448 typedef typename thrust::iterator_system<OutputIterator>::type System3;
454 return thrust::binary_search(select_system(system1,system2,system3), first, last, values_first, values_last, output, comp);
457 } // end namespace thrust