one_of.hpp 2.8 KB

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