is_sorted.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*==============================================================================
  2. Copyright (c) 2010-2011 Bryce Lelbach
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #ifndef BOOST_DETAIL_SORTED_HPP
  7. #define BOOST_DETAIL_SORTED_HPP
  8. #include <boost/detail/iterator.hpp>
  9. #include <functional>
  10. namespace boost {
  11. namespace detail {
  12. template<class Iterator, class Comp>
  13. inline Iterator is_sorted_until (Iterator first, Iterator last, Comp c) {
  14. if (first == last)
  15. return last;
  16. Iterator it = first; ++it;
  17. for (; it != last; first = it, ++it)
  18. if (c(*it, *first))
  19. return it;
  20. return it;
  21. }
  22. template<class Iterator>
  23. inline Iterator is_sorted_until (Iterator first, Iterator last) {
  24. typedef typename boost::detail::iterator_traits<Iterator>::value_type
  25. value_type;
  26. typedef std::less<value_type> c;
  27. return ::boost::detail::is_sorted_until(first, last, c());
  28. }
  29. template<class Iterator, class Comp>
  30. inline bool is_sorted (Iterator first, Iterator last, Comp c) {
  31. return ::boost::detail::is_sorted_until(first, last, c) == last;
  32. }
  33. template<class Iterator>
  34. inline bool is_sorted (Iterator first, Iterator last) {
  35. return ::boost::detail::is_sorted_until(first, last) == last;
  36. }
  37. } // detail
  38. } // boost
  39. #endif // BOOST_DETAIL_SORTED_HPP