one_of.hpp 2.7 KB

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