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 one_of.hpp
|
Chris@16
|
9 /// \brief Test ranges to see if only one element matches a value or predicate.
|
Chris@16
|
10 /// \author Marshall Clow
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_ALGORITHM_ONE_OF_HPP
|
Chris@16
|
13 #define BOOST_ALGORITHM_ONE_OF_HPP
|
Chris@16
|
14
|
Chris@16
|
15 #include <algorithm> // for std::find and std::find_if
|
Chris@16
|
16 #include <boost/algorithm/cxx11/none_of.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/range/begin.hpp>
|
Chris@16
|
19 #include <boost/range/end.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost { namespace algorithm {
|
Chris@16
|
22
|
Chris@16
|
23 /// \fn one_of ( InputIterator first, InputIterator last, Predicate p )
|
Chris@16
|
24 /// \return true if the predicate 'p' is true for exactly one item in [first, last).
|
Chris@16
|
25 ///
|
Chris@16
|
26 /// \param first The start of the input sequence
|
Chris@16
|
27 /// \param last One past the end of the input sequence
|
Chris@16
|
28 /// \param p A predicate for testing the elements of the sequence
|
Chris@16
|
29 ///
|
Chris@16
|
30 template<typename InputIterator, typename Predicate>
|
Chris@16
|
31 bool one_of ( InputIterator first, InputIterator last, Predicate p )
|
Chris@16
|
32 {
|
Chris@16
|
33 InputIterator i = std::find_if (first, last, p);
|
Chris@16
|
34 if (i == last)
|
Chris@16
|
35 return false; // Didn't occur at all
|
Chris@16
|
36 return boost::algorithm::none_of (++i, last, p);
|
Chris@16
|
37 }
|
Chris@16
|
38
|
Chris@16
|
39 /// \fn one_of ( const Range &r, Predicate p )
|
Chris@16
|
40 /// \return true if the predicate 'p' is true for exactly one item in the range.
|
Chris@16
|
41 ///
|
Chris@16
|
42 /// \param r The input range
|
Chris@16
|
43 /// \param p A predicate for testing the elements of the range
|
Chris@16
|
44 ///
|
Chris@16
|
45 template<typename Range, typename Predicate>
|
Chris@16
|
46 bool one_of ( const Range &r, Predicate p )
|
Chris@16
|
47 {
|
Chris@16
|
48 return boost::algorithm::one_of ( boost::begin (r), boost::end (r), p );
|
Chris@16
|
49 }
|
Chris@16
|
50
|
Chris@16
|
51
|
Chris@16
|
52 /// \fn one_of_equal ( InputIterator first, InputIterator last, const V &val )
|
Chris@16
|
53 /// \return true if the value 'val' exists only once in [first, last).
|
Chris@16
|
54 ///
|
Chris@16
|
55 /// \param first The start of the input sequence
|
Chris@16
|
56 /// \param last One past the end of the input sequence
|
Chris@16
|
57 /// \param val A value to compare against
|
Chris@16
|
58 ///
|
Chris@16
|
59 template<typename InputIterator, typename V>
|
Chris@16
|
60 bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
|
Chris@16
|
61 {
|
Chris@16
|
62 InputIterator i = std::find (first, last, val); // find first occurrence of 'val'
|
Chris@16
|
63 if (i == last)
|
Chris@16
|
64 return false; // Didn't occur at all
|
Chris@16
|
65 return boost::algorithm::none_of_equal (++i, last, val);
|
Chris@16
|
66 }
|
Chris@16
|
67
|
Chris@16
|
68 /// \fn one_of_equal ( const Range &r, const V &val )
|
Chris@16
|
69 /// \return true if the value 'val' exists only once in the range.
|
Chris@16
|
70 ///
|
Chris@16
|
71 /// \param r The input range
|
Chris@16
|
72 /// \param val A value to compare against
|
Chris@16
|
73 ///
|
Chris@16
|
74 template<typename Range, typename V>
|
Chris@16
|
75 bool one_of_equal ( const Range &r, const V &val )
|
Chris@16
|
76 {
|
Chris@16
|
77 return boost::algorithm::one_of_equal ( boost::begin (r), boost::end (r), val );
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 }} // namespace boost and algorithm
|
Chris@16
|
81
|
Chris@16
|
82 #endif // BOOST_ALGORITHM_ALL_HPP
|