is_partitioned.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <algorithm> // for std::is_partitioned, if available
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. namespace boost { namespace algorithm {
  15. #if __cplusplus >= 201103L
  16. // Use the C++11 versions of is_partitioned if it is available
  17. using std::is_partitioned; // Section 25.3.13
  18. #else
  19. /// \fn is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
  20. /// \brief Tests to see if a sequence is partitioned according to a predicate
  21. ///
  22. /// \param first The start of the input sequence
  23. /// \param last One past the end of the input sequence
  24. /// \param p The predicate to test the values with
  25. /// \note This function is part of the C++2011 standard library.
  26. /// We will use the standard one if it is available,
  27. /// otherwise we have our own implementation.
  28. template <typename InputIterator, typename UnaryPredicate>
  29. bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
  30. {
  31. // Run through the part that satisfy the predicate
  32. for ( ; first != last; ++first )
  33. if ( !p (*first))
  34. break;
  35. // Now the part that does not satisfy the predicate
  36. for ( ; first != last; ++first )
  37. if ( p (*first))
  38. return false;
  39. return true;
  40. }
  41. #endif
  42. /// \fn is_partitioned ( const Range &r, UnaryPredicate p )
  43. /// \brief Generates an increasing sequence of values, and stores them in the input Range.
  44. ///
  45. /// \param r The input range
  46. /// \param p The predicate to test the values with
  47. ///
  48. template <typename Range, typename UnaryPredicate>
  49. bool is_partitioned ( const Range &r, UnaryPredicate p )
  50. {
  51. return boost::algorithm::is_partitioned (boost::begin(r), boost::end(r), p);
  52. }
  53. }}
  54. #endif // BOOST_ALGORITHM_IS_PARTITIONED_HPP