| 1 | /* |
| 2 | Copyright (c) Marshall Clow 2008-2012. |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | */ |
| 7 | |
| 8 | /// \file all_of.hpp |
| 9 | /// \brief Test ranges to see if all elements match a value or predicate. |
| 10 | /// \author Marshall Clow |
| 11 | |
| 12 | #ifndef BOOST_ALGORITHM_ALL_OF_HPP |
| 13 | #define BOOST_ALGORITHM_ALL_OF_HPP |
| 14 | |
| 15 | #include <boost/config.hpp> |
| 16 | #include <boost/range/begin.hpp> |
| 17 | #include <boost/range/end.hpp> |
| 18 | |
| 19 | namespace boost { namespace algorithm { |
| 20 | |
| 21 | /// \fn all_of ( InputIterator first, InputIterator last, Predicate p ) |
| 22 | /// \return true if all elements in [first, last) satisfy the predicate 'p' |
| 23 | /// \note returns true on an empty range |
| 24 | /// |
| 25 | /// \param first The start of the input sequence |
| 26 | /// \param last One past the end of the input sequence |
| 27 | /// \param p A predicate for testing the elements of the sequence |
| 28 | /// |
| 29 | /// \note This function is part of the C++2011 standard library. |
| 30 | template<typename InputIterator, typename Predicate> |
| 31 | BOOST_CXX14_CONSTEXPR 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 | |
| 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 | BOOST_CXX14_CONSTEXPR bool all_of ( const Range &r, Predicate p ) |
| 48 | { |
| 49 | return boost::algorithm::all_of ( boost::begin (r), boost::end (r), p ); |
| 50 | } |
| 51 | |
| 52 | /// \fn all_of_equal ( InputIterator first, InputIterator last, const T &val ) |
| 53 | /// \return true if all elements in [first, last) are equal to 'val' |
| 54 | /// \note returns true on an empty range |
| 55 | /// |
| 56 | /// \param first The start of the input sequence |
| 57 | /// \param last One past the end of the input sequence |
| 58 | /// \param val A value to compare against |
| 59 | /// |
| 60 | template<typename InputIterator, typename T> |
| 61 | BOOST_CXX14_CONSTEXPR bool all_of_equal ( InputIterator first, InputIterator last, const T &val ) |
| 62 | { |
| 63 | for ( ; first != last; ++first ) |
| 64 | if ( val != *first ) |
| 65 | return false; |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | /// \fn all_of_equal ( const Range &r, const T &val ) |
| 70 | /// \return true if all elements in the range are equal to 'val' |
| 71 | /// \note returns true on an empty range |
| 72 | /// |
| 73 | /// \param r The input range |
| 74 | /// \param val A value to compare against |
| 75 | /// |
| 76 | template<typename Range, typename T> |
| 77 | BOOST_CXX14_CONSTEXPR bool all_of_equal ( const Range &r, const T &val ) |
| 78 | { |
| 79 | return boost::algorithm::all_of_equal ( boost::begin (r), boost::end (r), val ); |
| 80 | } |
| 81 | |
| 82 | }} // namespace boost and algorithm |
| 83 | |
| 84 | #endif // BOOST_ALGORITHM_ALL_OF_HPP |
| 85 | |