equal_to.hpp 9.9 KB

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