compare.hpp 5.0 KB

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