all_of.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. */
  6. /// \file all_of.hpp
  7. /// \brief Test ranges to see if all elements match a value or predicate.
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_ALL_OF_HPP
  10. #define BOOST_ALGORITHM_ALL_OF_HPP
  11. #include <algorithm> // for std::all_of, 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 all_of if it is available
  17. using std::all_of; // Section 25.2.1
  18. #else
  19. /// \fn all_of ( InputIterator first, InputIterator last, Predicate p )
  20. /// \return true if all elements in [first, last) satisfy the predicate 'p'
  21. /// \note returns true on an empty range
  22. ///
  23. /// \param first The start of the input sequence
  24. /// \param last One past the end of the input sequence
  25. /// \param p A predicate for testing the elements of the sequence
  26. ///
  27. /// \note This function is part of the C++2011 standard library.
  28. /// We will use the standard one if it is available,
  29. /// otherwise we have our own implementation.
  30. template<typename InputIterator, typename Predicate>
  31. bool all_of ( InputIterator first, InputIterator last, Predicate p )
  32. {
  33. for ( ; first != last; ++first )
  34. if ( !p(*first))
  35. return false;
  36. return true;
  37. }
  38. #endif
  39. /// \fn all_of ( const Range &r, Predicate p )
  40. /// \return true if all elements in the range satisfy the predicate 'p'
  41. /// \note returns true on an empty range
  42. ///
  43. /// \param r The input range
  44. /// \param p A predicate for testing the elements of the range
  45. ///
  46. template<typename Range, typename Predicate>
  47. bool all_of ( const Range &r, Predicate p )
  48. {
  49. return boost::algorithm::all_of ( boost::begin (r), boost::end (r), p );
  50. }
  51. /// \fn all_of_equal ( InputIterator first, InputIterator last, const T &val )
  52. /// \return true if all elements in [first, last) are equal to 'val'
  53. /// \note returns true on an empty range
  54. ///
  55. /// \param first The start of the input sequence
  56. /// \param last One past the end of the input sequence
  57. /// \param val A value to compare against
  58. ///
  59. template<typename InputIterator, typename T>
  60. bool all_of_equal ( InputIterator first, InputIterator last, const T &val )
  61. {
  62. for ( ; first != last; ++first )
  63. if ( val != *first )
  64. return false;
  65. return true;
  66. }
  67. /// \fn all_of_equal ( const Range &r, const T &val )
  68. /// \return true if all elements in the range are equal to 'val'
  69. /// \note returns true on an empty range
  70. ///
  71. /// \param r The input range
  72. /// \param val A value to compare against
  73. ///
  74. template<typename Range, typename T>
  75. bool all_of_equal ( const Range &r, const T &val )
  76. {
  77. return boost::algorithm::all_of_equal ( boost::begin (r), boost::end (r), val );
  78. }
  79. }} // namespace boost and algorithm
  80. #endif // BOOST_ALGORITHM_ALL_OF_HPP