equal_to.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Boost.Geometry Index
  2. //
  3. // Copyright (c) 2011-2016 Adam Wulkiewicz, Lodz, Poland.
  4. //
  5. // This file was modified by Oracle on 2019.
  6. // Modifications copyright (c) 2019 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. //
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_INDEX_EQUAL_TO_HPP
  13. #define BOOST_GEOMETRY_INDEX_EQUAL_TO_HPP
  14. #include <boost/geometry/algorithms/detail/equals/interface.hpp>
  15. #include <boost/geometry/index/indexable.hpp>
  16. namespace boost { namespace geometry { namespace index { namespace detail
  17. {
  18. template <typename Geometry,
  19. typename Tag = typename geometry::tag<Geometry>::type>
  20. struct equals
  21. {
  22. template <typename Strategy>
  23. inline static bool apply(Geometry const& g1, Geometry const& g2, Strategy const&)
  24. {
  25. return geometry::equals(g1, g2);
  26. }
  27. };
  28. template <typename Geometry>
  29. struct equals<Geometry, point_tag>
  30. {
  31. inline static bool apply(Geometry const& g1, Geometry const& g2, default_strategy const&)
  32. {
  33. return geometry::equals(g1, g2);
  34. }
  35. template <typename Strategy>
  36. inline static bool apply(Geometry const& g1, Geometry const& g2, Strategy const&)
  37. {
  38. return geometry::equals(g1, g2, typename Strategy::within_point_point_strategy_type());
  39. }
  40. };
  41. template <typename Geometry>
  42. struct equals<Geometry, box_tag>
  43. {
  44. inline static bool apply(Geometry const& g1, Geometry const& g2, default_strategy const&)
  45. {
  46. return geometry::equals(g1, g2);
  47. }
  48. template <typename Strategy>
  49. inline static bool apply(Geometry const& g1, Geometry const& g2, Strategy const&)
  50. {
  51. // NOTE: there is no strategy for equals(box, box) so pass dummy variable
  52. // TODO: there should be a strategy even if it is the same for all CSes in case undefined_cs was used
  53. return geometry::equals(g1, g2, 0);
  54. }
  55. };
  56. template <typename Geometry>
  57. struct equals<Geometry, segment_tag>
  58. {
  59. inline static bool apply(Geometry const& g1, Geometry const& g2, default_strategy const&)
  60. {
  61. return geometry::equals(g1, g2);
  62. }
  63. template <typename Strategy>
  64. inline static bool apply(Geometry const& g1, Geometry const& g2, Strategy const& s)
  65. {
  66. return geometry::equals(g1, g2, s.get_relate_segment_segment_strategy());
  67. }
  68. };
  69. template <typename Geometry, typename Tag>
  70. struct equals<Geometry *, Tag>
  71. {
  72. template <typename Strategy>
  73. inline static bool apply(const Geometry * g1, const Geometry * g2, Strategy const&)
  74. {
  75. return g1 == g2;
  76. }
  77. };
  78. template <typename T>
  79. struct equals<T, void>
  80. {
  81. template <typename Strategy>
  82. inline static bool apply(T const& v1, T const& v2, Strategy const&)
  83. {
  84. return v1 == v2;
  85. }
  86. };
  87. template <typename T>
  88. struct equals<T *, void>
  89. {
  90. template <typename Strategy>
  91. inline static bool apply(const T * v1, const T * v2, Strategy const&)
  92. {
  93. return v1 == v2;
  94. }
  95. };
  96. template <typename Tuple, size_t I, size_t N>
  97. struct tuple_equals
  98. {
  99. template <typename Strategy>
  100. inline static bool apply(Tuple const& t1, Tuple const& t2, Strategy const& strategy)
  101. {
  102. typedef typename boost::tuples::element<I, Tuple>::type T;
  103. return equals<T>::apply(boost::get<I>(t1), boost::get<I>(t2), strategy)
  104. && tuple_equals<Tuple, I + 1, N>::apply(t1, t2, strategy);
  105. }
  106. };
  107. template <typename Tuple, size_t I>
  108. struct tuple_equals<Tuple, I, I>
  109. {
  110. template <typename Strategy>
  111. inline static bool apply(Tuple const&, Tuple const&, Strategy const&)
  112. {
  113. return true;
  114. }
  115. };
  116. // TODO: Consider this: Since equal_to<> is using geometry::equals() it's possible that
  117. // two compared Indexables are not exactly the same! They will be spatially equal
  118. // but not strictly equal. Consider 2 Segments with reversed order of points.
  119. // Therefore it's possible that during the Value removal different value will be
  120. // removed than the one that was passed.
  121. /*!
  122. \brief The function object comparing Values.
  123. It compares Geometries using geometry::equals() function. Other types are compared using operator==.
  124. The default version handles Values which are Indexables.
  125. This template is also specialized for std::pair<T1, T2> and boost::tuple<...>.
  126. \tparam Value The type of objects which are compared by this function object.
  127. \tparam IsIndexable If true, Values are compared using boost::geometry::equals() functions.
  128. */
  129. template <typename Value,
  130. bool IsIndexable = is_indexable<Value>::value>
  131. struct equal_to
  132. {
  133. /*! \brief The type of result returned by function object. */
  134. typedef bool result_type;
  135. /*!
  136. \brief Compare values. If Value is a Geometry geometry::equals() function is used.
  137. \param l First value.
  138. \param r Second value.
  139. \return true if values are equal.
  140. */
  141. template <typename Strategy>
  142. inline bool operator()(Value const& l, Value const& r, Strategy const& strategy) const
  143. {
  144. return detail::equals<Value>::apply(l, r, strategy);
  145. }
  146. };
  147. /*!
  148. \brief The function object comparing Values.
  149. This specialization compares values of type std::pair<T1, T2>.
  150. It compares pairs' first values, then second values.
  151. \tparam T1 The first type.
  152. \tparam T2 The second type.
  153. */
  154. template <typename T1, typename T2>
  155. struct equal_to<std::pair<T1, T2>, false>
  156. {
  157. /*! \brief The type of result returned by function object. */
  158. typedef bool result_type;
  159. /*!
  160. \brief Compare values. If pair<> Value member is a Geometry geometry::equals() function is used.
  161. \param l First value.
  162. \param r Second value.
  163. \return true if values are equal.
  164. */
  165. template <typename Strategy>
  166. inline bool operator()(std::pair<T1, T2> const& l, std::pair<T1, T2> const& r,
  167. Strategy const& strategy) const
  168. {
  169. return detail::equals<T1>::apply(l.first, r.first, strategy)
  170. && detail::equals<T2>::apply(l.second, r.second, strategy);
  171. }
  172. };
  173. /*!
  174. \brief The function object comparing Values.
  175. This specialization compares values of type boost::tuple<...>.
  176. It compares all members of the tuple from the first one to the last one.
  177. */
  178. template <typename T0, typename T1, typename T2, typename T3, typename T4,
  179. typename T5, typename T6, typename T7, typename T8, typename T9>
  180. struct equal_to<boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>, false>
  181. {
  182. typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> value_type;
  183. /*! \brief The type of result returned by function object. */
  184. typedef bool result_type;
  185. /*!
  186. \brief Compare values. If tuple<> Value member is a Geometry geometry::equals() function is used.
  187. \param l First value.
  188. \param r Second value.
  189. \return true if values are equal.
  190. */
  191. template <typename Strategy>
  192. inline bool operator()(value_type const& l, value_type const& r,
  193. Strategy const& strategy) const
  194. {
  195. return detail::tuple_equals<
  196. value_type, 0, boost::tuples::length<value_type>::value
  197. >::apply(l, r, strategy);
  198. }
  199. };
  200. }}}} // namespace boost::geometry::index::detail
  201. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  202. #include <tuple>
  203. namespace boost { namespace geometry { namespace index { namespace detail {
  204. template <typename Tuple, size_t I, size_t N>
  205. struct std_tuple_equals
  206. {
  207. template <typename Strategy>
  208. inline static bool apply(Tuple const& t1, Tuple const& t2, Strategy const& strategy)
  209. {
  210. typedef typename std::tuple_element<I, Tuple>::type T;
  211. return equals<T>::apply(std::get<I>(t1), std::get<I>(t2), strategy)
  212. && std_tuple_equals<Tuple, I + 1, N>::apply(t1, t2, strategy);
  213. }
  214. };
  215. template <typename Tuple, size_t I>
  216. struct std_tuple_equals<Tuple, I, I>
  217. {
  218. template <typename Strategy>
  219. inline static bool apply(Tuple const&, Tuple const&, Strategy const&)
  220. {
  221. return true;
  222. }
  223. };
  224. /*!
  225. \brief The function object comparing Values.
  226. This specialization compares values of type std::tuple<Args...>.
  227. It's defined if the compiler supports tuples and variadic templates.
  228. It compares all members of the tuple from the first one to the last one.
  229. */
  230. template <typename ...Args>
  231. struct equal_to<std::tuple<Args...>, false>
  232. {
  233. typedef std::tuple<Args...> value_type;
  234. /*! \brief The type of result returned by function object. */
  235. typedef bool result_type;
  236. /*!
  237. \brief Compare values. If tuple<> Value member is a Geometry geometry::equals() function is used.
  238. \param l First value.
  239. \param r Second value.
  240. \return true if values are equal.
  241. */
  242. template <typename Strategy>
  243. bool operator()(value_type const& l, value_type const& r, Strategy const& strategy) const
  244. {
  245. return detail::std_tuple_equals<
  246. value_type, 0, std::tuple_size<value_type>::value
  247. >::apply(l, r, strategy);
  248. }
  249. };
  250. }}}} // namespace boost::geometry::index::detail
  251. #endif // !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  252. namespace boost { namespace geometry { namespace index {
  253. /*!
  254. \brief The function object comparing Values.
  255. The default version handles Values which are Indexables, std::pair<T1, T2>, boost::tuple<...>
  256. and std::tuple<...> if STD tuples and variadic templates are supported.
  257. All members are compared from left to right, Geometries using boost::geometry::equals() function,
  258. other types using operator==.
  259. \tparam Value The type of objects which are compared by this function object.
  260. */
  261. template <typename Value>
  262. struct equal_to
  263. : detail::equal_to<Value>
  264. {
  265. /*! \brief The type of result returned by function object. */
  266. typedef typename detail::equal_to<Value>::result_type result_type;
  267. /*!
  268. \brief Compare Values.
  269. \param l First value.
  270. \param r Second value.
  271. \return true if Values are equal.
  272. */
  273. inline bool operator()(Value const& l, Value const& r) const
  274. {
  275. return detail::equal_to<Value>::operator()(l, r, default_strategy());
  276. }
  277. template <typename Strategy>
  278. inline bool operator()(Value const& l, Value const& r, Strategy const& strategy) const
  279. {
  280. return detail::equal_to<Value>::operator()(l, r, strategy);
  281. }
  282. };
  283. }}} // namespace boost::geometry::index
  284. #endif // BOOST_GEOMETRY_INDEX_EQUAL_TO_HPP