none_of.hpp 2.7 KB

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