is_partitioned_until.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.by>, 2017.
  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_until.hpp
  7. /// \brief Tell if a sequence is partitioned
  8. /// \author Alexander Zaitsev
  9. #ifndef BOOST_ALGORITHM_IS_PARTITIONED_UNTIL_HPP
  10. #define BOOST_ALGORITHM_IS_PARTITIONED_UNTIL_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_until ( 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. ///
  23. /// \note Returns the first iterator 'it' in the sequence [first, last) for which is_partitioned(first, it, p) is false.
  24. /// Returns last if the entire sequence is partitioned.
  25. /// Complexity: O(N).
  26. template <typename InputIterator, typename UnaryPredicate>
  27. InputIterator is_partitioned_until ( InputIterator first, InputIterator last, UnaryPredicate p )
  28. {
  29. // Run through the part that satisfy the predicate
  30. for ( ; first != last; ++first )
  31. if ( !p (*first))
  32. break;
  33. // Now the part that does not satisfy the predicate
  34. for ( ; first != last; ++first )
  35. if ( p (*first))
  36. return first;
  37. return last;
  38. }
  39. /// \fn is_partitioned_until ( const Range &r, UnaryPredicate p )
  40. /// \brief Tests to see if a sequence is partitioned according to a predicate.
  41. /// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
  42. ///
  43. /// \param r The input range
  44. /// \param p The predicate to test the values with
  45. ///
  46. /// \note Returns the first iterator 'it' in the sequence [first, last) for which is_partitioned(first, it, p) is false.
  47. /// Returns last if the entire sequence is partitioned.
  48. /// Complexity: O(N).
  49. template <typename Range, typename UnaryPredicate>
  50. typename boost::range_iterator<const Range>::type is_partitioned_until ( const Range &r, UnaryPredicate p )
  51. {
  52. return boost::algorithm::is_partitioned_until (boost::begin(r), boost::end(r), p);
  53. }
  54. }}
  55. #endif // BOOST_ALGORITHM_IS_PARTITIONED_UNTIL_HPP