equal_to.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Boost.Geometry Index
  2. //
  3. // Copyright (c) 2011-2013 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/equals.hpp>
  11. namespace boost { namespace geometry { namespace index {
  12. namespace detail {
  13. template <typename Geometry, typename Tag>
  14. struct equals
  15. {
  16. static bool apply(Geometry const& g1, Geometry const& g2)
  17. {
  18. return geometry::equals(g1, g2);
  19. }
  20. };
  21. template <typename T>
  22. struct equals<T, void>
  23. {
  24. static bool apply(T const& v1, T const& v2)
  25. {
  26. return v1 == v2;
  27. }
  28. };
  29. template <typename Tuple, size_t I, size_t N>
  30. struct tuple_equals
  31. {
  32. inline static bool apply(Tuple const& t1, Tuple const& t2)
  33. {
  34. typedef typename boost::tuples::element<I, Tuple>::type T;
  35. return
  36. equals<
  37. T, typename geometry::traits::tag<T>::type
  38. >::apply(boost::get<I>(t1), boost::get<I>(t2))
  39. &&
  40. tuple_equals<Tuple, I+1, N>::apply(t1, t2);
  41. }
  42. };
  43. template <typename Tuple, size_t I>
  44. struct tuple_equals<Tuple, I, I>
  45. {
  46. inline static bool apply(Tuple const&, Tuple const&)
  47. {
  48. return true;
  49. }
  50. };
  51. } // namespace detail
  52. /*!
  53. \brief The function object comparing Values.
  54. It compares Geometries using geometry::equals() function. Other types are compared using operator==.
  55. The default version handles Values which are Indexables.
  56. This template is also specialized for std::pair<T1, T2> and boost::tuple<...>.
  57. \tparam Value The type of objects which are compared by this function object.
  58. */
  59. template <typename Value>
  60. struct equal_to
  61. {
  62. /*! \brief The type of result returned by function object. */
  63. typedef bool result_type;
  64. /*!
  65. \brief Compare values. If Value is a Geometry geometry::equals() function is used.
  66. \param l First value.
  67. \param r Second value.
  68. \return true if values are equal.
  69. */
  70. bool operator()(Value const& l, Value const& r) const
  71. {
  72. return detail::equals<Value, typename geometry::traits::tag<Value>::type>::apply(l ,r);
  73. }
  74. };
  75. /*!
  76. \brief The function object comparing Values.
  77. This specialization compares values of type std::pair<T1, T2>.
  78. It compares pairs' first values, then second values.
  79. \tparam T1 The first type.
  80. \tparam T2 The second type.
  81. */
  82. template <typename T1, typename T2>
  83. struct equal_to< std::pair<T1, T2> >
  84. {
  85. /*! \brief The type of result returned by function object. */
  86. typedef bool result_type;
  87. /*!
  88. \brief Compare values. If pair<> Value member is a Geometry geometry::equals() function is used.
  89. \param l First value.
  90. \param r Second value.
  91. \return true if values are equal.
  92. */
  93. bool operator()(std::pair<T1, T2> const& l, std::pair<T1, T2> const& r) const
  94. {
  95. typedef detail::equals<T1, typename geometry::traits::tag<T1>::type> equals1;
  96. typedef detail::equals<T2, typename geometry::traits::tag<T2>::type> equals2;
  97. return equals1::apply(l.first, r.first) && equals2::apply(l.second, r.second);
  98. }
  99. };
  100. /*!
  101. \brief The function object comparing Values.
  102. This specialization compares values of type boost::tuple<...>.
  103. It compares all members of the tuple from the first one to the last one.
  104. */
  105. template <typename T0, typename T1, typename T2, typename T3, typename T4,
  106. typename T5, typename T6, typename T7, typename T8, typename T9>
  107. struct equal_to< boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >
  108. {
  109. typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> value_type;
  110. /*! \brief The type of result returned by function object. */
  111. typedef bool result_type;
  112. /*!
  113. \brief Compare values. If tuple<> Value member is a Geometry geometry::equals() function is used.
  114. \param l First value.
  115. \param r Second value.
  116. \return true if values are equal.
  117. */
  118. bool operator()(value_type const& l, value_type const& r) const
  119. {
  120. return detail::tuple_equals<
  121. value_type, 0, boost::tuples::length<value_type>::value
  122. >::apply(l ,r);
  123. }
  124. };
  125. }}} // namespace boost::geometry::index
  126. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  127. #include <tuple>
  128. namespace boost { namespace geometry { namespace index {
  129. namespace detail {
  130. template <typename Tuple, size_t I, size_t N>
  131. struct std_tuple_equals
  132. {
  133. inline static bool apply(Tuple const& t1, Tuple const& t2)
  134. {
  135. typedef typename std::tuple_element<I, Tuple>::type T;
  136. return
  137. equals<
  138. T, typename geometry::traits::tag<T>::type
  139. >::apply(std::get<I>(t1), std::get<I>(t2))
  140. &&
  141. std_tuple_equals<Tuple, I+1, N>::apply(t1, t2);
  142. }
  143. };
  144. template <typename Tuple, size_t I>
  145. struct std_tuple_equals<Tuple, I, I>
  146. {
  147. inline static bool apply(Tuple const&, Tuple const&)
  148. {
  149. return true;
  150. }
  151. };
  152. } // namespace detail
  153. /*!
  154. \brief The function object comparing Values.
  155. This specialization compares values of type std::tuple<Args...>.
  156. It's defined if the compiler supports tuples and variadic templates.
  157. It compares all members of the tuple from the first one to the last one.
  158. */
  159. template <typename ...Args>
  160. struct equal_to< std::tuple<Args...> >
  161. {
  162. typedef std::tuple<Args...> value_type;
  163. /*! \brief The type of result returned by function object. */
  164. typedef bool result_type;
  165. /*!
  166. \brief Compare values. If tuple<> Value member is a Geometry geometry::equals() function is used.
  167. \param l First value.
  168. \param r Second value.
  169. \return true if values are equal.
  170. */
  171. bool operator()(value_type const& l, value_type const& r) const
  172. {
  173. return detail::std_tuple_equals<
  174. value_type, 0, std::tuple_size<value_type>::value
  175. >::apply(l ,r);
  176. }
  177. };
  178. }}} // namespace boost::geometry::index
  179. #endif // !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  180. #endif // BOOST_GEOMETRY_INDEX_EQUAL_TO_HPP