is_partitioned.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright (c) Marshall Clow 2011-2012.
  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. /// \file is_partitioned.hpp
  7. /// \brief Tell if a sequence is partitioned
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_IS_PARTITIONED_HPP
  10. #define BOOST_ALGORITHM_IS_PARTITIONED_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. namespace boost { namespace algorithm {
  15. /// \fn is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
  16. /// \brief Tests to see if a sequence is partitioned according to a predicate.
  17. /// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
  18. ///
  19. /// \param first The start of the input sequence
  20. /// \param last One past the end of the input sequence
  21. /// \param p The predicate to test the values with
  22. /// \note This function is part of the C++2011 standard library.
  23. template <typename InputIterator, typename UnaryPredicate>
  24. BOOST_CXX14_CONSTEXPR bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
  25. {
  26. // Run through the part that satisfy the predicate
  27. for ( ; first != last; ++first )
  28. if ( !p (*first))
  29. break;
  30. // Now the part that does not satisfy the predicate
  31. for ( ; first != last; ++first )
  32. if ( p (*first))
  33. return false;
  34. return true;
  35. }
  36. /// \fn is_partitioned ( const Range &r, UnaryPredicate p )
  37. /// \brief Tests to see if a sequence is partitioned according to a predicate.
  38. /// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
  39. ///
  40. /// \param r The input range
  41. /// \param p The predicate to test the values with
  42. ///
  43. template <typename Range, typename UnaryPredicate>
  44. BOOST_CXX14_CONSTEXPR bool is_partitioned ( const Range &r, UnaryPredicate p )
  45. {
  46. return boost::algorithm::is_partitioned (boost::begin(r), boost::end(r), p);
  47. }
  48. }}
  49. #endif // BOOST_ALGORITHM_IS_PARTITIONED_HPP