any_of.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright (c) Marshall Clow 2008-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. For more information, see http://www.boost.org
  6. */
  7. /// \file
  8. /// \brief Test ranges to see if any elements match a value or predicate.
  9. /// \author Marshall Clow
  10. #ifndef BOOST_ALGORITHM_ANY_OF_HPP
  11. #define BOOST_ALGORITHM_ANY_OF_HPP
  12. #include <boost/config.hpp>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. namespace boost { namespace algorithm {
  16. /// \fn any_of ( InputIterator first, InputIterator last, Predicate p )
  17. /// \return true if any of the elements in [first, last) satisfy the predicate
  18. /// \note returns false on an empty range
  19. ///
  20. /// \param first The start of the input sequence
  21. /// \param last One past the end of the input sequence
  22. /// \param p A predicate for testing the elements of the sequence
  23. ///
  24. template<typename InputIterator, typename Predicate>
  25. BOOST_CXX14_CONSTEXPR bool any_of ( InputIterator first, InputIterator last, Predicate p )
  26. {
  27. for ( ; first != last; ++first )
  28. if ( p(*first))
  29. return true;
  30. return false;
  31. }
  32. /// \fn any_of ( const Range &r, Predicate p )
  33. /// \return true if any elements in the range satisfy the predicate 'p'
  34. /// \note returns false on an empty range
  35. ///
  36. /// \param r The input range
  37. /// \param p A predicate for testing the elements of the range
  38. ///
  39. template<typename Range, typename Predicate>
  40. BOOST_CXX14_CONSTEXPR bool any_of ( const Range &r, Predicate p )
  41. {
  42. return boost::algorithm::any_of (boost::begin (r), boost::end (r), p);
  43. }
  44. /// \fn any_of_equal ( InputIterator first, InputIterator last, const V &val )
  45. /// \return true if any of the elements in [first, last) are equal to 'val'
  46. /// \note returns false on an empty range
  47. ///
  48. /// \param first The start of the input sequence
  49. /// \param last One past the end of the input sequence
  50. /// \param val A value to compare against
  51. ///
  52. template<typename InputIterator, typename V>
  53. BOOST_CXX14_CONSTEXPR bool any_of_equal ( InputIterator first, InputIterator last, const V &val )
  54. {
  55. for ( ; first != last; ++first )
  56. if ( val == *first )
  57. return true;
  58. return false;
  59. }
  60. /// \fn any_of_equal ( const Range &r, const V &val )
  61. /// \return true if any of the elements in the range are equal to 'val'
  62. /// \note returns false on an empty range
  63. ///
  64. /// \param r The input range
  65. /// \param val A value to compare against
  66. ///
  67. template<typename Range, typename V>
  68. BOOST_CXX14_CONSTEXPR bool any_of_equal ( const Range &r, const V &val )
  69. {
  70. return boost::algorithm::any_of_equal (boost::begin (r), boost::end (r), val);
  71. }
  72. }} // namespace boost and algorithm
  73. #endif // BOOST_ALGORITHM_ANY_OF_HPP