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-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 = geometry::tag_t<Geometry>>
  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. \param strategy Strategy to be used.
  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. \param strategy Strategy to be used.
  164. \return true if values are equal.
  165. */
  166. template <typename Strategy>
  167. inline bool operator()(std::pair<T1, T2> const& l, std::pair<T1, T2> const& r,
  168. Strategy const& strategy) const
  169. {
  170. return detail::equals<T1>::apply(l.first, r.first, strategy)
  171. && detail::equals<T2>::apply(l.second, r.second, strategy);
  172. }
  173. };
  174. /*!
  175. \brief The function object comparing Values.
  176. This specialization compares values of type boost::tuple<...>.
  177. It compares all members of the tuple from the first one to the last one.
  178. */
  179. template <typename T0, typename T1, typename T2, typename T3, typename T4,
  180. typename T5, typename T6, typename T7, typename T8, typename T9>
  181. struct equal_to<boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>, false>
  182. {
  183. typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> value_type;
  184. /*! \brief The type of result returned by function object. */
  185. typedef bool result_type;
  186. /*!
  187. \brief Compare values. If tuple<> Value member is a Geometry geometry::equals() function is used.
  188. \param l First value.
  189. \param r Second value.
  190. \param strategy Strategy to be used.
  191. \return true if values are equal.
  192. */
  193. template <typename Strategy>
  194. inline bool operator()(value_type const& l, value_type const& r,
  195. Strategy const& strategy) const
  196. {
  197. return detail::tuple_equals<
  198. value_type, 0, boost::tuples::length<value_type>::value
  199. >::apply(l, r, strategy);
  200. }
  201. };
  202. }}}} // namespace boost::geometry::index::detail
  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. \param strategy Strategy to be used.
  241. \return true if values are equal.
  242. */
  243. template <typename Strategy>
  244. bool operator()(value_type const& l, value_type const& r, Strategy const& strategy) const
  245. {
  246. return detail::std_tuple_equals<
  247. value_type, 0, std::tuple_size<value_type>::value
  248. >::apply(l, r, strategy);
  249. }
  250. };
  251. }}}} // namespace boost::geometry::index::detail
  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