equal_to.hpp 7.7 KB

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