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