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