compare.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2017-2023.
  6. // Modifications copyright (c) 2017-2023, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_STRATEGIES_COMPARE_HPP
  15. #define BOOST_GEOMETRY_STRATEGIES_COMPARE_HPP
  16. #include <algorithm>
  17. #include <cstddef>
  18. #include <functional>
  19. #include <boost/geometry/core/access.hpp>
  20. #include <boost/geometry/core/cs.hpp>
  21. #include <boost/geometry/core/coordinate_type.hpp>
  22. #include <boost/geometry/core/coordinate_dimension.hpp>
  23. #include <boost/geometry/core/static_assert.hpp>
  24. #include <boost/geometry/util/math.hpp>
  25. namespace boost { namespace geometry
  26. {
  27. namespace strategy { namespace compare
  28. {
  29. struct less
  30. {
  31. template <typename T1, typename T2>
  32. static inline bool apply(T1 const& l, T2 const& r)
  33. {
  34. return l < r;
  35. }
  36. };
  37. struct greater
  38. {
  39. template <typename T1, typename T2>
  40. static inline bool apply(T1 const& l, T2 const& r)
  41. {
  42. return l > r;
  43. }
  44. };
  45. struct equal_to
  46. {
  47. template <typename T1, typename T2>
  48. static inline bool apply(T1 const& , T2 const& )
  49. {
  50. return false;
  51. }
  52. };
  53. struct equals_epsilon
  54. {
  55. template <typename T1, typename T2>
  56. static inline bool apply(T1 const& l, T2 const& r)
  57. {
  58. return math::equals(l, r);
  59. }
  60. };
  61. struct equals_exact
  62. {
  63. template <typename T1, typename T2>
  64. static inline bool apply(T1 const& l, T2 const& r)
  65. {
  66. return l == r;
  67. }
  68. };
  69. #ifndef DOXYGEN_NO_DETAIL
  70. namespace detail
  71. {
  72. template
  73. <
  74. typename ComparePolicy,
  75. typename EqualsPolicy,
  76. std::size_t Dimension,
  77. std::size_t DimensionCount
  78. >
  79. struct compare_loop
  80. {
  81. template <typename Point1, typename Point2>
  82. static inline bool apply(Point1 const& left, Point2 const& right)
  83. {
  84. auto const& cleft = geometry::get<Dimension>(left);
  85. auto const& cright = geometry::get<Dimension>(right);
  86. if (EqualsPolicy::apply(cleft, cright))
  87. {
  88. return compare_loop
  89. <
  90. ComparePolicy,
  91. EqualsPolicy,
  92. Dimension + 1, DimensionCount
  93. >::apply(left, right);
  94. }
  95. else
  96. {
  97. return ComparePolicy::apply(cleft, cright);
  98. }
  99. }
  100. };
  101. template
  102. <
  103. typename ComparePolicy,
  104. typename EqualsPolicy,
  105. std::size_t DimensionCount
  106. >
  107. struct compare_loop<ComparePolicy, EqualsPolicy, DimensionCount, DimensionCount>
  108. {
  109. template <typename Point1, typename Point2>
  110. static inline bool apply(Point1 const& , Point2 const& )
  111. {
  112. // On coming here, points are equal.
  113. // Return false for less/greater.
  114. return false;
  115. }
  116. };
  117. template
  118. <
  119. typename EqualsPolicy,
  120. std::size_t DimensionCount
  121. >
  122. struct compare_loop<strategy::compare::equal_to, EqualsPolicy, DimensionCount, DimensionCount>
  123. {
  124. template <typename Point1, typename Point2>
  125. static inline bool apply(Point1 const& , Point2 const& )
  126. {
  127. // On coming here, points are equal.
  128. // Return true for equal_to.
  129. return true;
  130. }
  131. };
  132. } // namespace detail
  133. #endif // DOXYGEN_NO_DETAIL
  134. template
  135. <
  136. typename ComparePolicy,
  137. typename EqualsPolicy,
  138. int Dimension = -1
  139. >
  140. struct cartesian
  141. {
  142. template <typename Point1, typename Point2>
  143. static inline bool apply(Point1 const& left, Point2 const& right)
  144. {
  145. return compare::detail::compare_loop
  146. <
  147. ComparePolicy, EqualsPolicy, Dimension, Dimension + 1
  148. >::apply(left, right);
  149. }
  150. };
  151. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  152. template
  153. <
  154. typename ComparePolicy,
  155. typename EqualsPolicy
  156. >
  157. struct cartesian<ComparePolicy, EqualsPolicy, -1>
  158. {
  159. template <typename Point1, typename Point2>
  160. static inline bool apply(Point1 const& left, Point2 const& right)
  161. {
  162. return compare::detail::compare_loop
  163. <
  164. ComparePolicy,
  165. EqualsPolicy,
  166. 0,
  167. ((std::min)(geometry::dimension<Point1>::value,
  168. geometry::dimension<Point2>::value))
  169. >::apply(left, right);
  170. }
  171. };
  172. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  173. namespace services
  174. {
  175. template
  176. <
  177. typename ComparePolicy,
  178. typename EqualsPolicy,
  179. typename Point1,
  180. typename Point2 = Point1,
  181. int Dimension = -1,
  182. typename CSTag1 = cs_tag_t<Point1>,
  183. typename CSTag2 = cs_tag_t<Point2>
  184. >
  185. struct default_strategy
  186. {
  187. BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
  188. "Not implemented for these types.",
  189. CSTag1, CSTag2);
  190. };
  191. template
  192. <
  193. typename ComparePolicy,
  194. typename EqualsPolicy,
  195. typename Point1,
  196. typename Point2,
  197. int Dimension
  198. >
  199. struct default_strategy<ComparePolicy, EqualsPolicy, Point1, Point2, Dimension, cartesian_tag, cartesian_tag>
  200. {
  201. typedef compare::cartesian<ComparePolicy, EqualsPolicy, Dimension> type;
  202. };
  203. } // namespace services
  204. }} // namespace strategy compare
  205. }} // namespace boost::geometry
  206. #endif // BOOST_GEOMETRY_STRATEGIES_COMPARE_HPP