Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:23

0001 /* 
0002    Copyright (c) Marshall Clow 2008-2012.
0003 
0004    Distributed under the Boost Software License, Version 1.0. (See accompanying
0005    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 */
0007 
0008 /// \file one_of.hpp
0009 /// \brief Test ranges to see if only one element matches a value or predicate.
0010 /// \author Marshall Clow
0011 
0012 #ifndef BOOST_ALGORITHM_ONE_OF_HPP
0013 #define BOOST_ALGORITHM_ONE_OF_HPP
0014 
0015 #include <boost/config.hpp>
0016 #include <boost/range/begin.hpp>
0017 #include <boost/range/end.hpp>
0018 
0019 #include <boost/algorithm/cxx11/none_of.hpp>
0020 
0021 
0022 namespace boost { namespace algorithm {
0023 
0024 /// \fn one_of ( InputIterator first, InputIterator last, Predicate p )
0025 /// \return true if the predicate 'p' is true for exactly one item in [first, last).
0026 /// 
0027 /// \param first The start of the input sequence
0028 /// \param last  One past the end of the input sequence
0029 /// \param p     A predicate for testing the elements of the sequence
0030 ///
0031 template<typename InputIterator, typename Predicate> 
0032 BOOST_CXX14_CONSTEXPR bool one_of ( InputIterator first, InputIterator last, Predicate p )
0033 {
0034 //  find_if
0035     for (; first != last; ++first)
0036         if (p(*first))
0037             break;
0038 
0039     if (first == last)
0040         return false;    // Didn't occur at all
0041     return boost::algorithm::none_of (++first, last, p);
0042 }
0043 
0044 /// \fn one_of ( const Range &r, Predicate p )
0045 /// \return true if the predicate 'p' is true for exactly one item in the range.
0046 /// 
0047 /// \param r    The input range
0048 /// \param p    A predicate for testing the elements of the range
0049 ///
0050 template<typename Range, typename Predicate> 
0051 BOOST_CXX14_CONSTEXPR bool one_of ( const Range &r, Predicate p )
0052 {
0053     return boost::algorithm::one_of ( boost::begin (r), boost::end (r), p );
0054 }
0055 
0056 
0057 /// \fn one_of_equal ( InputIterator first, InputIterator last, const V &val )
0058 /// \return true if the value 'val' exists only once in [first, last).
0059 /// 
0060 /// \param first    The start of the input sequence
0061 /// \param last     One past the end of the input sequence
0062 /// \param val      A value to compare against
0063 ///
0064 template<typename InputIterator, typename V> 
0065 BOOST_CXX14_CONSTEXPR bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
0066 {
0067 //  find
0068     for (; first != last; ++first)
0069         if (*first == val)
0070             break;
0071 
0072     if (first == last)
0073         return false;                    // Didn't occur at all
0074     return boost::algorithm::none_of_equal (++first, last, val);
0075 }
0076 
0077 /// \fn one_of_equal ( const Range &r, const V &val )
0078 /// \return true if the value 'val' exists only once in the range.
0079 /// 
0080 /// \param r    The input range
0081 /// \param val  A value to compare against
0082 ///
0083 template<typename Range, typename V> 
0084 BOOST_CXX14_CONSTEXPR bool one_of_equal ( const Range &r, const V &val )
0085 {
0086     return boost::algorithm::one_of_equal ( boost::begin (r), boost::end (r), val );
0087 } 
0088 
0089 }} // namespace boost and algorithm
0090 
0091 #endif // BOOST_ALGORITHM_ALL_HPP