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