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