equal_to.hpp 10 KB

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