Chris@16: /* Chris@16: Copyright (c) Marshall Clow 2008-2012. Chris@16: Chris@16: Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: Chris@16: /// \file one_of.hpp Chris@16: /// \brief Test ranges to see if only one element matches a value or predicate. Chris@16: /// \author Marshall Clow Chris@16: Chris@16: #ifndef BOOST_ALGORITHM_ONE_OF_HPP Chris@16: #define BOOST_ALGORITHM_ONE_OF_HPP Chris@16: Chris@16: #include // for std::find and std::find_if Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace algorithm { Chris@16: Chris@16: /// \fn one_of ( InputIterator first, InputIterator last, Predicate p ) Chris@16: /// \return true if the predicate 'p' is true for exactly one item in [first, last). Chris@16: /// Chris@16: /// \param first The start of the input sequence Chris@16: /// \param last One past the end of the input sequence Chris@16: /// \param p A predicate for testing the elements of the sequence Chris@16: /// Chris@16: template Chris@16: bool one_of ( InputIterator first, InputIterator last, Predicate p ) Chris@16: { Chris@16: InputIterator i = std::find_if (first, last, p); Chris@16: if (i == last) Chris@16: return false; // Didn't occur at all Chris@16: return boost::algorithm::none_of (++i, last, p); Chris@16: } Chris@16: Chris@16: /// \fn one_of ( const Range &r, Predicate p ) Chris@16: /// \return true if the predicate 'p' is true for exactly one item in the range. Chris@16: /// Chris@16: /// \param r The input range Chris@16: /// \param p A predicate for testing the elements of the range Chris@16: /// Chris@16: template Chris@16: bool one_of ( const Range &r, Predicate p ) Chris@16: { Chris@16: return boost::algorithm::one_of ( boost::begin (r), boost::end (r), p ); Chris@16: } Chris@16: Chris@16: Chris@16: /// \fn one_of_equal ( InputIterator first, InputIterator last, const V &val ) Chris@16: /// \return true if the value 'val' exists only once in [first, last). Chris@16: /// Chris@16: /// \param first The start of the input sequence Chris@16: /// \param last One past the end of the input sequence Chris@16: /// \param val A value to compare against Chris@16: /// Chris@16: template Chris@16: bool one_of_equal ( InputIterator first, InputIterator last, const V &val ) Chris@16: { Chris@16: InputIterator i = std::find (first, last, val); // find first occurrence of 'val' Chris@16: if (i == last) Chris@16: return false; // Didn't occur at all Chris@16: return boost::algorithm::none_of_equal (++i, last, val); Chris@16: } Chris@16: Chris@16: /// \fn one_of_equal ( const Range &r, const V &val ) Chris@16: /// \return true if the value 'val' exists only once in the range. Chris@16: /// Chris@16: /// \param r The input range Chris@16: /// \param val A value to compare against Chris@16: /// Chris@16: template Chris@16: bool one_of_equal ( const Range &r, const V &val ) Chris@16: { Chris@16: return boost::algorithm::one_of_equal ( boost::begin (r), boost::end (r), val ); Chris@16: } Chris@16: Chris@16: }} // namespace boost and algorithm Chris@16: Chris@16: #endif // BOOST_ALGORITHM_ALL_HPP