is_sorted.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef BOOST_MOVE_DETAIL_IS_SORTED_HPP
  2. #define BOOST_MOVE_DETAIL_IS_SORTED_HPP
  3. ///////////////////////////////////////////////////////////////////////////////
  4. //
  5. // (C) Copyright Ion Gaztanaga 2017-2018. Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/container for documentation.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. namespace boost {
  19. namespace movelib {
  20. template<class ForwardIt, class Pred>
  21. bool is_sorted(ForwardIt const first, ForwardIt last, Pred pred)
  22. {
  23. if (first != last) {
  24. ForwardIt next = first, cur(first);
  25. while (++next != last) {
  26. if (pred(*next, *cur))
  27. return false;
  28. cur = next;
  29. }
  30. }
  31. return true;
  32. }
  33. template<class ForwardIt, class Pred>
  34. bool is_sorted_and_unique(ForwardIt first, ForwardIt last, Pred pred)
  35. {
  36. if (first != last) {
  37. ForwardIt next = first;
  38. while (++next != last) {
  39. if (!pred(*first, *next))
  40. return false;
  41. first = next;
  42. }
  43. }
  44. return true;
  45. }
  46. } //namespace movelib {
  47. } //namespace boost {
  48. #endif //BOOST_MOVE_DETAIL_IS_SORTED_HPP