any_of.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <algorithm> // for std::any_of, if available
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. namespace boost { namespace algorithm {
  16. // Use the C++11 versions of any_of if it is available
  17. #if __cplusplus >= 201103L
  18. using std::any_of; // Section 25.2.2
  19. #else
  20. /// \fn any_of ( InputIterator first, InputIterator last, Predicate p )
  21. /// \return true if any of the elements in [first, last) satisfy the predicate
  22. /// \note returns false on an empty range
  23. ///
  24. /// \param first The start of the input sequence
  25. /// \param last One past the end of the input sequence
  26. /// \param p A predicate for testing the elements of the sequence
  27. ///
  28. template<typename InputIterator, typename Predicate>
  29. bool any_of ( InputIterator first, InputIterator last, Predicate p )
  30. {
  31. for ( ; first != last; ++first )
  32. if ( p(*first))
  33. return true;
  34. return false;
  35. }
  36. #endif
  37. /// \fn any_of ( const Range &r, Predicate p )
  38. /// \return true if any elements in the range satisfy the predicate 'p'
  39. /// \note returns false on an empty range
  40. ///
  41. /// \param r The input range
  42. /// \param p A predicate for testing the elements of the range
  43. ///
  44. template<typename Range, typename Predicate>
  45. bool any_of ( const Range &r, Predicate p )
  46. {
  47. return boost::algorithm::any_of (boost::begin (r), boost::end (r), p);
  48. }
  49. /// \fn any_of_equal ( InputIterator first, InputIterator last, const V &val )
  50. /// \return true if any of the elements in [first, last) are equal to 'val'
  51. /// \note returns false on an empty range
  52. ///
  53. /// \param first The start of the input sequence
  54. /// \param last One past the end of the input sequence
  55. /// \param val A value to compare against
  56. ///
  57. template<typename InputIterator, typename V>
  58. bool any_of_equal ( InputIterator first, InputIterator last, const V &val )
  59. {
  60. for ( ; first != last; ++first )
  61. if ( val == *first )
  62. return true;
  63. return false;
  64. }
  65. /// \fn any_of_equal ( const Range &r, const V &val )
  66. /// \return true if any of the elements in the range are equal to 'val'
  67. /// \note returns false on an empty range
  68. ///
  69. /// \param r The input range
  70. /// \param val A value to compare against
  71. ///
  72. template<typename Range, typename V>
  73. bool any_of_equal ( const Range &r, const V &val )
  74. {
  75. return boost::algorithm::any_of_equal (boost::begin (r), boost::end (r), val);
  76. }
  77. }} // namespace boost and algorithm
  78. #endif // BOOST_ALGORITHM_ANY_OF_HPP