compare.hpp 8.0 KB

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