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