has_duplicates.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014-2021, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_HAS_DUPLICATES_HPP
  8. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_HAS_DUPLICATES_HPP
  9. #include <boost/core/ignore_unused.hpp>
  10. #include <boost/range/begin.hpp>
  11. #include <boost/range/end.hpp>
  12. #include <boost/range/size.hpp>
  13. #include <boost/geometry/core/closure.hpp>
  14. #include <boost/geometry/policies/compare.hpp>
  15. #include <boost/geometry/policies/is_valid/default_policy.hpp>
  16. #include <boost/geometry/views/closeable_view.hpp>
  17. #include <boost/geometry/algorithms/validity_failure_type.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. #ifndef DOXYGEN_NO_DETAIL
  21. namespace detail { namespace is_valid
  22. {
  23. template <typename Range>
  24. struct has_duplicates
  25. {
  26. template <typename VisitPolicy, typename Strategy>
  27. static inline bool apply(Range const& range, VisitPolicy& visitor,
  28. Strategy const& )
  29. {
  30. boost::ignore_unused(visitor);
  31. detail::closed_view<Range const> const view(range);
  32. if ( boost::size(view) < 2 )
  33. {
  34. return ! visitor.template apply<no_failure>();
  35. }
  36. geometry::equal_to
  37. <
  38. typename boost::range_value<Range>::type,
  39. -1,
  40. typename Strategy::cs_tag
  41. > equal;
  42. auto it = boost::begin(view);
  43. auto const end = boost::end(view);
  44. auto next = it;
  45. for (++next; next != end; ++it, ++next)
  46. {
  47. if ( equal(*it, *next) )
  48. {
  49. return ! visitor.template apply<failure_duplicate_points>(*it);
  50. }
  51. }
  52. return ! visitor.template apply<no_failure>();
  53. }
  54. };
  55. }} // namespace detail::is_valid
  56. #endif // DOXYGEN_NO_DETAIL
  57. }} // namespace boost::geometry
  58. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_HAS_DUPLICATES_HPP