check_iterator_range.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_CHECK_ITERATOR_RANGE_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_CHECK_ITERATOR_RANGE_HPP
  8. #include <boost/core/ignore_unused.hpp>
  9. namespace boost { namespace geometry
  10. {
  11. #ifndef DOXYGEN_NO_DETAIL
  12. namespace detail
  13. {
  14. // Check whether (each element of) an iterator range satisfies a given
  15. // predicate.
  16. // The predicate must be implemented as having a static apply unary
  17. // method that returns a bool.
  18. // By default an empty range is accepted
  19. template <typename Predicate, bool AllowEmptyRange = true>
  20. struct check_iterator_range
  21. {
  22. template <typename InputIterator>
  23. static inline bool apply(InputIterator first, InputIterator beyond)
  24. {
  25. for (InputIterator it = first; it != beyond; ++it)
  26. {
  27. if (! Predicate::apply(*it))
  28. {
  29. return false;
  30. }
  31. }
  32. return AllowEmptyRange || first != beyond;
  33. }
  34. // version where we can pass a predicate object
  35. template <typename InputIterator>
  36. static inline bool apply(InputIterator first,
  37. InputIterator beyond,
  38. Predicate const& predicate)
  39. {
  40. // in case predicate's apply method is static, MSVC will
  41. // complain that predicate is not used
  42. boost::ignore_unused(predicate);
  43. for (InputIterator it = first; it != beyond; ++it)
  44. {
  45. if (! predicate.apply(*it))
  46. {
  47. return false;
  48. }
  49. }
  50. return AllowEmptyRange || first != beyond;
  51. }
  52. };
  53. } // namespace detail
  54. #endif // DOXYGEN_NO_DETAIL
  55. }} // namespace boost::geometry
  56. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_CHECK_ITERATOR_RANGE_HPP