is_sorted.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2016-2016. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_DETAIL_IS_SORTED_HPP
  11. #define BOOST_CONTAINER_DETAIL_IS_SORTED_HPP
  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 container {
  20. namespace dtl {
  21. template <class ForwardIterator, class Pred>
  22. bool is_sorted (ForwardIterator first, ForwardIterator last, Pred pred)
  23. {
  24. if(first != last){
  25. ForwardIterator next = first;
  26. while (++next != last){
  27. if(pred(*next, *first))
  28. return false;
  29. ++first;
  30. }
  31. }
  32. return true;
  33. }
  34. template <class ForwardIterator, class Pred>
  35. bool is_sorted_and_unique (ForwardIterator first, ForwardIterator last, Pred pred)
  36. {
  37. if(first != last){
  38. ForwardIterator next = first;
  39. while (++next != last){
  40. if(!pred(*first, *next))
  41. return false;
  42. ++first;
  43. }
  44. }
  45. return true;
  46. }
  47. } //namespace dtl {
  48. } //namespace container {
  49. } //namespace boost {
  50. #endif //#ifndef BOOST_CONTAINER_DETAIL_IS_SORTED_HPP