compare.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2017-2022.
  4. // Modifications copyright (c) 2017-2022, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_POLICIES_COMPARE_HPP
  10. #define BOOST_GEOMETRY_POLICIES_COMPARE_HPP
  11. #include <cstddef>
  12. #include <boost/geometry/strategies/compare.hpp>
  13. #include <boost/geometry/strategies/spherical/compare.hpp>
  14. #include <boost/geometry/util/math.hpp>
  15. namespace boost { namespace geometry
  16. {
  17. /*!
  18. \brief Less functor, to sort points in ascending order.
  19. \ingroup compare
  20. \details This functor compares points and orders them on x,
  21. then on y, then on z coordinate.
  22. \tparam Point the geometry
  23. \tparam Dimension the dimension to sort on, defaults to -1,
  24. indicating ALL dimensions. That's to say, first on x,
  25. on equal x-es then on y, etc.
  26. If a dimension is specified, only that dimension is considered
  27. */
  28. template
  29. <
  30. typename Point = void,
  31. int Dimension = -1,
  32. typename CSTag = void
  33. >
  34. struct less
  35. {
  36. typedef Point first_argument_type;
  37. typedef Point second_argument_type;
  38. typedef bool result_type;
  39. inline bool operator()(Point const& left, Point const& right) const
  40. {
  41. typedef typename strategy::compare::services::default_strategy
  42. <
  43. strategy::compare::less,
  44. Point, Point,
  45. Dimension,
  46. CSTag, CSTag
  47. >::type strategy_type;
  48. return strategy_type::apply(left, right);
  49. }
  50. };
  51. template <int Dimension, typename CSTag>
  52. struct less<void, Dimension, CSTag>
  53. {
  54. typedef bool result_type;
  55. template <typename Point1, typename Point2>
  56. inline bool operator()(Point1 const& left, Point2 const& right) const
  57. {
  58. typedef typename strategy::compare::services::default_strategy
  59. <
  60. strategy::compare::less,
  61. Point1, Point2,
  62. Dimension,
  63. CSTag, CSTag
  64. >::type strategy_type;
  65. return strategy_type::apply(left, right);
  66. }
  67. };
  68. template <typename Point, int Dimension>
  69. struct less<Point, Dimension, void>
  70. {
  71. typedef Point first_argument_type;
  72. typedef Point second_argument_type;
  73. typedef bool result_type;
  74. inline bool operator()(Point const& left, Point const& right) const
  75. {
  76. typedef typename strategy::compare::services::default_strategy
  77. <
  78. strategy::compare::less,
  79. Point, Point,
  80. Dimension
  81. >::type strategy_type;
  82. return strategy_type::apply(left, right);
  83. }
  84. };
  85. template <int Dimension>
  86. struct less<void, Dimension, void>
  87. {
  88. typedef bool result_type;
  89. template <typename Point1, typename Point2>
  90. inline bool operator()(Point1 const& left, Point2 const& right) const
  91. {
  92. typedef typename strategy::compare::services::default_strategy
  93. <
  94. strategy::compare::less,
  95. Point1, Point2,
  96. Dimension
  97. >::type strategy_type;
  98. return strategy_type::apply(left, right);
  99. }
  100. };
  101. /*!
  102. \brief Greater functor
  103. \ingroup compare
  104. \details Can be used to sort points in reverse order
  105. \see Less functor
  106. */
  107. template
  108. <
  109. typename Point = void,
  110. int Dimension = -1,
  111. typename CSTag = void
  112. >
  113. struct greater
  114. {
  115. typedef Point first_argument_type;
  116. typedef Point second_argument_type;
  117. typedef bool result_type;
  118. bool operator()(Point const& left, Point const& right) const
  119. {
  120. typedef typename strategy::compare::services::default_strategy
  121. <
  122. strategy::compare::greater,
  123. Point, Point,
  124. Dimension,
  125. CSTag, CSTag
  126. >::type strategy_type;
  127. return strategy_type::apply(left, right);
  128. }
  129. };
  130. template <int Dimension, typename CSTag>
  131. struct greater<void, Dimension, CSTag>
  132. {
  133. typedef bool result_type;
  134. template <typename Point1, typename Point2>
  135. bool operator()(Point1 const& left, Point2 const& right) const
  136. {
  137. typedef typename strategy::compare::services::default_strategy
  138. <
  139. strategy::compare::greater,
  140. Point1, Point2,
  141. Dimension,
  142. CSTag, CSTag
  143. >::type strategy_type;
  144. return strategy_type::apply(left, right);
  145. }
  146. };
  147. template <typename Point, int Dimension>
  148. struct greater<Point, Dimension, void>
  149. {
  150. typedef Point first_argument_type;
  151. typedef Point second_argument_type;
  152. typedef bool result_type;
  153. bool operator()(Point const& left, Point const& right) const
  154. {
  155. typedef typename strategy::compare::services::default_strategy
  156. <
  157. strategy::compare::greater,
  158. Point, Point,
  159. Dimension
  160. >::type strategy_type;
  161. return strategy_type::apply(left, right);
  162. }
  163. };
  164. template <int Dimension>
  165. struct greater<void, Dimension, void>
  166. {
  167. typedef bool result_type;
  168. template <typename Point1, typename Point2>
  169. bool operator()(Point1 const& left, Point2 const& right) const
  170. {
  171. typedef typename strategy::compare::services::default_strategy
  172. <
  173. strategy::compare::greater,
  174. Point1, Point2,
  175. Dimension
  176. >::type strategy_type;
  177. return strategy_type::apply(left, right);
  178. }
  179. };
  180. /*!
  181. \brief Equal To functor, to compare if points are equal
  182. \ingroup compare
  183. \tparam Geometry the geometry
  184. \tparam Dimension the dimension to compare on, defaults to -1,
  185. indicating ALL dimensions.
  186. If a dimension is specified, only that dimension is considered
  187. */
  188. template
  189. <
  190. typename Point,
  191. int Dimension = -1,
  192. typename CSTag = void
  193. >
  194. struct equal_to
  195. {
  196. typedef Point first_argument_type;
  197. typedef Point second_argument_type;
  198. typedef bool result_type;
  199. bool operator()(Point const& left, Point const& right) const
  200. {
  201. typedef typename strategy::compare::services::default_strategy
  202. <
  203. strategy::compare::equal_to,
  204. Point, Point,
  205. Dimension,
  206. CSTag, CSTag
  207. >::type strategy_type;
  208. return strategy_type::apply(left, right);
  209. }
  210. };
  211. template <int Dimension, typename CSTag>
  212. struct equal_to<void, Dimension, CSTag>
  213. {
  214. typedef bool result_type;
  215. template <typename Point1, typename Point2>
  216. bool operator()(Point1 const& left, Point2 const& right) const
  217. {
  218. typedef typename strategy::compare::services::default_strategy
  219. <
  220. strategy::compare::equal_to,
  221. Point1, Point2,
  222. Dimension,
  223. CSTag, CSTag
  224. >::type strategy_type;
  225. return strategy_type::apply(left, right);
  226. }
  227. };
  228. template <typename Point, int Dimension>
  229. struct equal_to<Point, Dimension, void>
  230. {
  231. typedef Point first_argument_type;
  232. typedef Point second_argument_type;
  233. typedef bool result_type;
  234. bool operator()(Point const& left, Point const& right) const
  235. {
  236. typedef typename strategy::compare::services::default_strategy
  237. <
  238. strategy::compare::equal_to,
  239. Point, Point,
  240. Dimension
  241. >::type strategy_type;
  242. return strategy_type::apply(left, right);
  243. }
  244. };
  245. template <int Dimension>
  246. struct equal_to<void, Dimension, void>
  247. {
  248. typedef bool result_type;
  249. template <typename Point1, typename Point2>
  250. bool operator()(Point1 const& left, Point2 const& right) const
  251. {
  252. typedef typename strategy::compare::services::default_strategy
  253. <
  254. strategy::compare::equal_to,
  255. Point1, Point2,
  256. Dimension
  257. >::type strategy_type;
  258. return strategy_type::apply(left, right);
  259. }
  260. };
  261. }} // namespace boost::geometry
  262. #endif // BOOST_GEOMETRY_POLICIES_COMPARE_HPP