none_of.hpp 2.8 KB

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