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 <algorithm> // for std::all_of, if available
16#include <boost/range/begin.hpp>
17#include <boost/range/end.hpp>
18
19namespace 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/// We will use the standard one if it is available,
31/// otherwise we have our own implementation.
32template<typename InputIterator, typename Predicate>
33bool all_of ( InputIterator first, InputIterator last, Predicate p )
34{
35 for ( ; first != last; ++first )
36 if ( !p(*first))
37 return false;
38 return true;
39}
40
41/// \fn all_of ( const Range &r, Predicate p )
42/// \return true if all elements in the range satisfy the predicate 'p'
43/// \note returns true on an empty range
44///
45/// \param r The input range
46/// \param p A predicate for testing the elements of the range
47///
48template<typename Range, typename Predicate>
49bool all_of ( const Range &r, Predicate p )
50{
51 return boost::algorithm::all_of ( boost::begin (r), boost::end (r), p );
52}
53
54/// \fn all_of_equal ( InputIterator first, InputIterator last, const T &val )
55/// \return true if all elements in [first, last) are equal to 'val'
56/// \note returns true on an empty range
57///
58/// \param first The start of the input sequence
59/// \param last One past the end of the input sequence
60/// \param val A value to compare against
61///
62template<typename InputIterator, typename T>
63bool all_of_equal ( InputIterator first, InputIterator last, const T &val )
64{
65 for ( ; first != last; ++first )
66 if ( val != *first )
67 return false;
68 return true;
69}
70
71/// \fn all_of_equal ( const Range &r, const T &val )
72/// \return true if all elements in the range are equal to 'val'
73/// \note returns true on an empty range
74///
75/// \param r The input range
76/// \param val A value to compare against
77///
78template<typename Range, typename T>
79bool all_of_equal ( const Range &r, const T &val )
80{
81 return boost::algorithm::all_of_equal ( boost::begin (r), boost::end (r), val );
82}
83
84}} // namespace boost and algorithm
85
86#endif // BOOST_ALGORITHM_ALL_OF_HPP
87