2 Copyright (c) Marshall Clow 2008-2012.
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)
9 /// \brief Test ranges to see if only one element matches a value or predicate.
10 /// \author Marshall Clow
12 #ifndef BOOST_ALGORITHM_ONE_OF_HPP
13 #define BOOST_ALGORITHM_ONE_OF_HPP
15 #include <boost/config.hpp>
16 #include <boost/range/begin.hpp>
17 #include <boost/range/end.hpp>
19 #include <boost/algorithm/cxx11/none_of.hpp>
22 namespace boost { namespace algorithm {
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).
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
31 template<typename InputIterator, typename Predicate>
32 BOOST_CXX14_CONSTEXPR bool one_of ( InputIterator first, InputIterator last, Predicate p )
35 for (; first != last; ++first)
40 return false; // Didn't occur at all
41 return boost::algorithm::none_of (++first, last, p);
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.
47 /// \param r The input range
48 /// \param p A predicate for testing the elements of the range
50 template<typename Range, typename Predicate>
51 BOOST_CXX14_CONSTEXPR bool one_of ( const Range &r, Predicate p )
53 return boost::algorithm::one_of ( boost::begin (r), boost::end (r), p );
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).
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
64 template<typename InputIterator, typename V>
65 BOOST_CXX14_CONSTEXPR bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
68 for (; first != last; ++first)
73 return false; // Didn't occur at all
74 return boost::algorithm::none_of_equal (++first, last, val);
77 /// \fn one_of_equal ( const Range &r, const V &val )
78 /// \return true if the value 'val' exists only once in the range.
80 /// \param r The input range
81 /// \param val A value to compare against
83 template<typename Range, typename V>
84 BOOST_CXX14_CONSTEXPR bool one_of_equal ( const Range &r, const V &val )
86 return boost::algorithm::one_of_equal ( boost::begin (r), boost::end (r), val );
89 }} // namespace boost and algorithm
91 #endif // BOOST_ALGORITHM_ALL_HPP