one_of.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright (c) Marshall Clow 2008-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. /// \file one_of.hpp
  7. /// \brief Test ranges to see if only one element matches a value or predicate.
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_ONE_OF_HPP
  10. #define BOOST_ALGORITHM_ONE_OF_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/algorithm/cxx11/none_of.hpp>
  15. namespace boost { namespace algorithm {
  16. /// \fn one_of ( InputIterator first, InputIterator last, Predicate p )
  17. /// \return true if the predicate 'p' is true for exactly one item in [first, last).
  18. ///
  19. /// \param first The start of the input sequence
  20. /// \param last One past the end of the input sequence
  21. /// \param p A predicate for testing the elements of the sequence
  22. ///
  23. template<typename InputIterator, typename Predicate>
  24. BOOST_CXX14_CONSTEXPR bool one_of ( InputIterator first, InputIterator last, Predicate p )
  25. {
  26. // find_if
  27. for (; first != last; ++first)
  28. if (p(*first))
  29. break;
  30. if (first == last)
  31. return false; // Didn't occur at all
  32. return boost::algorithm::none_of (++first, last, p);
  33. }
  34. /// \fn one_of ( const Range &r, Predicate p )
  35. /// \return true if the predicate 'p' is true for exactly one item in the range.
  36. ///
  37. /// \param r The input range
  38. /// \param p A predicate for testing the elements of the range
  39. ///
  40. template<typename Range, typename Predicate>
  41. BOOST_CXX14_CONSTEXPR bool one_of ( const Range &r, Predicate p )
  42. {
  43. return boost::algorithm::one_of ( boost::begin (r), boost::end (r), p );
  44. }
  45. /// \fn one_of_equal ( InputIterator first, InputIterator last, const V &val )
  46. /// \return true if the value 'val' exists only once in [first, last).
  47. ///
  48. /// \param first The start of the input sequence
  49. /// \param last One past the end of the input sequence
  50. /// \param val A value to compare against
  51. ///
  52. template<typename InputIterator, typename V>
  53. BOOST_CXX14_CONSTEXPR bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
  54. {
  55. // find
  56. for (; first != last; ++first)
  57. if (*first == val)
  58. break;
  59. if (first == last)
  60. return false; // Didn't occur at all
  61. return boost::algorithm::none_of_equal (++first, last, val);
  62. }
  63. /// \fn one_of_equal ( const Range &r, const V &val )
  64. /// \return true if the value 'val' exists only once in the range.
  65. ///
  66. /// \param r The input range
  67. /// \param val A value to compare against
  68. ///
  69. template<typename Range, typename V>
  70. BOOST_CXX14_CONSTEXPR bool one_of_equal ( const Range &r, const V &val )
  71. {
  72. return boost::algorithm::one_of_equal ( boost::begin (r), boost::end (r), val );
  73. }
  74. }} // namespace boost and algorithm
  75. #endif // BOOST_ALGORITHM_ALL_HPP