minmax_element.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[email protected]>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_ALGORITHM_MINMAX_ELEMENT_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_MINMAX_ELEMENT_HPP
  12. #include <utility>
  13. #include <boost/static_assert.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/algorithm/max_element.hpp>
  17. #include <boost/compute/algorithm/min_element.hpp>
  18. #include <boost/compute/type_traits/is_device_iterator.hpp>
  19. namespace boost {
  20. namespace compute {
  21. /// Returns a pair of iterators with the first pointing to the minimum
  22. /// element and the second pointing to the maximum element in the range
  23. /// [\p first, \p last).
  24. ///
  25. /// \param first first element in the input range
  26. /// \param last last element in the input range
  27. /// \param compare comparison function object which returns true if the first
  28. /// argument is less than (i.e. is ordered before) the second.
  29. /// \param queue command queue to perform the operation
  30. ///
  31. /// Space complexity on CPUs: \Omega(1)<br>
  32. /// Space complexity on GPUs: \Omega(N)
  33. ///
  34. /// \see max_element(), min_element()
  35. template<class InputIterator, class Compare>
  36. inline std::pair<InputIterator, InputIterator>
  37. minmax_element(InputIterator first,
  38. InputIterator last,
  39. Compare compare,
  40. command_queue &queue = system::default_queue())
  41. {
  42. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  43. if(first == last){
  44. // empty range
  45. return std::make_pair(first, first);
  46. }
  47. return std::make_pair(min_element(first, last, compare, queue),
  48. max_element(first, last, compare, queue));
  49. }
  50. ///\overload
  51. template<class InputIterator>
  52. inline std::pair<InputIterator, InputIterator>
  53. minmax_element(InputIterator first,
  54. InputIterator last,
  55. command_queue &queue = system::default_queue())
  56. {
  57. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  58. if(first == last){
  59. // empty range
  60. return std::make_pair(first, first);
  61. }
  62. return std::make_pair(min_element(first, last, queue),
  63. max_element(first, last, queue));
  64. }
  65. } // end compute namespace
  66. } // end boost namespace
  67. #endif // BOOST_COMPUTE_ALGORITHM_MINMAX_ELEMENT_HPP